#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "your ssid";
const char* password = "your password";
const char* serverName1 = "https://api.iotsnacksbox.io/trigger/temperature?snacksboxtoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; // Replace the xxx with your token
const char* serverName2 = "https://api.iotsnacksbox.io/trigger/humidity?snacksboxtoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; // Replace the xxx with your token
const char* serverName3 = "https://api.iotsnacksbox.io/trigger/pressure?snacksboxtoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; // Replace the xxx with your token
const char* serverName4 = "https://api.iotsnacksbox.io/trigger/altitude?snacksboxtoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; // Replace the xxx with your token
#define SEALEVELPRESSURE_HPA (1009)
float Temperature;
float Pressure;
float Altitude;
float Humidity;
Adafruit_BME280 bme;
void setup() {
Serial.begin(115200);
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void Post_Temperature() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println("*C");
Temperature = bme.readTemperature();
HTTPClient http;
http.begin(serverName1);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Content-Type", "application/json");
const size_t capacity = 2 * JSON_OBJECT_SIZE(1);
char buffer[capacity];
DynamicJsonDocument doc(capacity);
JsonObject data = doc.createNestedObject("data");
data["Temperature"] = Temperature;
serializeJson(doc, buffer);
Serial.println(buffer);
String httpRequestData = buffer;
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
}
void Post_Humidity() {
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println("%");
Humidity = bme.readHumidity();
HTTPClient http;
http.begin(serverName2);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Content-Type", "application/json");
const size_t capacity = 2 * JSON_OBJECT_SIZE(1);
char buffer[capacity];
DynamicJsonDocument doc(capacity);
JsonObject data = doc.createNestedObject("data");
data["Humidity"] = Humidity;
serializeJson(doc, buffer);
Serial.println(buffer);
String httpRequestData = buffer;
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
}
void Post_Pressure() {
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println("hPa");
Pressure = (bme.readPressure() / 100.0F);
HTTPClient http;
http.begin(serverName3);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Content-Type", "application/json");
const size_t capacity = 2 * JSON_OBJECT_SIZE(1);
char buffer[capacity];
DynamicJsonDocument doc(capacity);
JsonObject data = doc.createNestedObject("data");
data["Pressure"] = Pressure;
serializeJson(doc, buffer);
Serial.println(buffer);
String httpRequestData = buffer;
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
}
void Post_Altitude() {
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println("m");
Altitude = (bme.readAltitude(SEALEVELPRESSURE_HPA));
HTTPClient http;
http.begin(serverName4);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Content-Type", "application/json");
const size_t capacity = 2 * JSON_OBJECT_SIZE(1);
char buffer[capacity];
DynamicJsonDocument doc(capacity);
JsonObject data = doc.createNestedObject("data");
data["Altitude"] = Altitude;
serializeJson(doc, buffer);
Serial.println(buffer);
String httpRequestData = buffer;
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
Post_Temperature();
Post_Humidity();
Post_Pressure();
Post_Altitude();
delay(5000);
}
else {
Serial.println("WiFi Disconnected");
}
}
Comments
Please log in or sign up to comment.