#include <WiFi.h>
#include <HTTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// 1. FILL IN YOUR NETWORK DETAILS
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// 2. FILL IN YOUR LAPTOP'S IP ADDRESS (e.g., http://192.168.1.15:5000/temp)
const char* serverUrl = "https://twenty-nights-roll.loca.lt/temp";
// Hardware Pins
const int SENSOR_PIN = 4; // DS18B20 Data pin
const int FAN_PIN = 5; // Pin connected to the NPN transistor
OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
pinMode(FAN_PIN, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
if (tempC != DEVICE_DISCONNECTED_C) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Keep it as http (no 's')
http.begin(serverUrl);
// These headers trick ngrok into thinking we are a normal browser
http.addHeader("Content-Type", "text/plain");
http.addHeader("User-Agent", "Mozilla/5.0");
http.addHeader("ngrok-skip-browser-warning", "true");
String payload = "TEMP:" + String(tempC);
int httpResponseCode = http.POST(payload);
Serial.print("HTTP Code: ");
Serial.println(httpResponseCode);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Server Response: " + response);
// Simple check for the command
if (response.indexOf("FAN:ON") != -1) {
digitalWrite(FAN_PIN, HIGH);
} else {
digitalWrite(FAN_PIN, LOW);
}
} else {
Serial.println("Error: " + String(httpResponseCode));
}
http.end();
}
}
delay(3000); // Wait 3 seconds before next reading
}