Night light to dimmer & percent extsens
This commit is contained in:
@@ -53,7 +53,7 @@ void loop() {
|
|||||||
send(msgMillis.set(cRun));
|
send(msgMillis.set(cRun));
|
||||||
sensorValue = analogRead(BATTERY_SENSE_PIN);
|
sensorValue = analogRead(BATTERY_SENSE_PIN);
|
||||||
v = sensorValue * 0.004659498;
|
v = sensorValue * 0.004659498;
|
||||||
batteryPcnt = (v * 100) / 4.2;
|
batteryPcnt = (v-3 * 100) / 1.2;
|
||||||
sendBatteryLevel(batteryPcnt);
|
sendBatteryLevel(batteryPcnt);
|
||||||
send(msgVolts.set(v, 2));
|
send(msgVolts.set(v, 2));
|
||||||
//}
|
//}
|
||||||
|
|||||||
@@ -1,62 +1,21 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
/*
|
|
||||||
* 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-2019 Sensnology AB
|
|
||||||
* Full contributor list: https://github.com/mysensors/MySensors/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.
|
|
||||||
*
|
|
||||||
*******************************
|
|
||||||
*
|
|
||||||
* REVISION HISTORY
|
|
||||||
* Version 1.0 - Henrik Ekblad
|
|
||||||
*
|
|
||||||
* DESCRIPTION
|
|
||||||
* Example sketch showing how to control physical relays.
|
|
||||||
* This example will remember relay state after power failure.
|
|
||||||
* http://www.mysensors.org/build/relay
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Enable debug prints to serial monitor
|
|
||||||
#define MY_DEBUG
|
#define MY_DEBUG
|
||||||
|
|
||||||
// Enable and select radio type attached
|
// Enable and select radio type attached
|
||||||
#define MY_RADIO_RF24
|
#define MY_RADIO_RF24
|
||||||
//#define MY_RADIO_NRF5_ESB
|
|
||||||
//#define MY_RADIO_RFM69
|
|
||||||
//#define MY_RADIO_RFM95
|
|
||||||
|
|
||||||
// Enable repeater functionality for this node
|
|
||||||
//#define MY_REPEATER_FEATURE
|
|
||||||
|
|
||||||
#include <MySensors.h>
|
#include <MySensors.h>
|
||||||
|
|
||||||
#define RELAY_PIN 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
|
#define RELAY_PIN 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
|
||||||
#define NUMBER_OF_RELAYS 1 // Total number of attached relays
|
|
||||||
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
|
|
||||||
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
|
|
||||||
|
|
||||||
|
//MyMessage msgLev(1, V_DIMMER);
|
||||||
|
uint16_t lightLevel;
|
||||||
|
bool lightOn = false;
|
||||||
|
|
||||||
void before()
|
void before()
|
||||||
{
|
{
|
||||||
for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
|
pinMode(RELAY_PIN, OUTPUT);
|
||||||
// Then set relay pins in output mode
|
lightLevel = loadState(0);
|
||||||
pinMode(pin, OUTPUT);
|
|
||||||
// Set relay to last known state (using eeprom storage)
|
|
||||||
digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
@@ -67,12 +26,8 @@ void setup()
|
|||||||
void presentation()
|
void presentation()
|
||||||
{
|
{
|
||||||
// Send the sketch version information to the gateway and Controller
|
// Send the sketch version information to the gateway and Controller
|
||||||
sendSketchInfo("Relay", "1.0");
|
sendSketchInfo("Night Light", "2.0");
|
||||||
|
present(0, S_DIMMER, "Dimmer");
|
||||||
for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
|
|
||||||
// Register all sensors to gw (they will be created as child devices)
|
|
||||||
present(sensor, S_BINARY);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -83,16 +38,29 @@ void loop()
|
|||||||
|
|
||||||
void receive(const MyMessage &message)
|
void receive(const MyMessage &message)
|
||||||
{
|
{
|
||||||
// We only expect one type of message from controller. But we better check anyway.
|
switch(message.getType()){
|
||||||
if (message.getType()==V_STATUS) {
|
case V_STATUS:
|
||||||
// Change relay state
|
if(message.getBool() == true) {
|
||||||
digitalWrite(message.getSensor()-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
|
analogWrite(RELAY_PIN, lightLevel);
|
||||||
// Store state in eeprom
|
lightOn = true;
|
||||||
saveState(message.getSensor(), message.getBool());
|
}
|
||||||
// Write some debug info
|
else {
|
||||||
|
analogWrite(RELAY_PIN, 0);
|
||||||
|
lightOn = false;
|
||||||
|
}
|
||||||
Serial.print("Incoming change for sensor:");
|
Serial.print("Incoming change for sensor:");
|
||||||
Serial.print(message.getSensor());
|
Serial.print(message.getSensor());
|
||||||
Serial.print(", New status: ");
|
Serial.print(", New status: ");
|
||||||
Serial.println(message.getBool());
|
Serial.println(message.getBool());
|
||||||
|
break;
|
||||||
|
case V_PERCENTAGE:
|
||||||
|
saveState(0, message.getUInt());
|
||||||
|
lightLevel = message.getUInt();
|
||||||
|
if(lightOn) analogWrite(RELAY_PIN, lightLevel);
|
||||||
|
Serial.print("Incoming change for dimmer:");
|
||||||
|
Serial.print(message.getSensor());
|
||||||
|
Serial.print(", New value: ");
|
||||||
|
Serial.println(message.getUInt());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user