Thank you
SO WHAT IS THE BEST SERIAL PROTOCOL I CAN USE @RUNAWAY_PANCAKE?
Hello everyone,
Can anyone help me with the code of how we can implement UART to send and receive data from multiple node mcus to arduino uno.
Hints: RS-485, ModBus
This will be a three-step project
step 1: send and receive data in a very basic way between one nodeMCU / and Arduino Uno
step 2: learn how to use start / endmarkers with the serial input basics tutorial
step3: connecting multiple nodeMCUs to an arduino uno
the step3 is somehow ambigous because an arduino uno has only one hardware uart. This UART is used for uploading code.
You should not use this uart for the communication with the nodeMCUs.
Connecting multiple nodeMCUs to the same UART might require some special hardware pre-cautions. (tri-state buffering) But I'm not sure.
Or as user @DrDiettrich mentioned RS485 ModBus
Using multiple instances of software-serial might be possible. Never done this before.
You will have to use software-serial. With software-serial reliable transmission-speed is limited to 19200 baud. And as it is a software-based UART your code will have to be non-blocking through and through to reliably read in all the data.
So it might be easier to use one more nodeMCU as the master leave away the Arduino-Uno completely
and then organise all datasending either via classical WiFi using UDP or using WiFi-ESP-NOW
best regards Stefan
@kp14, do not cross-post. Threads merged.
YOU SAID THAT ADDITIONAL HARDWARE IS A NO-GO.
(Maybe the guy in 'Jobs' can cook-up an I2C scheme for you. Start writing checks.)
I WOULD GO WITH THE NODEMCU's OWN CAPABILITY - USING THE WIFI FUNCTIONALITY. WHAT'S WRONG WITH THAT?
HAVE A GREAT DAY.
Hi everyone..found a link for node mcu as spi slavee
Can anyone help me rewrite this for multiple slaves
If you have working code that sets up your NodeMCU as an SPI slave device, then the basic SPI slave code is the same on all slaves.
hi @markd833 pls help me rewrite the master code for this case that has multiple functions and slave select must be performed.
Thanks
@markd833 currently single slave select has been enabled
Post your latest master code for us to see.
Sure @markd833 ,
#include <SPI.h>
class ESPSafeMaster
{
private:
uint8_t _ss_pin;
void _pulseSS()
{
digitalWrite(_ss_pin, HIGH);
delayMicroseconds(5);
digitalWrite(_ss_pin, LOW);
}
public:
ESPSafeMaster(uint8_t pin):_ss_pin(pin) {}
void begin()
{
pinMode(_ss_pin, OUTPUT);
_pulseSS();
}
uint32_t readStatus()
{
_pulseSS();
SPI.transfer(0x04);
uint32_t status = (SPI.transfer(0) | ((uint32_t)(SPI.transfer(0)) << 8) | ((uint32_t)(SPI.transfer(0)) << 16) | ((uint32_t)(SPI.transfer(0)) << 24));
_pulseSS();
return status;
}
void writeStatus(uint32_t status)
{
_pulseSS();
SPI.transfer(0x01);
SPI.transfer(status & 0xFF);
SPI.transfer((status >> 8) & 0xFF);
SPI.transfer((status >> 16) & 0xFF);
SPI.transfer((status >> 24) & 0xFF);
_pulseSS();
}
void readData(uint8_t * data)
{
_pulseSS();
SPI.transfer(0x03);
SPI.transfer(0x00);
for(uint8_t i=0; i<32; i++) {
data[i] = SPI.transfer(0);
}
_pulseSS();
}
void writeData(uint8_t * data, size_t len)
{
uint8_t i=0;
_pulseSS();
SPI.transfer(0x02);
SPI.transfer(0x00);
while(len-- && i < 32) {
SPI.transfer(data[i++]);
}
while(i++ < 32) {
SPI.transfer(0);
}
_pulseSS();
}
String readData()
{
char data[33];
data[32] = 0;
readData((uint8_t *)data);
return String(data);
}
void writeData(const char * data)
{
writeData((uint8_t *)data, strlen(data));
}
};
ESPSafeMaster esp(SS);
void send(const char * message)
{
Serial.print("Master: ");
Serial.println(message);
esp.writeData(message);
delay(10);
Serial.print("Slave: ");
Serial.println(esp.readData());
Serial.println();
}
void setup()
{
Serial.begin(115200);
SPI.begin();
esp.begin();
delay(1000);
send("Hello Slave!");
}
void loop()
{
delay(1000);
send("Are you alive?");
}
You need to go back and review how SPI works:
-
You don't just pulse the SS - you keep it asserted for the duration of the transfer.
-
You still don't seem to have provided for the necessary separate SS per slave.
Hi @awneil , i have sent a link above which you can refer. This is the only spi code that supports node mcu slave.
Now it supports 1 slave ..can u help me extend it to multiple slaves
Well, it's a very unconventional way to do it - but if it works for you ...
As the linked code says, you have to manage the data length - have you done that?
For multiple slaves, it's just a matter of the Master pulsing the appropriate SS line for the particular slave to be addressed.
I thought your UNO was going to be the master?
Also, as @awneil has already pointed out, that code you have with the _pulseSS function - the author doesn't seem to understand how SPI works.
Hi @awneil , my data is not going to exceed 15 characters...so this will do. if you could help me pulse the appropriate ss line, it would be great
yes @markd833 , uno is the master and this code works well, even though this is not pure spi, it does the job for me, could you help me extend for multiple slaves
Have an array of the pin numbers; your _pulseSS()
function takes the index as an argument?
Note that having user identifiers beginning with an underscore is Bad Practice - they are reserved.