This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP.ino
82 lines (64 loc) · 1.6 KB
/
ESP.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
int UDP_PORT = 4000;
int Server_Port = 3000;
WiFiUDP UDP;
char packet[255];
void setup() {
Serial.begin(19200);
UDP.begin(UDP_PORT);
}
void loop() {
// command handeling START
String command = "";
if(Serial.available())
{
command = Serial.readString();
}
if(command.indexOf("send") >= 0)
{
String server = command.substring(command.indexOf("_")+1,command.indexOf("+"));
String data = command.substring(command.indexOf("+")+1,command.indexOf("#"));
sendToServer(server,data);
}
else if (command.indexOf("wifi") >= 0 && command.indexOf("status") < 0)
{
String wName = command.substring(command.indexOf("_")+1,command.indexOf("+"));
String pass = command.substring(command.indexOf("+")+1,command.indexOf("#"));
connectWifi(wName,pass);
}
else if ( command.indexOf("wifi") >= 0 && command.indexOf("status") >= 0 )
{
Serial.println(checkWifi());
}
// command handeling END
// wifi read START
int packetSize = UDP.parsePacket();
if (packetSize) {
int len = UDP.read(packet, 255);
if (len > 0)
{
packet[len] = '\0';
}
Serial.println(packet);
}
// wifi read END
}
void connectWifi(String wName,String pass)
{
//wifi_NAME+PASSWORD#
WiFi.disconnect();
WiFi.begin(wName, pass);
}
int checkWifi()
{
//wifi_status
return WiFi.status() == WL_CONNECTED;
}
void sendToServer(String server,String data)
{
//send_IP+DATA#
UDP.beginPacket((char*)server.c_str(), Server_Port);
UDP.println(data);
UDP.endPacket();
}