I have been trying to setup my Arduino Nanode Remote to send temperature data to my Nanode Gateway via the RF12 radio.
I am using a one-wire setup with 3 ds18b20 temperature sensors with a 4.7kohm pullup resistor. They are powered off the Nanode itself using the 5volt pin.
Strangely, my code sends data to my gateway just fine until I insert the code to read the temperature sensors.
To trouble shoot I put together some bare minimal sketches. The bare sketch initializes the radio and then sends a temperature value via the radio to my gateway. My gateway then prints out the temperature value it received to the serial monitor.
The first example is my bare code that works and sends a temperature value of 65 to my gateway. It has none of my onewire code in the sketch except for the library.
#include <JeeLib.h>
#include <DallasTemperature.h> //library for temperature sensors
#include <OneWire.h> //library for the onewire bus
#define ONE_WIRE_BUS 4 //the onewire bus is connected to pin 7 on arduino
#define TEMPERATURE_PRECISION 10 //resolution of the sensors is set to 10bit
/*
RF12 Communications
*/
#define RF12_GROUPID 212 // all nodes must be a member of the same group
// to communicate with each other
#define RF12_NODEID 2 // Each node within a group must have a unique ID
OneWire oneWire(ONE_WIRE_BUS); // setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature
// variable to store the number of sensors
typedef struct {
byte node;
long time;
float temp;
} Payload;
// Create an instance of this data structure
Payload mypayload;
void setup(void) // setup-function runs at the startup of the arduino
{
Serial.begin(9600); // start the serial port
Serial.print("Remote Sensor\n");
// Initialize RF12 Radio
mypayload.node = rf12_initialize(RF12_NODEID, RF12_433MHZ, RF12_GROUPID);
}
void loop() // loop function runs over and over again
{
rfsend(); // calls up rfsend to send data via the RF12 Radio
}
void rfsend () //sends data via RF12
{
float temperaturetest;
if (rf12_recvDone() && rf12_crc == 0 && rf12_len == 0 && RF12_WANTS_ACK) {
// Fill payload with current time in milliseconds
mypayload.time = millis();
temperaturetest = 65;
// Fill payload with temperature info
mypayload.temp = temperaturetest;
Serial.println(temperaturetest);
// start transmission
rf12_sendStart(RF12_ACK_REPLY, &mypayload, sizeof mypayload);
}
}
Now here is the sketch I am trying out to read my onewire sensors. The code to read the sensors works just fine and prints their temperature values to the serial monitor on my remote.
The problem is the radio seems to stop sending data to my gateway. So when I view the serial monitor on my gateway I get no temperature value received.
To trouble shoot I even kept the data I was sending to the gateway exactly the same as in my previous example.
Any ideas on why my data is not being sent?
#include <JeeLib.h>
#include <DallasTemperature.h> //library for temperature sensors
#include <OneWire.h> //library for the onewire bus
// Start of Nanode Remote code
// Based on the pollee code from <jc@wippler.nl>
// 2011-11-23 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
//
#include <JeeLib.h>
/*
RF12 Communications
*/
#define RF12_GROUPID 212 // all nodes must be a member of the same group
// to communicate with each other
#define RF12_NODEID 2 // Each node within a group must have a unique ID
#define ONE_WIRE_BUS 4 //the onewire bus is connected to pin 4 on my arduino
#define TEMPERATURE_PRECISION 10 //resolution of the sensors is set to 10bit
OneWire oneWire(ONE_WIRE_BUS); // setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature
int i = 0;
// Definition of Data Structure used to send information from Remote to Gateway
typedef struct {
byte node;
long time;
float temp;
} Payload;
// Create an instance of this data structure
Payload mypayload;
//Arduino Setup code, run once after reset
void setup () {
Serial.begin(9600);
Serial.print("Remote Sensor\n");
// Initialize RF12 Radio
mypayload.node = rf12_initialize(RF12_NODEID, RF12_433MHZ, RF12_GROUPID);
sensors.begin(); // start up the library
int numSensors = sensors.getDeviceCount(); // store the number of sensors to the variable numSensors,
int temparray[numSensors]; // array with "numSensors" storage places for the temperature of each sensor
Serial.println("Enumerating and scanning for sensors.");
if(numSensors > 0) // if there is at least one sensor:
{
Serial.print("Enumerated "); //print the number of sensors to serial port
Serial.print(numSensors);
Serial.println( " sensors.");
}
else //if there is no sensor:
{
Serial.println("No sensors enumerated."); // tell the serial port
}
}
// main loop
void loop () {
int numSensors = sensors.getDeviceCount();
int temparray[numSensors]; // array with "numSensors" storage places for the temperature of each sensor
// "sensors.getDeviceCount" is a function in the library
sensors.requestTemperatures(); // send the request for temperature to sensors (all sensors)
delay(100);
for(i=0; i<numSensors; i++) // as long as "i" ( chosen number, starts at 0) is smaller than
//"numSensors" (number of sensors) do the "for" cycle
{
int temp = sensors.getTempCByIndex(i); // take temperature reading from sensor "i" and store it to the variable "temp"
temparray[i] = temp; // store the temperature from sensor i to storage place i in the array
}
for (i=0; i<numSensors; i++)
{
Serial.print("temp sensor #");
Serial.print(i); //prints sensor number
Serial.print("=");
Serial.println(temparray[i]); //prints temp
}
delay(100);
rfsend(); // calls up the rfsend to send data via the RF12 Radio
} // loop end
void rfsend () //sends data via rf12
{
float temperaturetest;
// wait to be polled by Gateway
if (rf12_recvDone() && rf12_crc == 0 && rf12_len == 0 && RF12_WANTS_ACK) {
// Fill payload with current time in milliseconds
mypayload.time = millis();
temperaturetest = 65; // used for testing to prove radio is working
// Fill payload with temperature info
mypayload.temp = temperaturetest;
Serial.println(temperaturetest);
// start transmission
rf12_sendStart(RF12_ACK_REPLY, &mypayload, sizeof mypayload);
}
} // rfsend end