I'm working on some remote temperature sensors. I'm using RadioHead and cheap ASK radios (FS1000A and XY-MK-5V) and developing on Nano clones, will port to Digisparks when the prototype is working.
I'd like to send a small message size; one byte to indicate one of 14 stations, and two bytes to carry an Arduino platform int value (which is stored in two bytes according to the doc). The int is just exactly what I pull from an analogRead() with the default 10bit resolution.
My weakness is in the language itself and how to manage these bytes accurately. Could someone knowledgeable in C++ take a peek and tell me what may be wrong?
The output I'm receiving is looking like:
Received bytes:
1 B B
Station called: 1
Measurement: 2297
Total packets received: 9
Received bytes:
1 17 17
Station called: 1
Measurement: 2297
Total packets received: 10
Received bytes:
1 CA CA
Station called: 1
Measurement: 2297
Total packets received: 11
...
The problems you can see are:
- The 2nd and third bytes are always the same!
- My reconstitution of the two bytes back into an int on the other side is not working, either.
Sender:
#include <RH_ASK.h>
#if defined ( ARDUINO_AVR_NANO )
int rxPin = 11;
int txPin = 12;
int pttPin = 10;
int thermistor = A1;
#elif defined ( ARDUINO_AVR_DIGISPARK )
int rxPin = PB2; // unused
int txPin = PB0;
int pttPin = PB1;
int thermistor = PB3;
#endif
RH_ASK rf_driver;
void setup() {
rf_driver = RH_ASK( 2000, rxPin, txPin, pttPin, false );
Serial.begin( 9600 );
if ( rf_driver.init() ) {
Serial.print( "Radio initialized. Max message lenth: " );
Serial.println( rf_driver.maxMessageLength() );
// initialise thermistor circuit
pinMode( thermistor, INPUT );
// I'm alive
pinMode( LED_BUILTIN, OUTPUT );
digitalWrite( LED_BUILTIN, HIGH );
delay( 500 );
digitalWrite( LED_BUILTIN, LOW );
delay( 500 );
}
}
void loop() {
// The solution will have fewer than 255 telemetry sending stations
const uint8_t STATION = 1;
int thermistorReading = analogRead( thermistor ); // 10 bit value stored in a 2-byte int
Serial.println( thermistorReading, DEC );
const uint8_t LENGTH = 3;
uint8_t message[ LENGTH ];
message[0] = STATION;
message[1] = thermistorReading & 0b0000000011111111; // first byte of this int
message[2] = thermistorReading & 0b1111111100000000 >> 8; // second byte of this int
// send message
// TODO set Digispark clock to 8MHz
digitalWrite( LED_BUILTIN, HIGH );
// rf_driver.send( ( uint8_t * ) msg, strlen( msg ) );
if ( rf_driver.send( message, LENGTH ) ) {
rf_driver.waitPacketSent();
}
digitalWrite( LED_BUILTIN, LOW );
// To be replaced with power saving delay feature
delay( 1000 );
}
Receiver:
#include <RH_ASK.h>
#if defined ( ARDUINO_AVR_NANO )
int rxPin = 11;
int txPin = 12;
int pttPin = 10;
int thermistor = A1;
#elif defined ( ARDUINO_AVR_DIGISPARK )
int rxPin = PB2; // unused
int txPin = PB0;
int pttPin = PB1;
int thermistor = PB3;
#endif
RH_ASK rf_driver;
void setup()
{
rf_driver = RH_ASK( 2000, rxPin, txPin, pttPin, false );
if ( ! rf_driver.init() ){
pinMode( LED_BUILTIN, OUTPUT );
while ( true ) {
digitalWrite( LED_BUILTIN, HIGH );
delay( 1000 );
digitalWrite( LED_BUILTIN, LOW );
delay( 1000 );
}
}
// Setup Serial Monitor
Serial.begin( 57600 );
Serial.println( "Listening with serial 57600" );
}
void loop()
{
// Set buffer to size of expected message
uint8_t buf[ 3 ];
uint8_t buflen = sizeof( buf );
memset( buf, '\0', sizeof( buf ) );
// A well-formed packet has been received
if ( rf_driver.recv( buf, &buflen ) )
{
rf_driver.printBuffer( "Received bytes: ", buf, buflen );
uint8_t station = buf[0];
uint8_t m[2] = { '\0', '\0' };
m[0] = buf[1];
m[1] = buf[2];
int measurement = (int) m; // reconstitute two bytes into an int value
Serial.print( "Station called: " );
Serial.println( station, DEC );
Serial.print( "Measurement: " );
Serial.println( measurement, DEC );
Serial.print( "Total packets received: " );
Serial.println( rf_driver.rxGood(), DEC );
Serial.println();
}
}