====== NodeMCU_compact_1 ====== {{:wiki:de.png |de}} Die NodeMCU wird in diesem Fall als kompakte Einheit über eine serielle Leitung mit dem Arduino verbunden. Befehle und Parameter können an sie übergeben werden, Telegramme werden per UPD an die Remote-Einheit übertragen. Die Parameter werden im NodeMCU-EEPROM gespeichert. {{:wiki:hu.png |hu}} A [[hu:esp:nodemcu|nodeMCU]] ebben az esetben kompakt egységként csatlakozik az Arduino-hoz egy serial vonalon keresztül. Parancsokat és paramétereket lehet átadni felé, a telegrammokat UPD-n keresztül továbbítja a távoli egységnek. A paramétereket a nodeMCU [[hu:arduino:start#eeprom|EEPROM]]-ban tárolja. // compact nodeMCU Modul for autonom UDP communication V1.0 // ob121.com // Vámos Sándor // 2020.06.05 #include #include #include #define blue_led 2 #define all 0 #define station_id 1 #define station_name 2 #define local_UDP_port 3 #define remote_UDP_port 4 #define remote_address 5 const char* ssid = "name"; const char* password = "secucode"; WiFiUDP Udp; unsigned int localUdpPort = 4321; // local port to listen on unsigned int remoteUdpPort = 3000; // local port to listen on char statId[5]; // station id char statName[32]; // station name char localIPAddr[18]; // local IP long portId; // port ID char remoteIPAddr[18]; // remote IP char incomingPacket[255]; // buffer for incoming UDPs char sendPacket[255]; // buffer for outgoing UDPs String sendit; // send string String fromSer; String toSer; String convert; char fromSerArray[255]; // serial char array incoming char toSerArray[255]; // serial char array outgoing void setup() { pinMode(blue_led, OUTPUT); // blue led on the nodeMCU, flash by send/receive digitalWrite(blue_led, HIGH); // off Serial.begin(115200); Serial.println(); WiFi.begin(ssid, password); // check wifi connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("connecting"); // wait for it } Serial.println("connected"); // found Udp.begin(localUdpPort); // UDP init read_eeprom(all); // read importand data from eeprom by start } void loop() { // ************************************* check serial ******************************************* if (Serial.available() > 0) { // serial incoming fromSer = Serial.readString(); // read serial to String fromSer.toCharArray(fromSerArray, sizeof(fromSer)); // convert serial input to char array // COMMANDS / FEEDBACKS // eeprom data: station_id: 5 char // station_name: 32 char // local_UDP_port: unsigned int // remote_UDP_port: unsigned int // remote_address: 15 char // local address not saved, because its comming over DHCP // commands:txxx : send xxx // loc : ip, port // rem : ip, port // id : station id // name : station name // all : all data (reset data from eeprom) // sidxxx : set station id 5 char (write it to eprom) // snaxxx : set station name 32 char (write it to eprom) // ripxxx : set remote ip (write it to eprom) // rpoxxx : set remote port (write it to eprom) // lpoxxx : set local port (write it to eprom) // Important: On AVR based boards, outgoing UDP packets are limited to 72 bytes // answers: at start-process : connecting, connected // rxxx : receive xxx // answer to ? : running, no_connection // answer to loc : LOC IP xxx, UDP xxx // answer to rem : REM IP xxx, UDP xxx // command from Arduino: send packet if (fromSerArray[0] == 't') { //Serial.println(fromSer); copyArray (fromSerArray, sendPacket, 254, 1); sendpack(); // send it over UDP (without "t" char) } // command form Arduino: all if (fromSer.substring(0, 3) == "all") { // all infos read_eeprom(all); // overwrite data from eeprom } // command from Arduino: what is local ip and port if (fromSer.substring(0, 3) == "loc" || fromSer.substring(0, 3) == "all") { Serial.printf("LOC IP %s, UDP %d\n", WiFi.localIP().toString().c_str(), localUdpPort); } // from Arduino: what is remote ip and port if (fromSer.substring(0, 3) == "rem" || fromSer.substring(0, 3) == "all") { Serial.printf("REM IP %s, UDP %d\n", remoteIPAddr, remoteUdpPort); } // command from Arduino: station ID if (fromSer.substring(0, 2) == "id" || fromSer.substring(0, 3) == "all") { Serial.printf("STAT ID: %s \n", statId); } // command from Arduino: station Name if (fromSer.substring(0, 4) == "name" || fromSer.substring(0, 3) == "all") { Serial.printf("STAT NAME: %s \n", statName); } // command from Arduino: new station ID if (fromSer.substring(0, 3) == "sid") { convert = fromSer.substring(3); convert.toCharArray(statId, convert.length() + 1); write_eeprom(station_id); read_eeprom(station_id); Serial.printf("STAT ID: %s \n", statId); } // command from Arduino: new station Name if (fromSer.substring(0, 3) == "sna") { convert = fromSer.substring(3); convert.toCharArray(statName, convert.length() + 1); write_eeprom(station_name); read_eeprom(station_name); Serial.printf("STAT NAME: %s \n", statName); } // command from Arduino: new remote IP if (fromSer.substring(0, 3) == "rip") { convert = fromSer.substring(3); convert.toCharArray(remoteIPAddr, convert.length() + 1); write_eeprom(remote_address); read_eeprom(remote_address); Serial.printf("REMOTE IP: %s \n", remoteIPAddr); } // command from Arduino: new remote port if (fromSer.substring(0, 3) == "rpo") { convert = fromSer.substring(3); portId = convert.toInt(); remoteUdpPort = (int)portId; write_eeprom(remote_UDP_port); read_eeprom(remote_UDP_port); Serial.printf("REMOTE IP: %d \n", remoteUdpPort); } // command from Arduino: new local port if (fromSer.substring(0, 3) == "lpo") { convert = fromSer.substring(3); portId = convert.toInt(); localUdpPort = (int)portId; write_eeprom(local_UDP_port); read_eeprom(local_UDP_port); Serial.printf("LOCAL IP: %d \n", localUdpPort); } } // ************************************* check UDP ********************************************* digitalWrite(blue_led, HIGH); // blue led off int packetSize = Udp.parsePacket(); if (packetSize) { // UDP telegramm incoming digitalWrite(blue_led, LOW); // flash blue led // receive incoming UDP packets int len = Udp.read(incomingPacket, 255); if (len > 0) { incomingPacket[len] = 0; } Serial.print("r"); // send incoming udp over serial to Arduino Serial.println(incomingPacket); // with adding an "r" char } } void sendpack() // send an UDP packet over wifi { digitalWrite(blue_led, LOW); // flash blue led Udp.beginPacket(remoteIPAddr, remoteUdpPort); Udp.write(sendPacket); Udp.endPacket(); } void copyArray (char inArray[], char outArray[], int copySize, int offset) { // copy char array with offset for (int i = 0; i < copySize; i++) { outArray[i] = inArray[i + offset]; } } void write_eeprom (int choice) { // write data to eeprom (byteweise) EEPROM.begin(512); // eeprom data: 0: all // 1: station_id: 5 char // 2: station_name: 32 char // 3: local_UDP_port: unsigned int // 4: remote_UDP_port: unsigned int // 5: remote_address: 16 char if (choice == station_id || choice == all) { // write station id for (int i = 0; i < 5; i++) { EEPROM.write(i, statId[i]); } } if (choice == station_name || choice == all) { // write station name for (int i = 0; i < 31; i++) { EEPROM.write(i + 6, statName[i]); } } if (choice == local_UDP_port || choice == all) { // write local_UDP_port int j = localUdpPort / 256; int k = localUdpPort - j * 256; EEPROM.write(40, j); EEPROM.write(41, k); } if (choice == remote_UDP_port || choice == all) { // write remote_UDP_port int j = remoteUdpPort / 256; int k = remoteUdpPort - j * 256; EEPROM.write(42, j); EEPROM.write(43, k); } if (choice == remote_address || choice == all) { // write remote_address for (int i = 0; i < 16; i++) { EEPROM.write(i + 50, remoteIPAddr[i]); } } delay(200); EEPROM.commit(); // Only needed for ESP8266 to get data written delay(200); EEPROM.end(); // Free RAM copy of structure } void read_eeprom (int choice) { // Read data from eeprom EEPROM.begin(512); // eeprom data: 0: all // 1: station_id: 5 char // 2: station_name: 32 char // 3: local_UDP_port: unsigned int // 4: remote_UDP_port: unsigned int // 5: remote_address: 15 char if (choice == station_id || choice == all) { // read station_id from EEPROM for (int i = 0; i < 5; i++) { statId[i] = EEPROM.read(i); } } if (choice == station_name || choice == all) { // read station_name from EEPROM //Serial.println("station_name"); for (int i = 0; i < 31; i++) { //Serial.println(EEPROM.read(i + 6)); statName[i] = EEPROM.read(i + 6); } } if (choice == local_UDP_port || choice == all) { // read local_UDP_port from EEPROM //Serial.println("local_UDP_port"); int j = EEPROM.read(40); int k = EEPROM.read(41); //Serial.println(j); //Serial.println(k); localUdpPort = j * 256 + k; } if (choice == remote_UDP_port || choice == all) { // read remote_UDP_port EEPROM //Serial.println("remote_UDP_port"); int j = EEPROM.read(42); int k = EEPROM.read(43); //Serial.println(j); //Serial.println(k); remoteUdpPort = j * 256 + k; } if (choice == remote_address || choice == all) { // read remote_address EEPROM //Serial.println("remote_address"); for (int i = 0; i < 16; i++) { //Serial.println(EEPROM.read(i + 50)); remoteIPAddr[i] = EEPROM.read(i + 50); } } EEPROM.end(); // Free RAM copy of structure }