char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
// UDP_TX_PACKET_MAX_SIZE is a constant of WiFiUDP arduino library (WiFiUDP.h)
void readUDPMessage() {
// if there’s data available, read a packet
int packetSize = UDP.parsePacket();
if (packetSize) {
Serial.println("");
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = UDP.remoteIP();
for (int i = 0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(UDP.remotePort());
// read the packet into packetBufffer
UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
String packetStr = String(packetBuffer);
String value0 = getValue(packetStr, '%', 0);
String value1 = getValue(packetStr, '%', 1);
Serial.println(value1);
if(value0 == "hi"){
turnOffLed(value1.toInt());
}
}
delay(10);
}
NodeMCU has weird pin mapping. Pin numbers written on the board itself do not correspond to ESP8266 GPIO pin numbers. We have constants defined to make using this board easier:
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
These are defined here. If you want to use NodeMCU pin 5, use D5 for pin number, and it will be translated to 'real' GPIO pin 14.
You can do this with
sudo usermod -a -G dialout terrik
if terrik is your username.