Zápisník experimentátora
Today, we continue to follow the previous article. We will work with radio modules using the RadioHead library. We will again use a pair of transmitter and receiver, which communicates at 433 MHz. Today's code will behave the same way but, according to my experience, it is not possible to combine VirtualWire and RadioHead. For the future, I will only be talking about RadioHead, because it is recommended by the author himself and I feel that this new library is more sophisticated.
Arduino Pro Mini and Arduino Mega are used in the photos. It does not limit you, you can use any Arduino.
Here the data differs slightly on the Internet, but it is usually 17.5 cm (alternatively 15 cm). This length must not be strictly observed in amateur conditions. Basically, every piece of wire you attach will significantly improve the properties of the transmitter.
You do not find this library in your library manager and you need to install it from a zip file. The library home page is http://www.airspayce.com/mikem/arduino/RadioHead/index.html. Install it in the [username]\documents\arduino\libraries\radiohead
directory. The library is designed to control multiple modules. Therefore, it should be remembered that for these 433 MHz modules it is necessary to use the RH_ASK
class.
These two simple programs communicate together. The first sends incrementing numbers to easily check connection failures, and the second program receives these numbers and displays them on the serial port. You see in the statement how many characters were received and what is the difference with the previous message. If there is a difference of 1, then there was no outage. If the number is bigger, then so many messages have gone out. Then it's easy to manipulate both Arduinos and watch what happened.
There is a shift to c++ in the RadioHead
library. Everything is programmed as a class and you call their functions. Since the RadioHead library is designed for a large number of radio modules, it is important to remember that the RH_ASK.H
header file is used for the cheapest modules.
The data pin is connected to pin 12 in Arduino. If this does not suit you, you can change it with the parameter in the constructor of the driver
object.
#include <RH_ASK.h>
RH_ASK driver;
char buffer[10];
int idx = 0;
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
sprintf(buffer, "%d", idx);
idx++;
driver.send((uint8_t *)buffer, strlen(buffer));
driver.waitPacketSent();
delay(200);
}
The data pin is connected to pin 11 in Arduine. If this does not suit you, you can change it with the parameter in the constructor of the driver
object.
#include <RH_ASK.h>
RH_ASK driver;
int last_idx = 0;
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
// Message with a good checksum received, dump it.
buf[buflen]=0;
Serial.print("Received(");
Serial.print(buflen);
Serial.print(",");
int idx=atoi(buf);
Serial.print(idx>last_idx ? "+" : "-");
Serial.print(idx-last_idx);
last_idx=idx;
Serial.print("}: ");
driver.printBuffer(buf, buf, buflen);
}
}
The source code is located on the GitHub server.
14.07.2017