Cdj IR-RF Box

6 years 1 week ago - 5 years 11 months ago #2325 by cdj
Cdj IR-RF Box was created by cdj
Ok guys, back again...

I'm doing a box with an IR-trasmitter (based on Lesjaw infrared sample - http://iot-playground.com/forum/my-project/262-infrared-transmitter-door-window-sensor#1660 ) and an RF trasmitter.

So :
Arduino is connected correctly ONLY to irled at the moment and it works with trasmitting sample.

Problem is always at ESP .... I'm using an ESP-07 with ElectroDragon adapter ( http://www.electrodragon.com/product/esp8266-smd-adapter-board/ ) and flashing with arduino ide working correctly.... but in serial monitor :
WiFi connected
192.168.0.151
connection failed
Connecting to AP...
WiFi connected
192.168.0.151
connection failed
......always the same

So, i've tried to replace sketch with Wifi Gas Sensor (that i use regulary) and always respond me with "connection failed"... WTF? other esp is working perfectly!

Problem is in WiFiClient client; what happen ?

Tnx everybody for helpin...

Please Log in or Create an account to join the conversation.

6 years 6 days ago - 6 years 6 days ago #2355 by cdj
Replied by cdj on topic Cdj IR-RF Box
Ok Fixed this... it was an access point problem !!!

Let's start another issue regarding this trasmitter.

I use RCSwitch Library.... and if i try to send code with :
  mySwitch.send(1394007,24);

it works perfectly.

But we want to receive code from EasyIoT automation, so i wrote in automation :
const string CODICEOFF = "1394004, 24"; 
const string CODICEON = "1394007, 24"; 

....and then ....

 if (m.Domain == "Virtual" && m.Address == MODULE_ADDRESS && p.Property == "Sensor.DigitalValue")

        if (p.Value == "1")
      {
         sendCommand(p.Value + "\n" + CODICEON);
        

      } 
      else 
      {     
         
         sendCommand(p.Value + "\n" + CODICEOFF);
      }

        return true;

    });

So code arrive and i read it with :
 String req = client.readStringUntil('\n');
 String CODICE = client.readStringUntil('\r');

then i convert it in a char array because RCSwitch want char as argument....
 int str_len = CODICE.length() + 1; 
  char code[str_len]; 
  CODICE.toCharArray(code,str_len ) ; 
 
  //send is defined in rcswitch library as 
  //void send(char* Code); 
  

//here i verify if conversion work and printed value is correct....
 
  Serial.print("Req:");
  Serial.println(req);
  Serial.print("Codice da Oggetto Stringa:");
  Serial.println(CODICE);
  Serial.print("Code Array Char convertito :");
  Serial.println(code);
    
 client.flush();

and then i send code :

 if (req.indexOf("/gpio/0") != -1)
 {
   val = 0;
  Serial.print("Mando il codice:");
  Serial.println(code);
  mySwitch.send(code);
    delay(1000);  
 }
 else if (req.indexOf("/gpio/1") != -1)
 {
   val = 1;\
  Serial.print("Mando il codice:");
  Serial.println(code);
  Serial.print("mySwitch.send(");
  Serial.print(code);
  Serial.print(");");
  mySwitch.send(code); 
   delay(1000);  
 }

Ok it compile perfectly, but nothing happen when receive gpio/0 or gpio/1 :(

What can be the error ?

EDIT: and for IR part....i must use arduino because irsend work on interrupt.... and i don't think that esp could have an "interrupt" pin... so... if i use relay sketch i can send an IRcode in ON position and another IRcode in off position using ..
void incomingMessage(const Esp8266EasyIoTMsg &message) {
  // We only expect one type of message from controller. But we better check anyway.

  Serial.println("New message");
  if (message.type==V_DIGITAL_VALUE) {
    // Change relay state
  
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);

  int statoswitch = message.getBool();
    
     if  ( statoswitch == 0) 
        {
       irsend.sendRC6(0x11, 20);
        delay(100);
         } 
     else
        { 
        irsend.sendSony(0xa90, 12); 
        delay(100);
        }
    }
    
    
    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
  } 

But in this way i can command only one device via ir... and this is not our goal... is it possible to send different code with automation ?. If i present (esp.present) 10 switch and EasyIoT recognize as N1S1....N1S10 how i can assign with automation 10 different codes to send on ON position and other 10 for OFF position?

goal is to create an automation script for each device.......IR AND RF in one box... this is a dream or not?

Thanks

Please Log in or Create an account to join the conversation.

6 years 1 day ago #2389 by cdj
Replied by cdj on topic Cdj IR-RF Box
WIN WIN WIN WIN !!! RF ISSUE FIXED !!!!!

need to send code with mySwitch.send(code,bit);

and LONG !!!

long code = stringToLong(CODICE);
long bittaggio = stringToLong(BIT);

THANKS

Please Log in or Create an account to join the conversation.

5 years 11 months ago - 5 years 11 months ago #2413 by cdj
Replied by cdj on topic Cdj IR-RF Box
Finally i do it only with ESP !!! Using IRremoteESP8266 library and normal RC-Switch library.... Arduino IDE.
Rock and Roll !!!

Here is my sketch :
/*
CDJ IR-RF Box.
 */
#include <IRremoteESP8266.h>
#include <RCSwitch.h>
#include <ESP8266WiFi.h>
RCSwitch mySwitch = RCSwitch(); //GPIO 15
IRsend irsend(13); //an IR led is connected to GPIO pin 13

const char* ssid = "myssd";
const char* password = "mypass";
IPAddress ip(192,168,0,54);  //Node static IP
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(15, OUTPUT);
  digitalWrite(15, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
//cdj : 
  WiFi.config(ip, gateway, subnet);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());

   mySwitch.enableTransmit(15);  // Using GPIO2
  //mySwitch.setProtocol(1);
    irsend.begin();
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\n');
  String TIPOREMOTE = client.readStringUntil('\n');
  String CODICE = client.readStringUntil('\n');
  String BIT = client.readStringUntil('\r');
// ----------------------------------------------------
  char codearray[11];
  CODICE.toCharArray(codearray, 11);
 
//Preparo il codice per RF:
  long codeRF = stringToLong(CODICE);

//Preparo il codice per IR:
  unsigned long  codeIR;
  codeIR = codice2ulong(codearray);

 //Preparo il secondo parametro bit
long bittaggio = stringToLong(BIT);

 
  Serial.print("Req:");
  Serial.println(req);
 /* DEBUG PURPOSE
  Serial.print("Codice da Oggetto Stringa:");
  Serial.println(CODICE);
  Serial.print("Code Array convertito :");
  Serial.println(codearray);
  Serial.print("Code IR Unsigned Long convertito :");
  Serial.println(codeIR);
  Serial.print("Bit da Oggetto Stringa:");
  Serial.println(BIT);
  Serial.print("Bit Long convertito :");
  Serial.println(bittaggio);
  */
  client.flush();

//definizione send in libreria : 
/*
 * void sendWhynter(unsigned long data, int nbits);
  void sendNEC(unsigned long data, int nbits);
  void sendSony(unsigned long data, int nbits);
  // Neither Sanyo nor Mitsubishi send is implemented yet
  //  void sendSanyo(unsigned long data, int nbits);
  //  void sendMitsubishi(unsigned long data, int nbits);
  void sendRaw(unsigned int buf[], int len, int hz);
  void sendRC5(unsigned long data, int nbits);
  void sendRC6(unsigned long data, int nbits);
  void sendDISH(unsigned long data, int nbits);
  void sendSharp(unsigned int address, unsigned int command);
  void sendSharpRaw(unsigned long data, int nbits);
  void sendPanasonic(unsigned int address, unsigned long data);
  void sendJVC(unsigned long data, int nbits, int repeat); // *Note instead of sending the REPEAT constant if you want the JVC repeat signal sent, send the original code value and change the repeat argument from 0 to 1. JVC protocol repeats by skipping the header NOT by sending a separate code value like NEC does.
  void sendSAMSUNG(unsigned long data, int nbits);
 */
 
  // Match the request
  int val;
 
 if (req.indexOf("/gpio/0") != -1)
 {
   val = 0;
    //mySwitch.send(1394004,24); // TEST DI CODICE
  Serial.print("Mando il codice:");
  Serial.println(TIPOREMOTE);
  Serial.print(codeIR);
  Serial.print("," );
  Serial.println(bittaggio);
   
   // esempio : irsend.sendNec(0x00FFE01F, 12)
   // altroesempio : //irsend.sendSony(0xa90, 12);
  
   
        if (TIPOREMOTE == "SONY") {
             Serial.println(TIPOREMOTE);
             irsend.sendSony(codeIR, bittaggio);
             delay(2000);
        }

         if (TIPOREMOTE == "NEC") {
            irsend.sendNEC(codeIR, bittaggio);
            delay(2000);
      }

         if (TIPOREMOTE == "RC5") {
            irsend.sendRC5(codeIR, bittaggio);
            delay(2000);
           }

       if (TIPOREMOTE == "RC6") {
             irsend.sendRC6(codeIR, bittaggio);
             delay(2000);
       }
      if (TIPOREMOTE == "SAMSUNG") {
             irsend.sendSAMSUNG(codeIR, bittaggio);
             delay(2000);
       }
        if (TIPOREMOTE == "RF") {
             mySwitch.send(codeRF,bittaggio); 
             delay(1000);
      }
  }
 
 
 else if (req.indexOf("/gpio/1") != -1)
 {
   val = 1;\
   //mySwitch.send(1394007,24); // TEST DI CODICE
  Serial.print("Mando il codice:");
  Serial.println(TIPOREMOTE);
  Serial.print(codeIR);
  Serial.print("," );
  Serial.println(bittaggio);

      
        if (TIPOREMOTE == "SONY") {
             Serial.println(TIPOREMOTE);
             irsend.sendSony(codeIR, bittaggio);
             delay(100);
             irsend.sendSony(codeIR, bittaggio);
             delay(2000);
        }

         if (TIPOREMOTE == "NEC") {
            irsend.sendNEC(codeIR, bittaggio);
            delay(100);
            irsend.sendNEC(codeIR, bittaggio);
            delay(2000);
      }

         if (TIPOREMOTE == "RC5") {
            irsend.sendRC5(codeIR, bittaggio);
            delay(100);
            irsend.sendRC5(codeIR, bittaggio);
            delay(2000);
           }

       if (TIPOREMOTE == "RC6") {
             irsend.sendRC6(codeIR, bittaggio);
             delay(100);
             irsend.sendRC6(codeIR, bittaggio);
             delay(2000);
       }
      if (TIPOREMOTE == "SAMSUNG") {
             irsend.sendSAMSUNG(codeIR, bittaggio);
             delay(100);
             irsend.sendSAMSUNG(codeIR, bittaggio);
             delay(2000);
       }
      
      if (TIPOREMOTE == "RF") {
             mySwitch.send(codeRF,bittaggio); 
             delay(1000);
        }
  
 }

  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

 
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disconnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed

}

//funzione per il secondo parametro bit
long stringToLong(String s)
{
   char arr[12];
   s.toCharArray(arr, sizeof(arr));
   return atol(arr);
}
 
 //funzione per il primo parametro IR che vuole unsigned long...
 unsigned long int codice2ulong(char * codice_funzione) {
     unsigned long int ret = 0xffffffff;
    int c,k,len,hex,n;

    if (codice_funzione[0] == '0' && codice_funzione[1] == 'x') {
      ret = 0;
      len = strlen(codice_funzione);
      for (k = 0,c = len-1; c>=2; c--,k+=4) {
         hex = codice_funzione[c];
         if (hex >= '0' && hex <= '9'){
           n = hex - 48;
         } else if (hex >= 'A' && hex <= 'Z'){
           n = hex - 55; /* chr65 + 10 s*/
         } else if (hex >= 'a' && hex <= 'z'){
           n = hex - 87; /* chr97 + 10*/
         }
         ret += (unsigned long int) (n * pow(2,k));
      }
    return ret;
  }
}

and automation code :
//const String ESP8266_IP_ADDRESS = "192.168.0.54";
const String ESP8266_IP_ADDRESS = "192.168.0.206";
const String MODULE_ADDRESS = "N6S0"; 
const string TIPOREMOTE = "RF"; //POSSIBILI TIPI : RF (per 433mhz), NEC, SONY, RC5, RC6, RAW
const string CODICEOFF = "1394004"; 
const string CODICEON = "1394007"; 
const string BIT = "24";

/*

  This code is running one time when program is enabled

*/

public void Setup()

{
  

 // System.Diagnostics.Process.Start("CMD.exe","");

  

  EventHelper.ModuleChangedHandler((o, m, p) =>

    {

    Console.WriteLine(m.Domain +" "+ m.Address + " in program id "+ Program.ProgramId.ToString()+ " property "+ p.Property + " value " + p.Value);

 

      if (m.Domain == "Virtual" && m.Address == MODULE_ADDRESS && p.Property == "Sensor.DigitalValue")

        if (p.Value == "1")
      {
         sendCommand(p.Value + "\n" + TIPOREMOTE + "\n" + CODICEON + "\n" + BIT);
        

      } 
      else 
      {     
         
         sendCommand(p.Value + "\n" + TIPOREMOTE + "\n" +CODICEOFF + "\n" + BIT);
      }

        return true;

    });

 

}

 

/*

  This code is running periodicaly when program is enabled. 

  Cron job detirmine running period.

*/

public void Run()

{

}


private void sendCommand(string value)

{

  sendToServer("/gpio/"+value);

}


private void sendToServer(String message)

{

  try

  {

 // Console.WriteLine("TCP client command:" + message);

   Int32 port = 80;

   System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient( ESP8266_IP_ADDRESS, port);

   Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); 

   System.Net.Sockets.NetworkStream stream = client.GetStream();

   stream.Write(data, 0, data.Length);

   // Close everything.

   stream.Close();         

   client.Close();

  }

  catch(Exception e)

  {

    Console.WriteLine(e.StackTrace);

  }

  
}

Sorry for Italian in code....
Bye
Dario
The following user(s) said Thank You: EasyIoT, NightOne

Please Log in or Create an account to join the conversation.

5 years 11 months ago #2416 by skywatch
Replied by skywatch on topic Cdj IR-RF Box
Nice work!

And thanks for posting, I am sure it will help others out there.

Please Log in or Create an account to join the conversation.

5 years 11 months ago #2418 by EasyIoT
Replied by EasyIoT on topic Cdj IR-RF Box
Nice. That's something on my TODO list, when I will have time...
The following user(s) said Thank You: cdj

Please Log in or Create an account to join the conversation.

Time to create page: 0.260 seconds

Forum latest

  • No posts to display.