#include <OneWire.h> // This library allows connection to 1-wire sensors
#include <DallasTemperature.h> // This library is for Dallas Temperature Sensors
#include <WiFi.h> // Include the WiFi library to connect to a network
#include <HTTPClient.h> // Include the HTTPClient library to send HTTP requests
// Store network parameters to use when establishing a connection
const char* ssid = "Wokwi-GUEST"; // WiFi network SSID (name)
const char* password = ""; // WiFi password (empty for open networks)
// Store URL and key used for requests
const char* url = "http://tgs.ipto.com.au/2026/26bla/y10_t3_2024/T3_database_to_ESP32.php";
const String apiKey = "API_KEY"; // API key used for authentication when interacting with the web page
const int buttonPin = 5; // Define the GPIO pin connected to the button
// GPIO where the DS18B20 is connected
const int oneWireBus = 25; // Pin for the DS18B20 sensor
const int ledPin = 23; // Pin for LED 1
const int ledPin1 = 21; // Pin for LED 2
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass the OneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
// Set up pins
pinMode(ledPin, OUTPUT); // Set LED 1 pin as output
pinMode(ledPin1, OUTPUT); // Set LED 2 pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
Serial.begin(115200); // Start serial communication for logging
WiFi.mode(WIFI_STA); // Set WiFi mode to station (connect to router)
WiFi.disconnect(); // Disconnect from any currently connected networks
WiFi.begin(ssid, password); // Start connection to specified WiFi network
// Wait for the connection to be established
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting via WiFi ... "); // Log connection attempts
}
// Display the IP address assigned to the device on the network
Serial.println("WiFi Connected");
Serial.print("Assigned IP: ");
Serial.println(WiFi.localIP());
// Start the DS18B20 sensor
sensors.begin();
}
void loop() {
// Wait for the button to be pressed
if (digitalRead(buttonPin) == LOW) {
// Measure temperature if the button is pressed
float temperatureC = sensors.getTempCByIndex(0); // Get temperature in Celsius
Serial.print(temperatureC);
Serial.println("ºC");
Serial.println("Starting request");
// Initialise WiFi and HTTP clients
WiFiClient wifiClient;
HTTPClient httpClient;
// Start an HTTP request
httpClient.begin(wifiClient, url);
// Specify content-type header (form URL encoded)
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Create data for the request
String requestData = "api_key=" + apiKey + "&buttonPressed=true&data=" + temperatureC; // Include API key and temperature data
Serial.print("Request data: ");
Serial.println(requestData);
// Send request
Serial.println("Sending request");
int statusCode = httpClient.POST(requestData); // Send the POST request
// Check if the request was successful
if (statusCode == HTTP_CODE_OK) {
Serial.println("Request successful, response:");
} else {
Serial.println("Request failed, response:");
}
// Write the server's response to the serial monitor
String response = httpClient.getString();
Serial.println(response);
// End the request
httpClient.end();
// Wait until the button is released
while (digitalRead(buttonPin) == LOW) {
delay(100);
Serial.print("."); // Print dots while waiting for the button to be released
}
// Control the LEDs based on temperature
if (temperatureC < 25) {
digitalWrite(ledPin, HIGH); // Turn on LED 1 if temperature is below 25°C
Serial.print("Light 1 On");
delay(500);
digitalWrite(ledPin, LOW); // Turn off LED 1
Serial.println(" - Off");
delay(500);
} else {
digitalWrite(ledPin1, HIGH); // Turn on LED 2 if temperature is 25°C or higher
Serial.print("Light 2 On");
delay(500);
digitalWrite(ledPin1, LOW); // Turn off LED 2
Serial.println(" - Off");
delay(500);
}
}
}