// Add libraries
#include <OneWire.h> // This library allows connection to 1-wire sensors
#include <DallasTemperature.h> // This library is for Dallas Temperature sensors
#include <WiFi.h> // Library for WiFi functionality
#include <HTTPClient.h> // Library for making HTTP requests
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Store URL and key used for HTTP requests
const char* url = "http://tgs.ipto.com.au/2026/26aal/Temperature%20receiver.php";
const String apiKey = "API_KEY";
// GPIO port where DS18B20 temperature sensor is connected
const int oneWireBus = 25;
// Set up oneWire instance to communicate with oneWire devices
OneWire oneWire(oneWireBus);
// Pass oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Define pins for temperature sensor and light sensor
const int TempPin = 22;
const int LightPin = 17;
void setup() {
pinMode(TempPin, INPUT_PULLUP); // Set TempPin as input
Serial.begin(115200); // Start serial communication
Serial.println("Start Displaying Temps....");
sensors.begin(); // Initialize the temperature sensor
// Set up pins
pinMode(oneWireBus, INPUT_PULLUP);
WiFi.mode(WIFI_STA); // Set WiFi mode to station (client mode)
WiFi.disconnect(); // Disconnect from any currently connected networks
WiFi.begin(ssid, password); // Start connection to specified WiFi network
// Wait for WiFi connection to be established
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting via WiFi ... ");
}
// Display the IP address assigned to this device on the network
Serial.println("WiFi Connected");
Serial.print("Assigned IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
if(digitalRead(TempPin) == LOW) { // Check if temperature button is pressed
sensors.requestTemperatures(); // Request temperature readings from all devices on the bus
float temperatureC = sensors.getTempCByIndex(0); // Get the temperature in Celsius from the first sensor
Serial.print(temperatureC);
Serial.println("C");
delay(200);
Serial.println();
Serial.println("Starting request");
// Initialize WiFi and HTTP clients
WiFiClient wifiClient;
HTTPClient httpClient;
// Start an HTTP request
httpClient.begin(wifiClient, url);
// Specify content-type header for form-encoded data
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Create data for request
String requestData = "api_key=" + apiKey + "&buttonPressed=true&data=" + temperatureC;
Serial.print("Request data: ");
Serial.println(requestData);
// Send POST request
Serial.println("Sending request");
int statusCode = httpClient.POST(requestData);
// Check if request was successful
if(statusCode == HTTP_CODE_OK) {
Serial.println("Request successful, response:");
} else {
Serial.println("Request failed, response:");
}
// Print the response from the server
String response = httpClient.getString();
Serial.println(response);
// End this HTTP request
httpClient.end();
// Wait until button is released
while(digitalRead(TempPin) == LOW){
delay(100);
Serial.print(".");
}
}
}