#define WOKWI
#ifndef WOKWI
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <TimeLib.h>
#endif
#ifdef WOKWI
#include "TimeLib.h"
#endif
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
#include "global_var.h"
#include "html_gen.h"
#include "RingBuf.h"
RingBuf<uint8_t, 10> myBuffer;
tmElements_t simulationTime; // Holds the simulated time
void insertData()
{
uint8_t d = 10;
for(int j = 0; j < 78; j++)
myBuffer.lockedPushOverwrite(--d);
for ( int i = 0; i < NO_OF_POINTS; i++)
{
year[i] = 2023;
month[i] = 8;
day[i] = 17;
hour[i] = i/4;
minutes[i] = (i%4)*15;
sp[i][0] = 20+i;
set[i][0] = 23;
}
for ( int i = 0; i < 123/*NO_OF_POINTS*/; i++)
{
t[0].lockedPushOverwrite(23.0+5.0*cos((6.0*3.141*i)/100.0));
}
}
void updateData()
{
// Print the simulated time
Serial.print("Simulated Time: ");
Serial.print(year()); // Year
Serial.print("-");
Serial.print(month()); // Month
Serial.print("-");
Serial.print(day()); // Day
Serial.print(" ");
Serial.print(hour()); // Hour
Serial.print(":");
Serial.print(minute()); // Minutes
Serial.print(":");
Serial.print(second()); // Seconds
Serial.println();
year[i] = year();
month[i] = month();
day[i] = day();
hour[i] = hour();
minutes[i] = minute();
sp[i][0] = 20+i;
set[i][0] = 23;
}
for ( int i = 0; i < 123/*NO_OF_POINTS*/; i++)
{
t[0].lockedPushOverwrite(23.0+5.0*cos((6.0*3.141*i)/100.0));
}
}
void handleRoot(); // function prototypes for HTTP handlers
void handleLogin();
void handleNotFound();
void handleSubmit();
void setup(void)
{
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
wifiMulti.addAP("Borgetjernsveien13", "99648882"); // add Wi-Fi networks you want to connect to
wifiMulti.addAP("Borgetjernsveien13_5G", "99648882");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
Serial.println("mDNS responder started");
} else {
Serial.println("Error setting up MDNS responder!");
}
server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
server.on("/login", HTTP_POST, handleLogin); // Call the 'handleLogin' function when a POST request is made to URI "/login"
server.on("/submit", HTTP_POST, handleSubmit); // Call the 'handleLogin' function when a POST request is made to URI "/login"
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
server.begin(); // Actually start the server
Serial.println("HTTP server started");
// Set the initial time manually (January 1, 2023, 00:00:00)
simulationTime.Year = 2023 - 1970; // Year since 1970
simulationTime.Month = 1;
simulationTime.Day = 1;
simulationTime.Hour = 0;
simulationTime.Minute = 0;
simulationTime.Second = 0;
setTime(makeTime(simulationTime)); // Set the initial time
insertData();
}
void loop(void)
{
server.handleClient(); // Listen for HTTP requests from clients
static const unsigned long REFRESH_INTERVAL = 1000; // ms
static unsigned long lastRefreshTime = 0;
if(millis() - lastRefreshTime >= REFRESH_INTERVAL)
{
lastRefreshTime += REFRESH_INTERVAL;
Serial.println("looping");
simulationTime.Minute += 15;
// Correct for overflow if needed
if (simulationTime.Minute >= 60) {
simulationTime.Minute -= 60;
simulationTime.Hour += 1;
}
setTime(makeTime(simulationTime)); // Update the internal time
updateData();
handleRoot();
}
}
int selectedTemp = 195;
int selectedZone = 1;
bool selectedMode = 1; //Manual = 0, Auto = 1
void handleRoot()
{
String heading, tail;
insertData();
makeHeading(heading, selectedZone , selectedTemp, selectedMode);
makeDataFromArray(0/*selectedZone*/ );
makeTail(tail, selectedZone );
String html = heading + buffer + tail;
// When URI / is requested, send a web page with a button to toggle the LED
// Use https://tomeko.net/online_tools/cpp_text_escape.php?lang=en to convert from html to string
server.send(200, "text/html", html);
}
void handleSubmit()
{
Serial.println("Submit clicked ");
if( !server.hasArg("temps"))
{
Serial.println("No argument named: temps!!");
}
else
{
selectedTemp = (server.arg("temps")).toInt();
//Serial.println("Selected temp = " + selectedTemp);
}
if( !server.hasArg("zones"))
{
Serial.println("No argument named: zones!!");
}
else
{
selectedZone = (server.arg("zones")).toInt();
Serial.println("Selected zone = " + server.arg("zones") );
}
if( !server.hasArg("mods"))
{
Serial.println("No argument named: mods!!");
}
else
{
selectedMode= (server.arg("mods")).toInt();
Serial.println("Selected mods = " + server.arg("mods"));
}
handleRoot();
}
void handleLogin()
{ // If a POST request is made to URI /login
if( ! server.hasArg("username") || ! server.hasArg("password")
|| server.arg("username") == NULL || server.arg("password") == NULL) { // If the POST request doesn't have username and password data
server.send(400, "text/plain", "400: Invalid Request"); // The request is invalid, so send HTTP status 400
return;
}
if(server.arg("username") == "Nils Tore" && server.arg("password") == "password123")
{ // If both the username and the password are correct
server.send(200, "text/html", "<h1>Welcome, " + server.arg("username") + "!</h1><p>Login successful</p>");
} else
{ // Username and password don't match
server.send(401, "text/plain", "401: Unauthorized");
}
}
void handleNotFound(){
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}