Skip to content

Instantly share code, notes, and snippets.

@crycode-de
Created February 1, 2023 12:08
Show Gist options
  • Save crycode-de/10496eedfdf82781e878d87b489892f8 to your computer and use it in GitHub Desktop.
Save crycode-de/10496eedfdf82781e878d87b489892f8 to your computer and use it in GitHub Desktop.
Example Arduino sketch for receiving data from the weather sensor.
#define SERVER_ADDRESS 0x01
#define RH_SPEED 2000
#define RH_RX_PIN 5
#define RH_BUF_LEN 9
uint8_t rh_buf[RH_BUF_LEN];
#define RH_ASK_MAX_MESSAGE_LEN RH_BUF_LEN
#include <RHDatagram.h>
#include <RH_ASK.h>
RH_ASK driver(RH_SPEED, RH_RX_PIN);
RHDatagram manager(driver, SERVER_ADDRESS);
float t = 0;
float h = 0;
uint8_t bat_percent = 0;
uint8_t bat_raw = 0;
void setup() {
Serial.begin(9600);
if (!manager.init()) {
Serial.println("init failed");
} else {
Serial.println("init done");
}
}
void loop() {
if (manager.available()) {
uint8_t len = sizeof(rh_buf);
uint8_t from;
if (manager.recvfrom(rh_buf, &len, &from)) {
Serial.print("got message from : 0x");
Serial.println(from, HEX);
switch (rh_buf[0]) {
case 0x00:
// start message
Serial.println("0x00 start");
break;
case 0x01:
// DHT data
Serial.println("0x01 DHT data");
memcpy(&t, &rh_buf[1], 4);
memcpy(&h, &rh_buf[5], 4);
Serial.print(t);
Serial.print("°C - ");
Serial.print(h);
Serial.println("%");
break;
case 0x02:
// battery data
Serial.println("0x02 battery data");
bat_percent = rh_buf[1];
bat_raw = rh_buf[2];
Serial.print(bat_percent);
Serial.print("% - ADC: ");
Serial.println(bat_raw);
break;
case 0xee:
// error
Serial.println("0xEE error");
break;
default:
// should never happen
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment