I am making a sensor that tracks humidity and temperature and reports it to the base station over RF using nRF24L01+ modules. The base station is an arduino MEGA and the sensor is a Duemilanove 328. I had it working pretty good for a bit. I havent changed the server code. But made a change to the sensor code to add a "," in between the 2 values. What makes no sense is the first values sent properly 25.00,23.00 but the next two values are shown in the serial as 25.00A,23.00 Here is the putty output:
Starting
37.000,21.000
37.000A,21.000
37.000A,21.000
This was the proper putty output I was getting before:
Starting
H49.00T23.00
H49.00T23.00
H49.00T23.00C
All I changed was commenting out the
transmit("H");
and
transmit("T");
lines and add
transmit(",");
Here is the code I am refering to:
Sensor:
/*Client
NANO 328:
MISO -> 12
MOSI -> 11
SCK -> 13
CE -> 8
CSN -> 7
GND -> GND
VCC -> 3.3v
*/
#include <SPI.h>
#include <Mirf.h>
#include <MirfHardwareSpiDriver.h>
#include <MirfSpiDriver.h>
#include <nRF24L01.h>
/*
* Sketch for testing sleep mode with wake up on WDT.
* Donal Morrissey - 2011.
*
*/
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
volatile int f_wdt=1;
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT11 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
int longtime = 0;
int sendcounter = 0;
// converts a float into a char
// and sends it via nRF24L01
void transmitf( float v)
{
byte c;
char buf[10];
dtostrf(v,6,3,buf);
for( int i=0 ; i<8 ; i++ )
{
c = buf[i];
Mirf.send(&c);
while( Mirf.isSending() ) ;
}
}
// sends a string via the nRF24L01
void transmit(const char *string)
{
byte c;
for( int i=0 ; string[i]!=0x00 ; i++ )
{
c = string[i];
Mirf.send(&c);
while( Mirf.isSending() ) ;
}
}
// send a CR/LF sequence via the nRF24L01
void transmitlf(void)
{
byte c;
c = '\r';
Mirf.send(&c);
while( Mirf.isSending() ) ;
c = '\n';
Mirf.send(&c);
while( Mirf.isSending() ) ;
}
void setup()
{
pinMode(A4, OUTPUT);
Mirf.cePin = 8;
Mirf.csnPin = 7;
// init the transceiver
Mirf.spi = &MirfHardwareSpi; // ad this
Mirf.init();
// we transmit only a single byte each time
Mirf.payload = 1;
// selecting a channel which is not too noisy
Mirf.channel = 90;
Mirf.config();
// Set 1MHz data rate
Mirf.configRegister(RF_SETUP,0x06);
// Set address - this one must match the
// address the receiver is using!
Mirf.setTADDR((byte *)"send1");
Serial.begin(9600);
dht.begin();
//WDT SETUP
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
}
void loop()
{
if(f_wdt == 1)
{
if (longtime > 3)
{
if (sendcounter < 3)
{
float v01,v02;
// read in some values
v01 = dht.readHumidity();
v02 = dht.readTemperature();
// transmit the first value
//transmit("H");
transmitf(v01);
// transmit a separator
transmit(",");
// transmit a second token
transmitf(v02);
//transmit("C");
// transmit a CR/LF for the PC
// software to sync to
transmitlf();
digitalWrite(A4, HIGH);
delay(700);
digitalWrite(A4, LOW);
Serial.print(v01);
Serial.print("%:");
Serial.print(v02);
Serial.println("C");
// ... just take your time
delay(3000);
sendcounter = sendcounter + 1;
loop();
}
longtime = 0;
sendcounter = 0;
f_wdt = 0;
/* Re-enter sleep mode. */
enterSleep();
}
longtime = longtime + 1;
enterSleep();
}
else
{
/* Do nothing. */
}
}
/***************************************************
* Name: ISR(WDT_vect)
*
* Returns: Nothing.
*
* Parameters: None.
*
* Description: Watchdog Interrupt Service. This
* is executed when watchdog timed out.
*
***************************************************/
ISR(WDT_vect)
{
if(f_wdt == 0)
{
f_wdt=1;
}
else
{
Serial.println("WDT Overrun!!!");
}
}
/***************************************************
* Name: enterSleep
*
* Returns: Nothing.
*
* Parameters: None.
*
* Description: Enters the arduino into sleep mode.
*
***************************************************/
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
Base Station:
/*Server
MEGA 2560:
MISO -> 50
MOSI -> 51
SCK -> 52
CE -> 8
CSN -> 7
GND -> GND
VCC -> 3.3v
*/
#include <SPI.h>
#include <Mirf.h>
#include <MirfHardwareSpiDriver.h>
#include <MirfSpiDriver.h>
#include <nRF24L01.h>
void setup()
{
Serial.begin(57600);
Mirf.cePin = 8;
Mirf.csnPin = 7;
Mirf.spi = &MirfHardwareSpi; // ad this
Mirf.init();
// name the receiving channel - must match tranmitter setting!
Mirf.setRADDR((byte *)"send1");
// just a single byte is transmitted
Mirf.payload = 1;
// we use channel 90 as it is outside of WLAN bands
// or channels used by wireless surveillance cameras
Mirf.channel = 90;
// now config the device....
Mirf.config();
// Set 1MHz data rate - this increases the range slightly
Mirf.configRegister(RF_SETUP,0x06);
Serial.println("Starting");
}
void loop()
{
byte c;
// is there any data pending?
if( Mirf.dataReady() )
{
// well, get it
Mirf.getData(&c);
// ... and write it out to the PC
Serial.print(c);
}
}
Any help is greatly appreciated !