Use Serial1 for UNO

Unfortunately that is not the case. See the leonardo variant(https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/variants/leonardo/pins_arduino.h#L368-L389):

// These serial port names are intended to allow libraries and architecture-neutral
// sketches to automatically default to the correct port name for a particular type
// of use.  For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
//
// SERIAL_PORT_MONITOR        Port which normally prints to the Arduino Serial Monitor
//
// SERIAL_PORT_USBVIRTUAL     Port which is USB virtual serial
//
// SERIAL_PORT_LINUXBRIDGE    Port which connects to a Linux system via Bridge library
//
// SERIAL_PORT_HARDWARE       Hardware serial port, physical RX & TX pins.
//
// SERIAL_PORT_HARDWARE_OPEN  Hardware serial ports which are open for use.  Their RX & TX
//                            pins are NOT connected to anything by default.
#define SERIAL_PORT_MONITOR        Serial
#define SERIAL_PORT_USBVIRTUAL     Serial
#define SERIAL_PORT_HARDWARE       Serial1
#define SERIAL_PORT_HARDWARE_OPEN  Serial1

// Alias SerialUSB to Serial
#define SerialUSB SERIAL_PORT_USBVIRTUAL

If you read the comment in the mega variant:

// SERIAL_PORT_HARDWARE       Hardware serial port, physical RX & TX pins.

it's clear that defining SERIAL_PORT_HARDWARE as Serial1 for leonardo is correct but there is no documentation of the intention of SERIAL_PORT_HARDWARE1 so I don't know if it's a bug that it's not also defined as Serial1 in the leonardo variant or if this is intentional.

At least HAVE_HWSERIAL1 works across all Arduino AVR Boards.

Sorry again... suffering with fever

Thanks for all your inputs...

Ok, so better I think I should explain the system that I'm working with currently.

Its a home automation system consisting of a

  1. single local server (Arduino Mega)
  2. many field nodes (Megs or Unos)
  3. ESP8266 attached on each node
  4. online MQTT server
  5. and an online data server which runs on node.js and mongodb

idea is to make a singe program to load to all the arduinos (both mega's and uno's), and the data is exchanged between the ESP's locally which works as a mesh using the library Files · master · painlessMesh / painlessMesh · GitLab

the mesh communications, the MQTT communications and the online server communications are all done with JSON using this library https://bblanchon.github.io/ArduinoJson/

The ESP's on mesh will be attached on Serial0 always

Only the server node will communicate with the MQTT server, and the ESP for MQTT will be connected on Serial1

So what I was thinking was, its fairly easy to determine which is the server looking the data on Serial1 port. If data receives from MQTT, thats it its the server then.

Each point at my home (I mean switch points) will have a Arduino+ESP and attached relays

SO I have to maintain the following set of codes

  1. for the Arduino server
  2. for Arduino field nodes
  3. for the ESP mesh
  4. for the ESP MQTT

Both Arduino server and field nodes are using the same codes, its just a variable change to identify Server (yes/no). So looking on the Serial1 data and setting this would be a good idea, so that same codes can be used on all Arduinos without any headaches.

Current problem is with the macro for mimicking the Serial1 with Serial cannot work as, reading the Serial1 will supply Serial data whenever it is called. (of-course we can filter for some MQTT specific out put and determine if its the server anyway).

In short, I think this is what I want.
Reading a Serial1 on UNO should not throw an error but also
should not supply the primary Serial data

@pert I'll post the code soon, sorry still sick.

OK, so a slave that happens to be installed on a MEGA will know that it's a slave? It's not supposed to issue commands on Serial?

I think each unit has two questions to answer:

  1. Am I a slave?
  2. How do I compile for an UNO when Serial1 will be a compilation error?

2 is the easiest one. If there's no HWSERIAL1, then just #define Serial1 Serial. For a slave, it's not going to ever transmit on Serial1, due to the program logic, so you never get interference. You also have to be sure that the slave never attempts to do a Serial1.read() because that will 'steal' a character from the Serial interface.

1 can be answered several ways:
a) detect that I'm an UNO with no HWSERIAL1 and therefore I must be a slave.
b) #define SLAVE each time you compile for a slave.
c) Store something in the EEPROM that says it's a slave.
d) Detect if there's valid MQTT traffic on Serial1 and then act as master over Serial.

Option a) will never let you install a slave on a Mega. This will be a problem for your future expansion.

Option b) requires you to remember to do this, but it will be at the top of the file and it's easy to comment or un-comment as required. You already have to switch the board type in the menu.

Option c) lets you re-purpose a spare board as either master or slave without having to recompile. You would have to have some serial-driven menu to log in and change this.

Option d) makes it totally dynamic - plug a Mega in to the MQTT channel and it immediately takes control of the mesh network.

Obviously a) and b) are easy to code. c) or d) might be more preferable for your future expansion plans.

@MorganS

Yes, i'm going forward implementing the preferable option d) itself

Since the server node consists of ESP (for Mesh), Arduino, and ESP (for MQTT)

Only the arduino decides (and knows) if it is the server node after parsing the MQTT input on serial1
and only the ESP (for Mesh) knows the mesh id

So I ended up sharing the data (is-server from arduino) and (node-id from ESP-mesh) in between them

they ping every 2 seconds to and fro so the data will be synced, and the mesh will pass its server-if to all field nodes on json header.

Thanks again everyone...