// // ringringrain - WIFI part. (esp8266) // // D. Yi @ 2023 5 5 // //wi-fi #include ////network! const char* ssid = "nolink"; const char* password = "2222277777"; const int NETWORK_PORT = 27016; IPAddress local_IP(192, 168, 221, 114); IPAddress gateway(192, 168, 221, 1); IPAddress subnet(255, 255, 255, 0); //wi-fi event handlers : re-connect automatically WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; void onWifiConnect(const WiFiEventStationModeGotIP& event) { Serial.println("Connected to Wi-Fi sucessfully."); Serial.print("IP address: "); Serial.println("Mac address: " + WiFi.macAddress()); Serial.println(WiFi.localIP()); digitalWrite(2, HIGH); } void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { Serial.println("Disconnected from Wi-Fi, trying to reconnect..."); WiFi.disconnect(); WiFi.begin(ssid, password); digitalWrite(2, LOW); } //firmata over wi-fi #include #include "utility/WiFiClientStream.h" #include "utility/WiFiServerStream.h" WiFiServerStream serverStream(NETWORK_PORT); //firmware update over the air #include //client device @ soft-serial (only RX valid. tx. is dummy.) #include SoftwareSerial client(D2, D8); // RX | TX //==> NO way to do RX/TX only mode?? how bad. later change library? //// void setup() { //wifi indicator pinMode(2, OUTPUT); digitalWrite(2, LOW); //disconnected. //serial (for debug & info) Serial.begin(115200); //to get data frames from the client device. client.begin(57600); //wifi wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); WiFi.mode(WIFI_STA); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); if (!WiFi.config(local_IP, gateway, subnet)) { Serial.println("STA Failed to configure"); } WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { // Serial.print("."); delay(500); } Serial.println(""); //firmata Firmata.disableBlinkVersion(); Firmata.begin(serverStream); //ota ArduinoOTA.onStart([]() { Serial.println("Start"); }); ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); } void loop() { // process client data frame if (client.available() >= 2) { // int keycoded = 0; //==> NOTE init=0 is mandatory! (sender is 16bit device. receiver 32bit device.) byte * b = (byte*)&keycoded; client.readBytes(b, 2); Serial.println(keycoded); // Firmata.sendAnalog(0, keycoded); } // send out alive counter every 1sec. static unsigned long last = 0; unsigned long now = millis(); if (now > last + 500) { last = now; // static int alivecounter = 0; Firmata.sendAnalog(1, alivecounter); alivecounter++; if (alivecounter == 1000) alivecounter = 0; // } //firmata over wi-fi serverStream.maintain(); // ArduinoOTA.handle(); //for wdt yield(); }