// // configurablefirmata_esp8266 // // D. Yi @ 2023 5 23 // #define WIRELESS #if defined(WIRELESS) //wi-fi #include //network! const char* ssid = "esp1"; const char* password = "1111100000"; const int NETWORK_PORT = 27016; // IPAddress local_IP(192, 168, 137, 111); // IPAddress gateway(192, 168, 137, 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(D4, HIGH); } void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { Serial.println("Disconnected from Wi-Fi, trying to reconnect..."); WiFi.disconnect(); WiFi.begin(ssid, password); digitalWrite(D4, LOW); } #endif //firmata over wi-fi #include #if defined(WIRELESS) #include "utility/WiFiClientStream.h" #include "utility/WiFiServerStream.h" WiFiServerStream serverStream(NETWORK_PORT); //firmware update over the air #include #endif //handle digitalWrite byte previousPORT[TOTAL_PORTS]; void digitalWriteCallback(byte port, int value) { byte i; byte currentPinValue, previousPinValue; if (port < TOTAL_PORTS && value != previousPORT[port]) { for (i = 0; i < 8; i++) { currentPinValue = (byte) value & (1 << i); previousPinValue = previousPORT[port] & (1 << i); if (currentPinValue != previousPinValue) { digitalWrite(i + (port * 8), currentPinValue); } } previousPORT[port] = value; } } //// void setup() { //pinmodes //digital inputs pinMode(D1, INPUT_PULLUP); pinMode(D2, INPUT_PULLUP); pinMode(D3, INPUT_PULLUP); pinMode(D5, INPUT_PULLUP); pinMode(D6, INPUT_PULLUP); pinMode(D7, INPUT_PULLUP); //digital outputs pinMode(D4, OUTPUT); //build-in LED && also used as wifi indicator! #if defined(WIRELESS) digitalWrite(D4, LOW); //LOW ==> LED ON ==> 'wifi. disconnected.' #else digitalWrite(D4, HIGH); //HIGH ==> LED OFF #endif //serial (for debug & info) Serial.begin(57600); #if defined(WIRELESS) //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(""); #endif //firmata Firmata.disableBlinkVersion(); Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback); #if defined(WIRELESS) Firmata.begin(serverStream); #else Firmata.begin(Serial); #endif #if defined(WIRELESS) //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(); #endif } void loop() { // loop frequency = 50hz (20 ms) static unsigned long last = 0; unsigned long now = millis(); if (now > last + 20) { last = now; // static int counter = 0; counter++; if (counter == 1000) counter = 0; //digital //==> https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/ byte port = 0; static byte port0_prev = 0; port = 0x00; port |= (0 ? 0x01 : 0x00); // EXCLUDED ==> no interrupt.. let's just leave it. port |= (digitalRead(D1) ? 0x02 : 0x00); port |= (digitalRead(D2) ? 0x04 : 0x00); port |= (digitalRead(D3) ? 0x08 : 0x00); // by design, PULLED-UP, being used for the FLASH button. port |= (0 ? 0x10 : 0x00); // EXCLUDED ==> LED output port |= (digitalRead(D5) ? 0x20 : 0x00); port |= (digitalRead(D6) ? 0x40 : 0x00); port |= (digitalRead(D7) ? 0x80 : 0x00); if (port0_prev != port) Firmata.sendDigitalPort(0, port); port0_prev = port; //analog 0 Firmata.sendAnalog(0, analogRead(0)); //send out alive counter every 1sec. (50 counts x 20ms == 1 sec.) if (counter % 50 == 0) { static int alivecounter = 0; Firmata.sendAnalog(1, alivecounter); alivecounter++; if (alivecounter == 1000) alivecounter = 0; } } //process firmata stream while (Firmata.available()) { Firmata.processInput(); } #if defined(WIRELESS) //firmata over wi-fi serverStream.maintain(); // ArduinoOTA.handle(); #endif //for wdt yield(); }