Go Down

Topic: Arduino to arduino Serial (Read 934 times) previous topic - next topic

PaulVdB

Power_Broker : You did it again ! :)
Works like a charm !!!

Code: [Select]
//NANO

#include "SerialTransfer.h"
SerialTransfer myTransfer;
int Temp;
int HumAir;
int HumSoil;
void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);
}
void loop()
{
  Temp = analogRead(A0);
  myTransfer.txBuff[0] = (Temp >> 8) & 0xFF;
  myTransfer.txBuff[1] = Temp & 0xFF;
 
  HumAir = analogRead(A1);  
  myTransfer.txBuff[2] = (HumAir >> 8) & 0xFF;
  myTransfer.txBuff[3] = HumAir & 0xFF;
  
  HumSoil = analogRead(A2);  
  myTransfer.txBuff[4] = (HumSoil >> 8) & 0xFF;
  myTransfer.txBuff[5] = HumSoil & 0xFF;
  
  myTransfer.sendData(6);
  delay(100);
}


Code: [Select]
//MEGA

#include "SerialTransfer.h"
SerialTransfer myTransfer;
byte data[] = {0, 0, 0, 0, 0, 0};
byte bytes;
byte i;
int Temp;
int HumAir;
int HumSoil;

void setup()
{
  Serial.begin(115200);
  Serial3.begin(115200);
  myTransfer.begin(Serial3);
}
void loop()
{
  if (myTransfer.available())
  {
    bytes = myTransfer.bytesRead;
    for (i = 0; i < bytes; i++)
    {
      data[i] = (myTransfer.rxBuff[i]);
    }
    Temp = (data[0] * 256) + data[1];
    HumAir = (data[2] * 256) + data[3];
    HumSoil = (data[4] * 256) + data[5];
    Serial.print("Temp = ");
    Serial.println(Temp);
    Serial.print("HumA = ");
    Serial.println(HumAir);
    Serial.print("HumS = ");
    Serial.println(HumSoil);
    Serial.println();
  }
  else if (myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}


1M thanks !
Now I'll try "Data request and answer" ... I'll keep you informed ! :)

P.S. I hope that also others can benefit from this thread. It is SUPER CLEAR !

PaulVdB

#16
Nov 21, 2019, 03:37 pm Last Edit: Nov 21, 2019, 03:40 pm by PaulVdB
YES !
request/reply seems to work well with these sketches :
Code: [Select]
//NANO

#include "SerialTransfer.h"
SerialTransfer myTransfer;
int Temp;
int HumAir;
int HumSoil;
byte rec;
void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);
}
void loop()
{
  if (myTransfer.available())
  {
    if (myTransfer.rxBuff[0] == 'X')
    {
      Temp = analogRead(A0);
      myTransfer.txBuff[0] = (Temp >> 8) & 0xFF;
      myTransfer.txBuff[1] = Temp & 0xFF;

      HumAir = analogRead(A1);
      myTransfer.txBuff[2] = (HumAir >> 8) & 0xFF;
      myTransfer.txBuff[3] = HumAir & 0xFF;

      HumSoil = analogRead(A2);
      myTransfer.txBuff[4] = (HumSoil >> 8) & 0xFF;
      myTransfer.txBuff[5] = HumSoil & 0xFF;

      myTransfer.sendData(6);
      delay(100);
    }
  }
}


Code: [Select]
//MEGA

#include "SerialTransfer.h"
SerialTransfer myTransfer;
byte data[] = {0, 0, 0, 0, 0, 0};
byte bytes;
byte i;
int Temp;
int HumAir;
int HumSoil;

void setup()
{
  Serial.begin(115200);
  Serial3.begin(115200);
  myTransfer.begin(Serial3);
}
void loop()
{
  if (millis() % 5000 == 0)
  {
    myTransfer.txBuff[0] = 'X';
    myTransfer.sendData(1);
    delay(100);
  }
  if (myTransfer.available())
  {
    bytes = myTransfer.bytesRead;
    for (i = 0; i < bytes; i++)
    {
      data[i] = (myTransfer.rxBuff[i]);
    }
    Temp = (data[0] * 256) + data[1];
    HumAir = (data[2] * 256) + data[3];
    HumSoil = (data[4] * 256) + data[5];
    Serial.print("Temp = ");
    Serial.println(Temp);
    Serial.print("HumA = ");
    Serial.println(HumAir);
    Serial.print("HumS = ");
    Serial.println(HumSoil);
    Serial.println();
  }
  else if (myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}


My Serial communication problem seems to be solved ! thanks to your super simple/straight- forward library.

Of course I'll have to test in more "real life" circumstances. I'm affraid that the lenght of the communication cable (7-10m) might give problems. If that's the case, I'll try a few options :
- adding RS485 converters,
- adding level converters (e.g. 5 to 12?24? volts)
- and maybe even go wireless.

I guess that your "SerialTransfer.h" library won't have any problems with these converters because they are just kinda "slaves" who only convert the native RS232(5Volt) signals.

One other thing what I'm not sure about : is it possible to use some kinda softwareSerial on the Nano ? (I'm getting tired to plug and unplug the rx/tx wires to upload a new sketch...  :smiley-roll-blue: )

Anyhow ... How can I thank you for your time/patience and CLEAR explanations ? (PM me :) )

Power_Broker

My Serial communication problem seems to be solved ! thanks to your super simple/straight- forward library.
Yay!  :)


I guess that your "SerialTransfer.h" library won't have any problems with these converters because they are just kinda "slaves" who only convert the native RS232(5Volt) signals.
Yes, the library works with all UART/USART devices, regardless if they are radios. In fact, I designed the library for me to transfer commands and telemetry wirelessly between an Arduino hand controller and an Arduino controlled RC plane.


One other thing what I'm not sure about : is it possible to use some kinda softwareSerial on the Nano ? (I'm getting tired to plug and unplug the rx/tx wires to upload a new sketch...  :smiley-roll-blue: )
Although it isn't advised, you can use softwareserial on the nano. It's best to use softwareserial for testing (for the reasons you mention), but convert the code to Serial once the rest of the code is complete and polished. Either way, it's your project!
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

PaulVdB

Quote
Although it isn't advised, you can use softwareserial on the nano. It's best to use softwareserial for testing (for the reasons you mention), but convert the code to Serial once the rest of the code is complete and polished.
I'll install a (push) button  :)
Quote
an Arduino hand controller and an Arduino controlled RC plane.
: GREAT !!! (I saw the video)
So you send commands to the plane and get back info (e.g. from GPS etc.) ?

Meanwhile, I integrated your communication "system" in my main (still incomplete) MEGA program, and again it works like a charm !

So again : thanks for sharing your knowledge !


Power_Broker

I'll install a (push) button  :)
You probably would want to use a triple-pole-double-throw (TPDT) switch instead

So you send commands to the plane and get back info (e.g. from GPS etc.) ?
GPS uses it's own packet standard (NMEA sentences), so I created a different library for GPS comms. It's more of the "inter-Arduino" communication that I use the SerialTransfer.h library

So again : thanks for sharing your knowledge !
Glad to help!
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

PaulVdB

Glad to help!
Maybe this post will be moved to another topic, but because you seem to have SIMPLE solutions for simple questions, I'll post it here. (that's what you get for being so friendly, helpful and patient  :smiley-red: )
In the same project I need to store and read floats (in this case Unix times, so nothing but whole numbers between now and 19/01/2039) in EEPROM. I've read 1000's (well... : actually just only 100's  ::)  or 10's...  :smiley-cool: ) articles about it, but they all seem so complex and not really serving my purposes ... You feel where I'm going ?  ;)  ;)  ;) I bet that with a few of your keystrokes heaven will open for me... 
In any case : MORE than appreciation for your time and patience !!!

P.S. the communication still works like a brilliant shiny dazzling charm !!!

Power_Broker

Are you saying you need to transfer floats? Are the dates in float format?
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

PaulVdB

Are you saying you need to transfer floats? Are the dates in float format?
Wow ... you answered before I let go the "Post" button !!! *LOL*

Nono... the data that get serial transferred are integers. (probably gonna change 'em into bytes because it's just ambient temperatures and humidities that are good enough without decimals : I don't need 21.34°C, 21 is OK for this project.).
But I have to store start and stop dates/times that are being kept even when the power goes down)
I tried to store these dates/times as bytes for year(-2000), month numbers, days, hours and minutes, but calculating with this kinda data is complex. That's why I want to work with Unix times (floats) It's just adding and subtractions... (e.g : what's the date 123 days from now, how many hours till 12pm on March, 6 2020  ? etc.)
OK. Math with floats takes time too, but I don't have to do 'em often. The whole project is based on minutes, so there's enough time to calculate  :)

Deep Bow as thanks !

Power_Broker

Okie, I gotchu: this is a really good EEPROM tutorial. In order to handle floats, you can use the following functions (provided you have #include <EEPROM.h> already in your sketch):

Code: [Select]
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
   const byte* p = (const byte*)(const void*)&value;
   int i;
   for (i = 0; i < sizeof(value); i++)
       EEPROM.write(ee++, *p++);
   return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
   byte* p = (byte*)(void*)&value;
   int i;
   for (i = 0; i < sizeof(value); i++)
       *p++ = EEPROM.read(ee++);
   return i;
}
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

PaulVdB

Ooooooo...K ....
of course I had the EEPROM.h included. Writing (updating), reading BYTES was no problem ...

gonna start reading, testing ... *s*

I suppose I can put the code you mentioned directly in my main sketch ? (out of setup() and loop(), of course...)

tanx tanx tanx (again ... *sigh* )  :)

PaulVdB

Hmmm ...

Code: [Select]
#include <EEPROM.h>
float a = 1234567890;
float b;

void setup()
{
  Serial.begin(115200);
  EEPROM_writeAnything(0, a);

  b = EEPROM_readAnything(0);
  Serial.println(b);
}

void loop()
{
}

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
  const byte* p = (const byte*)(const void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    EEPROM.write(ee++, *p++);
  return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
  byte* p = (byte*)(void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    *p++ = EEPROM.read(ee++);
  return i;
}

The compiler answers :
no matching function for call to 'EEPROM_readAnything(int)'

Seems like there's missing a second argument in "b = EEPROM_readAnything(0);"
I can't figure out what argument it needs ?? Don't I just ask to give me what's on address 0 ?

OK. I change to b = EEPROM_readAnything(0, 4); ( address 0, float = 4 bytes...)
then the compiler answers :
invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'

?? ?? ?? ...

PaulVdB

Now I used this :
Code: [Select]
#include <EEPROM.h>
float a = 1234567890.00;
float b;

void setup()
{
  Serial.begin(115200);
  EEPROM_writeAnything(0, a);
  EEPROM_readAnything(0, b);
  Serial.println(b);
}


it compiles OK, but the monitor gives me :
1234567936.00 (almost correct !  :) )

changed the name of variable a to value : same result...

What am I doing wrong ??
A floating point variable càn have 10 (decimal) digits, no ?

Aha !
if I do :
Code: [Select]
#include <EEPROM.h>
long a = 1234567890L;
long b;

void setup()
{
  Serial.begin(115200);
  EEPROM_writeAnything(0, a);
  EEPROM_readAnything(0, b);
  Serial.println(b);
}

I get the correct result on my monitor :
1234567890 yeeeeeeeeey !!!
Great !
But ... that "L" at the end of variable a ... it seems to be utmost necessary ... how do I get that L if my "a" is coming from a previous calculation ?

Oh ... just tried :
Code: [Select]
#include <EEPROM.h>
long a;
long b;
void setup()
{
  a = 1234567890 + 9;
  Serial.begin(115200);
  EEPROM_writeAnything(0, a);
  EEPROM_readAnything(0, b);
  Serial.println(b);
}


and then it works ! (Serial monitor says 1234567899 !!!

mysterious ...  8) ??
but it works !...


Go Up