-
Notifications
You must be signed in to change notification settings - Fork 7
/
ESP_Wahaj.h
94 lines (82 loc) · 2.31 KB
/
ESP_Wahaj.h
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
83
84
85
86
87
88
89
90
91
92
93
94
/* ESP8266 TO PY: LOCAL
* Written by Junicchi
* https://github.com/Kebablord
*Edited and modified to transfer full duplex
*https://github.com/wahajmurtaza/
* MAP
- start(ssid,password)---> Starts the connection with given username and password
- waitUntilNewReq() -----> Waits until a request is received from python
- returnThisInt(data) ---> sends your Integer data to localhost
- returnThisStr(data) ---> sends your String data to localhost
- getPath() -------------> gets the request's path as string, ex: https://192.113.133/here -> "/here"
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
// OUR SERVER'S PORT, 80 FOR DEFAULT
WiFiServer server(80);
WiFiClient client;
String rule;
void start(String ssid, String pass){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(),pass.c_str());
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (!MDNS.begin("esp8266")) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
server.begin();
Serial.println("TCP server started");
MDNS.addService("http", "tcp", 80);
}
bool isReqCame = false;
bool CheckNewReq(){
client = server.available();
if (!client) {
return 0;
}
/*
while (client.connected() && !client.available()) {
delay(1);
}*/ //to make data transfer fast
String req = client.readStringUntil('\r');
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return 0;
}
req = req.substring(addr_start + 1, addr_end);
rule = req;
isReqCame = true;
client.flush();
return 1;
}
void waitUntilNewReq(){
do {CheckNewReq();} while (!isReqCame);
isReqCame = false;
}
void returnThisStr(String final_data){
String s;
client.print(final_data);
}
void returnThisInt(int final_data){
returnThisStr(String(final_data));
}
String getPath(){
return rule;
}