123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include <WiFiEsp.h>
- #include <WiFiEspUdp.h>
- #ifndef HAVE_HWSERIAL1
- #include "SoftwareSerial.h"
- SoftwareSerial Serial1(6, 7);
- #endif
- char ssid[] = "Twim";
- char pass[] = "12345678";
- int status = WL_IDLE_STATUS;
- unsigned int localPort = 10002;
- char packetBuffer[255];
- char ReplyBuffer[] = "ACK";
- WiFiEspUDP Udp;
- void setup() {
-
- Serial.begin(115200);
-
- Serial1.begin(9600);
-
- WiFi.init(&Serial1);
-
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
-
- while (true);
- }
-
- while ( status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
-
- status = WiFi.begin(ssid, pass);
- }
-
- Serial.println("Connected to wifi");
- printWifiStatus();
- Serial.println("\nStarting connection to server...");
-
- Udp.begin(localPort);
-
- Serial.print("Listening on port ");
- Serial.println(localPort);
- }
- void loop() {
-
- int packetSize = Udp.parsePacket();
- if (packetSize) {
- Serial.print("Received packet of size ");
- Serial.println(packetSize);
- Serial.print("From ");
- IPAddress remoteIp = Udp.remoteIP();
- Serial.print(remoteIp);
- Serial.print(", port ");
- Serial.println(Udp.remotePort());
-
- int len = Udp.read(packetBuffer, 255);
- if (len > 0) {
- packetBuffer[len] = 0;
- }
- Serial.println("Contents:");
- Serial.println(packetBuffer);
-
- Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
- Udp.write(ReplyBuffer);
- Udp.endPacket();
- }
- }
- void printWifiStatus() {
-
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
-
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
-
- long rssi = WiFi.RSSI();
- Serial.print("signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- }
|