#include <WiFi.h>
#include <HTTPClient.h>
// Store network parameters to use when establishing connection
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Store URL and key used for requests
const char* url = "http://tgs.ipto.com.au/2026/26bro/Year10/Assignment/likes.php"; // Replace with your web page
const String apiKey = "API_KEY"; // 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 dislikebuttonPin = 25;
void setup() {
// Set up pins
pinMode(buttonPin, INPUT_PULLUP);
pinMode(dislikebuttonPin, INPUT_PULLUP);
Serial.begin(115200); // Start serial to log information to computer
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, password); // 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) {
Serial.println();
Serial.println("Starting request");
// 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";
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(100);
Serial.print(".");
}
}
// Wait for button to be pressed
if(digitalRead(dislikebuttonPin) == LOW) {
Serial.println();
Serial.println("Starting request");
// 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=Dislike";
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(dislikebuttonPin) == LOW)
{
delay(100);
Serial.print(".");
}
}
}