/**
* IMPORTANT!!!!!!!!!!:
* Note: I cannot check if the program works or not as the webpage cannot
* be viewed over the internet without port forwarding and the public IP,
* which is managed by Wokwi.
*
* However the Source Code is below. (scroll down)
*/
// To connect with WiFi
#include <WiFi.h>
// To make our device as server
#include <WebServer.h>
// ssid, no password as it is an open network
const char ssid[] = "Wokwi-GUEST";
// RGb led pins
byte r_pin = 25;
byte g_pin = 26;
byte b_pin = 27;
// defining channels
#define r_channel 0
#define g_channel 1
#define b_channel 2
// defining frequency and reolution
#define frequency 5000
#define resolution 8
// WiFiClient for finding public ip
WiFiClient client;
// ip finder url
char serverName[] = "api.ipify.org";
// starting a server at port 80
WebServer server(80);
void setup(){
// setting frequency and resolution at channels
ledcSetup(r_channel , frequency , resolution);
ledcSetup(g_channel , frequency , resolution);
ledcSetup(b_channel , frequency , resolution);
// attaching GPIOs
ledcAttachPin(r_pin , r_channel);
ledcAttachPin(g_pin , g_channel);
ledcAttachPin(b_pin , b_channel);
// starting serial communication
Serial.begin(115200);
// connecting with the WiFi and printing IP Address
Serial.print("Connecting with : ");
Serial.println(ssid);
WiFi.begin(ssid); //only needs ssid because the network is open (no password)
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("Connected with the Network !");
Serial.print("IP address assgined by the access point : ");
Serial.println(WiFi.localIP()); // local IP
get_public_ip();
// ^ gets public ip however we cant connect to the webpage
// over a different network as port 80 is not forwarded on the router
// for the esp32
// server will run specified functions on receiving a connection to each url
server.on("/", on_connect);
server.onNotFound(handle_NotFound);
server.on("/red", view_red);
server.on("/green", view_green);
server.on("/blue", view_blue);
server.on("/yellow", view_yellow);
server.on("/cyan", view_cyan);
server.on("/magenta", view_magenta);
server.on("/orange", view_orange);
// start the server
server.begin();
Serial.println("HTTP Server started");
}
void loop(){
// to handle the client requests at APIs
server.handleClient();
}
// function code
void on_connect() { server.send(200, "text/html", html_code()); }
void handle_NotFound(){ server.send(404, "text/plain", "Not found"); }
void color_generator(byte r , byte g , byte b){
ledcWrite(r_channel , r);
ledcWrite(g_channel , g);
ledcWrite(b_channel , b);
}
String html_code() {
return "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>ESP32 RGB CONTROLLER</title></head><body><div style=\"padding-left: 50%; padding-top: 10%\"><h1>COLOR GENERATOR</h1></div><div style=\"padding-left: 40%\"><a href=\"/red\"><button>Red</button></a><a href=\"/green\"><button>Green</button></a><a href=\"/blue\"><button>Blue</button></a><a href=\"/yellow\"><button>Yellow</button></a><a href=\"/cyan\"><button>Cyan</button></a><a href=\"/magenta\"><button>Magenta</button></a><a href=\"/orange\"><button>Orange</button></a></div></body></html>";
}
void view_red() { color_generator(255, 0, 0); }
void view_green() { color_generator(0, 255, 0); }
void view_blue() { color_generator(0, 0, 255); }
void view_yellow() { color_generator(255, 0, 255); }
void view_cyan() { color_generator(0, 255, 255); }
void view_magenta() { color_generator(255, 0, 255); }
void view_orange() { color_generator(255, 165, 255); }
void get_public_ip()
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET / HTTP/1.0"); //download text
client.println("Host: api.ipify.org");
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}