Antwort auf: IR Receive, Send + Webservice Call

Foren User Projekte IR Receive, Send + Webservice Call Antwort auf: IR Receive, Send + Webservice Call

#1107
Terra
Teilnehmer

Ich hab den Fehler inzwischen gefunden, es war wie vermutet die Variable/Konstante:

#define AVRStopMessage „<?xml version=\“1.0\“ encoding=\“utf-8\“?><YAMAHA_AV cmd=\“PUT\“><Main_Zone><Power_Control><Power>Standby</Power></Power_Control></Main_Zone></YAMAHA_AV>“

Wenn ich die Zeichenkette im obigen Programm reduziere schickt er den Webservice Call, ansonsten nicht.
Ich hab jetzt nochmal ein paar debug ausgaben verkürzt, HTTP Header entfernt und ein paar Sachen anders gelöst oder gelöscht, jetzt klappt alles :)

WENN:
– Ich 3x die “TV” Taste meiner Fernbedienung drücke

DANN:
– Soll sich mein Beamer ausschalten (durch 3x dasselbe IR Signal senden)
– Mein Yamaha AV-Receiver ausschalten (durch Webservice Call auf lokale IP)
– Zusätzlich blinkt eine LED bei jedem IR Signal das er empfängt, nur damit ich sehe ob er noch zuhört.

#include <IRremote.h>
#include <SoftwareSerial.h>
SoftwareSerial esp8266(11, 12); // RX, TX

const char* WiFiSSID       = "*****";
const char* WiFiPASSWORD   = "*****";

const char* AVRHost    = "192.168.0.12";
const char* AVRSubpage = "/YamahaRemoteControl/ctrl";

/*
Yamaha XML Messages
Note!
- AVROutputSetVolume has -39 hardcoded as defined volume
- AVROutputHDMI1 the outputs of the Yamaha must be set via the NAMES that were assigned in 
  the configuration. My HDMI1 output is called HTPC. Default it most likely the name is HDMI1. 
  "HTPC" is hardcoded in the request!
  All of the extra ones are disabled because of space issues...
*/
const char* AVRStopMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?><YAMAHA_AV cmd=\"PUT\"><Main_Zone><Power_Control><Power>Standby</Power></Power_Control></Main_Zone></YAMAHA_AV>";

/* Some IR Codes
TURN BEAMER ON    Value:      4CB340BF Type: 3 Length: 32
TURN BEAMER OFF   Value:      4CB3748B Type: 3 Length: 32
TURN RECEIVER ON/OFF Value:   7E8154AB Type: 3 Length: 32 - And add a FFFFFFFF Type 3 Length 0

Type 2
up type 2 800F041E
down type2 800F841F

*/
const char* sigBeamerOn        = "4CB340BF";
//const char* sigBeamerOff       = "4CB3748B";
//const char* sigAVReceiverOnOff = "7E8154AB";

unsigned long timenow = 1;
unsigned long receivedBeamerOnSignal1 = 0;
unsigned long receivedBeamerOnSignal2 = 0;
unsigned long receivedBeamerOnSignal3 = 0;
const long repeatTimeoutMillisThreshold = 1000;

const int LED_WLAN = 13;
const int GND = 2;
const int RECV_PIN = 7;
const int GND2 = 8;
const int VCC = 9;
const int LED = 5;

IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;

void setup() {

  Serial.begin(19200);
  irrecv.enableIRIn(); // Start the receiver
  esp8266.begin(19200);

  //Set Pins for LED and IR Monitor
  pinMode(GND, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(VCC, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite (GND, LOW);
  digitalWrite (GND2, LOW);
  digitalWrite (VCC, HIGH);

  if (!espConfig()) serialDebug();
  else digitalWrite(LED_WLAN, HIGH);

}

void loop() {

  //Serial.println("I am Alive!");

  //IR RECEIVER SIGNAL
  if (irrecv.decode(&results)) {
    timenow = millis();
    analyzeCode(&results);
    Serial.print(F("ir"));
    digitalWrite(LED, 1);
    delay(50);
    digitalWrite(LED, 0);
    irrecv.resume();
  }

}

void analyzeCode(decode_results *results) {

  results->decode_type;
  
  String receivedSignal;
  receivedSignal = String(results->value, HEX);
  receivedSignal.toUpperCase();

  // So now we are listening if someone has send the PROJECTOR ON ir signal 3 times in a row.
  // we have three variables to store the millisecond when the signal was received.
  // the next variable iteration will be populated by the system if the last signal was send less than
  // repeatTimeoutMillis milliseconds.
  // e.g. if repeatTimeoutMillis=500 it means a signal is valid repeated when the previous signal was 
  // send not longer than half a second before! 
  // Until it reaches the third iteration where it executes the command after 3 repeats of the signal.
  // In case the previous signal was send longer than repeatTimeoutMillis ago the iteration resets and 
  // sets the newly received signal as iteration 1 to start the cycle with this signal again.
  //
  if(receivedSignal == sigBeamerOn) {
    Serial.println(F("+")); //Detected My Signal
    if(receivedBeamerOnSignal1 == 0) {
      receivedBeamerOnSignal1 = timenow;
      Serial.println(F("1")); //Set 1
      
    } else if(receivedBeamerOnSignal1 != 0 && receivedBeamerOnSignal2 == 0) {
      receivedBeamerOnSignal2 = timenow;
      if( receivedBeamerOnSignal2 > receivedBeamerOnSignal1+repeatTimeoutMillisThreshold ) {
        receivedBeamerOnSignal1 = timenow;
        receivedBeamerOnSignal2 = 0;
        receivedBeamerOnSignal3 = 0;
        Serial.println(F("r2")); //Reset on 2
      } else {
        Serial.println(F("2")); //Set 2
      }
      
    } else if(receivedBeamerOnSignal2 != 0) {
      receivedBeamerOnSignal3 = timenow;
      if( receivedBeamerOnSignal3 > receivedBeamerOnSignal2+repeatTimeoutMillisThreshold ) {
        receivedBeamerOnSignal1 = timenow;
        receivedBeamerOnSignal2 = 0;
        receivedBeamerOnSignal3 = 0;
        Serial.println(F("r3")); //Reset on 3
      } else {
        Serial.println(F("3")); //Set 3

        delay(200);

        //TURN OFF PROJECTOR BEGIN
        Serial.println(F("xb")); //Turn off projector
        irsend.sendNEC(0x4CB3748B, 32);
        delay(200);
        irsend.sendNEC(0x4CB3748B, 32);
        delay(200);
        irsend.sendNEC(0x4CB3748B, 32);
        delay(200);
        irsend.sendNEC(0x4CB3748B, 32);
        
        //reenable IR receiver after sending
        irrecv.enableIRIn();
        //TURN OFF PROJECTOR END

        Serial.println(F("xr")); //Turn off AVR-Receiver
        //TURN OFF RECEIVER BEGIN
        manageAVR();
        //TURN OFF RECEIVER END

        receivedBeamerOnSignal1 = 0;
        receivedBeamerOnSignal2 = 0;
        receivedBeamerOnSignal3 = 0;
      }
    }
    
  }

}

//--------------------------------------Yamaha AVR Functions--------------------------------------

void manageAVR()
{

  //Usually there are more headers in the original request, but we need to save space and cannot add them
  String getRequest = "POST "+String(AVRSubpage)+" HTTP/1.1\r\nHost:"+String(AVRHost)+"\r\nContent-Length: " + String(strlen(AVRStopMessage)+1) + "\r\n\r\n" + String(AVRStopMessage) + "\r\n";

  sendCom("AT+CIPSTART=\"TCP\",\""+String(AVRHost)+"\",80", "OK");
  
  sendCom("AT+CIPSEND=" + String(getRequest.length() + 2), ">");
  esp8266.println(getRequest);

  Serial.println(getRequest);

  delay(1000);

  if (esp8266.find("+IPD"))
  {
    if (esp8266.find("\r\n\r\n"))
    {
      sendCom("AT+CIPCLOSE", "OK");
    }
  }

}

//-----------------------------------------Config ESP8266------------------------------------

boolean espConfig()
{
  boolean success = true;
  esp8266.setTimeout(5000);
  success &= sendCom("AT+RST", "ready");
  esp8266.setTimeout(1000);
  if (configStation()) {
    success &= true;
    Serial.println(F("WLAN OK"));
    Serial.println(F("IP:"));
    Serial.println(sendCom("AT+CIFSR"));
  }
  else
  {
    success &= false;
  }
  //shorter Timeout for faster wrong UPD-Comands handling
  success &= sendCom("AT+CIPMODE=0", "OK");
  success &= sendCom("AT+CIPMUX=0", "OK");

  return success;
}

boolean configStation()
{
  boolean success = true;
  success &= (sendCom("AT+CWMODE=1", "OK"));
  esp8266.setTimeout(20000);
  success &= (sendCom("AT+CWJAP=\"" + String(WiFiSSID) + "\",\"" + String(WiFiPASSWORD) + "\"", "OK"));
  esp8266.setTimeout(1000);
  return success;
}

//-----------------------------------------------Controll ESP-----------------------------------------------------

boolean sendCom(String command, char respond[])
{
  esp8266.println(command);
  if (esp8266.findUntil(respond, "ERROR"))
  {
    return true;
  }
  else
  {
    Serial.println("ESP ERR: " + command);
    return false;
  }
}

String sendCom(String command)
{
  esp8266.println(command);
  return esp8266.readString();
}

//-------------------------------------------------Debug Functions------------------------------------------------------

void serialDebug() {
}
  • Diese Antwort wurde geändert vor 9 Jahren von Terra.
  • Diese Antwort wurde geändert vor 9 Jahren von fk.