Link to home
Start Free TrialLog in
Avatar of peterliong
peterliongFlag for Singapore

asked on

C++ Script on ESP8266-12E Board not working. RF Transmitter not Transmitting. Urgently get it working!

Hi I am now doing a project Home Automation Using a Raspberry PI via a router. An ESP8266-12E board with RF Transmitter turns on 3 RF plug switches. It has problem turning on all 3 plug switches. I am using C++ using Arduino IDE which was loaded on the ESP8266-12E board. Can someone help me to find out what is wrong with the code that is causing this?

Status Now:
Switch 1: On (OK)  Off (No Response)
Switch 2: On (No Response)   Off (No Response)
Switch 3: On  (No Response)  Off (No Response)

Checked the frequency that was transmitted to the RF plug switches.

switch 1: On (OK) Off(wrong frequency)
Switch 2: On (Wrong Frequency), Off (Wrong Frequency)
Switch 3: On (No Frequency), Off (No Frequency).


Below is the code:
/*****
 
 All the resources for this project:
 https://rntlab.com/
 
*****/

// Loading the ESP8266WiFi library and the PubSubClient library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Loading the RCSwitch library to control the outlets
#include <RCSwitch.h>

// I've changed the fields below for security!
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "Liong-xxxx";
const char* password = "9222xxxx";

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "192.168.72.xxx";

// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);

// Initializes the RCSwitch
RCSwitch mySwitch = RCSwitch();

// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that 
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic home/office/esp1/gpio2, you check if the message is either 1 or 0. Turns the ESP GPIO according to the message
  if(topic=="home/bedroom/esp1/Fan"){
      Serial.print("Changing Fan to ");
      if(messageTemp == "1"){
        // Sends binary code to turn on Fan
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110001100");
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        // Sends binary code to turn off Fan
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101010101000101010100");
        Serial.print("Off");
      }
  }
  if(topic=="home/bedroom/esp1/Lamp"){
      Serial.print("Changing Lamp ");
      if(messageTemp == "1"){
        // Sends binary code to turn on Lamp
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101010101010001010101");
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        // Sends binary code to turn off Lamp
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110000010");
        Serial.print("Off");
      }
  }
  if(topic=="home/bedroom/esp1/Speaker"){
      Serial.print("Changing Speaker ");
      if(messageTemp == "1"){
        // Sends binary code to turn on Speaker
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110001001");
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        // Sends binary code to turn off Speaker
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110000001");
        Serial.print("Off");
      }
  }    
  Serial.println();
}

// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    
    /*
     YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
     To change the ESP device ID, you will have to give a new name to the ESP8266.
     Here's how it looks:
       if (client.connect("ESP8266Client")) {
     You can do it like this:
       if (client.connect("ESP1_Office")) {
     Then, for the other ESP:
       if (client.connect("ESP2_Garage")) {
      That should solve your MQTT multiple connections problem
    */
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  
      // Subscribe or resubscribe to a topic
      // You can subscribe to more topics (to control more LEDs in this example)
      client.subscribe("home/bedroom/esp1/Fan");
      client.subscribe("home/bedroom/esp1/Lamp");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {

  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  mySwitch.enableTransmit(16);
  
  // SET YOUR PULSE LENGTH
  mySwitch.setPulseLength(155);
  
  // SET YOUR PROTOCOL (default is 1, will work for most outlets)
  mySwitch.setProtocol(1);
  
  // Set number of transmission repetitions.
  mySwitch.setRepeatTransmit(15);
}

// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client");
}

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

the code submitted has to do with MQTT and not using RF modules. please select your code and use the code User generated image from the formatting options available from your edit screen
Avatar of peterliong

ASKER

It is the code that I through Arduino IDE, plug in the ESP8266-12E board and using sketch uploaded to the board!
The Node-Red would be the graphical program that I use to set the nodes, network and the MQTT. That part should be correct. The only part that I'm concern is the code. The signal transmitted to the RF plug switch is wrong! But the first one Switch 1 - On OK (Frequency correct). The rest all wrong!
By the way! Thank you David for responding!
Attached is the file for the decoding of the RF plug switches.Binary code 1.docx
how is the plug configured? Need details on the wireless plug itself.
Light OFF is different pulse rate of 154 ms
common code 15548 address
FAN 155/155 28/20
Light 155/154 26/18
Speaker 155/155 25/17
I didn't configure the wifi plug. Their frequency were detected using Arduino UNO board with RF Receiver. Viewed and recorded on Serial Monitor of Aduino IDE. This is the code use on Arduino UNO.

/*
  Example for receiving
 
  https://github.com/sui77/rc-switch/
 
  If you want to visualize a telegram copy the raw data and
  paste it into http://test.sui.li/oszi/
*/


#include <RCSwitch.h>


RCSwitch mySwitch = RCSwitch();


void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2
}


void loop() {
  if (mySwitch.available()) {
    output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
    mySwitch.resetAvailable();
  }
}

Open in new window

Avatar of phoffric
phoffric

@peterliong,
If you insert your code within the code tags, that makes your posts more readable. Another way is to do this is to highlight your code, and hit the Code Tag button available on each comment post.
/*****
 
 All the resources for this project:
 https://rntlab.com/
 
*****/

// Loading the ESP8266WiFi library and the PubSubClient library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Loading the RCSwitch library to control the outlets
#include <RCSwitch.h>

// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "Liong-Family";
const char* password = "92229486";

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "192.168.72.202";

// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);

// Initializes the RCSwitch
RCSwitch mySwitch = RCSwitch();

// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that 
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic home/office/esp1/gpio2, you check if the message is either 1 or 0. Turns the ESP GPIO according to the message
  if(topic=="home/bedroom/esp1/Fan"){
      Serial.print("Changing Fan to ");
      if(messageTemp == "1"){
        // Sends binary code to turn on Fan
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110001100");
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        // Sends binary code to turn off Fan
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101010101000101010100");
        Serial.print("Off");
      }
  }
  if(topic=="home/bedroom/esp1/Lamp"){
      Serial.print("Changing Lamp ");
      if(messageTemp == "1"){
        // Sends binary code to turn on Lamp
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101010101010001010101");
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        // Sends binary code to turn off Lamp
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110000010");
        Serial.print("Off");
      }
  }
  if(topic=="home/bedroom/esp1/Speaker"){
      Serial.print("Changing Speaker ");
      if(messageTemp == "1"){
        // Sends binary code to turn on Speaker
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110001001");
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        // Sends binary code to turn off Speaker
        // BINARY CODE EXAMPLE. REPLACE WITH YOUR BINARY CODE
        mySwitch.send("000101111011100110000001");
        Serial.print("Off");
      }
  }    
  Serial.println();
}

// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    
    /*
     YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
     To change the ESP device ID, you will have to give a new name to the ESP8266.
     Here's how it looks:
       if (client.connect("ESP8266Client")) {
     You can do it like this:
       if (client.connect("ESP1_Office")) {
     Then, for the other ESP:
       if (client.connect("ESP2_Garage")) {
      That should solve your MQTT multiple connections problem
    */
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  
      // Subscribe or resubscribe to a topic
      // You can subscribe to more topics (to control more LEDs in this example)
      client.subscribe("home/bedroom/esp1/Fan");
      client.subscribe("home/bedroom/esp1/Lamp");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {

  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  mySwitch.enableTransmit(16);
  
  // SET YOUR PULSE LENGTH
  mySwitch.setPulseLength(155);
  
  // SET YOUR PROTOCOL (default is 1, will work for most outlets)
  mySwitch.setProtocol(1);
  
  // Set number of transmission repetitions.
  mySwitch.setRepeatTransmit(15);
}

// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client");
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.