Gravar e ler o codigo do controle remoto direto do sd card

Bom Dia Pessoal do Forum.

Estou desevolvendo um app android de controle remoto para tv para controlar um arduino com Esp8266,
gostaria da ajuda de vcs com o codigo arduino.

Parte do codigo foi copiado do Albatross Master

O app tem um botão gravar IR, que envia um comando para o arduino gravar o codigo no sd card,

Primeiro o comando que pede para o arduino dar a permissão para gravar o codigo IR. Exemplo: http://192.168.0.132:8080=gravarIR

em seguida o comando que grava o nome do botão.

Exemplo: http://192.168.0.132:8080=pwr  (botão liga e desliga)

e depois é precionado o botão que deseja copiar do controler remo.

Depois que os codigos são gravados envia o comando.

Exemplo: http://192.168.0.132:8080=pwr  (botão liga e desliga) 

e o arduino ler o codigo que foi gravado no sd card. ex: pwr,0x00FFE01F

Codigo Exemplo do Arduino.

#include "ESP8266WiFi.h"
#include "WiFiUdp.h"
#include "SPI.h"
#include "SD.h"
#include "IRremoteESP8266.h"

int RECV_PIN = 2; //IR detector ( GPIO pin 2 = D4)

IRrecv irrecv(RECV_PIN);
IRsend irsend(0);

WiFiUDP Udp;

void setup() {
pinMode(SSpin_SD, OUTPUT);
digitalWrite(SSpin_SD, HIGH); //disable SD

// --- configure SD ---
EnableSD();
if(!mySD.begin(SSpin_SD)){
Serial.println("ERROR: SD not initialized");
SDinitialized = false;
//
} else {
Serial.println("SD initialized");
SDinitialized = true;
}

//create folders if doesn't exist
if(SDinitialized){
//IR folder
if(!mySD.exists(IRfolder.folder)){
mySD.mkdir(IRfolder.folder);
Serial.println("IR/ created");
}

//Access folder
if(!mySD.exists(RFIDfolder.folder)){
mySD.mkdir(RFIDfolder.folder);
Serial.println("Access/ created");

irsend.begin();

Serial.begin(115200);

int cnt = 0;

Udp.begin(port);

WiFi.mode(WIFI_STA);

while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
if(cnt++ >= 10){
WiFi.beginSmartConfig();
while(1){
delay(1000);
if(WiFi.smartConfigDone()){
Serial.println("SmartConfig Success");
break;
}
}
}
}
}
void loop() {

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]="";
int packetSize = Udp.parsePacket();
if(packetSize){
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());

Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
value=packetBuffer;

if(value.indexOf("gravarIR")==0) {
value.replace("gravarIR", " ");
int gravarIR;
gravarIR = convertToInt(value);
gravarIR = r*10;
}
IRRecord(gravarIR);
}
if(value.indexOf("pwr")==0) {
value.replace("pwr", " ");
int pwr;
pwr = convertToInt(value);
pwr = r*10;
IRSend(pwr);
}
if(value.indexOf("ch+")==0) {
value.replace("ch+", " ");
int ch+;
ch+ = convertToInt(value);
ch+ = r*10;
IRSend(ch+);
}
if(value.indexOf("ch-")==0) {
value.replace("ch-", " ");
int ch-;
ch- = convertToInt(value);
ch- = r*10;
IRSend(ch-);
}
if(value.indexOf("vl+")==0) {
value.replace("vl+", " ");
int vl+;
vl+ = convertToInt(value);
vl+ = r*10;
IRSend(vl+);
}
if(value.indexOf("vl-")==0) {
value.replace("vl-", " ");
int vl-;
vl- = convertToInt(value);
vl- = r*10;
IRSend(vl-);
}
if(value.indexOf("menu")==0) {
IRSend(int value);
}
}

int convertToInt(String value){
char buf[value.length()];
value.toCharArray(buf,value.length());
return atoi(buf);
}
// *************************************************************************************
// IR - RECORD *************************************************************************

// Record the IR command
void IRRecord(gravarIR){
Serial.println(); //for debugging
AvailableMemory(&Serial, true); //for debugging

//check if SD was initialized
if(!SDinitialized){
Serial.println("No SD");
RCdelay(2000); // wait a bit
BlinkStop();
return;
}

Serial.println("IR RECORD");
boolean res = false; //default
boolean gotoend = false; //Arduino DOES NOT allow labels after the goto statement
SaveIR = false; //reset

//record
decode_results Results;
IRreceiver.enableIRIn(); //start de receiver
Serial.println("Recording...");

boolean leave = false;
unsigned long start_time = millis();
unsigned long current_time;
while(!leave){
current_time = millis();

//check for timeout
if((current_time - start_time) >= RECORDING_TIMEOUT)
leave = true;

//check for valid input
if(IRreceiver.decode(&Results)){
leave = true;
SaveIR = true; //set
res = true; //set
}
}

IRreceiver.resume(); //must be called after enable

//check whether to save or not
if(!SaveIR){
Serial.println("\tTIMEOUT");
gotoend = true;
}

if(!gotoend){ //gotoend 2
Serial.println("Converting");

//convert date
// To store raw codes:
// Drop first value (gap)
// Convert from ticks to microseconds
// Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
IRlength = Results.rawlen - 1;
for(int i=1 ; i < Results.rawlen ; i++){
if (i % 2) {
// Mark
IRbuffer[i-1] = Results.rawbuf[i]*USECPERTICK - MARK_EXCESS; //IRbuffers starts at 0 and rawbuf at 1
Serial.print(" m");
Serial.print(IRbuffer[i-1], DEC);
}
else {
// Space
IRbuffer[i-1] = Results.rawbuf[i]*USECPERTICK + MARK_EXCESS; //IRbuffers starts at 0 and rawbuf at 1
Serial.print(" s");
Serial.print(IRbuffer[i-1], DEC);
}
}
Serial.println();

digitalWrite(pinIRRecord, HIGH); //turn LED on
res = true; //successful
} //gotoend 2

//send response
OSCMessage resMes;
resMes.setAddress(_mes->getIpAddress(), clientPort);
resMes.beginMessage("/RoboCore/IR/record");
resMes.addArgInt32(res);
client.send(&resMes);
}

// IR - SEND ***************************************************************************

// Send/Save the IR command
void IRSend(OSCMessage *_mes){
Serial.println(); //for debugging
AvailableMemory(&Serial, true); //for debugging

//check if SD was initialized
if(!SDinitialized){
Serial.println("No SD");
//blink Yellow
BlinkEnable(pinLEDRed, 100);
RCdelay(2000); // wait a bit
BlinkStop();
return;
}

if(SaveIR)
Serial.println("IR SAVE");
else
Serial.println("IR SEND");
boolean res = false; //default
boolean gotoend = false; //Arduino DOES NOT allow labels after the goto statement

//check for valid name
char *fname = NULL;
if(StrParserQty(_mes->getOSCAddress(), '/') >= 4){
fname = StrParser(_mes->getOSCAddress(), '/', 4); //get the name
}
if(fname != NULL){
if(!BuildFileName(&IRfolder, fname)){ //_mes
Serial.println("Invalid file name!");
gotoend = true;
}
}
Mfree(fname); //free the string allocated by StrParser()

if(!gotoend){ //gotoend 1
if(SaveIR){ // ---------------------------------------- Save to SD
EnableSD(); //enable the SD

//remove file if exists
if(mySD.exists(file_name)){
mySD.remove(file_name);
Serial.println("Removed file");
}

//write to file
File myfile = mySD.open(file_name, FILE_WRITE);
writeUINT(&myfile, IRlength);
myfile.write('|');
for(int i=0 ; i < IRlength ; i++){
writeUINT(&myfile, IRbuffer[i]);
}
myfile.close();

Serial.print("\tSaved as ");
Serial.print(file_name);

SaveIR = false; //reset
digitalWrite(pinIRRecord, LOW); //turn LED off
res = true; //successful
} else { // ---------------------------------------- Send from SD
EnableSD(); //enable the SD

//check if file exists
if(!mySD.exists(file_name)){
Serial.println("File DOES NOT exist");
gotoend = true;
}

if(!gotoend){ //gotoend 2
//parse number of times to repeat
int repeat = 1; //by default send just one time
if(StrParserQty(_mes->getOSCAddress(), '/') >= 5){
char *value = StrParser(_mes->getOSCAddress(), '/', 5);
repeat = atoi(value);
if((repeat <= 0) || (repeat > 30))
repeat = 1;
Mfree(value); //free the string allocated by StrParser()
}

//read from file
File myfile = mySD.open(file_name, FILE_READ);
IRlength = 0; //reset
if(myfile.available() >= 2){
IRlength = readUINT(&myfile); //read length
myfile.read(); // '|' character
}
//check if valid length
if(IRlength <= 0){ myfile.close(); Serial.println("INVALID length"); gotoend = true; } if(!gotoend){ //gotoend 3 //store in buffer unsigned int i = 0; while (myfile.available()) { IRbuffer[i] = readUINT(&myfile); i++; } myfile.close(); //start to blink LED BlinkEnable(pinIRSend, 10); // Assume 38 KHz for(int i=0 ; i < repeat ; i++){ IRsender.sendRaw(IRbuffer, IRlength, 3Cool; delayMicroseconds(20); } //stop blink BlinkStop(); digitalWrite(pinIRSend, LOW); res = true; //successful } //gotoend 3 } //gotoend 2 } } //gotoend 1 //END of function EnableEthernet(); //re-enable Ethernet OSCMessage resMes; resMes.setAddress(_mes->getIpAddress(), clientPort);
resMes.beginMessage("/RoboCore/IR/send");
resMes.addArgInt32(res);
client.send(&resMes);
}

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(Bord_Name);
Udp.endPacket();
}

desde já muito obrigado pela aajuda..

Exibições: 437

As respostas para este tópico estão encerradas.

Respostas a este tópico

Oi JPS, boa tarde.

Caramba, que tópico autoexplicativo.

Rui

Rui , você ainda não consegue ler a mente dos colegas do Lab ? he he he 

Eu também não ....

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço