Skip to content

Instantly share code, notes, and snippets.

@TheSkorm
Last active July 14, 2017 11:19
Show Gist options
  • Select an option

  • Save TheSkorm/07f4e7de4d4c8730c01a0d0b444a9c9d to your computer and use it in GitHub Desktop.

Select an option

Save TheSkorm/07f4e7de4d4c8730c01a0d0b444a9c9d to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "";
const char* password = "";
WiFiUDP Udp;
WiFiUDP Udp2;
WiFiUDP Udp3;
WiFiUDP Udp4;
char incomingPacket[255]; // buffer for incoming packets
const char wol[] {};
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(9);
Udp2.begin(7);
Udp3.begin(0);
Udp4.begin(5600);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), 79);
pinMode(D0,OUTPUT);
digitalWrite(D0, HIGH);
}
void loop()
{
checkPacket(Udp);
checkPacket(Udp2);
checkPacket(Udp3);
checkPacket(Udp4);
delay(10);
}
void checkPacket(WiFiUDP Listener){
int packetSize = Listener.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Listener.remoteIP().toString().c_str(), Listener.remotePort());
int len = Listener.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
for(int x; x<len-6;x++){
if (strncmp(incomingPacket+x, wol, 6) == 0){
Serial.printf("Received WoL Packet");
toggleThings();
break;
}
}
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
}
}
void toggleThings(){
digitalWrite(D0, LOW);
delay(100);
digitalWrite(D0, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment