diff --git a/iot_esp32/main.ino b/iot_esp32/main.ino new file mode 100644 index 0000000..6784998 --- /dev/null +++ b/iot_esp32/main.ino @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include + +#define DHT22_PIN 23 +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 +const char *ssid = "your_ssid"; +const char *password = "your_password"; +String serverAddress = "your_server_address"; + +DHT dht22(DHT22_PIN, DHT22); +Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); + +void setup() +{ + Serial.begin(9600); + + dht22.begin(); + 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 loop() +{ + float humi = dht22.readHumidity(); + float tempC = dht22.readTemperature(); + + if (isnan(tempC) || isnan(tempF) || isnan(humi)) + { + Serial.println("Failed to read from DHT22 sensor!"); + } + else + { + Serial.print("Humidity: "); + Serial.print(humi); + Serial.print("% | Temperature: "); + Serial.print(tempC); + Serial.println("°C"); + } + + if (WiFi.status() == WL_CONNECTED) + { + JsonDocument doc; + doc["temperature"] = tempC; + doc["humidity"] = humi; + char output[256]; + serializeJson(doc, output); + WiFiClient client; + HTTPClient http; + + http.begin(client, serverAddress); + http.addHeader("Content-Type", "application/json"); + int httpResponseCode = http.POST(output); + Serial.print("HTTP Response code: "); + Serial.println(httpResponseCode); + + http.end(); + } + else + { + Serial.println("WiFi Disconnected"); + } + + delay(2000); +}