Hello gfvalvo,
Sending and receiving across the uart between the ESP8266 and the UNO appears to be working well....mostly. I get some 'air' gaps.
Overview
My aim is to setup simple reliable gateways for a set of variables to be sent and received at both ends between an ESP and UNO or Mega. Bi-directional. This will become generic code that I would apply to any Wifi ESP/UNO or Wifi ESP/Mega project with very little or no modifications. So, part of this process was to determine the maximum number of variables I could send and receive.
My code follows. I then explain the two issues:
See my code:
ESP8266 Code:
//ESP uart send and receive to Arduino UNO and Mega. For Jaycar Wifi XC4411 and XC4421 communications.
#include "SerialTransfer.h"
SerialTransfer myTransfer;
struct STRUCT {
char character1;
float variable1;
float variable2;
float variable3;
float variable4;
float variable5;
float variable6;
int variable7;
int variable8;
int variable9; //this appears to be the limit.
//int variable10;
//int variable11;
//int variable12;
//int variable13;
//int variable14;
//int variable15;
//int variable16;
} attribute((packed)); //this packing aligns the bytes as there is a difference between ESP8266 and UNO/Mega it gets out of alignment or something like that. 3 verses 4 bytes.
struct STRUCT sendStruct;
struct STRUCT receiveStruct;
void setup()
{
Serial.begin(115200);
//Serial1.begin(115200);
myTransfer.begin(Serial);
//sendStruct.character1 = '$';
}
void loop()
{
sendStruct.variable1=4.21;
sendStruct.variable6=7.43;
sendStruct.variable7=1234;
sendStruct.variable8=5678;
myTransfer.sendDatum(sendStruct);
delay(500);
if(myTransfer.available())
{
myTransfer.rxObj(receiveStruct);
}
sendStruct.variable2=receiveStruct.variable1; //loop back test
}
UNO Code:
//Arduino UNO and Mega uart send and receive to ESP. For Jaycar Wifi XC4411 and XC4421 communications.
#include <LiquidCrystal.h>
//pin defs to suit LCD Shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#include "SerialTransfer.h"
SerialTransfer myTransfer;
struct STRUCT {
char character1;
float variable1;
float variable2;
float variable3;
float variable4;
float variable5;
float variable6;
int variable7;
int variable8;
int variable9; //appears to be the limit
//int variable10;
//int variable11;
//int variable12;
//int variable13;
//int variable14;
//int variable15;
//int variable16;
} attribute((packed)); //this packing aligns the bytes as there is a difference between ESP8266 and UNO/Mega it gets out of alignment or something like that. 3 verses 4 bytes.
struct STRUCT receiveStruct;
struct STRUCT sendStruct;
void setup()
{
Serial.begin(115200);
//Serial1.begin(115200);
myTransfer.begin(Serial);
lcd.begin(16, 2);
lcd.setCursor(0, 1);
//lcd.print(" Test");
}
void loop()
{
myTransfer.sendDatum(sendStruct);
delay(500);
if(myTransfer.available())
{
myTransfer.rxObj(receiveStruct);
lcd.setCursor(1, 0);
lcd.print(receiveStruct.variable1);
lcd.setCursor(1, 1);
lcd.print(receiveStruct.variable2);
lcd.setCursor(7, 0);
lcd.print(receiveStruct.variable3);
lcd.setCursor(7, 1);
lcd.print(receiveStruct.variable6);
lcd.setCursor(12, 0);
lcd.print(receiveStruct.variable7); //there is some kind of alignment issue. int variables don't line up. Every second one is empty.
//lcd.setCursor(12, 1);
//lcd.print(" ");
lcd.setCursor(12, 1);
lcd.print(receiveStruct.variable9); //variable9 aligns with variable8 in ESP
}
sendStruct.variable1 = receiveStruct.variable2+0.01; //loop back test
}
The issues:
Issue 1: Found a limit of around ten variables. If I increased further the data transfer appeared to stop.
Issue 2: A miss alignment of variables. In the code above I use a two line LCD 6 character display to show the data being transferred. I do a simple loop back test to prove data is going both ways. I do this as with the hardware config I need to set up DIP switches so the ESP and UNO can talk. Once this is done I can't use the serial monitor function in the IDE. So, I've noted when sending variables sendStruct.variable7 and sendStruct.variable8 then only sendStruct.variable7 aligns to the
receiveStruct.variable7 but receiveStruct.variable8 is empty and receiveStruct.variable9 has the data.
Questions
- What do you think the limit of struct data would be? I.e. how many variables can I expect to stream?
- What do you think is happening with the alignment? Note I did not appear to have an alignment issue with the floats (tested only 6). I have not checked what would happen if I changed the struct to only integers or put the integers first before the floats within the struct.
Any pointers would be appreciated.
Best regards,
Brian