#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
const char* password = "";
const char* ssid = "Wokwi-GUEST";
const char* dweetIOUrl = "http://dweet.io/dweet/for/KNUS-12-11";
void setup() {
Serial.begin(9600);
while (!Serial);
if (!bme.begin(0x76) && !bme.begin(0x77)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
connectToWiFi();
}
void loop() {
float temperature, humidity, pressure;
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = bme.readPressure() / 100.0;
String postData = "temperatureC=" + String(temperature) + "&pressure=" + String(pressure) + "&humidity=" + String(humidity);
sendDweetIORequest(postData);
delay(60000);
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
}
void sendDweetIORequest(String data) {
HTTPClient http;
if (http.begin(dweetIOUrl)) {
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(data);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("Dweet.io response: ");
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Failed to connect to Dweet.io");
}
}