/*
This program provides a basic example of how to send a HTTP request once
connected to the internet.
This program will send a request to the given web server every 5 seconds and
output the response to the terminal.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <NetworkClientSecure.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Store network parameters to use when establishing connection
const char* ssid = "Wokwi-GUEST"; //"REPLACE_WITH_SSID";
// Store URL and key used for requests
const char* url = "http://tgs.ipto.com.au/2026/26rus/Y10/Term%203/ardiouno.php"; // Replace with your web page
const String apiKey = "123457890"; // Choose a string to use here - similar to a password to verify who you are when you interact with the web page
const int buttonPin = 26;
const int buttonPin1 = 22;
const int oneWireBus = 33;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup() {
// Set up pins
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
Serial.begin(115200); // Start serial to log information to computer
sensors.begin();
WiFi.mode(WIFI_STA); // Set WiFi mode to station -> allows connections to access points such as routers
WiFi.disconnect(); // Disconnect from any currently connected networks
WiFi.begin(ssid); // Start connection to specified wifi network
// Wait for connection to be established
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting via WiFi ... ");
}
// Display the IP address that can be used to identify this device on the network
Serial.println("WiFi Connected");
Serial.print("Assigned IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Wait for button to be pressed
if(digitalRead(buttonPin) == LOW) { // If button pressed
Serial.println();
Serial.println("Starting request");
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0); // scan for current temp
Serial.print(temperatureC); // puts current temp into a var
Serial.println("ºC");
// Initialise clients
WiFiClient wifiClient;
HTTPClient httpClient;
// Start a HTTP request
httpClient.begin(wifiClient, url);
// Specify content-type header - this tells the server the format that the data
// will be sent in, in this case we will sent it in an encoded string
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Create data for request
String requestData = "api_key=" + apiKey + "&buttonPressed=true&data=like&temp=" + temperatureC; // sends temp to database
Serial.print("Request data: ");
Serial.println(requestData);
// Send request
Serial.println("Sending request");
int statusCode = httpClient.POST(requestData);
// Check that request was successful by checking the status code matches "OK"
if(statusCode == HTTP_CODE_OK) {
Serial.println("Request successful, response:"); // If sending data was sucessful
} else {
Serial.println("Request failed, response:"); // If sending data failed
}
// Write response into terminal
String response = httpClient.getString();
Serial.println(response);
// End this request
httpClient.end();
// Wait until button is released
while(digitalRead(buttonPin) == LOW)
{
delay(10);
}
}
// Wait for button to be pressed
if(digitalRead(buttonPin1) == LOW) {
Serial.println();
Serial.println("Starting request");
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
Serial.print(temperatureC);
Serial.println("ºC");
// Initialise clients
WiFiClient wifiClient;
HTTPClient httpClient;
// Start a HTTP request
httpClient.begin(wifiClient, url);
// Specify content-type header - this tells the server the format that the data
// will be sent in, in this case we will sent it in an encoded string
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Create data for request
String requestData = "api_key=" + apiKey + "&buttonPressed=true&data=bad&temp=" + temperatureC;
Serial.print("Request data: ");
Serial.println(requestData);
// Send request
Serial.println("Sending request");
int statusCode = httpClient.POST(requestData);
// Check that request was successful by checking the status code matches "OK"
if(statusCode == HTTP_CODE_OK) {
Serial.println("Request successful, response:");
} else {
Serial.println("Request failed, response:");
}
// Write response into terminal
String response = httpClient.getString();
Serial.println(response);
// End this request
httpClient.end();
// Wait until button is released
while(digitalRead(buttonPin) == LOW)
{
delay(10);
}
}
}