· 5 years ago · Jun 26, 2020, 07:08 AM
1#include
2#include
3// IoT platform Credentials
4String apiKey = “my thingspeak api key”;
5const char* logServer = “api.thingspeak.com”;
6
7// Internet router credentials
8const char* ssid = “ESP8266”;
9const char* password = “12345678”;
10
11ESP8266WebServer server(80);
12
13void setup() {
14Serial.begin(74880);
15WiFi.mode(WIFI_AP_STA);
16setupAccessPoint();
17pinMode(2, OUTPUT); // Make GPIO2 as an OUTPUT to drive Relay MOSFET
18}
19// Handling the / root web page from my server
20void handle_index() {
21server.send(200, “text/plain”, “Get the f**k out from my server!”);
22}
23
24// Handling the /feed page from my server
25void handle_feed() {
26String t = server.arg(“temp”);
27String h = server.arg(“hum”);
28
29server.send(200, “text/plain”, “This is response to client”);
30setupStMode(t, h);
31
32// new code added here for the relay
33long temp = t.toFloat();
34if (temp < 20.0)
35digitalWrite(2, HIGH); // Turn ON Relay on GPIO2
36else
37digitalWrite(2, LOW); // Turn OFF Relay on GPIO2
38}
39void setupAccessPoint(){
40Serial.println("** SETUP ACCESS POINT **");
41Serial.println("- disconnect from any other modes");
42WiFi.disconnect();
43Serial.println("- start ap with SID: "+ String(ssid));
44WiFi.softAP(ssid, password);
45IPAddress myIP = WiFi.softAPIP();
46Serial.print("- AP IP address is :");
47Serial.print(myIP);
48setupServer();
49}
50
51void setupServer(){
52Serial.println("** SETUP SERVER **");
53Serial.println("- starting server :");
54server.on("/", handle_index);
55server.on("/feed", handle_feed);
56server.begin();
57};
58
59void setupStMode(String t, String v){
60Serial.println("** SETUP STATION MODE **");
61Serial.println("- disconnect from any other modes");
62WiFi.disconnect();
63Serial.println();
64Serial.println("- connecting to Home Router SID: **********");
65WiFi.begin("my home router ssid", "my password");
66while (WiFi.status() != WL_CONNECTED) {
67delay(500);
68Serial.print(".");
69}
70Serial.println();
71Serial.println("- succesfully connected");
72Serial.println("- starting client");
73
74WiFiClient client;
75
76Serial.println("- connecting to Database server: " + String(logServer));
77if (client.connect(logServer, 80)) {
78Serial.println("- succesfully connected");
79String postStr = apiKey;
80postStr += "&field1=";
81postStr += String(t);
82postStr += "&field2=";
83postStr += String(v);
84postStr += "\r\n\r\n";
85Serial.println("- sending data…");
86client.print("POST /update HTTP/1.1\n");
87client.print("Host: api.thingspeak.com\n");
88client.print("Connection: close\n");
89client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
90client.print("Content-Type: application/x-www-form-urlencoded\n");
91client.print("Content-Length: ");
92client.print(postStr.length());
93client.print("\n\n");
94client.print(postStr);
95}
96client.stop();
97Serial.println("- stopping the client");
98/** If your ESP does not respond you can just
99*** reset after each request sending
100Serial.println("- trying reset");
101ESP.reset();
102**/
103}
104
105void loop() {
106server.handleClient();
107}