Radio’s have arrived:
How to Mysensors.
Radio’s have arrived:
looks really nice and compact, good work soldering soldier
did you use the same code as here on your arduino?? could you say something about your battery lifetime?
I’m using the same code like motionsensor to get the soil 0/1 of my plant every hour.
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define CHILD_ID 4
#define BUTTON_PIN 8 // Arduino Digital I/O pin for button/reed switch
MySensor gw;
Bounce debouncer = Bounce();
int oldValue=-1;
// Change to V_LIGHT if you use S_LIGHT in presentation below
MyMessage msg(CHILD_ID,V_TRIPPED);
void setup()
{
gw.begin();
// Setup the button
pinMode(BUTTON_PIN,INPUT);
pinMode(6, OUTPUT); // Power Pin for A/D Module
// Activate internal pull-up
digitalWrite(BUTTON_PIN,LOW);
// After setting up the button, setup debouncer
debouncer.attach(BUTTON_PIN);
debouncer.interval(5);
// Register binary input sensor to gw (they will be created as child devices)
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
gw.present(CHILD_ID, S_DOOR);
}
// Check if digital input has changed and send in new value
void loop()
{
digitalWrite(6, HIGH); // Power on YL-38 A/D Module
delay(2000);
debouncer.update();
// Get the update value
int value = debouncer.read();
if (value != oldValue) {
// Send in the new value
gw.send(msg.set(value==LOW ? 1 : 0));
oldValue = value;
}
delay(2000);
digitalWrite(6, LOW); // Power off YL-38 A/D Module
delay(3600000); // wait a hour, then reloop
}
If i dont care about the power of the soil-sensor-module the contacts corrode in a few days.
Maybe someone have a battery-friendlier solution… with delay(3600000) maybe its not the best solution for powering with batteries.
@xCite86 theoretically they should run for around 450 days. I’m using my own sketch.
It measures the battery life (uses readVcc).
Resends if a message fails, thanks to @n3ro.
Does also send a false signal.
#include <MySensor.h>
#include <SPI.h>
#include <readVcc.h>
// ********** CONFIG **********************************
#define NODE_ID AUTO // ID of node
#define CHILD_ID 1 // ID of sensor
#define PIR_PIN 3 // Pin connected to the PIR
#define MIN_V 2000 // empty voltage (0%)
#define MAX_V 3200 // full voltage (100%)
// ****************************************************
MyMessage msg(CHILD_ID, V_TRIPPED);
MySensor node;
int oldBatteryPcnt;
int sentValue;
int forceSend = 0;
void setup()
{
node.begin(NULL, NODE_ID, false);
node.sendSketchInfo("PIR Sensor", "1.2");
node.present(CHILD_ID, S_MOTION);
pinMode(PIR_PIN, INPUT);
digitalWrite(PIR_PIN, HIGH);
}
void loop()
{
// Get PIR
int value = digitalRead(PIR_PIN); // Get value of PIR
if (value != sentValue) { // If status of PIR has changed
resend(msg.set(value), 5); // Send PIR status to gateway
sentValue = value;
}
// Send batterylevel
sendBattery();
// Sleep until something happens with the sensor
node.sleep(PIR_PIN-2, CHANGE);
}
// FUNCTIONS
void sendBattery() // Send battery percentage to GW
{
forceSend++;
int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100); // Get VCC and convert to percentage
if (batteryPcnt != oldBatteryPcnt || forceSend >= 20) { // If battery percentage has changed
node.sendBatteryLevel(batteryPcnt); // Send battery percentage to gateway
oldBatteryPcnt = batteryPcnt;
forceSend = 0;
}
}
void resend(MyMessage &msg, int repeats) // Resend messages if not received by GW
{
int repeat = 0;
int repeatDelay = 0;
boolean ack = false;
while ((ack == false) and (repeat < repeats)) {
if (node.send(msg)) {
ack = true;
} else {
ack = false;
repeatDelay += 100;
}
repeat++;
delay(repeatDelay);
}
}
For your sketch, replace delay(3600000)) with sleep(3600000)
great sketch and the battery-lifetime was great
if i woult add readVcc in my sketch, did i need resistors too?? i’m in a voltage-range between 4 and 6 Volt.
replace delay with sleep(3600000); gets an error on compiling
sketch_mar16a.ino: In function 'void loop()':
sketch_mar16a:81: error: 'sleep' was not declared in this scope
'sleep' was not declared in this scope
@xCite86 readVcc only works if the batteries are directly connected to the vcc of the arduino/uC. So no regulator. If you want it your way you need resistors yes, and readVcc is not needed for that.
for good battery life you need to remove the powerled, don’t use the regulator and modify the fuses so it can run till around 1.8V.
You’re using an arduino nano, not great for battery life. Arduino Pro Mini 3.3v are the best.
Ah yea you need gw.sleep(3600000);
Hey folks,
i have modified the battery function some weeks ago to prevent sending to often:
void sendBattery() // Measure battery
{
bool force = false;
int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100);
float diffbatteryPcnt = abs(oldBatteryPcnt - batteryPcnt);
if (batteryReportCounter >= BATTERY_REPORT_CYCLE) {
force = true;
batteryReportCounter = 0;
}
if (diffbatteryPcnt > BAT_TRANSMIT_THRESHOLD or force == true) {
DEBUG_PRINT(force ? "F " : "S ");
gw.sendBatteryLevel(batteryPcnt); // Send battery percentage
oldBatteryPcnt = batteryPcnt;
}
DEBUG_PRINT("---------- Battery: ");
DEBUG_PRINTLN(batteryPcnt);
}
pimatic + MySensors + Homeduino + z-way
https://github.com/n3roGit/MySensors_n3ro
@n3ro i modified mine so it sends more often it can take weeks before it sends again.
Why did you sketch send to often?
@sweebee
The battery voltage differ a little bit on every mensure. If the PIR toggles and the voltage is low, my node sends the battery state everytime it wakes up.
pimatic + MySensors + Homeduino + z-way
https://github.com/n3roGit/MySensors_n3ro
found a very good thread for powerconsuming and Arduino Pro Mini modification here
Some people were interested how the auto discovery works in pimatic 0.9: https://youtu.be/5XN2iHnwouo
I also made some changes for the battery sensor. It uses icons now which i think looks much better. The look of the icon depends on the value of the battery.
@sweebee I built the same PIR sensor as you did with the Arduino mini pro, mini NRF with shield and battery.
Is the above sketch of 21 days ago still the latest version?
I’m very interested in the battery control you implemented.
When I have my DHT available I will probably extend this sensor with that and adjust my sketch.
@Petjepet This is my latest:
#include <MySensor.h>
#include <SPI.h>
#include <readVcc.h>
// ********** CONFIG **********************************
#define NODE_ID AUTO // ID of node
#define CHILD_ID 1 // ID of sensor
#define PIR_PIN 3 // Pin connected to the PIR
#define MIN_V 2000 // empty voltage (0%)
#define MAX_V 3200 // full voltage (100%)
// ****************************************************
MyMessage msg(CHILD_ID, V_TRIPPED);
MySensor node;
int oldBatteryPcnt = -1;
int sentValue = -1;
int forceSend = 0;
void setup()
{
node.begin(NULL, NODE_ID, false);
node.sendSketchInfo("PIR Sensor", "1.3");
node.present(CHILD_ID, S_MOTION);
pinMode(PIR_PIN, INPUT);
}
void loop()
{
// Get PIR
sendPir();
// Send batterylevel
sendBattery();
// Sleep until something happens with the sensor
node.sleep(PIR_PIN-2, CHANGE);
}
// FUNCTIONS
void sendPir()
{
int value = digitalRead(PIR_PIN); // Get value of PIR
if (value != sentValue) { // If status of PIR has changed
resend(msg.set(value), 5); // Send PIR status to gateway
sentValue = value;
}
}
void sendBattery() // Send battery percentage to GW
{
forceSend++;
int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100); // Get VCC and convert to percentage
if (batteryPcnt != oldBatteryPcnt || forceSend >= 20) { // If battery percentage has changed
node.sendBatteryLevel(batteryPcnt); // Send battery percentage to gateway
oldBatteryPcnt = batteryPcnt;
forceSend = 0;
}
}
void resend(MyMessage &msg, int repeats) // Resend messages if not received by GW
{
int repeat = 0;
int repeatDelay = 0;
boolean ack = false;
while ((ack == false) and (repeat < repeats)) {
if (node.send(msg)) {
ack = true;
} else {
ack = false;
repeatDelay += 100;
}
repeat++;
delay(repeatDelay);
}
}
@sweebee Thanks!
@sweebee Thanks for sharing!.
Just curious, but what’s the use of all those PIRs (you have 10+ of them, right?)
Do you use it to control lights or heating? as a security system? or is it just for fun?
I would want to use them as a kind of security system.
When adjusted and placed smart it PIRs would function as in a alarm system only detecting actually people in the room.
@lxz mainly for controlling the lights.
2 in the living room that automatically turn the lights on if the light value is Lower than xx%
1 in the kitchen that automatically turns the lights on if the light value is lower than xx%. And if it’s night only 1 light goes on and goes automatically off. You don’t want them to go off when your eating haha.
I have 3 hallways with pirs that turns the lights on and off on movement. Also depends on the light value. And one extra on the stairs so it goes on before I enter the hall room.
2 in the bathroom for lights. And 1 in the garden that turns the pumps of the water fountain on if it’s after sunrise, warmer than 15 degrees and if there is movement. They go auto off after sunset.
Oh and one on the toilet not to forget
i want to use your sketch too for a movement-controlled night-light and switching some devices based by room/time-combination like bathradio in the morning or turning LCD Infodisplays on/off - thanks for sharing @sweebee
i think i could also take your script for soil-measuring of my plants and window/door-contact sensors for heating-control
@sweebee
What does your device definition look like in Pimatic?
What USB programmer do you use for the Mini? I have a FT232RL and issues on getting ik work properly on Windows 10.
How do you check for your node id settings with the mini? Monitoring through the FT232RL with Ardino IDE doesn’t seem to work.
@Petjepet like this:
{
"id": "woonkamer-pir1",
"xPresentLabel": "Gedetecteerd",
"xAbsentLabel": "Wachten",
"name": "PIR woonkamer entree",
"class": "MySensorsPIR",
"nodeid": 1,
"sensorid": 1,
"autoReset": false,
"batterySensor": true
},
I use also a FT232RL and it works “fine” on my mac. The only problem i have is that once I have ejected it from my usb port and plug it back in it won’t work anymore until I reboot.
Easiest way to check the node id is boot the node up when attached to the arduino ide and the serialport should output something like sensor started at node id X.
Or look at the pimatic log to see which id sends.