/* * This sketch demonstrates how to scan WiFi networks. * The API is almost the same as with the WiFi Shield library, * the most obvious difference being the different file you need to include: */ #include #include #include "ESP8266WiFi.h" #include "relais.h" #include "wlan.h" WiFiUDP udpServer; #define UDP_PORT 2703 #define NWLANFAV 2 const char *favorite_wlans[2*NWLANFAV] = { // SSID, password (NULL if open) "Error-403", "2b4EWWpqAyCpgOQK1oeD", "franken.freifunk.net", NULL }; void setup_wifi() { bool connected = false; for(int i = 0; i < NWLANFAV; i++) { connected = (0 == wlan_connectto(favorite_wlans[2*i + 0], favorite_wlans[2*i + 1])); if(connected) { break; } } if(!connected) { Serial.println("All predefined WLANs failed to connect. Trying any open network."); connected = (0 == wlan_connect_any_open()); } if(connected) { wlan_print_conninfo(); } else { Serial.println("All WLAN connections failed. Giving up for now."); } } void setup() { Serial.begin(115200); setup_wifi(); relais_setup(); Serial.print("starting UDP server on port "); Serial.println(UDP_PORT); if(!udpServer.begin(UDP_PORT)) { Serial.println("UDP server start FAILED!"); } Serial.println("Setup done"); } void loop() { char buf[512]; if(WiFi.status() != WL_CONNECTED) { // wifi disconnected -> attempt reconnect delay(5000); setup_wifi(); } /*int n = udpServer.read(buf, 512); if(n > 0) { Serial.print("Received UDP packet with "); Serial.print(n); Serial.println(" bytes."); }*/ // if there's data available, read a packet int packetSize = udpServer.parsePacket(); if(packetSize) { Serial.print("Received packet of size "); Serial.println(packetSize); Serial.print("From "); IPAddress remoteIp = udpServer.remoteIP(); Serial.print(remoteIp); Serial.print(", port "); Serial.println(udpServer.remotePort()); // read the packet into packetBufffer int len = udpServer.read(buf,512); if (len >0) buf[len]=0; /* Serial.println("Contents:"); Serial.println(buf); */ // send a reply, to the IP address and port that sent us the packet we received udpServer.beginPacket(udpServer.remoteIP(), udpServer.remotePort()); udpServer.write("OK\n"); udpServer.endPacket(); } relais_loop(); }