Advertisement
Guest User

Untitled

a guest
May 14th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3.  
  4. // config:
  5.  
  6. const char *ssid = "ESPWifiTeszt";  // You will connect your phone to this Access Point
  7. const char *pw = "nincs"; // and this is the password
  8. IPAddress ip(192, 168, 1, 10); // From RoboRemo app, connect to this IP
  9. IPAddress netmask(255, 255, 255, 0);
  10.  
  11. WiFiUDP UDPTestServer;
  12. unsigned int UDPPort = 4000;
  13. const int packetSize = 20;
  14. char packetBuffer[packetSize];
  15.  
  16. void setup() {
  17.   Serial.begin(115200);
  18.   WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP
  19.   WiFi.softAP(ssid, pw); // configure ssid and password for softAP
  20.  
  21.   Serial.println("Wemos D1R2 UDP teszt");
  22.   Serial.println((String)"SSID: " + ssid + "  PASS: " + pw);
  23.   Serial.println((String)"RoboRemo app must connect to " + ip.toString() + ":" + UDPPort);
  24.  
  25.   UDPTestServer.begin(UDPPort);
  26.  
  27.   pinMode(LED_BUILTIN, OUTPUT);
  28.   digitalWrite(LED_BUILTIN, LOW);
  29. }
  30.  
  31. void loop() {  
  32.    handleUDPServer();
  33.    delay(1);
  34. }
  35.  
  36. void handleUDPServer() {
  37.   int cb = UDPTestServer.parsePacket();
  38.   if (cb) {
  39.     int len = UDPTestServer.read(packetBuffer, packetSize);
  40.     if (len > 0) {
  41.       packetBuffer[len-1] = '\0';
  42.       if (strcmp(packetBuffer, "led_on") == 0) {
  43.            Serial.println("Ha led_on szöveg érkezik...");
  44.           digitalWrite(LED_BUILTIN, HIGH);
  45.        }
  46.        if (strcmp(packetBuffer, "led_off") == 0) {
  47.           Serial.println("Ha led_off szöveg érkezik...");
  48.           digitalWrite(LED_BUILTIN, LOW);
  49.        }
  50.     }
  51.   }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement