-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_jul17b.ino
88 lines (59 loc) · 2.29 KB
/
sketch_jul17b.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
#include <DHT.h> // including the library of DHT11 temperature and humidity sensor
#define DHTTYPE DHT11 // DHT 11
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
DHT dht(D5, DHT11);
#define HOST "localipaddressipv4" // Enter HOST URL without "http:// " and "/" at the end of URL
#define WIFI_SSID "wifi" // WIFI SSID here
#define WIFI_PASSWORD "pass"
#include <WiFiClient.h>
WiFiClient wifiClient;
int val;
int val2;
String sendval, sendval2, postData;
void setup(void)
{ Serial.begin(115200);
Serial.println("Communication Started \n\n");
delay(1000);
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);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
delay(800);
HTTPClient http; // http object of clas HTTPClient
// Convert integer variables to string
sendval = String(h);
sendval2 = String(t);
postData = "sendval=" + sendval + "&sendval2=" + sendval2;
http.begin(wifiClient,"http://localipaddressipv4/sensordata/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; }
}