RS485/RS232/Serial transport class for mysensors.org
-
@hek
My modules are from http://www.aliexpress.com/item/FREE-SHIPPING-5PCS-LOT-MAX485-module-RS485-module-TTL-turn-RS-485-module-MCU-development-accessories/1718665649.html
They are the same as yours - based on MAX485.
However, it is possible to run a simple TTL signal level bus on a short distances.
(https://github.com/leodesigner/mysensors-serial-transport/blob/master/sch.png)I've incorporated a slimmed down of the RS485 transport layer in this branch (soon to be merged into development):
https://github.com/mysensors/Arduino/tree/serial-transport/libraries/MySensors
But I've still haven't received my RS485 modules... Do you have time to run a small test?
And a test-motion sensor sketch:
https://github.com/mysensors/Arduino/tree/serial-transport/libraries/MySensors/examples/MotionSensorRS485But you could use any example just replacing
#define MY_RADIO_RF24with#define MY_RS485I've used the AltSerial library for RS485 communication so we can cope with dual serial ports on the serial gateway. This library requires using pins documented in sketch.
-
I've incorporated a slimmed down of the RS485 transport layer in this branch (soon to be merged into development):
https://github.com/mysensors/Arduino/tree/serial-transport/libraries/MySensors
But I've still haven't received my RS485 modules... Do you have time to run a small test?
And a test-motion sensor sketch:
https://github.com/mysensors/Arduino/tree/serial-transport/libraries/MySensors/examples/MotionSensorRS485But you could use any example just replacing
#define MY_RADIO_RF24with#define MY_RS485I've used the AltSerial library for RS485 communication so we can cope with dual serial ports on the serial gateway. This library requires using pins documented in sketch.
@hek
My example boxes are installed right now on the site and doing a daily job :)
It will take time to build a new one. I may try a breadboard, but I am still busy to finish additional improvements to mysensors.org project. (I will share them later - it's will be interesting too - I hope :) )Actually, it's easy to test RS485 transport without a RS485 converters - just use a simple schematics I have been talking before.
Also, you may want to look to this: https://github.com/MajenkoLibraries/ICSC/pulls
It's untested support for SoftwareSerial option for library.
With AltSerial we will be forced to use only a documented pins.Anyway - I am glad to contribute a bit to great mysensors community.
-
Thanks,
@LeoDesigner said:
With AltSerial we will be forced to use only a documented pins.
Yes, I know. But AltSerial can handle higher speeds. I preferred that over the SoftwareSerial flexibility regarding pin selection. Hope I did the right choice ;)
-
Thanks,
@LeoDesigner said:
With AltSerial we will be forced to use only a documented pins.
Yes, I know. But AltSerial can handle higher speeds. I preferred that over the SoftwareSerial flexibility regarding pin selection. Hope I did the right choice ;)
@hek
Speed is one of the reasons why I have decided to use hardware serial. :)
But we should be flexible and universal as much as we can. I had been thinking also to add support for over the wire firmware update. As you have done with wireless. -
@hek
Sure - but it can be possible done with standard bootloader - we just have to emulate standard Arduino sketch upload process on the gateway side. But I am not sure if it's possible to do with half duplex protocol. Just an idea. -
If using the standard bootloader, wouldn't it break the FW update if one of the other nodes on the bus starts to transmit?
@tbowmo
Node will listen RS485 bus before it will try to transmit anything (just like Ethernet).
So other nodes will behave yourself :)
But we have another difficulty: standard bootloader know nothing about TX/RX (dePin) management. So, we need to modify it to be able correctly emulate Arduino upload process. Maybe a simple schematics with diode will work in that case.
I am still busy with other improvements. -
@hek I've tested your code and can't get it working yet.
I've installed the gateway and a DHT22 sketch on two nanos and connected them directly via D8 and D9.
// Enable RS485 transport layer #define MY_RS485 // Set RS485 baud rate to use #define MY_RS485_BAUD_RATE 9600 #include <MySensor.h> #include <DHT.h> #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define RS485_DE_PIN 4 unsigned long SLEEP_TIME = 3000; // Sleep time between reads (in milliseconds) // Initialize motion message DHT dht; boolean metric = true; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); void setup() { dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Humidity Sensor", "1.0"); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); } void loop() { float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else { send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else { send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(SLEEP_TIME); }The sketch is working fine according to serial output. But nothing is seen on the gateway.
So, to find out, where there is a problem, I connected my Programmer (mySmartUSB MK3) to D8/D9 of the sensor and I'm able to see that there is output but the chars are not readable so I tried different baudrates than 9600. That didn' help. What I can say is, that the output is generated every 3 seconds, so the ports are right.
To make sure I'm not doing something wrong with the programmer, I connected it to D0/D1 and switched baudrate to 115200 and it shows the serial output as expected.
Any idea where to look next?
-
So, I've tried to further invest the problem and there is something wrong with _dev.write() in transportSend(). It is too much C-Kungfu for me but there is something wrong with the conversion of data.
- _dev.write("bla") looks good in terminal
- _dev.write(to) throws trash in the terminal
- Serial.println(to) looks good in terminal
Here is the output of the soft-serial:
#���X��z �Humidity Sensor��X �� �1.0�X��b�X��b�X ��*�������X ��*�gfB�X ��*�������X ��*�gfB�XAnother thing:
The management of the DE-Pin is missing right?
-
to is a uint_t...so I guess it could generate trash in the terminal when showing the low ASCII values. Guess you'd have to use some other program to view the bytes sent.
Yes, de-pin was stripped away. Would it be useful in your setup?
(thankful for your help out testing it... I need some more time before I can setup a test rig)
-
Holy crap ...
It was too obvious. :-(Gateway can't work with this code:
https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewaySerialRS485/GatewaySerialRS485.ino#define MY_DEFAULT_ERR_LED_PIN 7 // Error led pin #define MY_DEFAULT_RX_LED_PIN 8 // Receive led pin #define MY_DEFAULT_TX_LED_PIN 9 // the PCB, on board LED@hek: Your code works!
The de-pin is set to high if a node wants to send something, otherwise it is set to ground.
I'll try to add that code. -
Holy crap ...
It was too obvious. :-(Gateway can't work with this code:
https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewaySerialRS485/GatewaySerialRS485.ino#define MY_DEFAULT_ERR_LED_PIN 7 // Error led pin #define MY_DEFAULT_RX_LED_PIN 8 // Receive led pin #define MY_DEFAULT_TX_LED_PIN 9 // the PCB, on board LED@hek: Your code works!
The de-pin is set to high if a node wants to send something, otherwise it is set to ground.
I'll try to add that code. -
:blush: I Sorry.. should have changed those pin assignments of course... I'll push an update ASAP.
@hek
I am wondering why did you remove the code responsible for dePin management ?
It's not possible to use MAX485 boards without tx/rx management. -
@LeoDesigner : yeah, you're right, that is needed.
But there seems to be another problem, I can't get the current development code to work. I've simply interconnected two Nanos at D8/D9. The gateway and the motion example come up fine according to serial output and the motion sensor definiatly sends something via AltSerial but the gateway doesn't seem to recognize it.
A simple test, where I pipe the input on hardware serial of one nano to AltSerial on the second nano and from there to hardware serial works just fine, so hardware and AltSerial is working.
Sender:
#include <AltSoftSerial.h> AltSoftSerial altSerial; void setup() { Serial.begin(115200); Serial.println("Demo begins"); altSerial.begin(115200); } void loop() { char c; if (Serial.available()) { c = Serial.read(); altSerial.print(c); } }Receiver:
#include <AltSoftSerial.h> AltSoftSerial altSerial; void setup() { Serial.begin(115200); Serial.println("Demo begins"); altSerial.begin(115200); } void loop() { char c; if (altSerial.available()) { c = altSerial.read(); Serial.print(c); } } -
I am very interested in doing this as I have many sensors local to my gateway. I use domoticz and see from http://www.domoticz.com/forum/viewtopic.php?f=6&t=8018 that people are doing this. I was just wondering would this do as a USB adaptor? Seems very cheap :
Also was looking at :
http://www.aliexpress.com/item/FREE-SHIPPING-5PCS-LOT-MAX485-module-RS485-module-TTL-turn-RS-485-module-MCU-development-accessories/1718665649.htmlAppreciate any feedback.
Thanks.
-
Hi everyone !
I needed a wired solution for my several nodes.
Here is the serial rs485/rs232 wired network transport for mysensors.
https://github.com/leodesigner/mysensors-serial-transport
It is based on the Majenko ICSC serial library.
Can you please test it? It is a beta version - but it is working for me.
(However, I am still waiting for my rs485 boards to arrive)
You can find more technical information at
http://sourceforge.net/p/arduino-icsc/wiki/RS-485/To use it, you have to:
- Put SerialTransport.cpp and SerialTransport.h to folder/directory/path SerialTransport in your library.
- Add #include <SerialTransport.h> to your .ino sketch
- Replace transport class with:
MyTransportSerial transport(Serial,0,-1); // serial port, node, dePin (-1 disabled)
Please let me know about bugs and how it is working for you.
I'm searching for a usb transport class, could i use this??
I'd like to connect my arduino (with sensors/actuators connected to it) to my rasberrypi through usb, without the wireless part, but still see them as mysensors sensors.
Is this possible or could someone make a transport class for it???
-
I'm searching for a usb transport class, could i use this??
I'd like to connect my arduino (with sensors/actuators connected to it) to my rasberrypi through usb, without the wireless part, but still see them as mysensors sensors.
Is this possible or could someone make a transport class for it???
@MarkV
Regarding the USB to RS485 adapters:
I think they are using a some kind of serial to USB chip like CH340/341. So your RPi will be able to see RS485 bus as the regular serial port. It may work. It worth trying, you can always build an Ethernet GW later in other case.@shabba
I am using exactly the same MAX485 modules. It is much more reliable media comparing to radio communication.@hek
Thanks for adding DE pin management code.