Hi all,
Hitting a wall getting serial communications between my ESP8266 device (ESP-01) and my nano using EasyTransfer. Basically I want to control the nano via MQTT on the ESP. I've got another device running the MQTT protocol fine, so that part is not the issue.
The device will simply be 8 digital inputs and 4 digital outputs to control an air conditioning duct system. I need the ESP to send the duct open/close commands and receive the statuses, but I can't even get a simple on/off command through. I've cut out all the other code to just get it down to the very basics and it still won't work. This latest iteration is for the ESP to change the output variable every 2 seconds, then the Arduino mirrors this output on the pin.
I've tried dozens of permutations! I'm completely open to using any other serial communication protocol. I'd rather not buy another Arduino w/ built-in esp8266 if I can avoid it (I have a bunch of nanos and ESP-01s available).
Here's the code:
ESP code:
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <MQTTClient.h>
#include <MQTT.h>
#include <EasyTransfer.h>
#define baud 9600
#define REGS 12
#define MSG_BUFFER_SIZE (50)
WiFiClient espClient;
MQTTClient client;
EasyTransfer ETin, ETout;
const char* ssid = "wifi";
const char* password = "password";
const char* mqtt_server = "192.168.1.10";
const char* mqtt_user= "mqtt";
const char* mqtt_password= "mqttpassword";
unsigned long lastMsg = 0;
unsigned int regAddr[REGS];
String regNames[REGS];
unsigned int regData[REGS];
signed int prevRead[REGS];
unsigned const int z1Control = 2;
unsigned const int z2Control = 3;
unsigned const int z3Control = 4;
unsigned const int z4Control = 5;
unsigned const int z1Half = 6;
unsigned const int z1Full = 7;
unsigned const int z2Half = 8;
unsigned const int z2Full = 9;
unsigned const int z3Half = 10;
unsigned const int z3Full = 11;
unsigned const int z4Half = 12;
unsigned const int z4Full = 12;
struct RECEIVE_DATA_STRUCTURE{
int regData[REGS];
};
struct SEND_DATA_STRUCTURE{
//int regAddr[REGS];
int regData[REGS];
};
RECEIVE_DATA_STRUCTURE rxdata;
SEND_DATA_STRUCTURE txdata;
void setupArrays() {
for(int i=0;i<=REGS;i++){
prevRead[i] = -1;
}
for(int i=0;i<=REGS;i++){
txdata.regData[i] = 1;
}
regAddr[0] = z1Control;
regAddr[1] = z2Control;
regAddr[2] = z3Control;
regAddr[3] = z4Control;
regAddr[4] = z1Half;
regAddr[5] = z1Full;
regAddr[6] = z2Half;
regAddr[7] = z2Full;
regAddr[8] = z3Half;
regAddr[9] = z3Full;
regAddr[10] = z4Half;
regAddr[11] = z4Full;
regNames[z1Control] = "z1Control";
regNames[z2Control] = "z2Control";
regNames[z3Control] = "z3Control";
regNames[z4Control] = "z4Control";
regNames[z1Half] = "z1Half";
regNames[z1Full] = "z1Full";
regNames[z1Half] = "z2Half";
regNames[z1Full] = "z2Full";
regNames[z1Half] = "z3Half";
regNames[z1Full] = "z3Full";
regNames[z1Half] = "z4Half";
regNames[z1Full] = "z4Full";
}
void setup(){
Serial.begin(baud);
// WiFi.begin(ssid, password);
//
// client.begin(mqtt_server, espClient);
// client.setOptions(5, false, 15000);
// client.onMessage(messageReceived);
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ETin.begin(details(rxdata), &Serial);
ETout.begin(details(txdata), &Serial);
setupArrays();
}
void messageReceived(String &topic, String &payload) {
String tempTopic = "AC/cmd/";
unsigned int temp = payload.toInt();
for (int i=0;i<=REGS; i++){
tempTopic += regNames[regAddr[i]];
if (topic == tempTopic){
txdata.regData[i] = temp;
}
tempTopic = "AC/cmd/";
// ETout.sendData();
}
}
void reconnect() {
char tempTopic[MSG_BUFFER_SIZE];
// Loop until we're reconnected
while (!client.connected()) {
// Attempt to connect
if (client.connect("DuctControl", mqtt_user, mqtt_password)) { // put your MQTT username and password here
// for (int i=0;i<=REGS;i++){
// snprintf (tempTopic, MSG_BUFFER_SIZE, "AC/cmd/%s", regNames[regAddr[i]].c_str());
// client.subscribe(tempTopic);
// }
client.subscribe("AC/cmd/z1Control");
}
else {
delay(3000);
}
}
}
void loop(){
unsigned long now = millis();
char msg[MSG_BUFFER_SIZE];
char tempTopic[MSG_BUFFER_SIZE];
// if (!client.connected()) {
// reconnect();
// }
// client.loop();
if (now - lastMsg > 1000) {
lastMsg = now;
if (txdata.regData[0]){
txdata.regData[0] = 0;
ETout.sendData();
}
else {
txdata.regData[0] = 1;
ETout.sendData();
}
delay(10);
}
// if (now - lastMsg > 100) {
//
// lastMsg = now;
//
// ETout.sendData();
//
// for(int i=0; i<5; i++){
//
// ETin.receiveData();
// for (int i=0; i<=REGS;i++){
// if(rxdata.regData[i] != prevRead[i]){
// snprintf (msg, MSG_BUFFER_SIZE, "%d", rxdata.regData[i]);
// snprintf (tempTopic, MSG_BUFFER_SIZE, "AC/state/%s", regNames[regAddr[i]].c_str());
// client.publish(tempTopic, msg,1,0);
// }
// prevRead[i] = rxdata.regData[i];
// }
// }
// }
}
Arduino Nano Code
#include <EasyTransfer.h>
#include <SoftwareSerial.h>
EasyTransfer ETin, ETout;
SoftwareSerial softSerial(8,9); //RX, TX
#define REGS 12
#define baud 9600
unsigned const int z1Control = 2;
unsigned const int z2Control = 3;
unsigned const int z3Control = 4;
unsigned const int z4Control = 5;
unsigned const int z1Half = 6;
unsigned const int z1Full = 7;
unsigned const int z2Half = 8;
unsigned const int z2Full = 9;
unsigned const int z3Half = 10;
unsigned const int z3Full = 11;
unsigned const int z4Half = 12;
unsigned const int z4Full = 13;
int regAddr[REGS];
unsigned long lastMsg = 0;
struct RECEIVE_DATA_STRUCTURE{
//int regAddr[REGS];
int regData[REGS];
};
struct SEND_DATA_STRUCTURE{
int regData[REGS];
};
RECEIVE_DATA_STRUCTURE rxdata;
SEND_DATA_STRUCTURE txdata;
void setupArrays() {
regAddr[0] = z1Control;
regAddr[1] = z2Control;
regAddr[2] = z3Control;
regAddr[3] = z4Control;
regAddr[4] = z1Half;
regAddr[5] = z1Full;
regAddr[6] = z2Half;
regAddr[7] = z2Full;
regAddr[8] = z3Half;
regAddr[9] = z3Full;
regAddr[10] = z4Half;
regAddr[11] = z4Full;
for(int i=0;i<=REGS;i++){
rxdata.regData[i] = 0;
}
}
void setup(){
Serial.begin(baud);
softSerial.begin(baud);
ETin.begin(details(rxdata), &softSerial);
ETout.begin(details(txdata), &softSerial);
pinMode(12, OUTPUT);
}
void loop(){
unsigned long now = millis();
if(ETin.receiveData()){
delay(1);
digitalWrite(z1Control, rxdata.regData[0]);
}
delay(10);
}