Skip to content

Instantly share code, notes, and snippets.

@Robotto
Created March 18, 2022 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robotto/5ef128fba05df807fc6ef656474fe854 to your computer and use it in GitHub Desktop.
Save Robotto/5ef128fba05df807fc6ef656474fe854 to your computer and use it in GitHub Desktop.
ESP8266 UDP RX
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
uint8_t state = 0;
String SSID = "IOT";
String PSK = "mrfunkfunk";
// Initialize the wifi Udp library
WiFiUDP Udp;
const unsigned int localPort = 1337; // local port to listen for UDP packets
const int UDP_PACKET_SIZE = 1;
byte packetBuffer[ UDP_PACKET_SIZE ]; //buffer to hold incoming and outgoing packets
void setup() {
Serial.begin(115200);
WiFi.hostname("UDPRX");
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PSK);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
Udp.begin(localPort);
}
void loop() {
if (Udp.parsePacket()) UDPrx();
yield();
if(state!=0){
//do stuff with state
state=0; //reset state
}
void UDPrx()
{
// We've received a packet, read the data from it
Serial.println("udpRX!");
memset(packetBuffer, 0, UDP_PACKET_SIZE); //reset packet buffer
int read_bytes=Udp.read(packetBuffer,UDP_PACKET_SIZE); // read the packet into the buffer
state = packetBuffer[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment