Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- //SOME CONTSTANS
- const char* ssid = "SlinkyBoard";
- const char* password = "letsmove";
- ////////////WIFI UDP PART///////////
- WiFiUDP Udp;
- unsigned int localUdpPort = 4210; // local port to listen on
- char incomingPacket[255]; // buffer for incoming packets
- char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
- void replyUDP();
- void getUDP();
- void setup()
- {
- Serial.begin(115200);
- Serial.println();
- //Połaczenie z WiFi - niepotrzebne - robimy SoftAP//
- /* Serial.printf("Connecting to %s ", ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println(" connected");
- */
- ///////////Łączenie z soft AP/////////////////
- Serial.print("Setting soft-AP ... ");
- WiFi.softAP(ssid, password) ? "Ready" : "Failed!";
- /////////Rozpoczęcie nasłuchiwania po pakiet UDP///////
- Udp.begin(localUdpPort);
- Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
- }
- void loop()
- {
- char[] dataRecived
- dataRecived[] = getUDP();
- replyUDP();
- }
- void replyUDP() // send back a reply, to the IP address and port we got the packet from
- {
- Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
- Udp.write(replyPacket);
- Udp.endPacket();
- }
- char[] getUDP()
- {
- int packetSize = Udp.parsePacket();
- if (packetSize)
- {
- // receive incoming UDP packets
- Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
- int len = Udp.read(incomingPacket, 255);
- if (len > 0)
- {
- incomingPacket[len] = 0;
- }
- Serial.printf("UDP packet contents: %s\n", incomingPacket);
- return incomingPacket;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement