Skip to content

Instantly share code, notes, and snippets.

@johnty
Created December 12, 2018 05:52
Show Gist options
  • Save johnty/380215ed61d189a3e9eb34c05be6de7d to your computer and use it in GitHub Desktop.
Save johnty/380215ed61d189a3e9eb34c05be6de7d to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <WiFiUdp.h>
WiFiUDP udp;
const char * ssid = "SSID";
const char * password = "PASS";
boolean connected = false;
void setup()
{
Serial.begin(115200);
connectToWiFi(ssid, password);
}
void loop()
{
if (connected) {
int len = udp.parsePacket();
if (len > 0) {
Serial.print("src IP: ");
Serial.print(udp.remoteIP());
Serial.print("; packet: [");
char buf[len];
udp.read(buf, len);
for (int i = 0; i < len; i++) {
Serial.print((char)buf[i]);
}
//String str(buf);
Serial.println("]");
}
}
}
void connectToWiFi(const char * ssid, const char * pwd) {
Serial.println("Connecting to WiFi network: " + String(ssid));
// delete old config
WiFi.disconnect(true);
//register event handler
WiFi.onEvent(WiFiEvent);
//Initiate connection
WiFi.begin(ssid, pwd);
Serial.println("Waiting for WIFI connection...");
}
//wifi event handler
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
//When connected set
Serial.print("WiFi connected! IP address: ");
Serial.println(WiFi.localIP());
udp.beginMulticast(IPAddress(224, 0, 1, 3), 7570); //libmapper admin bus multicast
//udp.begin(7000); //test regular UDP server endpoint
connected = true;
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
connected = false;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment