I'm making a sensor to stick out in our well house to monitor light levels (did the door come open?), humidity (is there another leak?), and temperature. Transmission is via 433mHz, which appeared to be the best choice.
Board is an Elegoo R3 Uno atmega 328p, and the transmitter/receiver are SRX882s.
The sensor unit is working fine, and as far as I know is transmitting data.
The receiver doesn't seem to get anything. At one point it was printing ovf (overflow) where it was supposed to print data, but now it doesn't print anything.
I used code borrowed the first reply to this post.
Apologies for the messy code. The data transmission/receiving is the most programming I've ever done in my life. I am about 60% sure that the problem is somewhere in the coding based around buffers and buflen and such, but I don't know how to find out what the length of the message being transmitted is since I can't tell how many digits everything is actually being measured to and thus how many numbers or bytes it'd be, or whether the structure is sending plaintext labels along, etc.
Here's the transmitter code:
//temp & humidity sensor
#include "DHT.h"
#define DHT22_PIN 5
DHT dht22(DHT22_PIN, DHT22);
//light sensor
int temt6000Pin = A0;
float light;
int light_value;
//LCD display
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // declare it exists I2C address 0x27, 16 column and 2 rows
//Wireless Tx
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
//now this is where I create a data structure.
struct dataStruct{
float light;
float tempF;
float humi;
unsigned long counter;
}myData;
byte tx_buf[sizeof(myData)] = {0}; //testing copied from internet
void setup() {
Serial.begin(9600);
dht22.begin(); //initialize DHT sensor
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
// Initialize ASK Object for wireless Tx
rf_driver.init();
myData.light=4; //setting base values for the float
myData.humi=30; //same
myData.tempF=70; //same
}
void loop() {
int light_value = analogRead(temt6000Pin);
light = light_value * 0.0976; //percentage calculation
Serial.print("Light: ");
Serial.print(light);
Serial.print(" | "); //spacer
float humi = dht22.readHumidity();
float tempF = dht22.readTemperature(true);
//check for failures
if (isnan(humi) || isnan(tempF)){
Serial.println("failed to read from DHT22 sensor");
}else{
Serial.print("DHT22# Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | "); //spacer
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println("F");
}
lcd.clear(); //clear display
lcd.setCursor(0,0);//moves to there
lcd.print("Light:");
lcd.print(light,1); //set to round to 1 decimal
lcd.setCursor(0,1);//moves to there
lcd.print("Temp:");
lcd.print(tempF,0);//set to round to 0 decimal
lcd.print("F");
lcd.setCursor(9,1);//moves to there
lcd.print("Hum:");
lcd.print(humi,0);//set to round to 0 decimal
lcd.print("%");
memcpy(tx_buf, &myData, sizeof(myData) );
byte zize=sizeof(myData);
rf_driver.send((uint8_t *)tx_buf, zize); //note the test code just had driver. this will need to be changed for the receiver too probably
// driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
myData.counter++;
delay(2000);
}
Here's the receiver unit
//LCD display
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // declare it exists I2C address 0x27, 16 column and 2 rows
//Wireless Rx
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
//now this is where I create a data structure. Same as transmitter
struct dataStruct{
float light;
float tempF;
float humi;
unsigned long counter;
}myData;
byte rx_buf[sizeof(myData)] = {0}; //testing copied from internet, doesn't seem to do anything
void setup() {
Serial.begin(9600);
lcd.init(); //initialize the lcd
//turned off backlight until I'm at the LCD printout phase lcd.backlight(); //open the backlight
// Initialize ASK Object for wireless Rx
rf_driver.init();
myData.light=4; //setting base values for the float
myData.humi=30; //same
myData.tempF=70; //same
}
void loop() {
// Set buffer to size of expected message
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
// Check if received packet is correct size
if (rf_driver.recv(buf, &buflen))
{
int i;
// Message with a good checksum received, dump it.
rf_driver.printBuffer("Got:", buf, buflen);
memcpy(&myData, buf, sizeof(myData));
Serial.print("Light ");
Serial.print(myData.light);
// Serial.print("Temp: ");
// Serial.print(myData.tempF);
// Serial.print("F");
// Serial.print("Humidity: ");
// Serial.print(myData.humi);
// Serial.println("%");
//commenting out LCD because it's giving errors
//lcd.clear(); //clear display
//lcd.setCursor(0,0);//moves to there
//lcd.print("Light:");
//lcd.print(light,1); //set to round to 1 decimal
//lcd.setCursor(0,1);//moves to there
//lcd.print("Temp:");
//lcd.print(tempF,0);//set to round to 0 decimal
//lcd.print("F");
//lcd.setCursor(9,1);//moves to there
//lcd.print("Hum:");
//lcd.print(humi,0);//set to round to 0 decimal
//lcd.print("%");
delay(2000);}
}
thank you!



