private static void DisplayEspState(GHIElectronics.TinyCLR.BrainPad.Display display, Esp8266WIFI esp8266Client) { display.Clear(); switch (esp8266Client.WifiState) { case WifiState.Disconnected: display.DrawSmallText(0, 0, "NO WIFI"); break; case WifiState.Connected: case WifiState.GotIP: display.DrawSmallText(0, 0, "WIFI CONNECTED"); break; case WifiState.NoAP: display.DrawSmallText(0, 0, "NO AP"); break; case WifiState.Busy: case WifiState.Unknown: default: display.DrawSmallText(0, 0, "ERROR"); break; } display.DrawSmallText(0, 10, $"IP: {esp8266Client.IPAddress}"); string ready = esp8266Client.ConnectionState == ConnectionState.Ready ? "REMOTE CONNECTED" : "REMOTE CLOSED"; display.DrawSmallText(0, 20, ready); display.RefreshScreen(); }
static void Main() { _buzzer = new GHIElectronics.TinyCLR.BrainPad.Buzzer(); //Setup wifi and remote computer connection _connection = new Connection { SSID = "MOTOROLA-3258C", Password = "******", Target = "192.168.0.6" }; //_connection = new Connection { SSID = "K7 8181", Password = "******", Target = "192.168.43.133" }; GpioController gpioController = GpioController.GetDefault(); var display = new GHIElectronics.TinyCLR.BrainPad.Display(); display.DrawSmallText(0, 0, "Hi there!"); display.RefreshScreen(); _pin1 = gpioController.OpenPin(BrainPad.Expansion.GpioPin.Int); _pin1.SetDriveMode(GpioPinDriveMode.Output); _pin1.Write(GpioPinValue.Low); _pins.Add(_pin1); InitializeButtonComponents(gpioController); GpioPin CH_PD = gpioController.OpenPin(BrainPad.Expansion.GpioPin.Cs); InitializeEsp8266Wifi(display, CH_PD); _buzzer.Beep(); Thread.Sleep(-1); }
private static void InitializeEsp8266Wifi(GHIElectronics.TinyCLR.BrainPad.Display display, GpioPin CH_PD) { _esp8266Client = new Esp8266WIFI(CH_PD, BrainPad.Expansion.UartPort.Usart1); _esp8266Client.OnWifiStateChanged += () => { DisplayEspState(display, _esp8266Client); }; _esp8266Client.OnIPAddressChanged += () => { DisplayEspState(display, _esp8266Client); CheckAndOpenConnection(_esp8266Client); }; _esp8266Client.OnDataConnectionChanged += () => { DisplayEspState(display, _esp8266Client); if (_esp8266Client.ConnectionState == ConnectionState.Ready) { SendDeviceInfo(_esp8266Client); } else if (_esp8266Client.ConnectionState == ConnectionState.Closed) { CH_PD.Write(GpioPinValue.Low); Thread.Sleep(200); CH_PD.Write(GpioPinValue.High); Thread.Sleep(20); _esp8266Client.SetupWifiConnection(_connection.SSID, _connection.Password, WifiMode.Client); } }; _esp8266Client.OnDataReceived += () => { string data = _esp8266Client.Data; if (data.IndexOf("CMD=") >= 0) { int idx = data.IndexOf("CMD="); data = data.Substring(idx + 4); if (data.ToUpper() == "GETDEVICEINFO") { SendDeviceInfo(_esp8266Client); } } else if (data.IndexOf("SETDIGITAL=") >= 0) { int idx = data.IndexOf("SETDIGITAL="); data = data.Substring(idx + 11); string[] splits = data.Split('|'); foreach (var split in splits) { if (string.IsNullOrEmpty(split)) { continue; } var parts = split.Split(':'); SetDigitalPin(int.Parse(parts[0]), parts[1]); } } else if (data.IndexOf("ERROR") >= 0 && data.IndexOf("CLOSED") >= 0) { CheckAndOpenConnection(_esp8266Client); } }; _esp8266Client.SetupWifiConnection(_connection.SSID, _connection.Password, WifiMode.Client); }