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:
@@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
MAX6675.cpp - Library for reading temperature from a MAX6675.
|
||||||
|
|
||||||
|
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
|
||||||
|
http://creativecommons.org/licenses/by-sa/3.0/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <MAX6675.h>
|
||||||
|
|
||||||
|
MAX6675::MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units)
|
||||||
|
{
|
||||||
|
pinMode(CS_pin, OUTPUT);
|
||||||
|
pinMode(SO_pin, INPUT);
|
||||||
|
pinMode(SCK_pin, OUTPUT);
|
||||||
|
|
||||||
|
digitalWrite(CS_pin, HIGH);
|
||||||
|
|
||||||
|
_CS_pin = CS_pin;
|
||||||
|
_SO_pin = SO_pin;
|
||||||
|
_SCK_pin = SCK_pin;
|
||||||
|
_units = units;
|
||||||
|
}
|
||||||
|
|
||||||
|
float MAX6675::read_temp()
|
||||||
|
{
|
||||||
|
uint16_t value = 0;
|
||||||
|
uint8_t error_tc = 0;
|
||||||
|
float temp = 0.0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Initiate a temperature conversion. According to MAX's tech notes FAQ's
|
||||||
|
for the chip, Line going high initiates a conversion, which means, we
|
||||||
|
need to clock the chip low to high to initiate the conversion, then wait
|
||||||
|
for the conversion to be complete before trying to read the data from
|
||||||
|
the chip.
|
||||||
|
*/
|
||||||
|
digitalWrite(_CS_pin,LOW);
|
||||||
|
delay(2);
|
||||||
|
digitalWrite(_CS_pin,HIGH);
|
||||||
|
delay(220);
|
||||||
|
|
||||||
|
/* Read the chip and return the raw temperature value */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Bring CS pin low to allow us to read the data from
|
||||||
|
the conversion process
|
||||||
|
*/
|
||||||
|
digitalWrite(_CS_pin,LOW);
|
||||||
|
|
||||||
|
/* Cycle the clock for dummy bit 15 */
|
||||||
|
digitalWrite(_SCK_pin,HIGH);
|
||||||
|
delay(1);
|
||||||
|
digitalWrite(_SCK_pin,LOW);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Read bits 14-3 from MAX6675 for the Temp. Loop for each bit reading
|
||||||
|
the value and storing the final value in 'temp'
|
||||||
|
*/
|
||||||
|
for (int i=11; i>=0; i--) {
|
||||||
|
digitalWrite(_SCK_pin,HIGH);
|
||||||
|
value += digitalRead(_SO_pin) << i;
|
||||||
|
digitalWrite(_SCK_pin,LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read the TC Input inp to check for TC Errors */
|
||||||
|
digitalWrite(_SCK_pin,HIGH);
|
||||||
|
error_tc = digitalRead(_SO_pin);
|
||||||
|
digitalWrite(_SCK_pin,LOW);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Read the last two bits from the chip, faliure to do so will result
|
||||||
|
in erratic readings from the chip.
|
||||||
|
*/
|
||||||
|
for (int i=1; i>=0; i--) {
|
||||||
|
digitalWrite(_SCK_pin,HIGH);
|
||||||
|
delay(1);
|
||||||
|
digitalWrite(_SCK_pin,LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable Device
|
||||||
|
digitalWrite(_CS_pin, HIGH);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Keep in mind that the temp that was just read is on the digital scale
|
||||||
|
from 0˚C to 1023.75˚C at a resolution of 2^12. We now need to convert
|
||||||
|
to an actual readable temperature (this drove me nuts until I figured
|
||||||
|
this out!). Now multiply by 0.25. I tried to avoid float math but
|
||||||
|
it is tough to do a good conversion to ˚F. THe final value is converted
|
||||||
|
to an int and returned at x10 power.
|
||||||
|
|
||||||
|
2 = temp in deg F
|
||||||
|
1 = temp in deg C
|
||||||
|
0 = raw chip value 0-4095
|
||||||
|
*/
|
||||||
|
if(_units == 2) {
|
||||||
|
temp = (value*0.25) * 9.0/5.0 + 32.0;
|
||||||
|
} else if(_units == 1) {
|
||||||
|
temp = (value*0.25);
|
||||||
|
} else {
|
||||||
|
temp = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Output negative of CS_pin if there is a TC error, otherwise return 'temp' */
|
||||||
|
if(error_tc != 0) {
|
||||||
|
return -_CS_pin;
|
||||||
|
} else {
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
MAX6675.h - Library for reading temperature from a MAX6675.
|
||||||
|
|
||||||
|
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
|
||||||
|
http://creativecommons.org/licenses/by-sa/3.0/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MAX6675_h
|
||||||
|
#define MAX6675_h
|
||||||
|
|
||||||
|
#if defined(ARDUINO) && ARDUINO >= 100
|
||||||
|
#include "Arduino.h"
|
||||||
|
#else
|
||||||
|
#include "WProgram.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class MAX6675
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units);
|
||||||
|
float read_temp();
|
||||||
|
private:
|
||||||
|
uint8_t _CS_pin;
|
||||||
|
uint8_t _SO_pin;
|
||||||
|
uint8_t _SCK_pin;
|
||||||
|
uint8_t _units;
|
||||||
|
uint8_t chip_read(uint8_t CS_pin, uint8_t &error_tc);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
MAX6675 Arduino Library
|
||||||
|
=======================
|
||||||
|
Version: **2.0.1**
|
||||||
|
|
||||||
|
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
|
||||||
|
|
||||||
|
|
||||||
|
Summary
|
||||||
|
-------
|
||||||
|
|
||||||
|
The primary use of the library is to easily interface with a MAX6675 chip via it's SPI interface. Use the following code to initialize the library. V2 also greatly improves performance and timing of the chip for more accurate reads. Thanks Ed!
|
||||||
|
|
||||||
|
MAX6675 temp0(CS,SO,SCK,units);
|
||||||
|
|
||||||
|
Variables:
|
||||||
|
|
||||||
|
* CS is chip select pin of chip
|
||||||
|
* SO is data out of the chip
|
||||||
|
* SCK is the clock pin of the chip
|
||||||
|
* units is one of the following:
|
||||||
|
* 0 = raw 0-4095 value
|
||||||
|
* 1 = temp in ˚C
|
||||||
|
* 2 = temp in ˚F
|
||||||
|
|
||||||
|
Following this you can use the _read_temp()_ function to return the temperature as a float.
|
||||||
|
|
||||||
|
temperature = temp0.read_temp();
|
||||||
|
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
Single_Temp.pde - Example using the MAX6675 Library.
|
||||||
|
Created by Ryan McLaughlin <ryanjmclaughlin@gmail.com>
|
||||||
|
|
||||||
|
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
|
||||||
|
http://creativecommons.org/licenses/by-sa/3.0/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <MAX6675.h>
|
||||||
|
|
||||||
|
int LED1 = 9; // Status LED Pin
|
||||||
|
int CS = 10; // CS pin on MAX6675
|
||||||
|
int SO = 12; // SO pin of MAX6675
|
||||||
|
int SCK = 13; // SCK pin of MAX6675
|
||||||
|
int units = 2; // Units to readout temp (0 = raw, 1 = ˚C, 2 = ˚F)
|
||||||
|
float temperature = 0.0; // Temperature output variable
|
||||||
|
|
||||||
|
|
||||||
|
// Initialize the MAX6675 Library for our chip
|
||||||
|
MAX6675 temp(CS,SO,SCK,units);
|
||||||
|
|
||||||
|
|
||||||
|
// Setup Serial output and LED Pin
|
||||||
|
// MAX6675 Library already sets pin modes for MAX6675 chip!
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
pinMode(LED1, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Read the temp from the MAX6675
|
||||||
|
temperature = temp.read_temp();
|
||||||
|
|
||||||
|
if(temperature < 0) {
|
||||||
|
// If there is an error with the TC, temperature will be < 0
|
||||||
|
Serial.print("Thermocouple Error on CS");
|
||||||
|
Serial.println( temperature );
|
||||||
|
digitalWrite(LED1, HIGH);
|
||||||
|
} else {
|
||||||
|
Serial.print("Current Temperature: ");
|
||||||
|
Serial.println( temperature );
|
||||||
|
digitalWrite(LED1, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait one second before reading again
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#######################################
|
||||||
|
# Syntax Coloring Map For MAX6675
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Datatypes (KEYWORD1)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
MAX6675 KEYWORD1
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Methods and Functions (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
read_temp KEYWORD2
|
||||||
|
|
||||||
@@ -13,12 +13,10 @@ MyMessage::MyMessage(uint8_t _sensor, uint8_t _type) {
|
|||||||
type = _type;
|
type = _type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool MyMessage::isAck() const {
|
bool MyMessage::isAck() const {
|
||||||
return miGetAck();
|
return miGetAck();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Getters for payload converted to desired form */
|
/* Getters for payload converted to desired form */
|
||||||
void* MyMessage::getCustom() const {
|
void* MyMessage::getCustom() const {
|
||||||
return (void *)data;
|
return (void *)data;
|
||||||
@@ -42,16 +40,20 @@ char MyMessage::i2h(uint8_t i) const {
|
|||||||
return 'A' + k - 10;
|
return 'A' + k - 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* MyMessage::getCustomString(char *buffer) const {
|
||||||
|
for (uint8_t i = 0; i < miGetLength(); i++)
|
||||||
|
{
|
||||||
|
buffer[i * 2] = i2h(data[i] >> 4);
|
||||||
|
buffer[(i * 2) + 1] = i2h(data[i]);
|
||||||
|
}
|
||||||
|
buffer[miGetLength() * 2] = '\0';
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
char* MyMessage::getStream(char *buffer) const {
|
char* MyMessage::getStream(char *buffer) const {
|
||||||
uint8_t cmd = miGetCommand();
|
uint8_t cmd = miGetCommand();
|
||||||
if ((cmd == C_STREAM) && (buffer != NULL)) {
|
if ((cmd == C_STREAM) && (buffer != NULL)) {
|
||||||
for (uint8_t i = 0; i < miGetLength(); i++)
|
return getCustomString(buffer);
|
||||||
{
|
|
||||||
buffer[i * 2] = i2h(data[i] >> 4);
|
|
||||||
buffer[(i * 2) + 1] = i2h(data[i]);
|
|
||||||
}
|
|
||||||
buffer[miGetLength() * 2] = '\0';
|
|
||||||
return buffer;
|
|
||||||
} else {
|
} else {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -73,12 +75,11 @@ char* MyMessage::getString(char *buffer) const {
|
|||||||
} else if (payloadType == P_LONG32) {
|
} else if (payloadType == P_LONG32) {
|
||||||
ltoa(lValue, buffer, 10);
|
ltoa(lValue, buffer, 10);
|
||||||
} else if (payloadType == P_ULONG32) {
|
} else if (payloadType == P_ULONG32) {
|
||||||
|
|
||||||
ultoa(ulValue, buffer, 10);
|
ultoa(ulValue, buffer, 10);
|
||||||
} else if (payloadType == P_FLOAT32) {
|
} else if (payloadType == P_FLOAT32) {
|
||||||
dtostrf(fValue,2,fPrecision,buffer);
|
dtostrf(fValue,2,fPrecision,buffer);
|
||||||
} else if (payloadType == P_CUSTOM) {
|
} else if (payloadType == P_CUSTOM) {
|
||||||
return getStream(buffer);
|
return getCustomString(buffer);
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
} else {
|
} else {
|
||||||
@@ -151,7 +152,6 @@ unsigned int MyMessage::getUInt() const {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MyMessage& MyMessage::setType(uint8_t _type) {
|
MyMessage& MyMessage::setType(uint8_t _type) {
|
||||||
type = _type;
|
type = _type;
|
||||||
return *this;
|
return *this;
|
||||||
@@ -175,12 +175,11 @@ MyMessage& MyMessage::set(void* value, uint8_t length) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MyMessage& MyMessage::set(const char* value) {
|
MyMessage& MyMessage::set(const char* value) {
|
||||||
uint8_t length = strlen(value);
|
uint8_t length = min(strlen(value), MAX_PAYLOAD);
|
||||||
miSetLength(length);
|
miSetLength(length);
|
||||||
miSetPayloadType(P_STRING);
|
miSetPayloadType(P_STRING);
|
||||||
strncpy(data, value, min(length, MAX_PAYLOAD));
|
strncpy(data, value, length);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,7 +190,6 @@ MyMessage& MyMessage::set(uint8_t value) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MyMessage& MyMessage::set(float value, uint8_t decimals) {
|
MyMessage& MyMessage::set(float value, uint8_t decimals) {
|
||||||
miSetLength(5); // 32 bit float + persi
|
miSetLength(5); // 32 bit float + persi
|
||||||
miSetPayloadType(P_FLOAT32);
|
miSetPayloadType(P_FLOAT32);
|
||||||
@@ -227,5 +225,3 @@ MyMessage& MyMessage::set(int value) {
|
|||||||
iValue = value;
|
iValue = value;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -122,6 +122,9 @@ typedef enum {
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
class MyMessage
|
class MyMessage
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
char* getCustomString(char *buffer) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructors
|
// Constructors
|
||||||
MyMessage();
|
MyMessage();
|
||||||
|
|||||||
@@ -74,9 +74,11 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
|
|||||||
nc.distance = DISTANCE_INVALID;
|
nc.distance = DISTANCE_INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_nodeId != AUTO) {
|
if ( (_nodeId != AUTO) && (nc.nodeId != _nodeId) ) {
|
||||||
// Set static id
|
// Set static id
|
||||||
nc.nodeId = _nodeId;
|
nc.nodeId = _nodeId;
|
||||||
|
// Save static id in eeprom
|
||||||
|
eeprom_write_byte((uint8_t*)EEPROM_NODE_ID_ADDRESS, _nodeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to fetch node-id from gateway
|
// Try to fetch node-id from gateway
|
||||||
@@ -253,12 +255,12 @@ void MySensor::sendSketchInfo(const char *name, const char *version, bool enable
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MySensor::request(uint8_t childSensorId, uint8_t variableType, uint8_t destination) {
|
void MySensor::request(uint8_t childSensorId, uint8_t variableType, uint8_t destination) {
|
||||||
sendRoute(build(msg, nc.nodeId, destination, childSensorId, C_REQ, variableType, false));
|
sendRoute(build(msg, nc.nodeId, destination, childSensorId, C_REQ, variableType, false).set(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
|
void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
|
||||||
timeCallback = _timeCallback;
|
timeCallback = _timeCallback;
|
||||||
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME, false));
|
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_TIME, false).set(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -287,11 +289,15 @@ boolean MySensor::process() {
|
|||||||
uint8_t destination = msg.destination;
|
uint8_t destination = msg.destination;
|
||||||
|
|
||||||
if (repeaterMode && command == C_INTERNAL && type == I_FIND_PARENT) {
|
if (repeaterMode && command == C_INTERNAL && type == I_FIND_PARENT) {
|
||||||
// Relaying nodes should always answer ping messages
|
if (nc.distance == 255) {
|
||||||
// Wait a random delay of 0-1.023 seconds to minimize collision
|
findParentNode();
|
||||||
// between ping ack messages from other relaying nodes
|
} else if (sender != nc.parentNodeId) {
|
||||||
delay(millis() & 0x3ff);
|
// Relaying nodes should always answer ping messages
|
||||||
sendWrite(sender, build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(nc.distance));
|
// Wait a random delay of 0-2 seconds to minimize collision
|
||||||
|
// between ping ack messages from other relaying nodes
|
||||||
|
delay(millis() & 0x3ff);
|
||||||
|
sendWrite(sender, build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(nc.distance), true);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
} else if (command == C_INTERNAL && type == I_TIME) {
|
} else if (command == C_INTERNAL && type == I_TIME) {
|
||||||
if (timeCallback != NULL) {
|
if (timeCallback != NULL) {
|
||||||
@@ -400,6 +406,10 @@ boolean MySensor::process() {
|
|||||||
//
|
//
|
||||||
// lookup route in table and send message there
|
// lookup route in table and send message there
|
||||||
sendWrite(route, msg);
|
sendWrite(route, msg);
|
||||||
|
} else if (sender == GATEWAY_ADDRESS && destination == BROADCAST_ADDRESS) {
|
||||||
|
// A net gateway reply to a message previously sent by us from a 255 node
|
||||||
|
// We should broadcast this back to the node
|
||||||
|
sendWrite(destination, msg, true);
|
||||||
} else {
|
} else {
|
||||||
// A message comes from a child node and we have no
|
// A message comes from a child node and we have no
|
||||||
// route for it.
|
// route for it.
|
||||||
@@ -487,6 +497,23 @@ void MySensor::sleep(unsigned long ms) {
|
|||||||
internalSleep(ms);
|
internalSleep(ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MySensor::wait(unsigned long ms) {
|
||||||
|
bool slept_enough = false;
|
||||||
|
unsigned long start = millis();
|
||||||
|
unsigned long now;
|
||||||
|
|
||||||
|
// Let serial prints finish (debug, log etc)
|
||||||
|
Serial.flush();
|
||||||
|
|
||||||
|
while (!slept_enough) {
|
||||||
|
MySensor::process();
|
||||||
|
now = millis();
|
||||||
|
if (now - start > ms) {
|
||||||
|
slept_enough = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
|
bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
|
||||||
// Let serial prints finish (debug, log etc)
|
// Let serial prints finish (debug, log etc)
|
||||||
bool pinTriggeredWakeup = true;
|
bool pinTriggeredWakeup = true;
|
||||||
|
|||||||
@@ -194,6 +194,15 @@ class MySensor
|
|||||||
*/
|
*/
|
||||||
void sleep(unsigned long ms);
|
void sleep(unsigned long ms);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for a specified amount of time to pass. Keeps process()ing.
|
||||||
|
* This does not power-down the radio nor the Arduino.
|
||||||
|
* Because this calls process() in a loop, it is a good way to wait
|
||||||
|
* in your loop() on a repeater node or sensor that listens to messages.
|
||||||
|
* @param ms Number of milliseconds to sleep.
|
||||||
|
*/
|
||||||
|
void wait(unsigned long ms);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sleep (PowerDownMode) the Arduino and radio. Wake up on timer or pin change.
|
* Sleep (PowerDownMode) the Arduino and radio. Wake up on timer or pin change.
|
||||||
* See: http://arduino.cc/en/Reference/attachInterrupt for details on modes and which pin
|
* See: http://arduino.cc/en/Reference/attachInterrupt for details on modes and which pin
|
||||||
|
|||||||
@@ -28,7 +28,7 @@
|
|||||||
#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
|
#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)
|
#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...
|
static int currentLevel = 0; // Current dim level...
|
||||||
MyMessage dimmerMsg(0, V_DIMMER);
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
|
|
||||||
Ethernet.begin(mac, myIp);
|
Ethernet.begin(mac, myIp);
|
||||||
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
|
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 lastPressure = -1;
|
||||||
float lastTemp = -1;
|
float lastTemp = -1;
|
||||||
int lastForecast = -1;
|
int lastForecast = -1;
|
||||||
char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
|
const char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
|
||||||
int minutes;
|
int minutes;
|
||||||
float pressureSamples[180];
|
float pressureSamples[7][6];
|
||||||
int minuteCount = 0;
|
int minuteCount = 0;
|
||||||
bool firstRound = true;
|
bool firstRound = true;
|
||||||
float pressureAvg[7];
|
float pressureAvg[7];
|
||||||
@@ -98,19 +98,48 @@ int sample(float pressure) {
|
|||||||
if (minuteCount == 180)
|
if (minuteCount == 180)
|
||||||
minuteCount = 5;
|
minuteCount = 5;
|
||||||
|
|
||||||
pressureSamples[minuteCount] = pressure;
|
//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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
minuteCount++;
|
minuteCount++;
|
||||||
|
|
||||||
if (minuteCount == 5) {
|
if (minuteCount == 5) {
|
||||||
// Avg pressure in first 5 min, value averaged from 0 to 5 min.
|
// Avg pressure in first 5 min, value averaged from 0 to 5 min.
|
||||||
pressureAvg[0] = ((pressureSamples[0] + pressureSamples[1]
|
pressureAvg[0] = ((pressureSamples[0][0] + pressureSamples[0][1]
|
||||||
+ pressureSamples[2] + pressureSamples[3] + pressureSamples[4])
|
+ pressureSamples[0][2] + pressureSamples[0][3]
|
||||||
/ 5);
|
+ pressureSamples[0][4] + pressureSamples[0][5]) / 6);
|
||||||
} else if (minuteCount == 35) {
|
} else if (minuteCount == 35) {
|
||||||
// Avg pressure in 30 min, value averaged from 0 to 5 min.
|
// Avg pressure in 30 min, value averaged from 0 to 5 min.
|
||||||
pressureAvg[1] = ((pressureSamples[30] + pressureSamples[31]
|
pressureAvg[1] = ((pressureSamples[1][0] + pressureSamples[1][1]
|
||||||
+ pressureSamples[32] + pressureSamples[33]
|
+ pressureSamples[1][2] + pressureSamples[1][3]
|
||||||
+ pressureSamples[34]) / 5);
|
+ pressureSamples[1][4] + pressureSamples[1][5]) / 6);
|
||||||
float change = (pressureAvg[1] - pressureAvg[0]);
|
float change = (pressureAvg[1] - pressureAvg[0]);
|
||||||
if (firstRound) // first time initial 3 hour
|
if (firstRound) // first time initial 3 hour
|
||||||
dP_dt = ((65.0 / 1023.0) * 2 * change); // note this is for t = 0.5hour
|
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.
|
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) {
|
} else if (minuteCount == 60) {
|
||||||
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
||||||
pressureAvg[2] = ((pressureSamples[55] + pressureSamples[56]
|
pressureAvg[2] = ((pressureSamples[2][0] + pressureSamples[2][1]
|
||||||
+ pressureSamples[57] + pressureSamples[58]
|
+ pressureSamples[2][2] + pressureSamples[2][3]
|
||||||
+ pressureSamples[59]) / 5);
|
+ pressureSamples[2][4] + pressureSamples[2][5]) / 6);
|
||||||
float change = (pressureAvg[2] - pressureAvg[0]);
|
float change = (pressureAvg[2] - pressureAvg[0]);
|
||||||
if (firstRound) //first time initial 3 hour
|
if (firstRound) //first time initial 3 hour
|
||||||
dP_dt = ((65.0 / 1023.0) * change); //note this is for t = 1 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
|
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) {
|
} else if (minuteCount == 95) {
|
||||||
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
||||||
pressureAvg[3] = ((pressureSamples[90] + pressureSamples[91]
|
pressureAvg[3] = ((pressureSamples[3][0] + pressureSamples[3][1]
|
||||||
+ pressureSamples[92] + pressureSamples[93]
|
+ pressureSamples[3][2] + pressureSamples[3][3]
|
||||||
+ pressureSamples[94]) / 5);
|
+ pressureSamples[3][4] + pressureSamples[3][5]) / 6);
|
||||||
float change = (pressureAvg[3] - pressureAvg[0]);
|
float change = (pressureAvg[3] - pressureAvg[0]);
|
||||||
if (firstRound) // first time initial 3 hour
|
if (firstRound) // first time initial 3 hour
|
||||||
dP_dt = (((65.0 / 1023.0) * change) / 1.5); // note this is for t = 1.5 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
|
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) {
|
} else if (minuteCount == 120) {
|
||||||
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
||||||
pressureAvg[4] = ((pressureSamples[115] + pressureSamples[116]
|
pressureAvg[4] = ((pressureSamples[4][0] + pressureSamples[4][1]
|
||||||
+ pressureSamples[117] + pressureSamples[118]
|
+ pressureSamples[4][2] + pressureSamples[4][3]
|
||||||
+ pressureSamples[119]) / 5);
|
+ pressureSamples[4][4] + pressureSamples[4][5]) / 6);
|
||||||
float change = (pressureAvg[4] - pressureAvg[0]);
|
float change = (pressureAvg[4] - pressureAvg[0]);
|
||||||
if (firstRound) // first time initial 3 hour
|
if (firstRound) // first time initial 3 hour
|
||||||
dP_dt = (((65.0 / 1023.0) * change) / 2); // note this is for t = 2 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
|
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) {
|
} else if (minuteCount == 155) {
|
||||||
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
||||||
pressureAvg[5] = ((pressureSamples[150] + pressureSamples[151]
|
pressureAvg[5] = ((pressureSamples[5][0] + pressureSamples[5][1]
|
||||||
+ pressureSamples[152] + pressureSamples[153]
|
+ pressureSamples[5][2] + pressureSamples[5][3]
|
||||||
+ pressureSamples[154]) / 5);
|
+ pressureSamples[5][4] + pressureSamples[5][5]) / 6);
|
||||||
float change = (pressureAvg[5] - pressureAvg[0]);
|
float change = (pressureAvg[5] - pressureAvg[0]);
|
||||||
if (firstRound) // first time initial 3 hour
|
if (firstRound) // first time initial 3 hour
|
||||||
dP_dt = (((65.0 / 1023.0) * change) / 2.5); // note this is for t = 2.5 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
|
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) {
|
} else if (minuteCount == 180) {
|
||||||
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
// Avg pressure at end of the hour, value averaged from 0 to 5 min.
|
||||||
pressureAvg[6] = ((pressureSamples[175] + pressureSamples[176]
|
pressureAvg[6] = ((pressureSamples[6][0] + pressureSamples[6][1]
|
||||||
+ pressureSamples[177] + pressureSamples[178]
|
+ pressureSamples[6][2] + pressureSamples[6][3]
|
||||||
+ pressureSamples[179]) / 5);
|
+ pressureSamples[6][4] + pressureSamples[6][5]) / 6);
|
||||||
float change = (pressureAvg[6] - pressureAvg[0]);
|
float change = (pressureAvg[6] - pressureAvg[0]);
|
||||||
if (firstRound) // first time initial 3 hour
|
if (firstRound) // first time initial 3 hour
|
||||||
dP_dt = (((65.0 / 1023.0) * change) / 3); // note this is for t = 3 hour
|
dP_dt = (((65.0 / 1023.0) * change) / 3); // note this is for t = 3 hour
|
||||||
|
|||||||
Reference in New Issue
Block a user