-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi.cpp
74 lines (55 loc) · 1.54 KB
/
wifi.cpp
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
#include "wifi.h"
//Conexiones de entrada y salida arduino RX TX
SoftwareSerial esp(8, 9);
WifiClass::WifiClass() {
}
void WifiClass::setup() {
esp.begin(115200);
reset();
}
void WifiClass::connect(String ssid, String password) {
String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
esp.println(cmd);
delay(4000);
if(esp.find("OK")) {
Serial.println("Connected!");
} else {
connect(ssid, password);
Serial.println("Cannot connect to wifi");
}
}
void WifiClass::http(String server, String uri, String data) {
esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",8080");//start a TCP connection.
if( esp.find("OK")) {
Serial.println("TCP connection ready");
}
delay(1000);
String postRequest =
"POST " + uri + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/json\r\n" +
"\r\n" + data;
String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
esp.print(sendCmd);
esp.println(postRequest.length() );
delay(500);
if(esp.find(">")) {
Serial.println("Sending.."); esp.print(postRequest);
if( esp.find("SEND OK")) {
Serial.println("Packet sent");
while (esp.available()) {
String tmpResp = esp.readString();
Serial.println(tmpResp);
}
esp.println("AT+CIPCLOSE");
}
}
}
void WifiClass::reset() {
esp.println("AT+RST");
delay(1000);
if(esp.find("OK")) Serial.println("Module Reset");
}
WifiClass Wifi = WifiClass();