The UART interface is designed to send text or hex commands between an external device (e.g., Arduino) and a host. The maximum command or data block size is 127 bytes. Every command line may terminate with a special configurable byte (Packet end): CR (0x0D), LF (0x0A), 0x03 (ETX), or any user-defined value in the Byte field. The firmware also adds the ending signature to each of the commands that the device receives from a host and outputs to UART. If the end signature is not defined, the firmware waits 300 ms and outputs the whole buffer to a host.
The firmware supports only one UART interface, even if a chip has more UART interfaces.
Communication parameters
Baud rate: you can select it in the configuration
Data bits: 8
Parity: None
Stop bits: 1
CC2530
UART uses fixed pin:
P02 – RX (receive).
P03 – TX (transmit).
All pins have the TTL level 3.3V.
You should enable UART on P02 on an output pin in the configuration. Pin P03 is optional.
CC2652
This chip allows using UART on any unused pin. All pins have the TTL level 3.3V.
- You should define the RX pin first.
- If you define only one pin in the configuration, the firmware supposes that it is the RX pin, and the firmware calculates the TX pin number automatically as TX = RX + 1
- If you define two UART pins, you can use any pin number, but the RX pin is always first.
The figure below shows an example with RX and TX pins: P12 – RX, P14 – TX.
FYI: TI LaunchPad™ kit with CC2652R uses P02 and P03 for RX and TX accordingly for XDS110 User UART.
Zigbee clusters
Zigbee cluster: ZCL_CLUSTER_ID_GEN_MULTISTATE_VALUE_BASIC
Zigbee attributes: ATTRID_IOV_BASIC_STATE_TEXT - UART data. You should use the "ZCL_DATATYPE_OCTET_STR" (0x41) or "ZCL_DATATYPE_CHAR_STR" (0x42) data types for this attribute.
Zigbee2MQTT commands
write – sends data to a UART.
Topic: zigbee2mqtt/[friedly_name]/set/action Payload: text command
The following command allows you to send any bytes. The first byte in the array (2) is the number of bytes following the length.
Topic: zigbee2mqtt/[friedly_name]/set Payload: {"action": [2, 0, 0]}
Example
if you want to send the following sequence of bytes: 0xAA 0x03 0x08 0x10 0x04 0xC9.
- Convert all bytes to decimal: 170 3 8 16 4 201.
- Count your bytes: 6.
- Make a payload string: {“action”: [6, 170, 3, 8, 16, 4, 201]}
- Send it using MQTT Explorer or a similar utility:
- Select the device in the tree (fig. 1).
- Go to “Publish” and specify the full topic name.
- Specify the payload.
- Click the Publish button.
The figure below shows data received from CC2530 on a Windows-based computer using Advanced Serial Port Monitor. Moreover, you can send data back using this terminal program by typing the necessary bytes in the edit box: AA#03#08#10#04#C9. Here, you do not need to count bytes and add the number in the front.
Nirm
Is it possible to bind the UART data transmission from end device to end device directly?
Owner
No, it is impossible.
Patrick
Can PTVO be used for transparent serial transmission between 2 hosts?
My usage example is sending UART commands from HomeAssistant with a CC2652P (Sonoff dongle) to a CC2530 with an mp3 module attached (connected via P02 and P03 to relevant RX/TX pins of the mp3 module) which can be controlled via UART commands.
Owner
Hi Patrick. I cannot say that it is fully transparent because it has the following limitations: strict communication parameters, bandwidth, data packet size, complexity of preparing command on the HA side.
So, if you like to send a short fixed command once per minute, it will work. Otherwise, you should try it in your application.
Lazabba
Thank you for sharing your work and such a generic design!
One of the hobby use-cases I implemented with ptvo is a Victron uart consumer with a zha quirk stitching, parsing and converting messages. I worked around the 127 bytes length limitation, but would it be possible to make the uart message length configurable (I think zigbee makes it possible to fragment messages, but I might be wrong and/or missing the details)?
Tom
Hi there, thank you for this awesome firmware.
I had been looking for a while for something to write custom zigbee devices with and was surprised to find you had done the work already.
I was trying, without success to get UART communication to work with a CC2530.
I kept getting “zigbee_publish_error” from zigbee2mqtt, regardless of where the message was sent from (even occured trying to set ‘action’ via the webui)
I found in one of the other feature comment sections that it might not work on OUTPUT1.
Changing UART to OUTPUT2 fixed my problem.
Not sure if this is specific to my boards (cheap Aliexpress), but hopefully this helps anyone else
JohnRob
I’m setting a CC2530 to use in the UART mode. I may be dense but I can’t figure out what the last parameter is “byte 239” where the 239 can be changed.
Thank you
John
Owner
It is a custom (user-defined) values for “Packet end”.
mantielero
Newbie speaking here. I have created a firmware with Output P0.2 UART 9600, Input P0.3 UART 9600. I connected VCC, GND, P0.2 and P0.3 to USB-FTDI and also P2.0 to GND. Then I use putty under Linux and connected to the /dev/ttyUSB0 interface using 9600. But nothing appears on the screen. What else should I bear in mind?
Owner
1. P03 and P20 are not necessary. 2. Did you send a command from MQTT or vice versa as described on this page?
Harrow
Hello, first of all thanks for your work.
I need to connect a CC2530 to a UART of a device configured in this mode : (speed : 9600, data : 7 bits, parity : even, end byte 0x03).
I tried to configure your firmware but it seems data settings is set to 8 bits. There is a way to change this option and the that of parity ?
Thanks
Owner
Sorry, but the firmware cannot use these communication parameters.
Serg
It seems I can only send at most 127 bytes. If I try to send more, nothing is sent out to UART. Is there any way to overcome this limitation?
Owner
You can send data in chunks but let the firmware send a previous chunk before.
gio
Hi, i’m using uart functions on cc2530 in both directions. It works fine but i have a question: it would be possible to arrange an external z2m converter in a way that permit to read a uart code as tag in Homeassistant? I mean using the MQTT Tag scanner integration: https://www.home-assistant.io/integrations/tag.mqtt/
Owner
It is possible if you will create a converter yourself. You should copy and edit
from Z2M\node_modules\zigbee-herdsman-converters\converters\fromZigbee.js to your custom converter.
gio
Sorry, could you show an example of converter to read uart code as tag, based on this guide?
I don’t have enough skills to edit it.
https://www.home-assistant.io/integrations/tag.mqtt/
gio
It would be great if you could add an option in the function to generate custom converter ,that automatically generate the converter for homeassistant tags.
Owner
Unfortunately, I don’t use HA and don’t know how this converter looks like.
Karl
Using SoftwareSerial library to print string to the cc2530 with a Arduino pro mini. Message received in Node-Red and MQTT Explorer 🙂 Arduino –> MQTT Broker works fine.
Can I send from MQTT Explorer or Node-Red and then receive string in Arduino? I have been trying to publish to the UART ….. no luck. Below is my Arduino code. Any pointers
#include
#define rxPin 13
#define txPin 12
#define ledPin LED_BUILTIN
// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
byte pinState = 0;
void setup() {
Serial.begin(9600);
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// set the data rate for the SoftwareSerial port
mySerial.begin(115200);
// test TX MQTT Explorer
mySerial.println(“From Arduino”);
delay(5000);
mySerial.println(“Arduino Second Try”);
Serial.println(“Test RX from MQTT Explorer”);
}
void loop() {
// listen for new serial coming in:
if (mySerial.isListening()) {
//Serial.println(“MySerial is listening!”);
}
if (mySerial.overflow()) {
Serial.println(“SoftwareSerial overflow!”);
}
if (mySerial.available()) {
Serial.write(mySerial.read());
}
}
Owner
Yes, you can send data in both directions. The page contains an example of how to do it using MQTT explorer.
Karl
Thx for confirming. Think I will prepare another flash file with inputs and outputs as normal switches. My setup only has the UART and the flashing LED. Device dos not show up like others in MQTT explorer. I had to write to l1 ON/OF before it showed up nicely.
I have tried all your examples before I wrote this. Will provide feedback once I have created a new flash version.
Parats
Hello, I need to get some ascii datas form uart communication.
Could I set up a uart input ?
Owner
If you define UART on “Output”, it can send data in both directions. But you can configure communication parameters.
Simon
Hi, I’m using in uart mode and I’m getting an empty action after every action I expect, is this normal? I’m also getting 3 status messages directly after each other, is there a way I can limit this to 1?
Owner
Hi. The problem is not clear. Please, open an issue on github and attach screenshots with your settings, logs, etc.
Peter
Hi, I created UART communication between CC2530 and Arduino nano. It communicates via json. Here is an example what I send and receive:
sending data:
topic: zigbee2mqtt//set/action
payload: {’tilt_corr2′:20}
receiving data:
topic: zigbee2mqtt//action
payload: {“pos1″:0,”pos2″:0,”pos3″:0,”speed”:700,”op_pos1″:0,”op_pos2″:0,”op_pos3″:0,”tilt_corr1″:0,”tilt_corr2″:20,”tilt_corr3″:0}
It works well about 80% of time. But sometimes instead of json string I get just a bunch of numbers:
topic: zigbee2mqtt//action
payload: 10,123,34,112,111,115,49,34,58,48,44,34,112,111,115,50,34,58,48,44,34,112,111,115,51,34,58,48,44,34,115,112,101,101,100,34,58,55,48,48,44,34,111,112,95,112,111,115,49,34,58,48,44,34,111,112,95,112,111,115,50,34,58,48,44,34,111,112,95,112,111,115,51,34,58,48,44,34,116,105,108,116,95,99,111,114,114,49,34,58,48,44,34,116,105,108,116,95,99,111,114,114,50,34,58,50,48,44,34,116,105,108,116,95,99,111,114,114,51,34,58,48,125
Do you have any idea what could be the issue ?
Owner
Hi! This data packet contains a non-printable character (10). The converter cannot encode it to string.
Peter
Thanks for the answer. I’ve played with the line ending settings to be specifically the same and it started to work fine. Now I just need to figure out why is a random character in string missing sometimes, especially with high traffic. But it looks like Arduino -> CC5230 issue..
Esa
Hi Peter
Any chance of getting your insights or even source code how you communicate through the UART via the JSON?
I got stm32 Blue Pill to send data through the UART but I’m stuck on how to actually use it.
Kind regards
Pavel
Somehow I am not getting anywhere.
I am trying to use CC2035 with a 4 relay.
The relays switch with:
turn_on_action:
– uart.write: [0xA0, 0x01, 0x01, 0xA2]
turn_off_action:
– uart.write: [0xA0, 0x01, 0x00, 0xA1]
i can’t figure out how to set these commands with mqtt.
Output1 -> P02 -> UART
Output2 -> P03 -> UART
Owner
Hi Pavel. Where do you execute the “uart.write” command?
Please, look here, how to send data using zigbee2mqtt
https://ptvo.info/zigbee-configurable-firmware-features/uart/
You should select “Packet end: none” in the UART settings in your case.
Pavel
This one example was from ESPHOME, and it worked fine. I want to replase esp01 with cc2530 like here: https://modkam.ru/?p=1551
After flashing it was integrated into z2m and over mqtt I try to send this commands like:
[0xA0, 0x01, 0x01, 0xA2]
[0xA0, 0x01, 0x00, 0xA1]
Owner
I’ve sent you a link where you can see the corresponding example. In your case, a payload will look like this:
2
{“action”: [4, 160, 1, 0, 161]}
Pavel
ok, I have to convert hex code into the number. This was the point. Thank you!
Roland
Thanks, did some testing but now I can’t get any UART data out, could be I’m making an error somewhere. I’m using a CC2531 USB dongle, this is the config:
/====
Board type: CC2530 (without external oscillator, like ??2531 dongle or Sonoff BasicZBR3)
Device type: Router
Transmit enable (TXEN): P12Receive enable (RXEN): P14
Update firmware’s timestamp : 2021-04-09
Status LED: P11, Periodic (every 5 seconds)
Set default reporting interval (s): 10
Output pins:
P00: Output 2, GPIO, External pull-up
P02: Output 5, UART (Baud rate: 9600, Packet end: 0x0D)
Input pins:
P12: Input 1, GPIO, External pull-up
====/
I do see that’s it is using TXEN and RXEN but there is no RF amp on the board and I can’t configure this in the main control panel when CC2531 is selected.
The board does pair ok, the LED works, only the UART does not.
Do I need a custom converter for the UART functions?
Owner
Do you expect UART data on the IO header or USB?
https://ptvo.info/zigbee-configurable-firmware-features/diy-configurable-router-firmware-for-cc2531-dongle/
Roland
I have a logic analyzer and connect that to P0.3 on the header. That’s where I’m expecting the serial data. I have found that P0.3 is TX and P.02 is RX. Did not do any testing with RX yet. Would like to use that as well in the future. USB is only for power in this configuration. I find the USB dongle a nice board because it has the leds and voltage regulator already on it. I use node red to send and recieve data to the board.
Owner
How do you test TX data? If you use Z2M in the middle, you should have a log file there. Maybe, it contains some messages or errors related to your device.
Roland
Done some testing with the UART, I got sending a string to work using the topic “zigbee2mqtt//l2/set/action”. Your example command does not contain the output number. If I leave this out I get a publish error. The second example (sending bytes) does not work. Can you give a more detailed example. What should I inject in the node? I did try a string, a JSON line but nothing works.
Thanks!
Owner
Did you try a firmware released 2021-04-05? I’ve fixed this problem there. Or you may configure UART on Output 1.
Roland
Was using version 201-03-01, but the download button gives version 2021-03-28. How can I get version 2021-04-05 ? Would like to try that one.
Owner
Please, try this link
https://ptvo.info/download/cc2530_io_1.2.2a.44539_firmware.zip?q=01617852571
Roland
Started with a clean configuration with only the status led on P11 and UART P02 on output 1, and now I have some serial data on pin P03. I think I had a false combination of inputs/outputs that prevented the UART from working. To get the RX portion working do I need to configure P03?
I bought you a coffee as a thank you for your support.
Owner
The old version of the firmware supported UART on Output 1 only. You need to configure only one pin, P02 or P03. The latest version supports UART on any output. Thank you for the coffee :).