added ntp server to pull time data, trying to update the webpage manually works but buggy

This commit is contained in:
2024-12-02 15:27:52 +01:00
parent dae00d6560
commit 204920c5fb

View File

@@ -2,19 +2,26 @@
#include <HTTPClient.h> #include <HTTPClient.h>
#include <WebServer.h> #include <WebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <IRremote.hpp>
#include <time.h>
const char* ssid = "paradicsom"; const char* ssid PROGMEM = "paradicsom";
const char* password = "19700318"; const char* password PROGMEM = "19700318";
const char* url PROGMEM = "http://192.168.50.69"; const char* url PROGMEM = "http://192.168.50.69";
const char* githubFileURL PROGMEM = "https://raw.githubusercontent.com/htamas1210/ArduinoRemoteWeb/master/index.html"; const char* githubFileURL PROGMEM = "https://raw.githubusercontent.com/htamas1210/ArduinoRemoteWeb/master/index.html";
const String githubToken PROGMEM = "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"; const String githubToken PROGMEM = "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";
String gitwebpage = ""; String gitwebpage = "";
WebServer server(80); const char* ntpServer PROGMEM = "hu.pool.ntp.org";
const long gmtOffset_sec PROGMEM = 3600;
const int daylightOffset PROGMEM = 3600;
unsigned long lastTime = 0; unsigned long lastRunTime = 0;
unsigned long timeDelay = 5000; const unsigned long interval PROGMEM = 1 * 60;
WebServer server(80);
HTTPClient httpClient;
//#define DECODE_NEC // Includes Apple and Onkyo. To enable all protocols , just comment/disable this line. //#define DECODE_NEC // Includes Apple and Onkyo. To enable all protocols , just comment/disable this line.
#define NEC_PROTOCOL 8 #define NEC_PROTOCOL 8
@@ -22,23 +29,10 @@ unsigned long timeDelay = 5000;
#define IR_RECEIVE_PIN 2 #define IR_RECEIVE_PIN 2
#define IR_TRANSMIT_PIN 3 #define IR_TRANSMIT_PIN 3
#define LED 4 #define LED 4
#include <IRremote.hpp>
#define DELAY_AFTER_SEND 1000 #define DELAY_AFTER_SEND 1000
#define DELAY_AFTER_LOOP 5000 #define DELAY_AFTER_LOOP 5000
bool switching = false;
void SendNecSignal(int buttonCode) {
digitalWrite(LED, HIGH);
Serial.print(F("Send NEC with 8 bit address code: "));
Serial.println(buttonCode);
Serial.flush();
IrSender.sendNEC(0x7F00, buttonCode, 1);
delay(DELAY_AFTER_SEND); //delay must be greater than 5 ms (RECORD_GAP_MICROS), otherwise the receiver sees it as one long signal
digitalWrite(LED, LOW);
}
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@@ -56,42 +50,21 @@ void setup() {
Serial.println(); Serial.println();
Serial.print("Connected to wifi. IP: "); Serial.print("Connected to wifi. IP: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
//Download html text from github //ntp server data
configTime(gmtOffset_sec, daylightOffset, ntpServer);
HTTPClient httpClient; /////////////////////////////////////////////////////////
httpClient.begin(githubFileURL); pullWebpageData();
httpClient.addHeader("Authorization", "token " + githubToken);
int httpResponse = httpClient.GET();
if(httpResponse > 0){
gitwebpage = httpClient.getString();
Serial.println("File content: ");
Serial.println(gitwebpage);
}else{
Serial.printf("Error code: %d\n", httpResponse);
}
httpClient.end();
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
IrSender.begin(IR_TRANSMIT_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
// Specify send pin and enable feedback LED at default feedback LED pin // Specify send pin and enable feedback LED at default feedback LED pin
IrSender.begin(IR_TRANSMIT_PIN, ENABLE_LED_FEEDBACK, /*USE_DEFAULT_FEEDBACK_LED_PIN*/ LED);
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED // Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, LED);
Serial.print(F("Ready to receive IR signals of protocols: ")); Serial.print(F("Ready to receive IR signals of protocols: "));
printActiveIRProtocols(&Serial); printActiveIRProtocols(&Serial);
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
// Define route for root
server.on("/", HTTP_GET, []() {
server.send(200, "text/html", gitwebpage);
});
// Define route for LED toggle // Define route for LED toggle
server.on("/sendSignal", HTTP_POST, []() { server.on("/sendSignal", HTTP_POST, []() {
String json = server.arg("plain"); // Get JSON payload as a string String json = server.arg("plain"); // Get JSON payload as a string
@@ -121,9 +94,63 @@ void setup() {
// Start the server // Start the server
server.begin(); server.begin();
digitalWrite(LED, LOW); digitalWrite(LED, LOW);
printTime();
} }
void loop() { void loop() {
//calculates time to run webpage pull function
time_t now = time(NULL);
if(now - lastRunTime >= interval){
lastRunTime = now;
pullWebpageData();
printTime();
}
server.handleClient(); // Handle incoming client requests server.handleClient(); // Handle incoming client requests
} }
void SendNecSignal(int buttonCode) {
digitalWrite(LED, HIGH);
Serial.print(F("Send NEC with 8 bit address code: "));
Serial.println(buttonCode);
Serial.flush();
IrSender.sendNEC(0x7F00, buttonCode, 1);
delay(DELAY_AFTER_SEND); //delay must be greater than 5 ms (RECORD_GAP_MICROS), otherwise the receiver sees it as one long signal
digitalWrite(LED, LOW);
}
void pullWebpageData(){
server.stop();
//Download html text from github
httpClient.begin(githubFileURL);
httpClient.addHeader("Authorization", "token " + githubToken);
int httpResponse = httpClient.GET();
if(httpResponse > 0){
gitwebpage = httpClient.getString();
Serial.println("File content: ");
Serial.println(gitwebpage);
Serial.println("Pulled web data");
}else{
Serial.printf("Error code: %d\n", httpResponse);
}
httpClient.end();
server.on("/", HTTP_GET, []() {
server.send(200, "text/html", gitwebpage);
});
server.begin();
}
void printTime(){
// Get and print current time
struct tm timeinfo;
if (getLocalTime(&timeinfo)) {
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
} else {
Serial.println("Failed to obtain time");
}
}