Merge branch 'master' into development

Conflicts:
	libraries/MySensors/MyGateway.cpp
	libraries/MySensors/MySensor.cpp
	libraries/MySensors/examples/EthernetGateway/EthernetGateway.ino
This commit is contained in:
Henrik Ekblad
2015-02-03 22:50:06 +01:00
14 changed files with 555 additions and 54 deletions
@@ -28,7 +28,7 @@
#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
MySensor gw(9,10);
MySensor gw;
static int currentLevel = 0; // Current dim level...
MyMessage dimmerMsg(0, V_DIMMER);
@@ -0,0 +1,146 @@
/***
* 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.
*
* DESCRIPTION
* This sketch provides an example how to implement a Dimmable Light
* It is pure virtual and it logs messages to the serial output
* It can be used as a base sketch for actual hardware.
* Stores the last light state and level in eeprom.
*
* Developed by GizMoCuz (Domoticz)
*
* REVISION HISTORY
* Version 1.0 - January 30, 2015
*
***/
#include <SPI.h>
#include <MySensor.h>
#define CHILD_ID_LIGHT 1
#define EPROM_LIGHT_STATE 1
#define EPROM_DIMMER_LEVEL 2
#define LIGHT_OFF 0
#define LIGHT_ON 1
#define SN "Dimable Light"
#define SV "1.0"
int LastLightState=LIGHT_OFF;
int LastDimValue=100;
MySensor gw;
MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);
MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
void setup()
{
gw.begin(incomingMessage, AUTO, false);
// Send the Sketch Version Information to the Gateway
gw.sendSketchInfo(SN, SV);
gw.present(CHILD_ID_LIGHT, S_DIMMER );
//Retreive our last light state from the eprom
int LightState=gw.loadState(EPROM_LIGHT_STATE);
if (LightState<=1) {
LastLightState=LightState;
int DimValue=gw.loadState(EPROM_DIMMER_LEVEL);
if ((DimValue>0)&&(DimValue<=100)) {
//There should be no Dim value of 0, this would mean LIGHT_OFF
LastDimValue=DimValue;
}
}
//Here you actualy switch on/off the light with the last known dim level
SetCurrentState2Hardware();
Serial.println( "Node ready to receive messages..." );
}
void loop()
{
//delay(1000); // Removed by hek
// Process incoming messages (like config and light state from controller)
gw.process();
}
void incomingMessage(const MyMessage &message)
{
if (message.type == V_LIGHT) {
Serial.println( "V_LIGHT command received..." );
int lstate= atoi( message.data );
if ((lstate<0)||(lstate>1)) {
Serial.println( "V_LIGHT data invalid (should be 0/1)" );
return;
}
LastLightState=lstate;
gw.saveState(EPROM_LIGHT_STATE, LastLightState);
if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) {
//In the case that the Light State = On, but the dimmer value is zero,
//then something (probably the controller) did something wrong,
//for the Dim value to 100%
LastDimValue=100;
gw.saveState(EPROM_DIMMER_LEVEL, LastDimValue);
}
//When receiving a V_LIGHT command we switch the light between OFF and the last received dimmer value
//This means if you previously set the lights dimmer value to 50%, and turn the light ON
//it will do so at 50%
}
else if (message.type == V_DIMMER) {
Serial.println( "V_DIMMER command received..." );
int dimvalue= atoi( message.data );
if ((dimvalue<0)||(dimvalue>100)) {
Serial.println( "V_DIMMER data invalid (should be 0..100)" );
return;
}
if (dimvalue==0) {
LastLightState=LIGHT_OFF;
}
else {
LastLightState=LIGHT_ON;
LastDimValue=dimvalue;
gw.saveState(EPROM_DIMMER_LEVEL, LastDimValue);
}
}
else {
Serial.println( "Invalid command received..." );
return;
}
//Here you set the actual light state/level
SetCurrentState2Hardware();
}
void SetCurrentState2Hardware()
{
if (LastLightState==LIGHT_OFF) {
Serial.println( "Light state: OFF" );
}
else {
Serial.print( "Light state: ON, Level: " );
Serial.println( LastDimValue );
}
//Send current state to the controller
SendCurrentState2Controller();
}
void SendCurrentState2Controller()
{
if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) {
gw.send(dimmerMsg.set(0));
}
else {
gw.send(dimmerMsg.set(LastDimValue));
}
}
@@ -101,7 +101,6 @@ void output(const char *fmt, ... ) {
void setup()
{
Ethernet.begin(mac, myIp);
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
@@ -0,0 +1,62 @@
// MAX6875 Hight temperature sensor with range: -200 degC - +1300 degC
// Contribution by MrLynx
// http://forum.mysensors.org/topic/641/max6675
//
// Connect:
// MAX6675 - Arduino
// CS0 - 4
// SO - 3
// SCLK - 5
// VCC - 5V
// GND - GND
#include <MySensor.h>
#include <MAX6675.h>
#include <SPI.h>
uint8_t CS0 = 4; // CS pin on MAX6675
uint8_t SO = 3; // SO pin of MAX6675
uint8_t SCLK = 5; // SCK pin of MAX6675
uint8_t units = 1; // Units to readout temp (0 = ˚F, 1 = ˚C)
float temperature = 0.0; // Temperature output variable
float lastTemperature;
unsigned long SLEEP_TIME = 30000;
boolean metric = true;
MySensor gw;
MyMessage msg(0, V_TEMP);
// Initialize the MAX6675 Library for our chip
MAX6675 temp0(CS0, SO, SCLK, units);
void setup()
{
// Startup and initialize MySensors library. Set callback for incoming messages.
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Max6675 Temp Sensor", "1.0");
// Present all sensors to controller
gw.present(0, S_TEMP);
}
void loop()
{
// Process incoming messages (like config from server)
gw.process();
temperature = temp0.read_temp(); // Read the temp
if (temperature < 0) { // If there is an error with the TC, temperature will be < 0
Serial.println("Thermocouple Error!!"); // There is a thermocouple error
} else {
Serial.print("Current Temperature: ");
Serial.println( temperature ); // Print the temperature to Serial
if (temperature != lastTemperature)
gw.send(msg.setSensor(0).set(temperature, 1));
lastTemperature = temperature;
}
gw.sleep(SLEEP_TIME);
}
@@ -14,9 +14,9 @@ MySensor gw;
float lastPressure = -1;
float lastTemp = -1;
int lastForecast = -1;
char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
const char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
int minutes;
float pressureSamples[180];
float pressureSamples[7][6];
int minuteCount = 0;
bool firstRound = true;
float pressureAvg[7];
@@ -97,20 +97,49 @@ int sample(float pressure) {
// http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
if (minuteCount == 180)
minuteCount = 5;
//From 0 to 5 min.
if (minuteCount <= 5){
pressureSamples[0][minuteCount] = pressure;
}
//From 30 to 35 min.
if ((minuteCount <= 30) && (minuteCount >= 35)){
pressureSamples[1][minuteCount - 30] = pressure;
}
//From 55 to 60 min.
if ((minuteCount <= 55) && (minuteCount >= 60)){
pressureSamples[2][minuteCount - 55] = pressure;
}
//From 90 to 95 min.
if ((minuteCount <= 90) && (minuteCount >= 95)){
pressureSamples[3][minuteCount - 90] = pressure;
}
//From 115 to 119 min.
if ((minuteCount <= 115) && (minuteCount >= 120)){
pressureSamples[4][minuteCount - 115] = pressure;
}
//From 150 to 155 min.
if ((minuteCount <= 150) && (minuteCount >= 155)){
pressureSamples[5][minuteCount - 150] = pressure;
}
//From 175 to 180 min.
if ((minuteCount <= 175) && (minuteCount >= 180)){
pressureSamples[6][minuteCount - 175] = pressure;
}
pressureSamples[minuteCount] = pressure;
minuteCount++;
if (minuteCount == 5) {
// Avg pressure in first 5 min, value averaged from 0 to 5 min.
pressureAvg[0] = ((pressureSamples[0] + pressureSamples[1]
+ pressureSamples[2] + pressureSamples[3] + pressureSamples[4])
/ 5);
pressureAvg[0] = ((pressureSamples[0][0] + pressureSamples[0][1]
+ pressureSamples[0][2] + pressureSamples[0][3]
+ pressureSamples[0][4] + pressureSamples[0][5]) / 6);
} else if (minuteCount == 35) {
// Avg pressure in 30 min, value averaged from 0 to 5 min.
pressureAvg[1] = ((pressureSamples[30] + pressureSamples[31]
+ pressureSamples[32] + pressureSamples[33]
+ pressureSamples[34]) / 5);
pressureAvg[1] = ((pressureSamples[1][0] + pressureSamples[1][1]
+ pressureSamples[1][2] + pressureSamples[1][3]
+ pressureSamples[1][4] + pressureSamples[1][5]) / 6);
float change = (pressureAvg[1] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = ((65.0 / 1023.0) * 2 * change); // note this is for t = 0.5hour
@@ -118,9 +147,9 @@ int sample(float pressure) {
dP_dt = (((65.0 / 1023.0) * change) / 1.5); // divide by 1.5 as this is the difference in time from 0 value.
} else if (minuteCount == 60) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[2] = ((pressureSamples[55] + pressureSamples[56]
+ pressureSamples[57] + pressureSamples[58]
+ pressureSamples[59]) / 5);
pressureAvg[2] = ((pressureSamples[2][0] + pressureSamples[2][1]
+ pressureSamples[2][2] + pressureSamples[2][3]
+ pressureSamples[2][4] + pressureSamples[2][5]) / 6);
float change = (pressureAvg[2] - pressureAvg[0]);
if (firstRound) //first time initial 3 hour
dP_dt = ((65.0 / 1023.0) * change); //note this is for t = 1 hour
@@ -128,9 +157,9 @@ int sample(float pressure) {
dP_dt = (((65.0 / 1023.0) * change) / 2); //divide by 2 as this is the difference in time from 0 value
} else if (minuteCount == 95) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[3] = ((pressureSamples[90] + pressureSamples[91]
+ pressureSamples[92] + pressureSamples[93]
+ pressureSamples[94]) / 5);
pressureAvg[3] = ((pressureSamples[3][0] + pressureSamples[3][1]
+ pressureSamples[3][2] + pressureSamples[3][3]
+ pressureSamples[3][4] + pressureSamples[3][5]) / 6);
float change = (pressureAvg[3] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 1.5); // note this is for t = 1.5 hour
@@ -138,9 +167,9 @@ int sample(float pressure) {
dP_dt = (((65.0 / 1023.0) * change) / 2.5); // divide by 2.5 as this is the difference in time from 0 value
} else if (minuteCount == 120) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[4] = ((pressureSamples[115] + pressureSamples[116]
+ pressureSamples[117] + pressureSamples[118]
+ pressureSamples[119]) / 5);
pressureAvg[4] = ((pressureSamples[4][0] + pressureSamples[4][1]
+ pressureSamples[4][2] + pressureSamples[4][3]
+ pressureSamples[4][4] + pressureSamples[4][5]) / 6);
float change = (pressureAvg[4] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 2); // note this is for t = 2 hour
@@ -148,9 +177,9 @@ int sample(float pressure) {
dP_dt = (((65.0 / 1023.0) * change) / 3); // divide by 3 as this is the difference in time from 0 value
} else if (minuteCount == 155) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[5] = ((pressureSamples[150] + pressureSamples[151]
+ pressureSamples[152] + pressureSamples[153]
+ pressureSamples[154]) / 5);
pressureAvg[5] = ((pressureSamples[5][0] + pressureSamples[5][1]
+ pressureSamples[5][2] + pressureSamples[5][3]
+ pressureSamples[5][4] + pressureSamples[5][5]) / 6);
float change = (pressureAvg[5] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 2.5); // note this is for t = 2.5 hour
@@ -158,9 +187,9 @@ int sample(float pressure) {
dP_dt = (((65.0 / 1023.0) * change) / 3.5); // divide by 3.5 as this is the difference in time from 0 value
} else if (minuteCount == 180) {
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
pressureAvg[6] = ((pressureSamples[175] + pressureSamples[176]
+ pressureSamples[177] + pressureSamples[178]
+ pressureSamples[179]) / 5);
pressureAvg[6] = ((pressureSamples[6][0] + pressureSamples[6][1]
+ pressureSamples[6][2] + pressureSamples[6][3]
+ pressureSamples[6][4] + pressureSamples[6][5]) / 6);
float change = (pressureAvg[6] - pressureAvg[0]);
if (firstRound) // first time initial 3 hour
dP_dt = (((65.0 / 1023.0) * change) / 3); // note this is for t = 3 hour