-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_sensor
94 lines (70 loc) · 2.21 KB
/
esp32_sensor
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
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define SEALEVELPRESSURE_HPA (1013.25)
#include <BH1750.h>
String postMessage;
BH1750 lightMeter;
Adafruit_BME280 bme; // I2C
const char* ssid = "SSID";
const char* password = "PASSWORD";
char jsonOutput[128];
unsigned long delayTime;
void setup() {
Serial.begin(115200);
delay(4000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
Serial.println(F("BME280 test"));
Wire.begin();
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);
Serial.println(F("BH1750 One-Time Test"));
bool status;
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
Serial.println("-- Default Test --");
delayTime = 1000;
Serial.println();
}
void loop(){
float lux = lightMeter.readLightLevel();
float temperature = bme.readTemperature();
float pressure = bme.readPressure() / 100.0F;
float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
float humidity = bme.readHumidity();
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient client;
client.begin("http://192.168.1.116:5001/sensor"); //Specify the URL
client.addHeader("Content-Type", "application/json");
const size_t CAPACITY = JSON_OBJECT_SIZE(10);
StaticJsonDocument<CAPACITY> doc;
JsonObject object = doc.to<JsonObject>();
object["lux"] = lux;
object["temperature"] = temperature;
object["pressure"] = pressure;
object["altitude"] = altitude;
object["humidity"] = humidity;
serializeJson(doc, jsonOutput);
int httpCode = client.POST(String(jsonOutput));
client.end();
if (httpCode > 0) { //Check for the returning code
String payload = client.getString();
Serial.println("\nStatuscode: " + String(httpCode));
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
}
delay(10000);
}