#include <SoftwareSerial.h>
const byte HC12RxdPin = 4; // Recieve Pin on HC12
const byte HC12TxdPin = 5; // Transmit Pin on HC12
SoftwareSerial HC12(HC12TxdPin,HC12RxdPin); // Create Software Serial Port
void setup() {
Serial.begin(115200); // Open serial port to computer
HC12.begin(115200); // Open serial port to HC12
}
void loop() {
if(HC12.available()){ // If Arduino's HC12 rx buffer has data
Serial.write(HC12.read()); // Send the data to the computer
}
if(Serial.available()){ // If Arduino's computer rx buffer has data
HC12.write(Serial.read()); // Send that data to serial
}
}
This code runs on 2 Arduino Nano, each connected to a HC-12 LoRa 433Mhz with pigtail antennas. It does work, in a sense that both can transmit and receive data, but the data is full potatoe. Look at this garbage : https://i.imgur.com/eN2SN8P.png
How am I supposed to trust this thing? It sends/receives erratic chars.
Is this notorious on this module?
How can I solve this? What do I need to investigate?
Both Arduinos are powered from usb 5v (1 from desktop pc, 1 from laptop). Voltage is stable. The HC-12 get their share of that juice from a LDO voltage regulator so I can feed 3.3v (LM1117) as per the datasheet. RX/TX (via software serial) go in and out through a logic level shifter so that it can shift 5v<-->3.3v between Nano and HC-12
imagine sending "30°C" and receiving "40°C" instead...
Better antennas (17 cm straight wire) would help, but as already noted, Software Serial does not work well or at all, at baud rates higher than 38400. I'm surprised that you get results that are actually readable.
I thought 433mhz qualifies as "LoRa". Is LoRa 868mhz only?
Yes, i configured both HC-12 to 115200 as I experienced awful latency (~2 sec) but I later figured it might have been my code (I was using String class which waits for carriage return and automatically times out after 2-3sec if it does not find anymore chars coming into serial I believe).
I will switch back to 9600 and try it again later today and send you a feedback. My main concern is latency. Range in my case won't exceed 100m at most.
I'm using softwareserial because I have to upload code to the Nano and monitor/debug.
you might want to go to FU3. The module automatically adjust the baud rate of wireless transmission in the air according to serial port baud rate and unfortunately at 9600 bauds you'll get 15,000bps over the air
For high rate and low latency i use websockets with esp8266 anyway. By low latency I mean i do not want to wait 2 sec. Below 200ms is fine for my use case.
Now that I have switched back both HC-12 to 9600 baud rate, I can confirm the data is consistent and without any malformed char. Latency is very acceptable (below 200ms). Thank you very much for the hint guys.
Regarding "String", I read about how evil it is but it is hard to get rid of it. I'd love to, considering how often my heavy firmwares crash due to heap, but even the most simple operations like concatenation in C is a pain in the a**. I'm very comfortable programming web languages but even with 20 years xp in web dev I figured I spend most of my time trying to solve strong type issues when coding in C. This is not fun. In fact, it such a pain that my code is full of cr*p like this :
As you can see, this could be refactored (as the token string or hostname are the same) but I do not even dare to think about it.
If you know a good tutorial with PRACTICAL EXAMPLES i'd be glad to learn more.