#include "WiFi.h"
#include <HTTPClient.h>
// https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.h
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define DEBUG_MODE true
#define PURPLE_BUTTON_PIN 26
#define YELLOW_BUTTON_PIN 27
#define BLUE_BUTTON_PIN 14
#define GREEN_BUTTON_PIN 12
#define RED_BUTTON_PIN 13
#define N_BUTTONS 5
// const uint16_t kPort = 61616;
// const String kHost = "rothberrypi.duckdns.org";
// const String kRoute = "change_color";
const uint16_t kPort = 443; // because https
const String kHost = "https://clara-lights.onrender.com";
const String kRoute = "change_color";
HTTPClient http;
const int buttonPins[] = {
PURPLE_BUTTON_PIN, YELLOW_BUTTON_PIN, BLUE_BUTTON_PIN,
GREEN_BUTTON_PIN, RED_BUTTON_PIN};
// Initialize all button states to HIGH for memory; this works
// because we will run all the button pins in INPUT_PULLUP mode,
// so when pressed the voltage will drop to LOW.
int buttonState[] = {HIGH, HIGH, HIGH, HIGH, HIGH};
int checkForButtonPress() {
// Read the voltage over all the buttons and check whether
// the current signal is different than what is logged.
for (int i=0; i < N_BUTTONS; i++) {
int button_state = digitalRead(buttonPins[i]);
int last_button_state = buttonState[i];
if (last_button_state != button_state) {
buttonState[i] = button_state;
}
if (last_button_state == LOW && button_state == HIGH) {
return buttonPins[i];
}
}
return -1;
}
bool sendAPIRequest(int color_code, bool debug) {
String color_str;
switch (color_code) {
case RED_BUTTON_PIN:
color_str = "red";
break;
case BLUE_BUTTON_PIN:
color_str = "blue";
break;
case GREEN_BUTTON_PIN:
color_str = "green";
break;
case YELLOW_BUTTON_PIN:
color_str = "yellow";
break;
case PURPLE_BUTTON_PIN:
color_str = "purple";
break;
default:
color_str = "black";
}
String full_route = "/" + kRoute + "/" + color_str;
Serial.print("Attemping to access route ");
Serial.println(full_route);
// - USE IF PI
// http.begin(kHost, kPort, full_route);
// - USE IF RENDER.COM
http.begin(kHost + full_route);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
if (debug) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println();
Serial.println(payload);
}
return true;
}
else {
if (debug) {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println();
Serial.println(payload);
Serial.println();
Serial.println(":-(");
}
return false;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(" .");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
Serial.print("Setting up button pins");
for (int i=0; i < N_BUTTONS; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
Serial.print(" .");
}
Serial.println(" done.");
}
void loop() {
// put your main code here, to run repeatedly:
int button_pressed;
button_pressed = checkForButtonPress();
if (button_pressed > 0) {
Serial.print("Button press -> ");
Serial.println(button_pressed);
Serial.print("API Call -> ");
bool result = sendAPIRequest(button_pressed, DEBUG_MODE);
if (result) {
Serial.println("success.");
} else {
Serial.println("failure.");
}
}
delay(10); // this speeds up the simulation
}