Hi folks,
This is a general request for advice.
As you can see I’m part way through building this and I’m waiting for some hardware to be able to complete the project. My goal is to update here (if that’s appropriate) and gather feedback as i go.
My aim is to measure temperature in 5 different places and have these scrolled across an LCD display at the hub (Uno or Due on mains).
These units need to run from batteries, and small ones, too. So powering the board (3.3v Pro Mini) down is important
Code section 1 - run a sleep program to increase battery life -ACHIEVED
Section 2 - read a DHT 22 - ACHIEVED
Section 3 - Send readings via an NRF24L01 to the hub - Waiting for Hardware. I’ve been leaning on this Nrf tutorial pretty heavily. Still feel a bit intimidated by this. Especially getting the different pipes set up, and I’m still researching this.
Section 4 - Check bettery life - If low ping a buzzer - Thinking and research - This seems fairly easy in code but the electronic set up seems more challenging
Section 5 - Maybe?? Use a watchdog timer to reset the board, plus reset the counter would replace parts of section 6.
Section 6 - Increase loop counter and reset - ACHIEVED
For sections 1, 4, and 5 I’m using Gammon's power article to help. But, I’ll tackle those once I have the NRFs up and running.
/*
This is the testing area for the Temp transmitter.
This sketch needs to:
sleep for 8 seconds
wake up and check how many times it has been asleep
wake and complete the loop code (reading approx. every minute)
read a DHT 22
Send DHT data information back to the hub via NRF24L01
This will be part of a network of 5 DHT sensors, one reciever and 4 transmitters
reset the counter
go back to sleep
Code section 1 - run a sleep program for the desired amount of time to increase battery life. Multiples of 8 second sleep times -ACHIEVED
Section 2 - read a DHT 22 - ACHIEVED
Section 3 - Send readings via an NRF24L01 to the hub - Waiting for Hardware and Thinking and research
Section 4 - Check bettery life - If low ping a buzzer - Thinking and research - If works order more buzzers
Section 5 - Use a watchdog timer to reset the board and reduce hangs, plus reset the counter in the sleep loop
Section 6 - Increase loop number - ACHIEVED
Design spec
Run on a 3.3v Pro mini
Fit in a 60x60x20mm vented housing
Power from single CR2032
Be one of 4 transmitters to one hub (hub also reads DHT 22 and displays all readings on 20x4 LCD (mains power)
Paul Hoskins
This code is complied from examples and other open source code, as such it remains open source
*/
// ///LIBRARIES//////
// In here for the power saving on the Pro Mini
#include <LowPower.h>
// In here to read the DHT
#include <DHT.h>
// In here for when i need the NRF in place
#include <RF24.h>
#include <SPI.h>
//include wathcdog timer
#include <avr/wdt.h> //not using this yet will set up for a hang.
/////DEFINITIONS/////
// Set up the DHT
// the pin the DHT is connected to
#define DHTPIN 6
// The type of DHT sensor
#define DHTTYPE DHT22
// initialise DHT setup
DHT dht(DHTPIN, DHTTYPE);
// pulled from blink no delay, can't get this to work using delay instead. me=noob
// set up for delay period to enable a good read. i want to delay the beginning of the loop and after the serial text
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
// unsigned long dhtStarted = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to read (milliseconds) - can't make this work - do i even need it?
//number tracker for loops
static int count = 0; //start count at 0
static long reading = 7; // loop 7, or a minute ish, before reading dht
int setBack = 0; // sets the count back to 0
int readBack = 7; // sets reading back to 7
int resetTrigger = 1440; // final version =reset every 1440 counts
//Buzzer set up
const int buzzer = 9; //buzzer to arduino pin 9
void setup()
{ // put your setup code here, to run once:
Serial.begin(115200); //begin serial for debug
dht.begin();//begin the DHT 22
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output - Note to self, go buy buzzer
} // end of setup
void loop()
{// start loop code here
// unsigned long currentMillis = millis(); // set millis as variable - not using this as could not make this work in the code (remove?)
//CODE SECTION 1 - sleep loop
if (count <= reading)
{// open if loop
// puts the board to sleep for 8 seconds
LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
}// close if loop
else // This loop contains CODE SECTIONS 2 thorugh 4
{// Open else loop
//if (currentMillis - dhtStarted >= interval) // could not make this work (remove?)
{ // start if block
// save the last time you ran the loop - could not make this work (remove?)
// dhtStarted += 1000; // could not make this work (remove?)
//Read DHT
float hCam = dht.readHumidity();// read humidity from dht 22 for Cam's room
float tCam = dht.readTemperature(); // Read temperature as Celsius (the default) for Cam's room
delay(500); // gives time to read the sensor
//Increase the sleep number
reading = reading + 7; //increase reading number so it keeps looping
//remove for final, or replace with a warning buzz
if (isnan(hCam) || isnan(tCam)) // Check if any reads failed
{//start if block
Serial.println("Failed to read from DHT sensor!"); //replace with buzzer ding
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
/*
* Think about adding a buzzer sound here as monitor won't be connected to final work
*/
}//end if block
//to check everything is reading- remove later
Serial.print(" Humidity: ");
Serial.print(hCam);
Serial.print(" %\t");
Serial.print(" Temperature: ");
Serial.print(tCam);
Serial.println(" *C ");
delay (500); // gives time to print the sensor info
//CODE SECTION 3 - Transmit data via NRF24L01
{
Serial.println(F("Code section 3 NRF24L01 Module")); //So much information available it's a little confronting
delay(50);
}// Close 3
//CODE SECTION 4 - check battery - Buzz if voltage low
{
Serial.println(F("Code section 4 battery monitor")); //Research https://forum.arduino.cc/index.php?topic=83978.0
delay(50);
/*Buzzer loop code as above
* tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
*/
} //Close 4
} // close else block
//CODE SECTION 5 - Watchdog to reset board every 24 hours
{
Serial.println(F("Code section 5 Watch dog timer")); //More research needed for this. Is it better than the counter
delay(50);
} //Close 5
}
{// CODE SECTION 6 - Increase loop number and reset
// increase the counter
count ++;
}
if (count >= resetTrigger)
{
count = setBack;
reading = readBack;
//Serial to monitor progress - remove before final
Serial.println("Reset loop");
}
}//Close 6
//end of loop
As I don’t have a specific question, I errmd and arrd about posting because it’s difficult to comment, but please do comment constructively if you see things that could be done better.
Also, I know people don’t like delay, I tried using millis and it just sent everything crazy. I’ll look at this again before the final version, but it’s not a priority right now.
Thanks,
Cup