-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266_http_xampp.ino
129 lines (110 loc) · 2.98 KB
/
esp8266_http_xampp.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN D2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
""" Setup wifi and server """
// String wifi name
const char* ssid = "Wifi Name";
// String password name
const char* password = "Wifi Password";
// Set server
// http://[IP Wifi]:8080/[Xampp path]/[php file]
String api_server = "http://[192.168.x.x]:8080/iot_nutshell_php_code/add_data.php";
DHT_Unified dht(DHTPIN, DHTTYPE);
ESP8266WiFiMulti WiFiMulti;
void setup()
{
Serial.begin(115200);
// Serial.setDebugOutput(true);
dht.begin();
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--)
{
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("EPS-TOT", "");
}
void loop()
{
String sensor_name = "node1";
sensors_event_t event;
dht.temperature().getEvent(&event);
float temperature;
float humidity;
if (isnan(event.temperature))
{
Serial.println(F("Error reading temperature!"));
}
else
{
temperature = event.temperature;
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity))
{
Serial.println(F("Error reading humidity!"));
}
else
{
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
}
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED))
{
WiFiClient client;
HTTPClient http;
String urlString = "http://10.10.9.69/temperature_app/add_data.php";
urlString += "?temp=";
urlString += String(temperature);
urlString += "&humid=";
urlString += String(event.relative_humidity);
urlString += "&name='";
urlString += sensor_name;
urlString += "'";
Serial.print(urlString);
Serial.print("[HTTP] begin...\n");
if (http.begin(client, urlString))
{ // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
Serial.printf("[HTTP} Unable to connect\n");
}
}
delay(10000);
}