-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_esp8266
More file actions
67 lines (55 loc) · 1.58 KB
/
Copy pathcode_esp8266
File metadata and controls
67 lines (55 loc) · 1.58 KB
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
#include <ESP8266WiFi.h>
WiFiClient client;
WiFiServer server(80);
/* WIFI settings */
const char* ssid = " WIFI_NAME "; //WIFI SSID
const char* password = " WIFI_PASSWORD "; //WIFI PASSWORD
/* data received from application */
String data ="";
/* define L298N or L293D motor control pins */
int Relay1 = 2;
void setup()
{
/* initialize control pins as output */
pinMode(Relay1, OUTPUT);
digitalWrite(Relay1,LOW);
//LOW one will be turned on
/* start server communication */
Serial.begin(115200);
connectWiFi();
server.begin();
}
void loop()
{
/* If the server available, run the "checkClient" function */
client = server.available();
if (!client) return;
data = checkClient ();
Serial.print(data);
/************************ Run function according to incoming data from application *************************/
if (data == "1") digitalWrite(Relay1,HIGH);
else if (data == "2") digitalWrite(Relay1,LOW);
}
void connectWiFi()
{
Serial.println("Connecting to WIFI");
WiFi.begin(ssid, password);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("NodeMCU Local IP is : ");
Serial.print((WiFi.localIP()));
}
/********************************** RECEIVE DATA FROM the APP ******************************************/
String checkClient (void)
{
while(!client.available()) delay(1);
String request = client.readStringUntil('\r');
request.remove(0, 5);
request.remove(request.length()-9,9);
return request;
}