#include <WiFi.h>
#include <HTTPClient.h>
const int goodButtonPin = 27; // Pin for the "good" feedback button
const int averageButtonPin = 26; // Pin for the "average" feedback button
const int poorButtonPin = 25; // Pin for the "poor" feedback button
int good_count=0;
int average_count=0;
int bad_count=0;
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
void Feedback(void);
const char *serverUrl = "https://console.thingzmate.com/api/v1/device-types/himalini/devices/himalini/uplink"; // Replace with your server endpoint
String AuthorizationToken = "Bearer 84e949338dcb717f6333ccfc0d301ea5";
void setup() {
pinMode(goodButtonPin, INPUT_PULLUP);
pinMode(averageButtonPin, INPUT_PULLUP);
pinMode(poorButtonPin, INPUT_PULLUP);
Serial.begin(115200);
delay(4000); // Delay to let serial settle
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
Feedback();
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", AuthorizationToken); //Authorization token
// Create JSON payload
String payload = "{\"Good\":" + String(good_count) +",\"Average\":" + String(average_count) + ", \"Poor\":" + String(bad_count) +"}";
// Send POST request
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("HTTP Response code: " + String(httpResponseCode));
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
delay(60000); // Wait for 1 minute before sending next request
}
void Feedback(void)
{
if (digitalRead(goodButtonPin) == LOW) {
good_count++;
//Serial.println(good_count);
// Add your code to handle good feedback here
delay(1000); // Debouncing delay
}
if (digitalRead(averageButtonPin) == LOW) {
// Serial.println("Average feedback received!");
// Add your code to handle average feedback here
average_count++;
// Serial.println(average_count);
delay(1000); // Debouncing delay
}
if (digitalRead(poorButtonPin) == LOW) {
// Serial.println("Poor feedback received!");
// Add your code to handle poor feedback here
bad_count++;
//Serial.println(bad_count);
delay(1000); // Debouncing delay
}
if(millis() % 10000==0)
{
Serial.println("===========================================");
Serial.print("Good feedback received :");
Serial.println(good_count);
Serial.print("Average feedback received:");
Serial.println(average_count);
Serial.print("Poor feedback received:");
Serial.println(bad_count);
Serial.println("===========================================");
}
}