• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Using ESP-01 wifi enabled relay board problems

Q&A Forum › Category: ESP8266 › Using ESP-01 wifi enabled relay board problems
0 Vote Up Vote Down
johan1 asked 7 years ago

Hi,
I have configured mosquitto server on RasPi and I am using the following 8266 ESP-01 wifi board programmed via the Arduino IDE.
http://www.chinalctech.com/index.php?_m=mod_product&_a=view&p_id=1204
I have it “working” as it joins the WiFi and connects to the mosquitto server.  It subscribes sucessfully to the topic and then receives the commands to open and close the relay.  Up to now I am happy.
Problem is that it uses serial comms between the 8266 and the relay board to instruct on open and close of the relay and this I am having endless problems.  I have not been able to trigger the relay at all.
This should be really simple and I cannot see why I am having so much problems.
Basic code I am using to program the device:
//———————————————————————————
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = “xxxxxx”;
const char* password = “*******”;
const char* mqttServer = “192.168.8.8”;
const int mqttPort = 1883;
const char* mqttUser = “yyyyyy”;
const char* mqttPassword = “********”;
byte openRelay[] = {0xA0, 0x01, 0x01, 0xA2 }; //A00101A2
byte closeRelay[] = {0xA0, 0x01, 0x00, 0xA1 }; //A00100A1
char* stateDesc = “state-closed”;
bool nowOpen = false;
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

Serial.begin(9600, SERIAL_8N1);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);

while (!client.connected()) {
if (!client.connect(“ESP8266Client”, mqttUser, mqttPassword )) {
delay(2000);
}
}

client.publish(“home/esp01”, “state-closed”);
client.subscribe(“home/esp01”);
}

void callback(char* topic, byte* payload, unsigned int length) {

if (length > 0) {
if ((char)payload[0] == ‘0’) {
//Close the valve
delay(1);
Serial.write(closeRelay, sizeof(closeRelay));
/* Serial.write(0xA0);
Serial.write((0x01));
Serial.write((0x00));
Serial.write((0xA1)); */
Serial.flush();
stateDesc = “state-closed”;
nowOpen = false;
client.publish(“home/esp01”, stateDesc);
}
if ((char)payload[0] == ‘1’) {
//Open the valve
delay(1);
Serial.write(openRelay, sizeof(openRelay));
/* Serial.write(0xA0);
Serial.write((0x01));
Serial.write((0x01));
Serial.write((0xA2));*/
Serial.flush();
stateDesc = “state-open”;
nowOpen = true;
client.publish(“home/esp01”, stateDesc);
}
if ((char)payload[0] == ‘2’) {
//Respond with valve status
client.publish(“home/esp01”, stateDesc);
}
}
}

void loop() {
client.loop();
}
//———————————————————————————

Question Tags: ESP01, ESP8266, home automation
3 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

How’s the connection to the device that needs to receive those serial commands? Did you double-check the TX/RX connections? Are they using the same baudrate?
Regards,
Rui

0 Vote Up Vote Down
johan1 answered 7 years ago

Hi Rui,
 
Have a look at the link I posted above, it is a complete board built around the ESP-01.  According to the manufacturer it uses 9600 baud.  I was hoping that someone else may have been working with the same setups and have some advice.  They seem to be similar to the Sonoff units you can buy except that you can re-program the 8266.

0 Vote Up Vote Down
Rui Santos Staff answered 7 years ago

But are you using the default firmware? Or did you flash your own firmware to the module?

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.