I don't know a whole lot about the subject but iv'e read that there can be compatibility issues with memory arrangement when trying to transfer a struct from one cpu to another directly. What would be a good way to send a struct from esp to arduino?
I managed to transfer structs between esp modules over tcp. Below is my receiving test code. it still needs to communicate via serial to the arduino.
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <DNSServer.h>
#include <vector>
#include "config.h"
#define SSID "ESP-TEST"
#define PASSWORD "123456789"
#define SERVER_HOST_NAME "esp_server"
#define TCP_PORT 7050
#define DNS_PORT 53
static DNSServer DNS;
struct sampleStruct {
int var1 = 99;
//byte Array[];
float var2 = 0;
unsigned long var3 = 0;
unsigned long var4 = 0;
unsigned long var5 = 0;
unsigned long var6 = 0;
unsigned long var7 = 0;
unsigned long var8 = 0;
bool var9 = 0;
};
sampleStruct st;
static std::vector<AsyncClient*> clients; // a list to hold all clients
static void handleData(void* arg, AsyncClient* client, void *data, size_t len) {
Serial.printf("\n data received from client %s \n", client->remoteIP().toString().c_str());
// Serial.write((char *)data, len);
memcpy(&st, data, sizeof(data));
Serial.println(st.var1);
// reply to client
if (client->space() > 32 && client->canSend()) {
char reply[32];
sprintf(reply, "this is from %s", SERVER_HOST_NAME);
client->add(reply, strlen(reply));
client->send();
}
}
/* server events */
static void handleNewClient(void* arg, AsyncClient* client) {
Serial.printf("\n new client has been connected to server, ip: %s", client->remoteIP().toString().c_str());
// add to list
clients.push_back(client);
// register events
client->onData(&handleData, NULL);
}
void setup() {
Serial.begin(115200);
delay(20);
Serial.println("server");
// create access point
while (!WiFi.softAP(SSID, PASSWORD, 6, false, 15)) {
delay(500);
}
// start dns server
if (!DNS.start(DNS_PORT, SERVER_HOST_NAME, WiFi.softAPIP()))
Serial.printf("\n failed to start dns service \n");
AsyncServer* server = new AsyncServer(TCP_PORT); // start listening on tcp port 7050
server->onClient(&handleNewClient, server);
server->begin();
}
void loop() {
DNS.processNextRequest();
}
Using a TCI / IP connection helps a lot, but in the case of the serial port, the programmer needs to use a protocol for data transfer, preferably also needs to use an error checking mode, checksum.
Maybe these protocols can help you with something:
rtek1000:
Using a TCI / IP connection helps a lot, but in the case of the serial port, the programmer needs to use a protocol for data transfer, preferably also needs to use an error checking mode, checksum.
the only option i see is using the serial protocol. i2c will be used for lcd and spi used for flash mode on the esp. I'm not sure how to approach this. yes error checking would be nice but i need to successfully transfer the data from the esp to the arduino structure first. How can i do this without using another library? i'm trying to picture how i would take apart the structure 1 bit at a time and send it over serial and on the arduino use the received bits to recreate the structure ?
PacketSerial looks like i may have something to offer. I want to use as little libraries as possible. I need some more ideas please. I will be sending 8 different structures to this arduino over serial. please give me a break im not a c++ programmer
You are aware that you can have multiple I2C devices on the bus?
Same applies to SPI; it's however my understanding that it might be 'tricky' if you use it for programming as well (never had a need for it)'
Maybe these examples can help you with something:
Felix (said: Reply #1): ...Pointers are involved so you have to understand pointers a little bit to be able to get out of the fog alive...I added struct examples...
Source: https://lowpowerlab.com/forum/moteino/data-struct-for-sendingreceiving-(solved)/
1. Assume this is your structure data at the ESP8266 side:
#include<SoftwareSerial.h>
SoftwareSerial mySerial(D5, D6); //SRX = D5, STX=D6
struct sampleStruct
{
int var1;// = 99;
float var2;// = 0;
unsigned long var3;// = 0;
unsigned long var4;// = 0;
unsigned long var5;// = 0;
unsigned long var6;// = 0;
unsigned long var7;// = 0;
unsigned long var8;// = 0;
bool var9;//; = 0;
};
sampleStruct st;
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
Serial.print(" ");
st.var1 = 99; //
st.var2 = 1.37;
st.var3 = 0x12345678;
st.var4 = 0xABCDEF12;
st.var5 = 0x00;
st.var6 = 0xFE345678;
st.var5 = 0xCD345678;
st.var6 = 0xA1345678;
st.var7 = 0xB2345678;
st.var8 = 0xCF345678;
st.var9 = true;
}
void loop()
{
Serial.println("Sending data to UNO over SUART Port.");
mySerial.print(st.var1);
mySerial.print(',');
mySerial.print(st.var2, 2);
mySerial.print(',');
mySerial.print(st.var3);
//.....................
mySerial.print('\n'); //message terminating charcatervar2,1.37
delay(5000); //test interval
}
2. Send the data items using print() command; use comma (,) as the data item separators and '\n' as the message terminating character.
3. At the receiver side catch all characters until '\n' is found using the following command:
byte m = mySerial.readBytesUntil('\n', myData, 250);
4. Process array of Step-3 on the basis of comma (,) character to retrieve individual data items that have been sent from ESP8266.
Hope, this strategy may help you to begin until you find a better way.
GolamMostafa:
1. Assume this is your structure data at the ESP8266 side:
#include<SoftwareSerial.h>
SoftwareSerial mySerial(D5, D6); //SRX = D5, STX=D6
struct sampleStruct
{
int var1;// = 99;
float var2;// = 0;
unsigned long var3;// = 0;
unsigned long var4;// = 0;
unsigned long var5;// = 0;
unsigned long var6;// = 0;
unsigned long var7;// = 0;
unsigned long var8;// = 0;
bool var9;//; = 0;
};
sampleStruct st;
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
Serial.print(" ");
st.var1 = 99; //
st.var2 = 1.37;
st.var3 = 0x12345678;
st.var4 = 0xABCDEF12;
st.var5 = 0x00;
st.var6 = 0xFE345678;
st.var5 = 0xCD345678;
st.var6 = 0xA1345678;
st.var7 = 0xB2345678;
st.var8 = 0xCF345678;
st.var9 = true;
}
void loop()
{
Serial.println("Sending data to UNO over SUART Port.");
mySerial.print(st.var1);
mySerial.print(',');
mySerial.print(st.var2, 2);
mySerial.print(',');
mySerial.print(st.var3);
//.....................
mySerial.print('\n'); //message terminating charcatervar2,1.37
delay(5000); //test interval
}
**2.** Send the data items using print() command; use comma (,) as the data item separators and '\n' as the message terminating character.
**3.** At the receiver side catch all characters until '\n' is found using the following command:
byte m = mySerial.readBytesUntil('\n', myData, 250);
**4.** Process array of Step-3 on the basis of comma (,) character to retrieve individual data items that have been sent from ESP8266.
Hope, this strategy may help you to begin until you find a better way.
I notice the use of SoftwareSerial. should i be able to do this without it? I been seeing alot of talk about using unions from searching google. Is that something that i should be concerned with. i was loking at Robin2's ReceiveBinary sketch. I didnt know if printing the items 1 by 1 was ideal. if i did the print method i could probably use Robin2's serial #5 example
notsolowki:
I notice the use of SoftwareSerial. should i be able to do this without it?
It is recommended that you use Software UART Port (SUART) as the Hardware UART Port (UART) is permanently engaged with the IDE/SM for sketch uploading/debugging.
I been seeing alot of talk about using unions from searching google. Is that something that i should be concerned with.
Your data types are of struct -- a conglomeration of items of different sizes/types; so, you can't use here union which shares one common memory space for various types of data but of the same size.
i was loking at Robin2's ReceiveBinary sketch. I didnt know if printing the items 1 by 1 was ideal. if i did the print method i could probably use Robin2's serial #5 example
@Robin2's tutorials on Serial Basics are worth to practice.[/quote]
GolamMostafa:
It is recommended that you uese Software UART Port (SUART) as the Hrdaware UART Port (UART) is permanently engaged with the IDE/SM for sketch uploading/debugging.
[quoute]I been seeing alot of talk about using unions from searching google. Is that something that i should be concerned with.[/quote
Your data types are of struct -- a conglomeration of items of different sizes/types; so, you can't use here union which shares one common memory space for various types of data but of the same size.
@Robin2's tutorials on Serial Basics are worth to practice.
you make some interesting points. But i have to say i'm using a mega and i thought it has 2 UARTS. i will have the esp connected via SPI and will be using that as a programming method. the only thing connected to the serial ports will be the ESP strictly for sharing data from and to the ESP tcp server? Am i still missing something? besides a brain
notsolowki:
you make some interesting points. But i have to say i'm using a mega and i thought it has 2 UARTS. i will have the esp connected via SPI and will be using that as a programming method. the only thing connected to the serial ports will be the ESP strictly for sharing data from and to the ESP tcp server? Am i still missing something? besides a brain
MEGA has 4 UART Ports: UART0 is engaged with IDE/SM; so, you can use UART1 to communicate with ESP8266. There is no need to employ SUART port for the MEGA side.
You will be programming ESP using SPI Port -- this is fine. If you are not using UART Port of ESP with SM for debugging, then that UART Port could be used to MEGA. There is no need to use SUART port at the ESP side.
GolamMostafa:
MEGA has 4 UART Ports: UART0 is engaged with IDE/SM; so, you can use UART1 to communicate with ESP8266. There is no need to employ SUART port for the MEGA side.
You will be programming ESP using SPI Port -- this is fine. If you are not using UART Port of ESP with SM for debugging, then that UART Port could be used to MEGA. There is no need to use SUART port at the ESP side.
Could you think of a way to write the struct to an byte array and use serial.print to write the array compare the size and then populate the remote struct with the incoming array? does this make sense. idk how i would move the data from the buffer to the struct
anyone else can help me out please? i dont even know what is the "ideal"method.
struct MYSTRUCT
{
...
...
};
MYSTRUCT data;
byte *ptr;
ptr = (byte *)&data;
Serial.write(ptr, sizeof(MYSTRUCT));
Just to give you the idea.
At the receiver side, read into a byte array (you know how many bytes you will receive) and cast to the struct.
Be aware of endianess that might swap bytes around in 16 and 32 bit integers.
sterretje:
ptr = (byte *)&data;
Serial.write(ptr, sizeof(MYSTRUCT));
Will that work across two different architectures? What if there's an int in there? (4 bytes on an ESP, 2 on AVR)
Ah, that was another problem; but that's easily solved with int8_t and the likes. Still does not solve a possible problem with endianess.
The examples cited in post #6 cannot be adapted?
// **********************************************************************************
// Struct Send RFM69 Example
// **********************************************************************************
// Copyright Felix Rusu 2018, http://www.LowPowerLab.com/contact
// **********************************************************************************
// License
// **********************************************************************************
// This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
// **********************************************************************************
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
#include <SPI.h> //included with Arduino IDE install (www.arduino.cc)
//*********************************************************************************************
//************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NODEID 99
#define NETWORKID 100
#define GATEWAYID 1
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
//#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
//*********************************************************************************************
//Auto Transmission Control - dials down transmit power to save battery
//Usually you do not need to always transmit at max output power
//By reducing TX power even a little you save a significant amount of battery power
//This setting enables this gateway to work with remote nodes that have ATC enabled to
//dial their power down to only the required level
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
//*********************************************************************************************
#define SERIAL_BAUD 115200
#if defined (MOTEINO_M0) && defined(SERIAL_PORT_USBVIRTUAL)
#define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
#endif
#ifdef ENABLE_ATC
RFM69_ATC radio;
#else
RFM69 radio;
#endif
SPIFlash flash(SS_FLASHMEM, 0xEF30); //EF40 for 16mbit windbond chip
int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms)
byte sendSize=0;
boolean requestACK = false;
typedef struct {
int nodeId; //store this nodeId
unsigned long uptime; //uptime in ms
float temp; //temperature maybe?
} Payload;
Payload theData;
void setup() {
Serial.begin(SERIAL_BAUD);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW_HCW
radio.setHighPower(); //must include this only for RFM69HW/HCW!
#endif
radio.encrypt(ENCRYPTKEY);
char buff[50];
sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
Serial.println(buff);
if (flash.initialize())
Serial.println("SPI Flash Init OK!");
else
Serial.println("SPI Flash Init FAIL! (is chip present?)");
}
long lastPeriod = -1;
void loop() {
//process any serial input
if (Serial.available() > 0)
{
char input = Serial.read();
if (input >= 48 && input <= 57) //[0,9]
{
TRANSMITPERIOD = 100 * (input-48);
if (TRANSMITPERIOD == 0) TRANSMITPERIOD = 1000;
Serial.print("\nChanging delay to ");
Serial.print(TRANSMITPERIOD);
Serial.println("ms\n");
}
if (input == 'r') //d=dump register values
radio.readAllRegs();
//if (input == 'E') //E=enable encryption
// radio.encrypt(ENCRYPTKEY);
//if (input == 'e') //e=disable encryption
// radio.encrypt(null);
if (input == 'd') //d=dump flash area
{
Serial.println("Flash content:");
int counter = 0;
while(counter<=256){
Serial.print(flash.readByte(counter++), HEX);
Serial.print('.');
}
while(flash.busy());
Serial.println();
}
if (input == 'e')
{
Serial.print("Erasing Flash chip ... ");
flash.chipErase();
while(flash.busy());
Serial.println("DONE");
}
if (input == 'i')
{
Serial.print("DeviceID: ");
word jedecid = flash.readDeviceId();
Serial.println(jedecid, HEX);
}
}
//check for any received packets
if (radio.receiveDone())
{
Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
for (byte i = 0; i < radio.DATALEN; i++)
Serial.print((char)radio.DATA[i]);
Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
if (radio.ACKRequested())
{
radio.sendACK();
Serial.print(" - ACK sent");
delay(10);
}
Blink(LED_BUILTIN,5);
Serial.println();
}
int currPeriod = millis()/TRANSMITPERIOD;
if (currPeriod != lastPeriod)
{
//fill in the struct with new values
theData.nodeId = NODEID;
theData.uptime = millis();
theData.temp = 91.23; //it's hot!
Serial.print("Sending struct (");
Serial.print(sizeof(theData));
Serial.print(" bytes) ... ");
if (radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData)))
Serial.print(" ok!");
else Serial.print(" nothing...");
Serial.println();
Blink(LED_BUILTIN,3);
lastPeriod=currPeriod;
}
}
void Blink(byte PIN, int DELAY_MS)
{
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
}
// **********************************************************************************
// Struct Receive RFM69 Example
// **********************************************************************************
// Copyright Felix Rusu 2018, http://www.LowPowerLab.com/contact
// **********************************************************************************
// License
// **********************************************************************************
// This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
// **********************************************************************************
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
#include <SPI.h> //included with Arduino IDE install (www.arduino.cc)
//*********************************************************************************************
//************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NODEID 1
#define NETWORKID 100
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
//#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
//*********************************************************************************************
//Auto Transmission Control - dials down transmit power to save battery
//Usually you do not need to always transmit at max output power
//By reducing TX power even a little you save a significant amount of battery power
//This setting enables this gateway to work with remote nodes that have ATC enabled to
//dial their power down to only the required level
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
//*********************************************************************************************
#define SERIAL_BAUD 115200
#if defined (MOTEINO_M0) && defined(SERIAL_PORT_USBVIRTUAL)
#define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
#endif
#ifdef ENABLE_ATC
RFM69_ATC radio;
#else
RFM69 radio;
#endif
SPIFlash flash(SS_FLASHMEM, 0xEF30); //EF40 for 16mbit windbond chip
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network
typedef struct {
int nodeId; //store this nodeId
unsigned long uptime; //uptime in ms
float temp; //temperature maybe?
} Payload;
Payload theData;
void setup() {
Serial.begin(SERIAL_BAUD);
delay(10);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW_HCW
radio.setHighPower(); //must include this only for RFM69HW/HCW!
#endif
radio.encrypt(ENCRYPTKEY);
radio.promiscuous(promiscuousMode);
char buff[50];
sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
Serial.println(buff);
if (flash.initialize())
Serial.println("SPI Flash Init OK!");
else
Serial.println("SPI Flash Init FAIL! (is chip present?)");
}
byte ackCount=0;
void loop() {
//process any serial input
if (Serial.available() > 0)
{
char input = Serial.read();
if (input == 'r') //d=dump all register values
radio.readAllRegs();
if (input == 'E') //E=enable encryption
radio.encrypt(ENCRYPTKEY);
if (input == 'e') //e=disable encryption
radio.encrypt(null);
if (input == 'p')
{
promiscuousMode = !promiscuousMode;
radio.promiscuous(promiscuousMode);
Serial.print("Promiscuous mode ");Serial.println(promiscuousMode ? "on" : "off");
}
if (input == 'd') //d=dump flash area
{
Serial.println("Flash content:");
int counter = 0;
while(counter<=256){
Serial.print(flash.readByte(counter++), HEX);
Serial.print('.');
}
while(flash.busy());
Serial.println();
}
if (input == 'D')
{
Serial.print("Deleting Flash chip content... ");
flash.chipErase();
while(flash.busy());
Serial.println("DONE");
}
if (input == 'i')
{
Serial.print("DeviceID: ");
word jedecid = flash.readDeviceId();
Serial.println(jedecid, HEX);
}
}
if (radio.receiveDone())
{
Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
if (promiscuousMode)
{
Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
}
if (radio.DATALEN != sizeof(Payload))
Serial.print("Invalid payload received, not matching Payload struct!");
else
{
theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else
Serial.print(" nodeId=");
Serial.print(theData.nodeId);
Serial.print(" uptime=");
Serial.print(theData.uptime);
Serial.print(" temp=");
Serial.print(theData.temp);
}
if (radio.ACKRequested())
{
byte theNodeID = radio.SENDERID;
radio.sendACK();
Serial.print(" - ACK sent.");
// When a node requests an ACK, respond to the ACK
// and also send a packet requesting an ACK (every 3rd one only)
// This way both TX/RX NODE functions are tested on 1 end at the GATEWAY
if (ackCount++%3==0)
{
Serial.print(" Pinging node ");
Serial.print(theNodeID);
Serial.print(" - ACK...");
delay(3); //need this when sending right after reception .. ?
if (radio.sendWithRetry(theNodeID, "ACK TEST", 8, 0)) // 0 = only 1 attempt, no retries
Serial.print("ok!");
else Serial.print("nothing");
}
}
Serial.println();
Blink(LED_BUILTIN,3);
}
}
void Blink(byte PIN, int DELAY_MS)
{
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
}
sterretje:
struct MYSTRUCT
{
...
...
};
MYSTRUCT data;
byte *ptr;
ptr = (byte *)&data;
Serial.write(ptr, sizeof(MYSTRUCT));
Just to give you the idea.
At the receiver side, read into a byte array (you know how many bytes you will receive) and cast to the struct.
Be aware of endianess that might swap bytes around in 16 and 32 bit integers.
I have found your post very interesting and tried to implement it between ESP8266 (Sender) and NANO (receiver) using OP's data. I have posted the screen shots of both devices. Data are going well byte by byte and are being received well in the same order except that the Sender Codes can't recognize the int/bool types data of the struct -- they are being taken as long (4 bytes). I am unable to figure out the disturbing agent.
Sender Screen shot:

Sender Codes:
#include<SoftwareSerial.h>
SoftwareSerial mySerial(D5, D6); //SRX = D5, STX=D6
byte *ptr;
struct sampleStruct
{
int var1;// = 99;
float var2;// = 0;
unsigned long var3;// = 0;
unsigned long var4;// = 0;
unsigned long var5;// = 0;
unsigned long var6;// = 0;
// unsigned long var7;// = 0;
// unsigned long var8;// = 0;
bool var9;//; = 0;
};
sampleStruct st;
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
Serial.print(" ");
st.var1 = 0x1299; //
st.var2 = 1.37;
st.var3 = 0x12345678;
st.var4 = 0xABCDEF12;
st.var5 = 0xDF123456;
st.var6 = 0xFE345678;
st.var5 = 0xCD345678;
st.var6 = 0xA1345678;
// st.var7 = 0xB2345678;
// st.var8 = 0xCF345678;
st.var9 = true;
ptr = (byte*)&st;
}
void loop()
{
Serial.println("Sending data to UNO over SUART Port.");
Serial.println(sizeof st, DEC);
mySerial.write(ptr, sizeof st);
mySerial.print('\n'); //message terminating character
delay(2000); //teset interval
}
Receiver Screen shot:
Receiver Codes:
#include<SoftwareSerial.h>
SoftwareSerial mySerial(5, 6); //SRX = GPIO-5, STX=GPIO-6
byte myData[50];
struct sampleStruct
{
int var1;// = 99;
float var2;// = 0;
unsigned long var3;// = 0;
unsigned long var4;// = 0;
unsigned long var5;// = 0;
unsigned long var6;// = 0;
unsigned long var7;// = 0;
unsigned long var8;// = 0;
bool var9;//; = 0;
};
sampleStruct st;
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
}
void loop()
{
byte n = mySerial.available();
if (n != 0)
{
byte m = mySerial.readBytesUntil('\n', myData, 50);
Serial.println(m);
for (int i = 0; i < m; i++)
{
Serial.print(myData[i], HEX);
Serial.print(' ');
}
Serial.println();
Serial.println("===========================");
}
}
