WiFi connected
192.168.0.151
connection failed
Connecting to AP...
WiFi connected
192.168.0.151
connection failed
......always the same
Please Log in or Create an account to join the conversation.
mySwitch.send(1394007,24);
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;
});
String req = client.readStringUntil('\n');
String CODICE = client.readStringUntil('\r');
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);
}
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());
}
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
/*
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;
}
}
//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);
}
}
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.