Very interesting !! Thanks for the idea !
David.
Very interesting !! Thanks for the idea !
David.
Very good job !
When will share the schematic ?
How is responding the PIR ?
And off course, how is the battery usage ? So many sensors could discharge it faster ...
David.
@scalz : I missed your reply ! Interesting withe paper !
@scalz said:
@Nca78 you can have brownout if the battery is near end of life and nothing has been designed to handle a bit the internal res. but with a fresh a coincell and some capa, you can do some chain tx but that's not the best for the battery life.
The only case where i had issue with brownout at startup, plus it was a fresh coincell was with a crappy ali coincell batt!! very bad quality! now I'm using only good quality coincell like duracell, varta, maxell.. and that's day and night
@carlierd If you're interested this doc explains very well the capacitor calc etc... http://www.ti.com.cn/cn/lit/wp/swra349/swra349.pdf
- capa ideally calculated/estimated (on mine I have 200uF, plus others for sensors, radio is 86uA etc..) and after multiple msg presentation on frsh coincell i don't fall under 2.85 (voltage starts 3.05V) because I can't recover voltage during this period.
Hopefully a transmit is not 1sec! More something like says 30+ ms (depending if signing is needed, ack, retries.. etc). To not break capa benefits, it's better to sleep between tx during chains..to recover voltage and optimize battery life. Sleeping 200ms can be enough- For the capa leakage, it depends of capa. common good quality ceramic have not those leakage (few nano). Sure on this design it's not a ceramic.
- You're right at runtime 1Mhz consumes less than 8Mhz; mA vs thousands uA. But at 1Mhz everything is slower, comms too (I mean code execution). During deep sleep, there is no difference in power consumption. Oscillator is stopped so mhz does not mean. Note: at 8Mhz you can wake in few uA, at 1Mhz it's slower of course.
Hello,
What is the source of the 3,3 ? I got some issues with poor voltage converter. Using an arduino uno board which provides 5 and 3,3V solved my problem.
David.
Hello,
Some pictures of my scene controller inspired by this post ! I modified the original sketch to have two functions: keyboard for my alarm (from key 1 to 9) and scene controller (from key 10 to 16). Thanks @awi for the idea and the original sketch !
David.
@Fleischtorte So I am not the only one David waiting for ATC and RSSI
David.
Interesting !
I will check in few weeks the result. There is several capacitors to help the stability maybe it could be enough. If it's not the case I will replace the CR2032 by 2 * AAA.
@Nca78 Like @mfalkvidd wrote, the expected life duration is greater than 2 years. But it's just theory. My first node (on 2*AA) was started several weeks ago and the battery level did not change. So I am confident
I measure the power consumption when working at 1 MHz and there was no difference between 1 or 8 MHz. As I have to decrease the transmission speed at 1 MHz, I got some issues. The main was probably with the signing feature.
@scalz I will wait. I am curious to see the result on my nodes !
Hello,
Very complete board !
How did you report RSSI ? It is RSSI of the node or the gateway ?
David.
Hello,
After several weeks I finally succeed to create a dedicated PCB ! It was started yesterday evening and works fine.
Some pictures:
Measure is reported each two hours. So I expect more than two years on a CR2032 (using @GertSanders firmware).
I can off course provide any information as PCB.
Thanks @mfalkvidd for the idea and the code. The project is now WAF
David.
Hello,
All nodes are still working correctly but I still want to replace DHT22. But nothing done for the moment. Not at top position on my todo list
David.
Hello,
As written in the subject I am looking for a box for the TTP229 keyboard:
@awi made one in this post but it's not WAF enough ...
Someone found one ? Or a design I can print ?
Thanks,
David.
So easy ! I finally used directly BRD file and not GERBER.
Thanks for the advice on DRU.
The strange thing is that I tried with CAM job of OSH Park but the result was the same. But it's working with BRD so let's order my first board
David.
Hello,
I draw a board in Eagle. Everything seems ok but when uploading gerber files to OSHPark, the board top and bottom sizes are not correct:
All other layers sizes are correct:
I verify lot of things and the board is correctly defined on layer 20 (Dimension).
This is the board if you want to check :
0_1462540785593_Office plant monitor v1.0.brd
Thanks !!
David.
Hello,
Last modification:
I still have your make minor modification on marking and then go to production
David.
@hek : The plant-ground stick is not used. @mfalkvidd did the voltage divider using pull-up. I plan to directly solder the sensor to the pcb.
@scalz : will upgrade it. I already modified a little by using more space and have a less dense board.
Thanks for your remarks @m26872 and @scalz !! Will try to implement. Easy for RX-TX twice but not so simple for gnd plane ! I will try for the v2 !
@scalz : my board routing lessons are very very far (17 years from now) so I forgot them completely. It's why I used the autoroute function.
David
Hello,
I finally spent some hours on Eagles.
See the result in this post !
Do not hesitate to give me advice.
David.
Hello,
Thanks to @mfalkvidd I implement my first plant monitor. As it's working perfectly I decided to create a specific design base on the following post.
The schematic :
The board (route by eagles):
I tried to make the smaller board possible.
Any comment / advice is really recommended
David
@scalz : very interesting ! Will you plan to make one ? I am pretty sure that your design could be very very very small
Hello,
Thanks for this useful and simple project !
My first node is running perfectly since 10 days.
Next step is to create a PCB and try running on a CR2032 to minimize the size (WAF request ;))
One question on the uses : when do you set a warning on the result ? When I need to water the flower ?
My first node is close to 50%, is it time ? Off course it depends on the nature of the flower but I was not able to found the information on Google ...
David.
Very nice design !!
One question, do you sleep ? I know the answer : no !!!
Merci pour le lien ! Trรจs instructif !
@scalz said:
http://www.dorkbotpdx.org/blog/paul/better_spi_bus_design_in_3_steps
First time I see this resistor ! Will add it in my design. But I need to learn more information on the SPI first.
David.
Hello !
I was inspired by your post and adapt your code to my needs:
For fun I added a buzzer and a final song is played in case the code is sent.
No control of the code in the sensor, it will be done in the controller (Jeedom).
The code if someone is interested:
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*
* Code modified from AWI
* (http://forum.mysensors.org/topic/3128/slim-node-scene-controller-keypad)
*
* Buzzer code from Sparkfun
* (SparkFun Inventor's Kit / Example sketch 11)
*
*/
/**************************************************************************************/
/* Keyboard sensor. */
/* */
/* Version : 1.5.10 */
/* Date : 17/04/2016 */
/* Modified by : David Carlier */
/**************************************************************************************/
/* --------------- */
/* RST | | A5 */
/* RX | | A4 */
/* TX | ARDUINO | A3 */
/* RFM69 (DIO0) --------- D2 | UNO | A2 */
/* Keyboard SDA --------- D3 | | A1 */
/* Keyboard SCL --------- D4 | ATMEGA 328p | A0 */
/* +3v --------- VCC | | GND --------- GND */
/* GND --------- GND | 8MHz int. | REF */
/* OSC | | VCC --------- +3v */
/* OSC | | D13 --------- RFM69 (SCK) */
/* D5 | | D12 --------- RFM69 (MISO) */
/* D6 | | D11 --------- RFM69 (MOSI) */
/* D7 | | D10 --------- RFM69 (NSS) */
/* LED --------- D8 | | D9 --------- Buzzer */
/* --------------- */
/* */
/* +3v = 2*AA */
/* */
/**************************************************************************************/
#include <SPI.h>
#include <MySensor.h>
#include <MyTransportRFM69.h>
#include <MySigningAtsha204Soft.h>
//Constants for MySensors
#define SKETCH_NAME "Keyboard Sensor"
#define SKETCH_VERSION "1.5.10"
#define CHILD_ID_DIGICODE 0
#define KEYPAD_ID_SCENE 1
#define CHILD_ID_VOLTAGE 2
#define SDA_PIN 3
#define SCL_PIN 4
#define LED_PIN 8
#define BUZZER_PIN 9
#define BUZZ_FREQ_KEY 350
#define BUZZ_TIME_KEY 100
#define BUZZ_FREQ_ERROR 75
#define KEYBOARD_SIZE 16
#define MAX_SCENE 7
#define BATTERY_FULL 3143 // 2xAA usually gives 3.143V when full
#define BATTERY_ZERO 2340 // 2.34V limit for 328p at 8MHz
//Length must equal the total number of notes and spaces
const byte songLength = 18;
//Notes is an array of text characters corresponding to the notes
//in your song. A space represents a rest (no tone)
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
//Beats is an array of values for each note and rest.
//A "1" represents a quarter-note, 2 a half-note, etc.
//Don't forget that the rests (spaces) need a length as well.
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
//The tempo is how fast to play the song.
//To make the song play faster, decrease this value.
int tempo = 113;
//Misc. variables
unsigned long WAITING_TIME = 2000; //Waiting time for key pressing
boolean keyState[KEYBOARD_SIZE];
//Construct MySensors library
MySigningAtsha204Soft signer;
MyHwATMega328 hw;
MyTransportRFM69 transport;
MySensor node(transport, hw, signer);
MyMessage msgCode(CHILD_ID_DIGICODE, V_VAR1);
MyMessage msgSceneOn(KEYPAD_ID_SCENE, V_SCENE_ON);
MyMessage msgSceneOff(KEYPAD_ID_SCENE, V_SCENE_OFF);
MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE);
/**************************************************************************************/
/* Initialization */
/**************************************************************************************/
void setup()
{
//Get time (for setup duration)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Setup buzzer and LED pins
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
blinkLedFastly(3);
//Set keyboard pin
pinMode(SDA_PIN,INPUT);
pinMode(SCL_PIN, OUTPUT);
digitalWrite(SCL_PIN, HIGH);
//Start MySensors and send the Sketch Version Information to the Gateway
node.begin();
node.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
//Register all sensors
node.present(CHILD_ID_DIGICODE, S_CUSTOM);
node.present(KEYPAD_ID_SCENE, S_SCENE_CONTROLLER);
node.present(CHILD_ID_VOLTAGE, S_MULTIMETER);
//Load last Scenestates from EEPROM (7 scenestates available)
for (int i = 0 ; i < MAX_SCENE; i++)
{
keyState[i] = node.loadState(i);
#ifdef DEBUG
Serial.print(" "); Serial.print(keyState[i]);
#endif
}
//Setup done !
blinkLedFastly(3);
//Print setup debug
#ifdef DEBUG
int duration = millis() - startTime;
Serial.println("");
Serial.print("[Setup duration: "); Serial.print(duration, DEC); Serial.println(" ms]");
#endif
}
/**************************************************************************************/
/* Main loop */
/**************************************************************************************/
void loop()
{
//Sleep, node wakes up on key interrupt
byte interuptionType = node.sleep(SDA_PIN - 2, FALLING);
//Verify if a key was pressed
if (interuptionType == 0)
{
return;
}
//Get time (for a complete loop)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Get first digit
byte key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0)
{
Serial.print("bah si !");
Serial.println(interuptionType);
blinkLedFastlyAndBuzz(3);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
int digicode = key * 1000;
//Check if key corresponds to a scene touch or a code
if (key > 9)
{
//Update scene state
node.saveState(key - 10, (boolean)node.loadState(key - 10)?false:true);
//Report data to the gateway
long voltage = getVoltage();
node.send(node.loadState(key - 10)?msgSceneOn.set(key - 10):msgSceneOff.set(key - 10));
node.send(msgVolt.set(voltage / 1000.0, 3));
int batteryPcnt = round((voltage - BATTERY_ZERO) * 100.0 / (BATTERY_FULL - BATTERY_ZERO));
if (batteryPcnt > 100) {batteryPcnt = 100;}
node.sendBatteryLevel(batteryPcnt);
//Print debug
#ifdef DEBUG
Serial.print("Scene ");
Serial.print(key - 10);
Serial.print(": ");
Serial.print((boolean)node.loadState(key - 10)?" 1 ":" 0 ");
Serial.print(" ");
Serial.print(voltage / 1000.0);
Serial.print(" v");
int duration = millis() - startTime;
Serial.print(" ");
Serial.print("["); Serial.print(duration, DEC); Serial.println(" ms]");
Serial.flush();
#endif
}
else
{
//Wait for next key pressed or WAITING_TIME without action
interuptionType = node.sleep(SDA_PIN - 2, FALLING, WAITING_TIME);
//Verify if a key was pressed
if (interuptionType == 0)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Get second digit
key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0 || key > 9)
{
blinkLedFastlyAndBuzz(3);
node.sleep(1000);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
digicode = digicode + key * 100;
//Wait for next key pressed or WAITING_TIME without action
interuptionType = node.sleep(SDA_PIN - 2, FALLING, WAITING_TIME);
//Verify if a key was pressed
if (interuptionType == 0)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Get third digit
key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0 || key > 9)
{
blinkLedFastlyAndBuzz(3);
node.sleep(1000);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
digicode = digicode + key * 10;
//Wait for next key pressed or WAITING_TIME without action
interuptionType = node.sleep(SDA_PIN - 2, FALLING, WAITING_TIME);
//Verify if a key was pressed
if (interuptionType == 0)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Get fourth digit
key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0 || key > 9)
{
blinkLedFastlyAndBuzz(3);
node.sleep(1000);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
digicode = digicode + key;
//Report data to the gateway
long voltage = getVoltage();
node.send(msgCode.set(digicode));
node.send(msgVolt.set(voltage / 1000.0, 3));
int batteryPcnt = round((voltage - BATTERY_ZERO) * 100.0 / (BATTERY_FULL - BATTERY_ZERO));
if (batteryPcnt > 100) {batteryPcnt = 100;}
node.sendBatteryLevel(batteryPcnt);
//Print debug
#ifdef DEBUG
Serial.print("Complete code: ");
Serial.print(digicode);
Serial.print(" ");
Serial.print(voltage / 1000.0);
Serial.print(" v");
int duration = millis() - startTime;
Serial.print(" ");
Serial.print("["); Serial.print(duration, DEC); Serial.println(" ms]");
Serial.flush();
#endif
//Play final song instead of sleeping
playFinalSong();
}
//Wait to avoid false interruption
node.sleep(1000);
}
/**************************************************************************************/
/* Allows to play the final song. */
/**************************************************************************************/
void playFinalSong()
{
//Declare local variables
int i, duration;
// step through the song arrays
for (i = 0; i < songLength; i++)
{
// length of note/rest in ms
duration = beats[i] * tempo;
// is this a rest?
if (notes[i] == ' ')
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(BUZZER_PIN, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
}
/**************************************************************************************/
/* takes a note character (a-g), and returns the corresponding frequency in Hz */
/* for the tone() function. */
/**************************************************************************************/
int frequency(char note)
{
//Declare local variables
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
/**************************************************************************************/
/* Allows to get the pressed key. */
/**************************************************************************************/
byte getKeyPressed()
{
//Get first bit
digitalWrite(SCL_PIN, LOW);
delayMicroseconds(100);
digitalWrite(SCL_PIN, HIGH);
//Read all keys
for(int i = 0; i < KEYBOARD_SIZE; i++)
{
keyState[i] = (digitalRead(SDA_PIN) == LOW);
digitalWrite(SCL_PIN, LOW);
delayMicroseconds(100);
digitalWrite(SCL_PIN, HIGH);
delayMicroseconds(100);
}
//Read keyboard table to find the pressed key
byte keyPressed = -1;
for(int i = 0 ; i < KEYBOARD_SIZE; i++)
{
if (keyState[i])
{
keyPressed = i;
}
}
//Return the result
return (keyPressed + 1);
}
/**************************************************************************************/
/* Allows to fastly blink the LED. */
/**************************************************************************************/
void blinkLedFastly(byte loop)
{
byte delayOn = 150;
byte delayOff = 150;
for (int i = 0; i < loop; i++)
{
blinkLed(LED_PIN, delayOn);
delay(delayOff);
}
}
/**************************************************************************************/
/* Allows to fastly blink the LED and to play a sound on the buzzer. */
/**************************************************************************************/
void blinkLedFastlyAndBuzz(byte loop)
{
byte delayOn = 150;
byte delayOff = 150;
for (int i = 0; i < loop; i++)
{
blinkLed(LED_PIN, delayOn);
tone(BUZZER_PIN, BUZZ_FREQ_ERROR, delayOff);
delay(delayOff);
}
}
/**************************************************************************************/
/* Allows to blink a LED. */
/**************************************************************************************/
void blinkLed(byte pinToBlink, int delayInMs)
{
digitalWrite(pinToBlink,HIGH);
delay(delayInMs);
digitalWrite(pinToBlink,LOW);
}
/**************************************************************************************/
/* Allows to get the real Vcc (return value in mV). */
/* http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ */
/**************************************************************************************/
long getVoltage()
{
ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
delay(50); // Let mux settle a little to get a more stable A/D conversion
//Start a conversion
ADCSRA |= _BV( ADSC );
//Wait for it to complete
while (bit_is_set(ADCSRA, ADSC));
//Compute and return the value
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
David.
@scalz : one question about RFM, why RFM69CW and not HW ? And why the 10k resistor between CS and VCC ?
David.
Hello @m26872 , I set some serial.print in the code and found that i need to add some node.sleep when an incorrect key is pushed. The problem was due to the fact that the interruption pin is still valid when i restart the loop function !
David.
Hello,
This is the code:
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*
* Code modified from AWI
* (http://forum.mysensors.org/topic/3128/slim-node-scene-controller-keypad)
*
* Buzzer code from Sparkfun
* (SparkFun Inventor's Kit / Example sketch 11)
*
*/
/**************************************************************************************/
/* Keyboard sensor. */
/* */
/* Version : 1.4.10 */
/* Date : 15/04/2016 */
/* Modified by : David Carlier */
/**************************************************************************************/
/* --------------- */
/* RST | | A5 */
/* RX | | A4 */
/* TX | ARDUINO | A3 */
/* RFM69 (DIO0) --------- D2 | UNO | A2 */
/* Keyboard SDA --------- D3 | | A1 */
/* Keyboard SCL --------- D4 | ATMEGA 328p | A0 */
/* +3v --------- VCC | | GND --------- GND */
/* GND --------- GND | 8MHz int. | REF */
/* OSC | | VCC --------- +3v */
/* OSC | | D13 --------- RFM69 (SCK) */
/* D5 | | D12 --------- RFM69 (MISO) */
/* D6 | | D11 --------- RFM69 (MOSI) */
/* D7 | | D10 --------- RFM69 (NSS) */
/* LED --------- D8 | | D9 --------- Buzzer */
/* --------------- */
/* */
/* +3v = 2*AA */
/* */
/**************************************************************************************/
#include <SPI.h>
#include <MySensor.h>
#include <MyTransportRFM69.h>
#include <MySigningAtsha204Soft.h>
//Constants for MySensors
#define SKETCH_NAME "Keyboard Sensor"
#define SKETCH_VERSION "1.4.10"
#define CHILD_ID_DIGICODE 0
#define KEYPAD_ID_SCENE 1
#define CHILD_ID_VOLTAGE 2
#define SDA_PIN 3
#define SCL_PIN 4
#define LED_PIN 8
#define BUZZER_PIN 9
#define BUZZ_FREQ_KEY 350
#define BUZZ_TIME_KEY 100
#define BUZZ_FREQ_ERROR 75
#define KEYBOARD_SIZE 16
#define MAX_SCENE 7
#define BATTERY_FULL 3143 // 2xAA usually gives 3.143V when full
#define BATTERY_ZERO 2340 // 2.34V limit for 328p at 8MHz
//Length must equal the total number of notes and spaces
const byte songLength = 18;
//Notes is an array of text characters corresponding to the notes
//in your song. A space represents a rest (no tone)
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
//Beats is an array of values for each note and rest.
//A "1" represents a quarter-note, 2 a half-note, etc.
//Don't forget that the rests (spaces) need a length as well.
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
//The tempo is how fast to play the song.
//To make the song play faster, decrease this value.
int tempo = 113;
//Misc. variables
unsigned long WAITING_TIME = 2000; //Waiting time for key pressing
boolean keyState[KEYBOARD_SIZE] ;
//Construct MySensors library
MySigningAtsha204Soft signer;
MyHwATMega328 hw;
MyTransportRFM69 transport;
MySensor node(transport, hw, signer);
MyMessage msgCode(CHILD_ID_DIGICODE, V_VAR1);
MyMessage msgSceneOn(KEYPAD_ID_SCENE, V_SCENE_ON);
MyMessage msgSceneOff(KEYPAD_ID_SCENE, V_SCENE_OFF);
MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE);
/**************************************************************************************/
/* Initialization */
/**************************************************************************************/
void setup()
{
//Get time (for setup duration)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Setup buzzer and LED pins
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
blinkLedFastly(3);
//Set keyboard pin
pinMode(SDA_PIN,INPUT);
pinMode(SCL_PIN, OUTPUT);
digitalWrite(SCL_PIN, HIGH);
//Start MySensors and send the Sketch Version Information to the Gateway
node.begin();
node.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
//Register all sensors
node.present(CHILD_ID_DIGICODE, S_CUSTOM);
node.present(KEYPAD_ID_SCENE, S_SCENE_CONTROLLER);
node.present(CHILD_ID_VOLTAGE, S_MULTIMETER);
//Load last Scenestates from EEPROM (7 scenestates available)
for (int i = 0 ; i < MAX_SCENE; i++)
{
keyState[i] = node.loadState(i);
#ifdef DEBUG
Serial.print(" "); Serial.print(keyState[i]);
#endif
}
//Setup done !
blinkLedFastly(3);
//Print setup debug
#ifdef DEBUG
int duration = millis() - startTime;
Serial.println("");
Serial.print("[Setup duration: "); Serial.print(duration, DEC); Serial.println(" ms]");
#endif
}
/**************************************************************************************/
/* Main loop */
/**************************************************************************************/
void loop()
{
//Sleep, node wakes up on key interrupt
byte interuptionType = node.sleep(SDA_PIN - 2, FALLING);
//Verify if a key was pressed
if (interuptionType == 0)
{
return;
}
//Get time (for a complete loop)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Get first digit
byte key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
int digicode = key * 1000;
//Check if key corresponds to a scene touch or a code
if (key > 9)
{
//Update scene state
node.saveState(key - 10, (boolean)node.loadState(key - 10)?false:true);
//Report data to the gateway
long voltage = getVoltage();
node.send(node.loadState(key - 10)?msgSceneOn.set(key - 10):msgSceneOff.set(key - 10));
node.send(msgVolt.set(voltage / 1000.0, 3));
int batteryPcnt = round((voltage - BATTERY_ZERO) * 100.0 / (BATTERY_FULL - BATTERY_ZERO));
if (batteryPcnt > 100) {batteryPcnt = 100;}
node.sendBatteryLevel(batteryPcnt);
//Print debug
#ifdef DEBUG
Serial.print("Scene ");
Serial.print(key - 10);
Serial.print(": ");
Serial.print((boolean)node.loadState(key - 10)?" 1 ":" 0 ");
Serial.print(" ");
Serial.print(voltage / 1000.0);
Serial.print(" v");
int duration = millis() - startTime;
Serial.print(" ");
Serial.print("["); Serial.print(duration, DEC); Serial.println(" ms]");
Serial.flush();
#endif
}
else
{
//Wait for next key pressed or WAITING_TIME without action
interuptionType = node.sleep(SDA_PIN - 2, FALLING, WAITING_TIME);
//Verify if a key was pressed
if (interuptionType == 0)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Get second digit
key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0 || key > 9)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
digicode = digicode + key * 100;
//Wait for next key pressed or WAITING_TIME without action
interuptionType = node.sleep(SDA_PIN - 2, FALLING, WAITING_TIME);
//Verify if a key was pressed
if (interuptionType == 0)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Get third digit
key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0 || key > 9)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
digicode = digicode + key * 10;
//Wait for next key pressed or WAITING_TIME without action
interuptionType = node.sleep(SDA_PIN - 2, FALLING, WAITING_TIME);
//Verify if a key was pressed
if (interuptionType == 0)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Get fourth digit
key = getKeyPressed();
tone(BUZZER_PIN, BUZZ_FREQ_KEY, BUZZ_TIME_KEY);
//Check validity
if (key == 0 || key > 9)
{
blinkLedFastlyAndBuzz(3);
return;
}
//Inform user that key is valid
blinkLedFastly(1);
digicode = digicode + key;
//Report data to the gateway
long voltage = getVoltage();
node.send(msgCode.set(digicode));
node.send(msgVolt.set(voltage / 1000.0, 3));
int batteryPcnt = round((voltage - BATTERY_ZERO) * 100.0 / (BATTERY_FULL - BATTERY_ZERO));
if (batteryPcnt > 100) {batteryPcnt = 100;}
node.sendBatteryLevel(batteryPcnt);
//Print debug
#ifdef DEBUG
Serial.print("Complete code: ");
Serial.print(digicode);
Serial.print(" ");
Serial.print(voltage / 1000.0);
Serial.print(" v");
int duration = millis() - startTime;
Serial.print(" ");
Serial.print("["); Serial.print(duration, DEC); Serial.println(" ms]");
Serial.flush();
#endif
//Play final song instead of sleeping
playFinalSong();
}
//node.sleep(1000);
}
/**************************************************************************************/
/* Allows to play the final song. */
/**************************************************************************************/
void playFinalSong()
{
//Declare local variables
int i, duration;
// step through the song arrays
for (i = 0; i < songLength; i++)
{
// length of note/rest in ms
duration = beats[i] * tempo;
// is this a rest?
if (notes[i] == ' ')
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
tone(BUZZER_PIN, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
}
/**************************************************************************************/
/* takes a note character (a-g), and returns the corresponding frequency in Hz */
/* for the tone() function. */
/**************************************************************************************/
int frequency(char note)
{
//Declare local variables
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
/**************************************************************************************/
/* Allows to get the pressed key. */
/**************************************************************************************/
byte getKeyPressed()
{
//Get first bit
digitalWrite(SCL_PIN, LOW);
delayMicroseconds(100);
digitalWrite(SCL_PIN, HIGH);
//Read all keys
for(int i = 0; i < KEYBOARD_SIZE; i++)
{
keyState[i] = (digitalRead(SDA_PIN) == LOW);
digitalWrite(SCL_PIN, LOW);
delayMicroseconds(100);
digitalWrite(SCL_PIN, HIGH);
delayMicroseconds(100);
}
//Read keyboard table to find the pressed key
byte keyPressed = -1;
for(int i = 0 ; i < KEYBOARD_SIZE; i++)
{
if (keyState[i])
{
keyPressed = i;
}
}
//Return the result
return (keyPressed + 1);
}
/**************************************************************************************/
/* Allows to fastly blink the LED. */
/**************************************************************************************/
void blinkLedFastly(byte loop)
{
byte delayOn = 150;
byte delayOff = 150;
for (int i = 0; i < loop; i++)
{
blinkLed(LED_PIN, delayOn);
delay(delayOff);
}
}
/**************************************************************************************/
/* Allows to fastly blink the LED and to play a sound on the buzzer. */
/**************************************************************************************/
void blinkLedFastlyAndBuzz(byte loop)
{
byte delayOn = 150;
byte delayOff = 150;
for (int i = 0; i < loop; i++)
{
blinkLed(LED_PIN, delayOn);
tone(BUZZER_PIN, BUZZ_FREQ_ERROR, delayOff);
delay(delayOff);
}
}
/**************************************************************************************/
/* Allows to blink a LED. */
/**************************************************************************************/
void blinkLed(byte pinToBlink, int delayInMs)
{
digitalWrite(pinToBlink,HIGH);
delay(delayInMs);
digitalWrite(pinToBlink,LOW);
}
/**************************************************************************************/
/* Allows to get the real Vcc (return value in mV). */
/* http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ */
/**************************************************************************************/
long getVoltage()
{
ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
delay(50); // Let mux settle a little to get a more stable A/D conversion
//Start a conversion
ADCSRA |= _BV( ADSC );
//Wait for it to complete
while (bit_is_set(ADCSRA, ADSC));
//Compute and return the value
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
Not very easy to explain the problem !
David.
Very good ! So small !
I really need to learn how solder SMD part ! More and more design are using small components.
I am more a jujitsuka but not JJB. But due to the small number of hours in a day (only 24 ) I spent most of sport time to run ! I started jujitsu too late (34 years), very difficult with people which started at 5 !!
Hello. It's what I done, no problem.
I will try to clarify what happens:
David.
Unfortunately it's not me on the avatar but it's my preferred judo technic
Hello,
I am working on a keyboard project allowing to enter a code on 4 keys and as the keyboard as 16 touch, I will use key 10 to 16 as a scene controller.
Everything is working correctly except a small thing. I use this sleep function:
interuptionType = node.sleep(SDA_PIN - 2, FALLING, WAITING_TIME);
The idea is to sleep between two keys of the code:
If the second key is greater than 9 I cancelled the code but the sleep of 2 seconds is still actif !
How I can disable the sleep timer ? Is there a specific function ?
Thanks for your help.
David.
Very beautiful design and, as usual, so interesting !
@AWI
Led removed and power consumption really low but not stable ! I measure with my uCurrent a consumption between 1.8uA and 2.5. And sometimes some pikes close to 9 or 15 uA. It seems to be every 8 seconds (link to arduino wake-up ?).
Not a real issue as it's very low.
I will now work on the package
David.
Hello @AWI !
For an unknown reason I missed your reply. Thanks. I wait the week-end to test it.
I spent several hours on the net to find a "soft" solution. I think that it's very strange to add a led on this circuit. As the component's consumption is very low, a solution to disable the led in a proper way allows to use it in battery solution.
David.
@the-cosmic-gate
Hello,
Motion nodes are still working correctly. The oldest node started the 4 of February is still at 3,29v. My other nodes (temp / humidity / light) are also working correctly but I will replace DHT22 for another sensor.
David.
Very good idea ! Especially for the DIY kit !!
Very interesting !!
Not for me because SMD components are too small but the schematic is very useful !
How did you do the 3D view ?
On the coincell you are using 4 capacitors with different values, it's for filtering ? Is it useful with battery ?
Thanks for sharing !
David.
@AWI Hello, did you set something (jumper) to reduce the power consumption ? Did you removed the LED on the keyboard ?
I measured the power consumption at something close to 1.3mA. Really far to your 2.2uA !
David.
Very good job ! I will be happy when I will able to do 10% of your work
@engy Hello. It's why I am using RFM69 as there is no fake
Nice tutorial.
I already bought and received your eval board. I ordered it more for ATSHA204A but OTA is interesting.
Is OTA using eeprom possible with RFM69 ? I imagine that it's the case.
Thanks.
David.
@GertSanders
Yes, very impressive ! But finally not a lot of difference with the same node using interruption and sleep.
The auto-discharge of AA batteries is really important compare to node consumption.
David.
@GertSanders Hello. I tried the full deep sleep and it's really amazing !
I used your 8MHz with internal crystal and B0 led, BOD disabled. RFM69 for radio and just a switch via a 10MOhm resistor.
With the switch open: 280nA
With the switch close: 580nA
Measures done with a uCurrent Gold.
So I am sure now that I found the good bootloader for my application. It's simple to install and I can change easily the BOD !
Thanks !
@Samuel235 No issue to with the FTDI.
I just found a small problem due to the Arduino IDE. As I often change board (from UNO to different bootloader) I discover that the menu is sometime not coherent with the real setting. So I found that selecting two or three times board parameters (including port) is the solution to avoid communication issue.
@GertSanders Thanks for explanation. It confirms what I imagine !
I am using a 10MOhm resistor but not the full deep sleep as I want to periodically send the battery.
I will try it because 1-2 uA is very low
I am using RFM69. No fake as it's working perfectly at other frequency.
@GertSanders
I tried three different configurations:
I measured power consumption in sleep mode and found that it's very close to 4uA with all configurations. So why using the 1 MHz ? Do you have better consumption when using 1 MHz ?
The best configuration for me will be the 8 MHz internal as it avoid to used the crystal.
David.
@Anticimex @tomkxy
Perhaps the arduino and RFM69 can't run at 1MHz. I have capacitors and I tried with two different power sources. Without signing it's better but still a lot of st=fail. No matter, it was just for testing purpose
Hello, as I got some issue with the 1MHz bootloader, I tried without the signing feature and I still got st=fail.
One question for you, perhaps stupid but I need to be sure
Do you need to set the same serial speed on both node and gateway ?
David.
@Anticimex Hello. Everything is working at 16 or 8MHz so I am pretty sure it's not an issue with the material.
I will burn the bootloader again and create a new post if it's still not correct. I will also disable signing feature to be sure there is no impact.
David.
Hello.
I am using soft signing.
find parent
send: 255-255-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,st=bc:
read: 255-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
sensor started, id=255, parent=255, distance=255
find parent
send: 255-255-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,st=bc:
read: 0-0-255 s=255,c=3,t=8,pt=1,l=1,sg=0:0
parent=0, d=1
read: 0-0-255 s=255,c=3,t=8,pt=1,l=1,sg=0:0
req id
send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok:
read: 0-0-255 s=255,c=3,t=4,pt=0,l=1,sg=0:9
send: 9-9-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=fail:1
read and drop: 9-9-0 s=255,c=3,t=15,pt=2,l=2,sg=0:1
read: 0-0-9 s=255,c=3,t=15,pt=2,l=2,sg=0:1
send: 9-9-0-0 s=255,c=3,t=16,pt=0,l=0,sg=0,st=fail:
nonce tr err
send: 9-9-0-0 s=255,c=3,t=16,pt=0,l=0,sg=0,st=fail:
nonce tr err
read and drop: 9-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0:0
read: 0-0-9 s=255,c=3,t=17,pt=6,l=25,sg=0:0129D04B64916F5E805EFDF704C34F56B47E547FDDE93805BE
id=9
send: 9-9-0-0 s=0,c=0,t=0,pt=0,l=0,sg=0,st=ok:
send: 9-9-0-0 s=1,c=0,t=30,pt=0,l=0,sg=0,st=fail:
[Setup duration: 9928 ms]
send: 9-9-0-0 s=0,c=3,t=16,pt=0,l=0,sg=0,st=fail:
nonce tr err
send: 9-9-0-0 s=1,c=3,t=16,pt=0,l=0,sg=0,st=fail:
nonce tr err
send: 9-9-0-0 s=255,c=3,t=16,pt=0,l=0,sg=0,st=fail:
nonce tr err
Value is 1 Cycle is 1 3.39 v [753 ms]
Thanks,
David.
Hello,
I currently testing various bootloader to measure impact on CPU speed on the power consumption.
I got a lot of nonce error
when using 1 MHz configuration.
Is signing feature possible at 1 MHz ?
Thanks.
David.
Hello,
I tested two bootloaders today:
But I am pretty sure you already tested them.
I got some issue with 1MHz bootloader but probably because I am using signing feature.
I will check the forum to be sure.
David.
So it confirms that your bootloader was correctly burnt in the atmega as the first time I failed to upload a sketch the LED flash three time again and again !
Thanks !
David.
@GertSanders One more question on the LED connected on B0.
When you wrote that the LED lit on arduino reset, it's when the reset button is pushed ? when the arduino start ? or both ?
David.
I burn the bootloader one more time and now I can upload without any change on configuration.
Perhaps I made a mistake the first time
I will continue my test.
David.
@GertSanders
No doubt that I using your file as I got the same problem as @alexsh1 with the arduino pin file.
Look at my board choice:
I find this method very good for deployment but only when it's working
I check the bootloader with Nick Gammon sketch and it seems good.
David.
@GertSanders Hello ! I am trying to upload a sketch without success for the moment.
Burning of the bootloader was done correctly (led on pin 8 lit three times) but impossible to load a sketch.
For easy deployment I choose to create a new folder to include your bootloaders:
But it seems that something is missing as upload is not possible:
Le croquis utilise 1ย 030 octets (3%) de l'espace de stockage de programmes. Le maximum est de 32ย 256 octets.
Les variables globales utilisent 9 octets (0%) de mรฉmoire dynamique, ce qui laisse 2ย 039 octets pour les variables locales. Le maximum est de 2ย 048 octets.
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -carduino -P/dev/cu.usbmodem1411 -b38400 -D -Uflash:w:/var/folders/2y/_n7gfzjx5_sgmtyh9t_lp0wc0000gn/T/build8919458311701309495.tmp/Blink.cpp.hex:i
avrdude: Version 6.0.1, compiled on Apr 14 2015 at 16:30:25
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2009 Joerg Wunsch
System wide configuration file is "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf"
User configuration file is "/Users/carlierd/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : /dev/cu.usbmodem1411
Using Programmer : arduino
Overriding Baud Rate : 38400
avrdude: ser_open(): can't open device "/dev/cu.usbmodem1411": No such file or directory
ioctl("TIOCMGET"): Inappropriate ioctl for device
avrdude done. Thank you.
Problรจme de tรฉlรฉversement vers la carte. Voir http://www.arduino.cc/en/Guide/Troubleshooting#upload pour suggestions.
Any suggestion ?
David.
@GertSanders I tried today and now I can download sketch correctly. One strange thing is that I have to open the serial monitor to have the arduino working after the download ...
In // I think I have a problem with a bad RFM69 ...
@AWI already done without problem ! I think I got problem with ATMEGA in DIP format ...
@AWI no surprise. It's what I've done except for the capacitor on AREF pin but I'm pretty sure there is no link.
Thanks.
Perhaps a problem due to my Mac. I don't know how I can check the connection and isolate the problem.
David.
@AWI : may you post the schematic diagram of your reset circuit ?
I got so many connection issue with my FTDI. I of course check internet but nothing is working correctly.
Thanks !
David.
Hello,
In order to continue node development I use two MySensors network. One operational and one for dev/debugging.
My networks are using RFM69 so the difference for both network is only on the variable RFM69_NETWORKID
.
I am not sure (and don't want to test again) but each time my two gateways are switched one (on two computers) the operational network stopped working.
Is someone doing same thing ? Do you have issue ?
David.
@Dylano : My nodes are really new so difficult to say exactly but for the moment the oldest node was started one month ago (04/02) and battery still at 3.32v.
David.
Very nice !!
I just started learning Eagles to modify your board for RFM69 compatibility. The road will be long
Could be good also to add ATSHA204A but in a second time !
David.
Hello,
I will try some bootloader so I will be able to give you some fuse settings I have tested to.
One more question, what B0 means ? I understood it's the pin for the LED ? Why B ? It's not D0 for digital 0 ?
David.
Hello,
I will start some test on bootloader impact on battery so the timing is perfect !
One very useful thing to add in your project could be fuse settings and IDE board files ?
David.
Yes it is the resistor.
I don't why it's easier but it is Probably because heat propagation is better.
The motion can be adapt to run on 3v easily (connect the Vcc to the H pad if available. You can find information on the forum). The DHT could also run on 3v. I have several nodes like that. No problem.
Can't reply for the step-up because I don't use it.
David.
My advice: leave the LED and remove the resistor ! Really easier !
Hello,
I usually desolder the regulator as it's not too difficult but now I did not desolder the LED. I prefer desolder the resistor before the LED. It's really easier !
At the beginning I was used to desolder the LED and damaged one card ...
David.
Hello,
9v means that you are using a voltage regulator which probably consumes a lot of power.
Why not using AA or AAA battery directly connected to Vcc ?
David
It's strange ! Can you post a picture of your sensor and gateway ?
Have you try with a second nano ? It's not normal to have nothing on the serial monitor when using USB.
One thing you can do it's to connect a LED to any digital pin and switch it on at the end of the init() function ??
David.
@fredmet8 As mentioned by @minscof you have to check your network configuration. Verify it's ok and then we can look for something else
I never configured IP in the MySensors plugin !
David.
Hello,
You have message on the serial monitor ?
David.
It depends on the activity close to the sensor so not possible to say but some people reach several month without problem.
In my house it's very fresh so I have not a lot of feedback to give.
David.
The error is because I enable the signing function !
Remove the following line:
MySigningAtsha204Soft signer;
And replace
MySensor node(transport, hw, signer);
by
MySensor node(transport, hw);
I don't know who had the best sketch.
I measure internal battery status using the 1.1 reference.
David.
Hello,
This is the code I use but it's not perfect especially with the battery report.
The principle is as follow when the sensor start:
This is to avoid having many report (and power consumption) to the gateway.
The code (not perfect ! I have to rework on it but it's ok for the moment):
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*/
/**************************************************************************************/
/* Motion sensor. */
/* */
/* Version : 1.1.5 */
/* Date : 30/01/2016 */
/* Modified by : David Carlier */
/**************************************************************************************/
/* --------------- */
/* RST | | A5 */
/* RX | | A4 */
/* TX | ARDUINO | A3 */
/* RFM69 (DIO0) --------- D2 | UNO | A2 */
/* Motion --------- D3 | | A1 */
/* Power --------- D4 | ATMEGA 328p | A0 */
/* +3v --------- VCC | | GND --------- GND */
/* GND --------- GND | 8MHz int. | REF */
/* OSC | | VCC --------- +3v */
/* OSC | | D13 --------- RFM69 (SCK) */
/* D5 | | D12 --------- RFM69 (MISO) */
/* D6 | | D11 --------- RFM69 (MOSI) */
/* D7 | | D10 --------- RFM69 (NSS) */
/* D8 | | D9 */
/* --------------- */
/* */
/* +3v = 2*AA */
/* */
/**************************************************************************************/
#include <SPI.h>
#include <MySensor.h>
#include <MyTransportRFM69.h>
#include <MySigningAtsha204Soft.h>
//Constants
#define SKETCH_NAME "Motion Sensor"
#define SKETCH_VERSION "1.1.5"
#define CHILD_ID 0
#define CHILD_ID_VOLTAGE 1
#define DIGITAL_INPUT_SENSOR 3
#define BATTERY_REPORT_CYCLE 4 //Report each hour (4 * 15')
//Misc. variables
uint8_t switchState = 2;
unsigned long SLEEP_TIME = 820000; // Sleep time between reads (in milliseconds) (close to 15')
unsigned long SLEEP_TIME2 = 275000; // Sleep time after int. (in milliseconds) (close to 5')
uint8_t cycleInProgress = 0;
uint8_t firstReportDone = 0;
uint8_t firstReportWithZeroDone = 0;
//Construct MySensors library
MySigningAtsha204Soft signer;
MyHwATMega328 hw;
MyTransportRFM69 transport;
MySensor node(transport, hw, signer);
MyMessage msgSwitch(CHILD_ID, V_TRIPPED);
MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE);
/**************************************************************************************/
/* Initialization */
/**************************************************************************************/
void setup()
{
//Get time (for setup duration)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Start MySensors and send the Sketch Version Information to the Gateway
node.begin();
node.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
//Setup the button
pinMode(DIGITAL_INPUT_SENSOR, INPUT);
//Register all sensors
node.present(CHILD_ID, S_MOTION);
node.present(CHILD_ID_VOLTAGE, S_MULTIMETER);
//Print setup debug
#ifdef DEBUG
int duration = millis() - startTime;
Serial.print("[Setup duration: "); Serial.print(duration, DEC); Serial.println(" ms]");
#endif
}
/**************************************************************************************/
/* Main loop */
/**************************************************************************************/
void loop()
{
//Get time (for a complete loop)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Read digital motion value
uint8_t value = digitalRead(DIGITAL_INPUT_SENSOR);
//Get voltage for battery capacity report (add 0.1v to the read value)
float realVoltage = (getVoltage() / 100.0) + 0.1;
int batteryPcnt = realVoltage * 100 / 3.0;
if (batteryPcnt > 100) {batteryPcnt = 100;}
//Check is state change
if (value != switchState)
{
//Report result if not first loop
if (firstReportDone != 0)
{
node.send(msgSwitch.set(value==HIGH ? 1 : 0));
}
//Store result
switchState = value;
}
//Report data to the gateway each hour
if (cycleInProgress >= BATTERY_REPORT_CYCLE || firstReportDone == 0)
{
node.send(msgSwitch.set(value==HIGH ? 1 : 0));
node.send(msgVolt.set(realVoltage, 2));
node.sendBatteryLevel(batteryPcnt);
cycleInProgress = 0;
firstReportDone = 1;
}
//Change cycle number
cycleInProgress++;
//Print debug
#ifdef DEBUG
Serial.print("Value is ");
Serial.print(value, 1);
Serial.print(" ");
Serial.print("Cycle is ");
Serial.print(cycleInProgress, 1);
Serial.print(" ");
Serial.print(realVoltage);
Serial.print(" v");
int duration = millis() - startTime;
Serial.print(" ");
Serial.print("["); Serial.print(duration, DEC); Serial.println(" ms]");
Serial.flush();
#endif
//Check if the motion was triggered and if it's not the first report
if (switchState == 0 && firstReportWithZeroDone == 1)
{
//Sleep 5' without interrupt
node.sleep(SLEEP_TIME2);
}
//Check if it's the first loop
if (switchState == 0 && firstReportWithZeroDone == 0)
{
firstReportWithZeroDone = 1;
}
//Sleep until something happens with the sensor
node.sleep(DIGITAL_INPUT_SENSOR-2, CHANGE, SLEEP_TIME);
}
/**************************************************************************************/
/* Allows to get the real Vcc (return value * 100). */
/**************************************************************************************/
int getVoltage()
{
const long InternalReferenceVoltage = 1056L;
ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
delay(50); // Let mux settle a little to get a more stable A/D conversion
//Start a conversion
ADCSRA |= _BV( ADSC );
//Wait for it to complete
while (((ADCSRA & (1<<ADSC)) != 0));
//Scale the value
int result = (((InternalReferenceVoltage * 1023L) / ADC) + 5L) / 10L;
return result;
}
On one sensor I tweak the PIR (remove regulator and diode) but on a second one, I just plug the 3 v to the H pin (see other topic on the forum).
Working correctly for the moment (one month) but it depends on the presence in the house.
David.
@nagelc Dialog with MYSControler is correct so there is probably no issue with 5v.
@spacejay I used a 1/2 length antenna (17,23 cm) without any problem on many sensors. I imagine it's a copper wire not flexible ?
Hello,
What is your sketch ? It seems that your sensors have no presentation !
Example in the setup()
function:
node.present(CHILD_ID, S_MOTION);
node.present(CHILD_ID_VOLTAGE, S_MULTIMETER);
It's mandatory for jeedom if I'm right.
David.
What is the frequency of the RFM69 ?
I used 4,7uF for the capacitor but I am pretty sure that more is not a problem.
David.
Yes you need a gateway and a controller like MYSController.
Your gateway is also on nano ? or uno ? You have level adapter ? voltage converter ?
Hello,
Are you using HW version of RFM69 ?
David
Hello,
As mentioned on the MySensors page, NSS, MOSI and SCK are not 5v tolerant so you need to connect them to a level converter. Don't connect MISO to the level converter, it's not necessary and not working for me !
Hope it helps !
David.
@ar91 said:
@carlierd Are you running your Atmega at 1Mhz, Because I tried to use your sketch and i am getting "Checksum Error".
No, I am working at 8 MHz (internal). Perhaps your DHT is out of order ? You try with another one ?
David.
@GertSanders I will try to modify your design to replace the NRF with a RFM69. Thanks.
Very good job !!
I saw that you provide a bootloader. What specificities ?
Other (very selfish) question: next version of your not will be compatible with RFM69HW ??
David.
@chrille I also use simple sketch to validate hardware without MySensors. It's easier to be sure everything is working correctly. Another reason is that with MySensors there is no RSSI. I found this very useful to verify antenna connection especially with home-made antenna
All my nodes work correctly when powering via 2 AA or via USB adaptor (FTDI).
I only got issue on the gateway using a uno and a voltage converter.
On my nodes (arduino pro mini on 3.3v / 8MHz) I used caps of 100uF on the power supply and 4.7uF for the RFM.
On the gateway I only add the 4.7uF for the RFM.
David.
Hello,
Is there a 3v output on the nano ? I got some issue with my uno and a voltage converter. To solve the issue I removed the voltage converter and used the 3v output on the UNO.
Hope it helps !
David.
@ar91 Please find the code I used in 3 different nodes. The good thing with the playground lib is that there is error message if dialog with DHT22 failed.
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*/
/**************************************************************************************/
/* Temperature, humidity and luminosity measurements. */
/* */
/* Version : 1.1.6 */
/* Date : 10/01/2016 */
/* Modified by : David Carlier */
/**************************************************************************************/
/* --------------- */
/* RST | | A5 */
/* RX | | A4 */
/* TX | ARDUINO | A3 */
/* RFM69 (DIO0) --------- D2 | UNO | A2 */
/* DHT22 --------- D3 | | A1 */
/* Power --------- D4 | ATMEGA 328p | A0 --------- Light dep. resistor */
/* +3v --------- VCC | | GND --------- GND */
/* GND --------- GND | 8MHz int. | REF */
/* OSC | | VCC --------- +3v */
/* OSC | | D13 --------- RFM69 (SCK) */
/* D5 | | D12 --------- RFM69 (MISO) */
/* D6 | | D11 --------- RFM69 (MOSI) */
/* D7 | | D10 --------- RFM69 (NSS) */
/* D8 | | D9 */
/* --------------- */
/* */
/* Power = Vcc for LDR. */
/* +3v = 2*AA */
/* */
/**************************************************************************************/
#include <SPI.h>
#include <MySensor.h>
#include <dht.h>
#include <MyTransportRFM69.h>
#include <MySigningAtsha204Soft.h>
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_LIGHT 2
#define CHILD_ID_VOLTAGE 3
#define LIGHT_SENSOR_ANALOG_PIN 0
#define HUMIDITY_SENSOR_DIGITAL_PIN 3
#define POWER_PIN 4
//unsigned long SLEEP_TIME = 850000; // Sleep time between reads (in milliseconds) (close to 15')
unsigned long SLEEP_TIME = 275000; // Sleep time between reads (in milliseconds) (close to 5')
//Construct MySensors library
MySigningAtsha204Soft signer;
MyHwATMega328 hw;
MyTransportRFM69 transport;
MySensor gw(transport, hw, signer);
dht DHT;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgLum(CHILD_ID_LIGHT, V_LEVEL);
MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE);
/**************************************************************************************/
/* Initialization */
/**************************************************************************************/
void setup()
{
//Get time (for setup duration)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Start MySensors
gw.begin();
//Send the Sketch Version Information to the Gateway
gw.sendSketchInfo("GHAS sensor", "1.1.5");
//Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
gw.present(CHILD_ID_VOLTAGE, S_MULTIMETER);
//Delay for DHT22
delay(1500);
//Print setup debug
#ifdef DEBUG
int duration = millis() - startTime;
Serial.print("[Setup duration: "); Serial.print(duration, DEC); Serial.println(" ms]");
#endif
}
/**************************************************************************************/
/* Main loop */
/**************************************************************************************/
void loop()
{
//Get time (for a complete loop)
#ifdef DEBUG
unsigned long startTime = millis();
#endif
//Power on
powerOnPeripherals();
//Get DHT22 data
int dht22Result = DHT.read22(HUMIDITY_SENSOR_DIGITAL_PIN);
switch (dht22Result)
{
case DHTLIB_OK:
//Serial.println("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
#ifdef DEBUG
Serial.println("Checksum error,\t");
#endif
break;
case DHTLIB_ERROR_TIMEOUT:
#ifdef DEBUG
Serial.println("Time out error,\t");
#endif
break;
case DHTLIB_ERROR_CONNECT:
#ifdef DEBUG
Serial.println("Connect error,\t");
#endif
break;
case DHTLIB_ERROR_ACK_L:
#ifdef DEBUG
Serial.println("Ack Low error,\t");
#endif
break;
case DHTLIB_ERROR_ACK_H:
#ifdef DEBUG
Serial.println("Ack High error,\t");
#endif
break;
default:
#ifdef DEBUG
Serial.println("Unknown error,\t");
#endif
break;
}
//Get temperature and humidity
float temperature = 0;
float humidity = 0;
if (dht22Result == DHTLIB_OK)
{
temperature = DHT.temperature;
humidity = DHT.humidity;
}
//Get power before luminosity to use real voltage
float realVoltage = getVoltage() / 100.0;
int batteryPcnt = realVoltage * 100 / 3.0;
if (batteryPcnt > 100) {batteryPcnt = 100;}
int lux = computeIlluminance(realVoltage);
//Power off
powerOffPeripherals();
//Send data to gateway
gw.send(msgHum.set(humidity, 1));
gw.send(msgTemp.set(temperature, 1));
gw.send(msgLum.set(lux));
gw.send(msgVolt.set(realVoltage, 2));
gw.sendBatteryLevel(batteryPcnt);
//Print debug
#ifdef DEBUG
Serial.print(temperature, 1);
Serial.print(" degC");
Serial.print(" ");
Serial.print(humidity, 1);
Serial.print(" %");
Serial.print(" ");
Serial.print(lux);
Serial.print(" lx");
Serial.print(" ");
Serial.print(realVoltage);
Serial.print(" v");
int duration = millis() - startTime;
Serial.print(" ");
Serial.print("["); Serial.print(duration, DEC); Serial.println(" ms]");
Serial.flush();
#endif
//Sleep
gw.sleep(SLEEP_TIME);
}
/**************************************************************************************/
/* Allows to compute illuminance (in LUX) from LIGHT_SENSOR_ANALOG_PIN. */
/**************************************************************************************/
int computeIlluminance(float realVoltage)
{
//Get luminosity
int luminosity = analogRead(LIGHT_SENSOR_ANALOG_PIN);
//Calculating the voltage in the input of the ADC
double voltage = realVoltage * ((double)luminosity / 1024.0);
//Calculating the resistance of the photoresistor in the voltage divider
double resistance = (10.0 * realVoltage) / voltage - 10.0;
//Calculating the intensity of light in lux and return it
int illuminance = 255.84 * pow(resistance, -10/9);
return illuminance;
}
/**************************************************************************************/
/* Allows to get the real Vcc (return value * 100). */
/**************************************************************************************/
int getVoltage()
{
const long InternalReferenceVoltage = 1056L;
ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
delay(50); // Let mux settle a little to get a more stable A/D conversion
//Start a conversion
ADCSRA |= _BV( ADSC );
//Wait for it to complete
while (((ADCSRA & (1<<ADSC)) != 0));
//Scale the value
int result = (((InternalReferenceVoltage * 1023L) / ADC) + 5L) / 10L;
return result;
}
/**************************************************************************************/
/* Allows to power ON peripherals. */
/**************************************************************************************/
void powerOnPeripherals()
{
//Power-up
pinMode (POWER_PIN, OUTPUT);
digitalWrite (POWER_PIN, HIGH);
delay(1);
}
/**************************************************************************************/
/* Allows to power OFF peripherals. */
/**************************************************************************************/
void powerOffPeripherals()
{
//Power off
digitalWrite (HUMIDITY_SENSOR_DIGITAL_PIN, LOW);
digitalWrite (POWER_PIN, LOW);
pinMode (HUMIDITY_SENSOR_DIGITAL_PIN, INPUT);
pinMode (POWER_PIN, INPUT);
}
Hope it helps !
David.
@ar91 Hello. I am using the arduino playground lib. No issue with it ! I can post a sketch if necessary.
David.