-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNodeMCU.ino
107 lines (64 loc) · 2.73 KB
/
NodeMCU.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Code Written by Rishi Tiwari
* Website:- https://tricksumo.com
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// Update HOST URL here
#define HOST "example.com" // Enter HOST URL without "http:// " and "/" at the end of URL
#define WIFI_SSID "#######" // WIFI SSID here
#define WIFI_PASSWORD "#######" // WIFI password here
// Declare global variables which will be uploaded to server
int val = 1;
int val2 = 99;
String sendval, sendval2, postData;
void setup() {
Serial.begin(115200);
Serial.println("Communication Started \n\n");
delay(1000);
pinMode(LED_BUILTIN, OUTPUT); // initialize built in led on the board
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{ Serial.print(".");
delay(500); }
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
delay(30);
}
void loop() {
HTTPClient http; // http object of clas HTTPClient
WiFiClient wclient; // wclient object of clas HTTPClient
// Convert integer variables to string
sendval = String(val);
sendval2 = String(val2);
postData = "sendval=" + sendval + "&sendval2=" + sendval2;
// We can post values to PHP files as example.com/dbwrite.php?name1=val1&name2=val2&name3=val3
// Hence created variable postDAta and stored our variables in it in desired format
// For more detials, refer:- https://www.tutorialspoint.com/php/php_get_post.htm
// Update Host URL here:-
http.begin(wclient, "http://example.com/dbwrite.php"); // Connect to host where MySQL databse is hosted
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
Serial.println("Values are, sendval = " + sendval + " and sendval2 = "+sendval2 );
// if connection eatablished then do this
if (httpCode == 200) { Serial.println("Values uploaded successfully."); Serial.println(httpCode);
String webpage = http.getString(); // Get html webpage output and store it in a string
Serial.println(webpage + "\n");
}
// if failed to connect then return and restart
else {
Serial.println(httpCode);
Serial.println("Failed to upload values. \n");
http.end();
return; }
delay(3000);
digitalWrite(LED_BUILTIN, LOW);
delay(3000);
digitalWrite(LED_BUILTIN, HIGH);
val+=1; val2+=10; // Incrementing value of variables
}