Skip to content

Instantly share code, notes, and snippets.

@HadiElnemr
Last active August 4, 2021 13:05
Show Gist options
  • Save HadiElnemr/0428ed3705ed011cebe46837aa66a7d2 to your computer and use it in GitHub Desktop.
Save HadiElnemr/0428ed3705ed011cebe46837aa66a7d2 to your computer and use it in GitHub Desktop.
ESP with Serial monitor controlling and sending 0/1
#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');
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // 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.1", 4210);
Udp.write("Initial msg");
Udp.endPacket();
}
void loop() {
int packetSize = Udp.parsePacket();
if(Serial.available())
{
char x = Serial.read();
Udp.beginPacket("192.168.4.1", 4210);
Udp.write(x);
Serial.print("Writing ");
Serial.println(x);
Udp.endPacket();
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment