Skip to content

Instantly share code, notes, and snippets.

@HadiElnemr
Last active August 4, 2021 13:03
Show Gist options
  • Save HadiElnemr/74c4d06299e09aa19360d02443195164 to your computer and use it in GitHub Desktop.
Save HadiElnemr/74c4d06299e09aa19360d02443195164 to your computer and use it in GitHub Desktop.
ESP having controlled LED
#include <ESP8266WiFi.h> // Include the Wi-Fi library
#include <WiFiUdp.h>
const char *ssid = "ESP8266"; // The name of the Wi-Fi network that will be created
const char *password = "00000000"; // The password required to connect to it, leave blank for an open network
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 setup() {
Serial.begin(115200);
delay(10);
Serial.println('\n');
pinMode(2, OUTPUT);
WiFi.softAP(ssid, password); // Start the access point
Serial.print("Access Point \"");
Serial.print(ssid);
Serial.println("\" started");
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP()); // Send the IP address of the ESP8266 to the computer
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
Udp.beginPacket("192.168.4.3", 4210);
Udp.write("Initial msg");
Udp.endPacket();
}
void loop() {
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);
if(incomingPacket[len-1] == '0')
digitalWrite(2,1);
else if(incomingPacket[len-1] == '1')
digitalWrite(2,0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment