#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 19
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "Wokwi-GUEST"
#define WLAN_PASS ""
/****************************************************************************/
Adafruit_BMP085 bmp;
const char* thingsSpeakUrl ="https://api.thingspeak.com/update.json?api_key=";
const char* apiKey = "<api key>";
void setup()
{
Serial.begin(9600);
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!bmp.begin())
{
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
dht.begin();
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
WiFiClient client;
float temperature, pressure;
Serial.print("Temperature = ");
temperature = bmp.readTemperature();
Serial.print(temperature);
pressure = bmp.readPressure();
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" Pa");
String url = String(thingsSpeakUrl) + String(apiKey) + "&field4=" + String(temperature) + "&field5=" + String(pressure);
sendThingsSpeakRequest(url);
}
else
{
Serial.println("WiFi Disconnected");
}
delay(5000);
}
void sendThingsSpeakRequest(String url) {
HTTPClient http;
if (http.begin(url)) {
// http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("Thingspeak response: ");
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Failed to connect to Thingspeak");
}
}