#include "DHT.h" #include #include #include #define DHTPIN 4 #define DHTTYPE DHT11 uint8_t h; uint8_t t; uint16_t interval = 5000; unsigned long currentMillis; unsigned long lastMillis; WiFiMulti wifiMulti; WebSocketsClient webSocket; DHT dht(DHTPIN, DHTTYPE); void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { // Handle WebSocket events here if needed } void setup() { Serial.begin(9600); Serial.println(F("DHTxx test!")); dht.begin(); wifiMulti.addAP("iotroam", "sGMINiJDcU"); wifiMulti.addAP("Ziggo6565749", "Ziggobroek1@"); Serial.println("Connecting Wifi..."); if (wifiMulti.run() == WL_CONNECTED) { Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Connect to WebSocket server webSocket.begin("145.92.8.114", 80, "/ws"); // Replace with your Raspberry Pi's IP address and port webSocket.onEvent(webSocketEvent); webSocket.setReconnectInterval(500); } } void loop() { // update when interval is met if (currentMillis - lastMillis >= interval){ lastMillis = millis(); update(); } // update the counter currentMillis = millis(); float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // Compute heat index in Celsius (isFahrenheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.print(F("°C ")); Serial.print(hic); Serial.print(F("°C ")); // Ensure WebSocket communication webSocket.loop(); } void update(){ webSocket.sendTXT("{\"node\": \"" + String(WiFi.macAddress()) + "\", \"Temp\":\"" + String(t) + "\",\"Humi\":\"" + String(h) + "\"}"); }