Hardware: ESP-WROOM-32E
- Blniking an External LED
public class Program
{
public static void Main()
{
var gpio = new GpioController();
using (var led = gpio.OpenPin(15, PinMode.Output))
{
led.Write(PinValue.Low);
while (true)
{
for (int i = 0; i < 3; i++)
{
led.Toggle();
Thread.Sleep(125);
}
Thread.Sleep(525);
}
}
}
}
- Blinking the Onboard Blue LED
public class Program
{
public static void Main()
{
var gpio = new GpioController();
using (var led = gpio.OpenPin(2, PinMode.Output))
{
while (true)
{
led.Write(PinValue.High);
Thread.Sleep(500);
led.Write(PinValue.Low);
Thread.Sleep(500);
}
}
}
}
- Button-Cnotrolled LED
using System.Device.Gpio;
using Iot.Device.Button;
public class Program
{
public static void Main()
{
Debug.WriteLine("Hello from nanoFramework!");
var gpio = new GpioController();
using (var led = gpio.OpenPin(15, PinMode.Output))
using (var button = new GpioButton(13))
{
button.IsDoublePressEnabled = true;
button.IsHoldingEnabled = true;
button.ButtonDown += (_, __) => { led.Write(PinValue.High); };
button.ButtonUp += (_, __) => { led.Write(PinValue.Low); };
while (true)
{
Thread.Sleep(Timeout.Infinite);
}
}
}
}
- PWM-Based LED Brightness Control
using System.Device.Pwm;
public class Program
{
public static void Main()
{
int pinLed = 2;
Configuration.SetPinFunction(pinLed, DeviceFunction.PWM1);
using (var pwm = PwmChannel.CreateFromPin(pinLed, 40000, 0))
{
pwm.Start();
while (true)
{
for (double dutyCycle = 0; dutyCycle <= 1; dutyCycle += 0.01)
{
pwm.DutyCycle = dutyCycle;
Thread.Sleep(20);
}
for (double dutyCycle = 1; dutyCycle >= 0; dutyCycle -= 0.01)
{
pwm.DutyCycle = dutyCycle;
Thread.Sleep(20);
}
}
}
}
}
- Humidity Sensor Reading
public class Program
{
public static void Main()
{
using (var adc = new AdcController())
{
int min = adc.MinValue;
int max = adc.MaxValue;
using (var channel = adc.OpenChannel(6))
{
while (true)
{
int raw = channel.ReadValue();
double voltage = ((double)(raw - min) / (max - min)) * 3.3;
if (voltage < 1.5)
Debug.WriteLine("Wet");
else
Debug.WriteLine("Dry");
Thread.Sleep(1000);
}
}
}
}
}
- Relay Control
public static class RelayManager
{
private const int RELAY_PIN = 2;
private static GpioPin relay;
public static void Initialize()
{
var gpio = new GpioController();
relay = gpio.OpenPin(RELAY_PIN, PinMode.Output);
relay.Write(PinValue.Low);
}
public static void Toggle()
{
relay.Write(relay.Read() == PinValue.High ? PinValue.Low : PinValue.High);
}
public static void Cleanup()
{
relay.Dispose();
}
}
- Web-Based Relay Controller
public class Program
{
private const string WIFI_SSID = "aaa";
private const string WIFI_PASSWORD = "zqyo850619";
public static void Main()
{
var gpio = new GpioController();
var relayPin = gpio.OpenPin(15, PinMode.Output);
relayPin.Write(PinValue.High);
if (ConnectToWiFi(WIFI_SSID, WIFI_PASSWORD))
{
StartWebServer();
}
}
private static bool ConnectToWiFi(string ssid, string password)
{
return WifiNetworkHelper.ConnectDhcp(ssid, password, requiresDateTime: true);
}
private static void StartWebServer()
{
HttpListener listener = new HttpListener("http", 8080);
listener.Start();
new Thread(() =>
{
while (true)
{
try
{
var context = listener.GetContext();
ProcessRequest(context);
}
catch { }
}
}).Start();
}
private static void ProcessRequest(HttpListenerContext context)
{
var response = context.Response;
try
{
if (context.Request.RawUrl.StartsWith("/relay/"))
{
var parts = context.Request.RawUrl.Split('/');
int relayNumber = int.Parse(parts[2]);
string action = parts[3].ToLower();
if (action == "on" || action == "off")
{
var gpio = new GpioController();
var pin = gpio.OpenPin(15, PinMode.Output);
pin.Write(action == "on" ? PinValue.Low : PinValue.High);
pin.Dispose();
}
response.StatusCode = 200;
response.ContentType = "application/json";
response.ContentLength64 = Encoding.UTF8.GetByteCount("{\"status\":\"ok\"}");
response.OutputStream.Write(Encoding.UTF8.GetBytes("{\"status\":\"ok\"}"), 0, "{\"status\":\"ok\"}".Length);
}
else
{
response.StatusCode = 404;
}
}
finally
{
response.OutputStream.Close();
}
}
}