/***************************************** ****************************************** ** ** ** ESP8266_WiFI_UDP_Server_OLED.ino ** ** ** ****************************************** *****************************************/ #include #include #include #include #include // Name and password of the WLAN access point #define SSID "dummy_wlan_box" #define PASSWORD "dummy_password" // The ESP-12 has a blue LED on GPIO2 #define LED 2 String newHostname = "ESP8266NodeMCU"; // our server accepts connections on this port #define PORT 100 // create the server WiFiUDP udpServer; // OLED display class pointer Adafruit_SSD1306 *display; // Buffer for incoming UDP messages char udp_buffer[WIFICLIENT_MAX_PACKET_SIZE+1]; volatile uint16_t seconds = 0; volatile uint16_t newmsg = 0; // flag to display new message volatile uint32_t led_on = 0; // LED on time uint8_t newMACAddress[] = {0x00, 0x90, 0xB7, 0x33, 0x22, 0x11}; volatile uint16_t recv_msg_valid = 0; /*************************************************/ /* Receive UDP messages and send an echo back */ /*************************************************/ void process_incoming_udp (void) { if (udpServer.parsePacket()) { uint16_t ll; newmsg = 1; // flag to display new message recv_msg_valid = 1; // Fetch received message int len = udpServer.read(udp_buffer,sizeof(udp_buffer)-1); udp_buffer[len] = 0; // Display the message Serial.print(F("Received from ")); Serial.print(udpServer.remoteIP()); Serial.print(":"); Serial.print(udpServer.remotePort()); Serial.print(": "); Serial.println(udp_buffer); // Send echo back udpServer.beginPacket(udpServer.remoteIP(), udpServer.remotePort()); udpServer.write("Echo: "); ll = strlen (udp_buffer); udpServer.write(udp_buffer, ll+1); // make len including 0 at the end udpServer.endPacket(); // Execute some commands if (strstr(udp_buffer, "on")) { LED_on (); udpServer.println(F("LED is on")); } else if (strstr(udp_buffer, "off")) { LED_off (); udpServer.println(F("LED is off")); } } } /* --- process_incoming_udp --- */ /*************************************************/ /* Optional: Notify about AP connection */ /* status changes */ /*************************************************/ void check_ap_connection (void) { static wl_status_t preStatus = WL_DISCONNECTED; wl_status_t newStatus = WiFi.status(); if (newStatus != preStatus) { if (newStatus == WL_CONNECTED) { digitalWrite(LED, LOW); // Display the own IP address and port Serial.print(F("AP connection established, listening on ")); Serial.print(WiFi.localIP()); Serial.print(":"); Serial.println(PORT); Serial.println(" "); } else { digitalWrite(LED, HIGH); Serial.println(F("AP conection lost")); Serial.println(" "); } preStatus = newStatus; } } /* --- check_ap_connection --- */ /************************************/ /* OLED Output */ /************************************/ void handle_oled (int count) { display->clearDisplay(); display->setTextSize(1); display->setTextColor(SSD1306_WHITE); display->setCursor(0, 0); display->println("ESP8266 UDP Server"); display->print("Uptime: "); display->print(count); display->println(" sec"); display->print("IP "); display->print(WiFi.localIP()); display->print(":"); display->println(PORT); display->println(""); if (recv_msg_valid != 0) { // Display the message display->print(F("Rcv ")); display->print(udpServer.remoteIP()); display->print(":"); display->print(udpServer.remotePort()); display->println(""); display->println(udp_buffer); } display->display(); } /* --- handle_oled --- */ /************************************/ /* Runs once at startup */ /************************************/ void setup (void) { uint16_t ii; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define OLED_ADDRESS 0x3C // If not work please scan the bus #define OLED_SDA 14 // D6 #define OLED_SCL 12 // D5 display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Wire.begin(OLED_SDA, OLED_SCL); display->begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS); //handle_oled (1); title_screen (); // LED off pinMode(LED, OUTPUT); digitalWrite(LED, HIGH); // Initialize the serial port Serial.begin(115200); for (ii=0; ii<10; ii++) { Serial.println (".... "); delay(200); } Serial.println (" "); Serial.println ("--- UDP Server ---"); Serial.println ("Setup start .... "); Serial.print ("MAC address: "); Serial.println(WiFi.macAddress()); Serial.println (" "); // Give the serial monitor of the Arduino IDE time to start delay(500); Serial.print ("Connecting to access point ... "); Serial.println(SSID); // Use an external AP WiFi.mode(WIFI_STA); #if 0 // we could change the MAC address if desired wifi_set_macaddr (STATION_IF, &newMACAddress[0]); Serial.print ("MAC address: "); Serial.println(WiFi.macAddress()); Serial.println (" "); #endif WiFi.hostname(newHostname.c_str()); WiFi.begin(SSID, PASSWORD); Serial.println ("Starting the UDP server ... "); udpServer.begin(PORT); Serial.println("Setup completed. "); Serial.println(" "); } /* --- void setup() --- */ void LED_off (void) {digitalWrite(LED, HIGH);} void LED_on (void) {digitalWrite(LED, LOW);} /************************************/ /* Opening msg on OLED display */ /************************************/ void title_screen (void) { LED_on (); display->clearDisplay(); display->setTextSize(2); display->setTextColor(SSD1306_WHITE); display->setCursor(0, 0); display->println(" ESP8266"); display->println("UDP Server"); display->println("Startup..."); display->display(); }/* --- title_screen --- */ /************************************/ /* Main loop, executed repeatedly */ /************************************/ void loop (void) { uint32_t new_millis; static uint32_t old_millis = 0; process_incoming_udp(); // check if there is a new message check_ap_connection(); // WIFI status ok? new_millis = millis(); if (newmsg != 0) // flag to display new message { handle_oled (seconds); newmsg = 0; // clear flag to display new message } else if ( (new_millis-old_millis) > 1000) { seconds++; handle_oled (seconds); old_millis = new_millis; led_on = new_millis; // LED on time LED_on(); // LED flashing } if ( (new_millis-led_on) > 35) { led_on = 0; // LED on time LED_off(); } } /* --- void loop() --- */ /* ------------------------------------------------------ */ /* ---- end of file ESP8266_WiFI_UDP_Server_OLED.ino ---- */ /* ------------------------------------------------------ */