Skip to content

Instantly share code, notes, and snippets.

@orejuelajd
Last active February 4, 2017 03:16
Show Gist options
  • Save orejuelajd/6ba97160587f0cdad02553cc6b4071b0 to your computer and use it in GitHub Desktop.
Save orejuelajd/6ba97160587f0cdad02553cc6b4071b0 to your computer and use it in GitHub Desktop.

Receive a UDP package

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);
}

Pins mapping arduino-nodemcu

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.

Permission denied in serial port (Ubuntu)

You can do this with

sudo usermod -a -G dialout terrik 

if terrik is your username.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment