#include <WiFi.h>
#include <DHT.h>
#include <Wire.h> // Required for STM32
#include "ThingSpeak.h"
#define DHTPIN PA0 // Define the GPIO pin connected to DHT22 (for STM32)
#define DHTTYPE DHT22 // DHT type is DHT22
#define LED_PIN PB1 // Define the LED pin (for STM32)
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 1914951;
const char* myApiKey = "Q35JGHUROYYFOJSL";
const char* server = "api.thingspeak.com";
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(LED_PIN, OUTPUT);
WiFi.begin((char*)WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("WiFi connected !");
// Print local IP address
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
delay(2000); // Allow some time for sensor to stabilize
float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity in %
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Update ThingSpeak fields
ThingSpeak.writeField(myChannelNumber, 1, temperature, myApiKey);
ThingSpeak.writeField(myChannelNumber, 2, humidity, myApiKey);
// Control LED based on conditions
if (temperature > 35 || temperature < 12 || humidity > 70 || humidity < 40) {
digitalWrite(LED_PIN, HIGH); // Turn on LED if conditions met
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
// Output temperature and humidity to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Check ThingSpeak write status
int status = ThingSpeak.writeFields(myChannelNumber, myApiKey);
if (status == 200) {
Serial.println("Data sent successfully to ThingSpeak!");
} else {
Serial.print("Failed to send data. Error code: ");
Serial.println(status);
}
Serial.println("---");
delay(10000); // Delay between updates (in ms)
}
Loading
stm32-bluepill
stm32-bluepill