Added Saving Functionality

This commit is contained in:
Aeris 2018-07-19 21:51:40 +02:00
parent 16d538369a
commit 2f6c7888d5

View file

@ -6,6 +6,7 @@
#include <time.h> #include <time.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <EEPROM.h>
//#include <IRremoteESP8266/src/> //#include <IRremoteESP8266/src/>
#define IRLED_PIN 4 #define IRLED_PIN 4
#define RECV_PIN 5 #define RECV_PIN 5
@ -15,14 +16,65 @@
const char* ssid = "foobar"; const char* ssid = "foobar";
const char* password = ""; const char* password = "";
const char* mqtt_server = "10.42.0.244"; const char* mqtt_server = "10.42.0.244";
decode_results results;
IRsend irsend(IRLED_PIN); IRsend irsend(IRLED_PIN);
IRrecv irrecv(RECV_PIN); //1024, 15U, true); IRrecv irrecv(RECV_PIN); //1024, 15U, true);
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
long lastMsg = 0;
char msg[50]; typedef struct {
int value = 0; /* 501 bytes for 20 commands */
uint32_t codes[20];
char commands[20][20];
uint8_t command_lengths[20];
uint8_t size;
} storage;
storage store;
int deleteCommand(char * command, uint8_t length){
if (length < 20 && store.size > 1){
for ( uint8_t i = 0 ; i < store.size ; i++){
if ( strncmp(command, store.commands[i], length ) == 0){
memcpy( store.commands[i], store.commands[store.size], store.command_lengths[store.size]);
store.command_lengths[i] = store.command_lengths[store.size];
store.codes[i] = store.codes[store.size];
store.size--;
saveStore();
return 0;
}
}
}
return -1;
}
int saveCommand(char * command, uint8_t length, uint32_t code){
if ( length < 20){
memcpy( store.commands[store.size], command, length );
store.command_lengths[store.size] = length;
store.codes[store.size] = code;
store.size++;
saveStore();
return 0;
}
return -1;
}
int saveStore(){
EEPROM.begin(512);
EEPROM.put( 0, store ); // address of begin of eeprom "0"
delay(200);
EEPROM.commit(); // Only needed for ESP8266 to get data written
EEPROM.end(); // Free RAM copy of structure
}
int loadStore(){
EEPROM.begin(512);
EEPROM.get(0, store);
EEPROM.end();
}
void setup_wifi() { void setup_wifi() {
@ -110,7 +162,72 @@ void callback(char* topic, byte* p, unsigned int length) {
} }
} }
if ( length > 11 && strncmp(payload, "nec:", 4) == 0){ //custom:9999 FF if ( store.size > 0){
for ( uint8_t i = 0; i < store.size; i++){
if (strncmp(payload, store.commands[i], store.command_lengths[i]) == 0){
irsend.sendNEC(store.codes[i],32);
}
}
}
if ( length > 16 && strncmp(payload, "record:", 7) == 0){ //record:command:code
//Serial.println("Custom0");
char * a = payload + 7;
char command[20];
uint8_t length = 0;
uint32_t code = 0;
memcpy(command, NULL, 20);
int i = 0;
for ( ; i < 20; i++ ){
if (*(a+i) == ':'){
memcpy(command, a, i);
length = i;
}
}
if ( checkHex(a+i+1,8) ){
char *start = a+1+i;
char *end = a+i+8;
*(end+1) = ' ';
code = strtoul(start,&end,16);
}
if ( code != 0 && length != 0){
if ( saveCommand(command, length, code) == 0 ){
char buff[35];
char * pos = buff;
pos += sprintf(pos, "%s","Added command: ");
pos += sprintf(pos, "%s", command);
client.publish(TOPIC, buff);
}
}
}
if ( length > 16 && strncmp(payload, "delete:", 7) == 0){ //delete:comamnd:
//Serial.println("Custom0");
char * a = payload + 7;
char command[20];
uint8_t length = 0;
memcpy(command, NULL, 20);
int i = 0;
for ( ; i < 20; i++ ){
if (*(a+i) == ':'){
memcpy(command, a, i);
length = i;
}
}
if ( length > 0 ){
if (deleteCommand(command, length) == 0){
char buff[37];
char * pos = buff;
pos += sprintf(pos, "%s","deleted command: ");
pos += sprintf(pos, "%s", command);
client.publish(TOPIC, buff);
}
}
}
if ( length > 11 && strncmp(payload, "nec:", 4) == 0){
//Serial.println("Custom0"); //Serial.println("Custom0");
if ( checkHex(payload+4, 8)){ if ( checkHex(payload+4, 8)){
char *start = payload + 4; char *start = payload + 4;
@ -150,9 +267,9 @@ void setup() {
client.setServer(mqtt_server, 1883); client.setServer(mqtt_server, 1883);
client.setCallback(callback); client.setCallback(callback);
irrecv.enableIRIn(); // Start the receiver irrecv.enableIRIn(); // Start the receiver
loadStore();
} }
decode_results results;
void loop() { void loop() {
if (!client.connected()) { if (!client.connected()) {
@ -173,3 +290,14 @@ void loop() {
irrecv.resume(); irrecv.resume();
} }
} }
/*
7e81fe01 standby
7e817e81 power
5ea1c837 dvr
5ea1936c md/cd-r
5ea16897 tuner
5ea1e11e multi channel in
Code:5ea1837c dvd
Code:5ea12ad5 dtv
*/