Hi, I'm trying to make 2 Arduinos communicate over RF 434MHz using the RadioHead library. One of the Arduinos will take some sensor readings, right now it's only temperature and soil moisture content but I plan to add more sensors later. The temperature reading is stored as a float and the soil moisture as an int. What I want to do is pass this variables to the send-function and transmit them both at the same time. So I want all the readings to be sent in the same transmission and I will then have to split them up on the receiving side.
My sketch now looks like this (far from being finished but I want to solve this problem first):
#include <RHDatagram.h>
#include <RH_ASK.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//RH communication
#define CLIENT_ADDRESS 2
#define SERVER_ADDRESS 1
// Singleton instance of the radio driver
RH_ASK driver;
// Class to manage message delivery and receipt, using the driver declared above
RHDatagram manager(driver, CLIENT_ADDRESS);
//OneWire sensors
// Data wire is plugged into port 16 on the Arduino
#define ONE_WIRE_BUS 10
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup()
{
Serial.begin(9600);
//RF communication
if (!manager.init())
Serial.println("init failed");
// OneWire sensors
sensors.begin();
}
// RH communication, Dont put this on the stack:
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
void loop()
{
float tempValue;
int soilValue;
tempValue = temperature();
delay(1000);
soilValue = soil();
delay(1000);
SendValues();
Serial.println ("");
delay(2000);
}
//RH communication
void SendValues()
{
uint8_t data[] = "Hello World";
Serial.println("Sending to ask_datagram_server");
int i = 0;
int repeat = 5; //Times to repeat same message
// Send a message to manager_server
while (i<repeat)
{
if (manager.sendto(data, sizeof(data), SERVER_ADDRESS))
{
Serial.println("Message sent");
}
else
{
Serial.println("sendto failed");
}
delay(100);
i = i+1;
}
}
//Get temperatures from Dallas sensor
float temperature()
{
float temp;
// issue a global temperature request to all devices on the bus
Serial.print("Requesting temperatures from all...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Print temperatures
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
temp = sensors.getTempCByIndex(0);
return temp;
}
//Soil hygrometer
int soil()
{
int humidity = 0;
// read soil moisture from sensor
int sensorValue = analogRead(A1);
int sensorValueCons = sensorValue;
sensorValueCons = constrain (sensorValue, 300,1023);
//Sensor value 300 equals 100% humidity and sensor value 1023 equals 0% humidity
humidity = map (sensorValueCons, 300, 1023, 100, 0);
Serial.print ("Soil hygrometer: ");
Serial.print (humidity);
Serial.println ("%");
//Print sensor value
Serial.println ("Soil hygrometer sensor value: ");
Serial.println (sensorValue);
return humidity;
}
As you can see right now the send-function only sends "Hello world!" and the function does not accept any variables to be passed on to it. The values I want to send are stored in soilValue and tempValue present in the main loop. I assume I will need to convert the variable type and then combine them in some way, then pass it on to the send-function. I'm however not sure how to do this and could use some help here Perhaps I got it completely wrong and there is a much smarter way to do all of this
char int_packet[3]; //2 for int bytes and one for end of string thingy '/0'.
Convert the int in to 2 bytes:
int_packet[0]= the_int_to_be_sent;
int_packet[1]= the_int_to_be_sent >> 8; //Shift 8 bits along and store the least sig. 8 in a char.
int_packet[2]= '/0';
Then just send the contents of "int_packet".
The RX code then takes each byte at a time:
Reverse the bit shifting process to make the original int:
byte lowbyte=recieved_byte[0];
byte highbyte=recieved_byte[1];
int recieved_int=lowbyte+(highbyte << 8);