Files
ArduinoOnlineRemote/IrSendRecv.ino

157 lines
4.3 KiB
Arduino
Raw Permalink Normal View History

2024-11-05 13:02:19 +01:00
#include <WiFi.h>
#include <HTTPClient.h>
2024-11-05 13:02:19 +01:00
#include <WebServer.h>
#include <ArduinoJson.h>
#include <IRremote.hpp>
#include <time.h>
2024-11-05 13:02:19 +01:00
const char* ssid PROGMEM = "paradicsom";
const char* password PROGMEM = "19700318";
2024-11-05 13:02:19 +01:00
const char* url PROGMEM = "http://192.168.50.69";
const char* githubFileURL PROGMEM = "https://raw.githubusercontent.com/htamas1210/ArduinoRemoteWeb/master/index.html";
const String githubToken PROGMEM = "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";
String gitwebpage = "";
2024-11-05 13:02:19 +01:00
const char* ntpServer PROGMEM = "hu.pool.ntp.org";
const long gmtOffset_sec PROGMEM = 3600;
const int daylightOffset PROGMEM = 3600;
2024-11-05 13:02:19 +01:00
unsigned long lastRunTime = 0;
const unsigned long interval PROGMEM = 24 * 60 * 60;
WebServer server(80);
HTTPClient httpClient;
2024-11-05 13:02:19 +01:00
//#define DECODE_NEC // Includes Apple and Onkyo. To enable all protocols , just comment/disable this line.
#define NEC_PROTOCOL 8
#define IR_RECEIVE_PIN 2
#define IR_TRANSMIT_PIN 3
2024-11-07 10:06:01 +01:00
#define LED 4
2024-11-05 13:02:19 +01:00
#define DELAY_AFTER_SEND 1000
#define DELAY_AFTER_LOOP 5000
void setup() {
Serial.begin(115200);
2024-11-28 15:39:40 +01:00
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
2024-11-05 13:02:19 +01:00
WiFi.begin(ssid, password);
Serial.println("connecting to wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.println(".");
}
Serial.println();
Serial.print("Connected to wifi. IP: ");
Serial.println(WiFi.localIP());
/////////////////////////////////////////////////////////
//ntp server data
configTime(gmtOffset_sec, daylightOffset, ntpServer);
/////////////////////////////////////////////////////////
pullWebpageData();
//////////////////////////////////////////////////////
2024-11-05 13:02:19 +01:00
// 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);
2024-11-05 13:02:19 +01:00
// 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, LED);
2024-11-05 13:02:19 +01:00
Serial.print(F("Ready to receive IR signals of protocols: "));
printActiveIRProtocols(&Serial);
/////////////////////////////////////////////////////////
2024-11-05 13:02:19 +01:00
// Define route for LED toggle
server.on("/sendSignal", HTTP_POST, []() {
String json = server.arg("plain"); // Get JSON payload as a string
Serial.print("Received JSON: ");
Serial.println(json);
// Parse JSON
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print("Failed to parse JSON: ");
Serial.println(error.c_str());
server.send(400, "text/plain", "Invalid JSON");
return;
}
int code = doc["code"];
Serial.print("code: ");
Serial.println(code);
SendNecSignal(code);
Serial.println();
server.send(200, "text/plain", "SentSignal: " + code);
});
// Start the server
server.begin();
2024-11-28 15:39:40 +01:00
digitalWrite(LED, LOW);
printTime();
2024-11-05 13:02:19 +01:00
}
void loop() {
//calculates time to run webpage pull function
time_t now = time(NULL);
if(now - lastRunTime >= interval){
lastRunTime = now;
pullWebpageData();
printTime();
}
2024-11-05 13:02:19 +01:00
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");
}
}