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();
}
//———————————————————————————
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
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.