Files
ArduinoOnlineRemote/IrSendRecv.ino

254 lines
6.7 KiB
C++

#include <WiFi.h>
#include <HTTPClient.h>
#include <WebServer.h>
#include <ArduinoJson.h>
#include <IRremote.hpp>
#include <time.h>
#include <Wire.h>
#include <Adafruit_PN532.h>
#include <WiFiUdp.h>
#include <WakeOnLan.h>
const char* ssid PROGMEM = "paradicsom";
const char* password PROGMEM = "19700318";
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 = "";
const char* wakeUrl PROGMEM = "http://192.168.50.69:5000/wake";
const char* ntpServer PROGMEM = "hu.pool.ntp.org";
const long gmtOffset_sec PROGMEM = 3600;
const int daylightOffset PROGMEM = 3600;
unsigned long lastRunTime = 0;
const unsigned long interval PROGMEM = 5 * 60;
WebServer server(80);
HTTPClient httpClient;
WiFiUDP UDP;
WakeOnLan WOL(UDP);
//#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
#define LED 4
#define DELAY_AFTER_SEND 1000
#define DELAY_AFTER_LOOP 5000
#define SDA A4
#define SCL A5
Adafruit_PN532 nfc(SDA, SCL);
// Known NFC card UID (replace with your specific card UID)
const uint8_t knownUID[] PROGMEM = { 0x23, 0xD8, 0x4F, 0x15 }; // Replace with your card UID
const int uidLength PROGMEM = sizeof(knownUID) / sizeof(knownUID[0]);
void setup() {
Serial.flush();
delay(2000);
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
//nfc
Serial.println("Initializing nfc board...");
nfc.begin();
uint32_t versionData = nfc.getFirmwareVersion();
if(!versionData){
Serial.println("Did not find pn532 compatible board!");
while(true){
Serial.print(".");
}
}
nfc.SAMConfig();
Serial.println("NFC is initialized");
//wifi
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();
//////////////////////////////////////////////////////
// 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
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, LED);
Serial.print(F("Ready to receive IR signals of protocols: "));
printActiveIRProtocols(&Serial);
/////////////////////////////////////////////////////////
// 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();
digitalWrite(LED, LOW);
printTime();
}
void loop() {
//check every time if wifi is active
if(!WiFi.isConnected()) {
WiFi.disconnect();
WiFi.begin(ssid, password);
Serial.print("Reconnecting to wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("\n\n");
}
//calculates time to run webpage pull function
time_t now = time(NULL);
if(now - lastRunTime >= interval){
lastRunTime = now;
pullWebpageData();
printTime();
}
//nfc
uint8_t success;
uint8_t uid[7]; // Buffer to hold the UID
uint8_t uidLength; // Length of the UID
// Look for an NFC card
Serial.println("Waiting for an NFC card...");
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
Serial.println("NFC Card detected!");
Serial.print("UID Length: "); Serial.println(uidLength);
Serial.print("UID Value: ");
for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
Serial.println();
// Check if the detected card matches the known UID
if (uidLength == sizeof(knownUID) && memcmp(uid, knownUID, uidLength) == 0) {
Serial.println("Known NFC card detected! Sending HTTP request...");
/*httpClient.begin(wakeUrl);
int httpResponseCode = httpClient.GET();
if (httpResponseCode > 0) {
Serial.printf("HTTP Response Code: %d\n", httpResponseCode);
} else {
Serial.printf("Error sending HTTP request: %s\n", httpClient.errorToString(httpResponseCode).c_str());
}
httpClient.end();*/
wakePc();
} else {
Serial.println("Unknown NFC card.");
}
delay(2000); // Debounce delay
}
//nfc end
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");
}
}
void wakePc(){
Serial.println("waking pc");
const char* MacAddress PROGMEM = "70:85:C2:94:14:33";
WOL.sendMagicPacket(MacAddress);
}