Move master branch from from mios git repo

This commit is contained in:
hek
2014-01-23 00:23:15 +01:00
commit 609d9e5be9
352 changed files with 53418 additions and 0 deletions
@@ -0,0 +1,20 @@
/*
*
* This sketch clears radioId, relayId and routing info in EEPROM
*
*/
#include <SPI.h>
#include <EEPROM.h>
void setup()
{
for (int i=0;i<512;i++) {
EEPROM.write(i, 0xff);
}
}
void loop()
{
// Nothing to do here...
}
@@ -0,0 +1,70 @@
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <RF24.h>
#include <Sensor.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Sensor gw(9, 10);
Sleep sleep;
#define MAX_ATTACHED_DS18B20 16
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
boolean metric = true;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
sensors.begin();
gw.begin(RADIO_ID);
// Fetch the number of attached sensors
numSensors = sensors.getDeviceCount();
// Register all sensors to gw (they will be created as child devices)
for (int i=0; i<numSensors; i++) {
gw.sendSensorPresentation(i, S_TEMP);
}
metric = gw.isMetricSystem();
}
void loop()
{
sensors.requestTemperatures(); // Fetch temperatures from Dallas
delay(100);
for (int i=0; i<numSensors; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((metric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Only send data if temperature has changed and no error
if (lastTemperature[i] != temperature && temperature != -127.00) {
gw.powerUp(); // Powerup introduces a small delay (which is missing in radio.write powerup)
// Send variable (using registered shortcut) to gw
gw.sendVariable(i, V_TEMP, temperature, 1);
lastTemperature[i]=temperature;
}
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(500);
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
}
@@ -0,0 +1,58 @@
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
#include <NewPing.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID 8
#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
unsigned long SLEEP_TIME = 5; // Sleep time between reads (in seconds)
Sensor gw(9, 10);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Sleep sleep;
int lastDist;
boolean metric;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
// Register all sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(0, S_DISTANCE);
metric = gw.isMetricSystem();
}
void loop()
{
int dist = metric?sonar.ping_cm():sonar.ping_in();
Serial.print("Ping: ");
Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(metric?" cm":" in");
if (dist != lastDist) {
gw.sendVariable(0, V_DISTANCE, dist);
lastDist = dist;
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(1000); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
}
@@ -0,0 +1,118 @@
// Use this sensor to measure KWH and Watt of your house meeter
// You need to set the correct pulsefactor of your meeter (blinks per KWH).
// The sensor starts by fetching current KWH value from gateway.
// Reports both KWH and Watt back to gateway.
//
// Unfortunately millis() won't increment when the Arduino is in
// sleepmode. So we cannot make this sensor sleep if we also want
// to calculate/report watt-number.
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your light sensor. (Only 2 and 3 generates interrupt!)
#define PULSE_FACTOR 1000 // Nummber of blinks per KWH of your meeter
#define SLEEP_MODE false // Watt-value can only be reported when sleep mode is false.
#define MAX_WATT 10000 // Max watt value to report. This filetrs outliers.
unsigned long SEND_FREQUENCY = 20; // Minimum time between send (in seconds). We don't wnat to spam the gateway.
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 5 // Id of the sensor child
Sensor gw(9,10);
Sleep sleep;
//double kwhPerBlink = 1.0/((double)PULSE_FACTOR);
double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
volatile unsigned long pulseCount = 0;
volatile unsigned long lastBlink = 0;
volatile unsigned long watt = 0;
unsigned long oldPulseCount = 0;
unsigned long oldWatt = 0;
double oldKwh;
unsigned long lastSend;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
// Register this device as power sensor
gw.sendSensorPresentation(CHILD_ID, S_POWER);
// Fetch last known pulse count value from gw
pulseCount = oldPulseCount = atol(gw.getStatus(CHILD_ID, V_VAR1));
Serial.print("Last pulse count from gw:");
Serial.println(pulseCount);
attachInterrupt(INTERRUPT, onPulse, RISING);
lastSend=millis();
}
void loop()
{
unsigned long now = millis();
// Only send values at a maximum frequency or woken up from sleep
if (SLEEP_MODE || now - lastSend > 1000*SEND_FREQUENCY) {
// New watt value has been calculated
if (!SLEEP_MODE && watt != oldWatt) {
// Check that we dont get unresonable large watt value.
// could hapen when long wraps or false interrupt triggered
if (watt<((unsigned long)MAX_WATT)) {
gw.sendVariable(CHILD_ID, V_WATT, watt); // Send watt value to gw
}
Serial.print("Watt:");
Serial.println(watt);
oldWatt = watt;
}
// Pulse cout has changed
if (pulseCount != oldPulseCount) {
gw.sendVariable(CHILD_ID, V_VAR1, pulseCount); // Send kwh value to gw
double kwh = ((double)pulseCount/((double)PULSE_FACTOR));
oldPulseCount = pulseCount;
//Serial.print("Pulse count:");
//Serial.println(pulseCount);
if (kwh != oldKwh) {
gw.sendVariable(CHILD_ID, V_KWH, kwh, 4); // Send kwh value to gw
//Serial.print("KWH:");
//Serial.println(kwh);
oldKwh = kwh;
}
}
lastSend = now;
}
if (SLEEP_MODE) {
delay(300); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SEND_FREQUENCY * 1000); //sleep for: sleepTime
}
}
void onPulse()
{
if (!SLEEP_MODE) {
unsigned long newBlink = micros();
unsigned long interval = newBlink-lastBlink;
if (interval<10000L) { // Sometimes we get interrupt on RISING
return;
}
watt = (3600000000.0 /interval) / ppwh;
lastBlink = newBlink;
}
pulseCount++;
}
@@ -0,0 +1,74 @@
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
#include <DHT.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define HUMIDITY_SENSOR_DIGITAL_PIN 3
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
Sensor gw(9, 10);
DHT dht;
Sleep sleep;
float lastTemp;
float lastHum;
boolean metric = true;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
// Register all sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID_HUM, S_HUM);
gw.sendSensorPresentation(CHILD_ID_TEMP, S_TEMP);
metric = gw.isMetricSystem();
}
void loop()
{
delay(dht.getMinimumSamplingPeriod());
float temperature = dht.getTemperature();
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
} else if (temperature != lastTemp) {
lastTemp = temperature;
if (!metric) {
temperature = dht.toFahrenheit(temperature);
}
gw.sendVariable(CHILD_ID_TEMP, V_TEMP, temperature, 1);
Serial.print("T: ");
Serial.println(temperature);
}
float humidity = dht.getHumidity();
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
} else if (humidity != lastHum) {
lastHum = humidity;
gw.sendVariable(CHILD_ID_HUM, V_HUM, humidity, 1);
Serial.print("H: ");
Serial.println(humidity);
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(1000); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
}
@@ -0,0 +1,119 @@
// Example sketch showing how to control ir devices
// An IR LED must be connected to Arduino PWM pin 3.
// An optional ir receiver can be connected to PWM pin 8.
// All receied ir signals will be sent to gateway device stored in VAR_1.
// When binary light on is clicked - sketch will send volume up ir command
// When binary light off is clicked - sketch will send volume down ir command
#include <Sensor.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <IRremote.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
int RECV_PIN = 8;
#define CHILD_1 3 // childId
IRsend irsend;
IRrecv irrecv(RECV_PIN);
decode_results results;
Sensor gw(9,10);
void setup()
{
Serial.begin(BAUD_RATE); // Used to write debug info
irrecv.enableIRIn(); // Start the ir receiver
gw.begin(RADIO_ID);
// Register a sensors to gw. Use binary light for test purposes.
gw.sendSensorPresentation(CHILD_1, S_LIGHT);
}
void loop()
{
if (gw.messageAvailable()) {
message_s message = gw.getMessage();
int incomingRelayStatus = atoi(message.data);
if (incomingRelayStatus == 1) {
irsend.sendNEC(0x1EE17887, 32); // Vol up yamaha ysp-900
} else {
irsend.sendNEC(0x1EE1F807, 32); // Vol down yamaha ysp-900
}
// Start receiving ir again...
irrecv.enableIRIn();
}
if (irrecv.decode(&results)) {
char buffer[10];
sprintf(buffer, "%08lx", results.value);
dump(&results);
// Send ir result to gw
gw.sendVariable(CHILD_1, V_VAR1, buffer);
irrecv.resume(); // Receive the next value
}
}
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
// decode_results *results = (decode_results *)v
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded PANASONIC - Address: ");
Serial.print(results->panasonicAddress,HEX);
Serial.print(" Value: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++) {
if ((i % 2) == 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
}
@@ -0,0 +1,47 @@
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <RF24.h>
#include <EEPROM.h>
#include <Sensor.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define CHILD_ID_LIGHT 0
#define LIGHT_SENSOR_ANALOG_PIN 0
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
Sensor gw(9,10);
int lastLightLevel;
Sleep sleep;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
// Register all sensors to gateway (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
}
void loop()
{
int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
Serial.println(lightLevel);
if (lightLevel != lastLightLevel) {
gw.sendVariable(CHILD_ID_LIGHT, V_LIGHT_LEVEL, lightLevel);
lastLightLevel = lightLevel;
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(1000); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
}
@@ -0,0 +1,45 @@
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 0 // Id of the sensor child
Sensor gw(9,10);
Sleep sleep;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
// Register all sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID, S_MOTION);
}
void loop()
{
// Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
Serial.println(tripped);
gw.sendVariable(CHILD_ID, V_TRIPPED, tripped?"1":"0"); // Send tripped value to gw
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(200); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepInterrupt(INTERRUPT,CHANGE);
}
@@ -0,0 +1,198 @@
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define LIGHT_SENSOR_ANALOG_PIN 0
unsigned long SLEEP_TIME = 60; // Sleep time between reads (in seconds)
Adafruit_BMP085 bmp = Adafruit_BMP085(); // Digital Pressure Sensor
Sensor gw(9, 10);
float lastPressure = -1;
float lastTemp = -1;
int lastForecast = -1;
Sleep sleep;
char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
int minutes;
float pressureSamples[180];
int minuteCount = 0;
bool firstRound = true;
float pressureAvg[7];
float dP_dt;
boolean metric;
void setup() {
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) { }
}
// Register sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(0, S_BARO);
gw.sendSensorPresentation(1, S_TEMP);
metric = gw.isMetricSystem();
}
void loop() {
float pressure = bmp.readPressure();
float temperature = bmp.readTemperature();
if (!metric) {
// Convert to fahrenheit
temperature = temperature * 9.0 / 5.0 + 32.0;
}
int forecast = sample(pressure);
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(metric?" *C":" *F");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" Pa");
Serial.println(weather[forecast]);
if (temperature != lastTemp) {
gw.sendVariable(1, V_TEMP, temperature,1);
lastTemp = temperature;
}
if (pressure != lastPressure) {
gw.sendVariable(0, V_PRESSURE, pressure, 0);
lastPressure = pressure;
}
if (forecast != lastForecast) {
gw.sendVariable(0, V_FORECAST, weather[forecast]);
lastForecast = forecast;
}
/*
DP/Dt explanation
0 = "Stable Weather Pattern"
1 = "Slowly rising Good Weather", "Clear/Sunny "
2 = "Slowly falling L-Pressure ", "Cloudy/Rain "
3 = "Quickly rising H-Press", "Not Stable"
4 = "Quickly falling L-Press", "Thunderstorm"
5 = "Unknown (More Time needed)
*/
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(1000); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); // sleep to conserve power
}
int sample(float pressure) {
// Algorithm found here
// http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
if (minuteCount > 180)
minuteCount = 6;
pressureSamples[minuteCount] = pressure;
minuteCount++;
if (minuteCount == 5) {
// Avg pressure in first 5 min, value averaged from 0 to 5 min.
pressureAvg[0] = ((pressureSamples[1] + pressureSamples[2]
+ pressureSamples[3] + pressureSamples[4] + pressureSamples[5])
/ 5);
} 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);
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
else
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);
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
else
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);
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
else
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);
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
else
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);
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
else
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);
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
else
dP_dt = (((65.0 / 1023.0) * change) / 4); // divide by 4 as this is the difference in time from 0 value
pressureAvg[0] = pressureAvg[5]; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
}
if (minuteCount < 35 && firstRound) //if time is less than 35 min on the first 3 hour interval.
return 5; // Unknown, more time needed
else if (dP_dt < (-0.25))
return 4; // Quickly falling LP, Thunderstorm, not stable
else if (dP_dt > 0.25)
return 3; // Quickly rising HP, not stable weather
else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05)))
return 2; // Slowly falling Low Pressure System, stable rainy weather
else if ((dP_dt > 0.05) && (dP_dt < 0.25))
return 1; // Slowly rising HP stable good weather
else if ((dP_dt > (-0.05)) && (dP_dt < 0.05))
return 0; // Stable weather
else
return 5; // Unknown
}
@@ -0,0 +1,69 @@
// Example sketch showing how to control physical relays.
#include <Relay.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 2
#define RELAY_ON 0
#define RELAY_OFF 1
Relay gw(9,10);
void setup()
{
Serial.begin(BAUD_RATE); // Used to write debug info
gw.begin(RADIO_ID);
// Register all sensors to gw (they will be created as child devices)
for (int i=0; i<NUMBER_OF_RELAYS;i++) {
gw.sendSensorPresentation(RELAY_1+i, S_LIGHT);
}
// Fetch relay status
for (int i=0; i<NUMBER_OF_RELAYS;i++) {
// Make sure relays are off when starting up
digitalWrite(RELAY_1+i, RELAY_OFF);
// Then set relay pins in output mode
pinMode(RELAY_1+i, OUTPUT);
// Request/wait for relay status
gw.getStatus(RELAY_1+i, V_LIGHT);
setRelayStatus(gw.getMessage()); // Wait here until status message arrive from gw
}
}
/*
* Example on how to asynchronously check for new messages from gw
*/
void loop()
{
if (gw.messageAvailable()) {
message_s message = gw.getMessage();
setRelayStatus(message);
}
}
void setRelayStatus(message_s message) {
if (message.header.messageType=M_SET_VARIABLE &&
message.header.type==V_LIGHT) {
int incomingRelayStatus = atoi(message.data);
// Change relay state
digitalWrite(message.header.childId, incomingRelayStatus==1?RELAY_ON:RELAY_OFF);
// Write some debug info
Serial.print("Incoming change for relay on pin:");
Serial.print(message.header.childId);
Serial.print(", New status: ");
Serial.println(incomingRelayStatus);
}
}
@@ -0,0 +1,96 @@
// Example sketch för a "light switch" where you can control light or something
// else from both vera and a local physical button (connected between digital
// pin 3 and +5V).
#include <Relay.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define RELAY_PIN 4 // Arduino Digital I/O pin number for relay
#define BUTTON_PIN 3 // Arduino Digital I/O pin number for button (with 10k pulldown)
#define CHILD_ID 1 // Id of the sensor child
#define RELAY_ON 0
#define RELAY_OFF 1
int buttVal;
int previousVal;
int state;
int switched = 0;
long pressTime = 0; // the last time the button pin was pressed
long debounce = 200; // the debounce time, increase if the output flickers
Relay gw(9,10);
void setup()
{
Serial.begin(BAUD_RATE); // Used to write debug info
gw.begin(RADIO_ID);
// Set buttonPin as Input
pinMode(BUTTON_PIN, INPUT);
buttVal = digitalRead(BUTTON_PIN); //read initial state
// Register all sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID, S_LIGHT);
// Make sure relays are off when starting up
digitalWrite(RELAY_PIN, RELAY_OFF);
// Then set relay pins in output mode
pinMode(RELAY_PIN, OUTPUT);
// Request/wait for relay status
gw.getStatus(CHILD_ID, V_LIGHT);
setRelayStatus(gw.getMessage()); // Wait here until status message arrive from gw
}
/*
* Example on how to asynchronously check for new messages from gw
*/
void loop()
{
if (gw.messageAvailable()) {
// ot new messsage from gw
message_s message = gw.getMessage();
setRelayStatus(message);
}
buttVal = digitalRead(BUTTON_PIN); // read input value and store it in val
if (buttVal == HIGH && previousVal == LOW) {
// Start counter from when button was pressed
pressTime = millis();
switched = 0;
}
if (buttVal == HIGH && switched == 0 && millis() - pressTime > debounce) {
// Switch state if button pressed more than 200 msec
state = state==1?0:1;
gw.sendVariable(CHILD_ID, V_LIGHT, state); // We will receive an ack message
switched = 1; // No more switches until button is released
}
previousVal = buttVal;
}
void setRelayStatus(message_s message) {
if (message.header.type==V_LIGHT) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE
state = atoi(message.data);
// Change relay state
digitalWrite(RELAY_PIN, state==1?RELAY_ON:RELAY_OFF);
// Write some debug info
Serial.print(message.header.messageType == M_ACK_VARIABLE ? "Button":"Gateway");
Serial.print(" change. New state: ");
Serial.println(state == 1 ?"on":"off" );
}
}
@@ -0,0 +1,31 @@
// Example sketch showing how to create a node thay relays messages
// from nodes far from gateway back to gateway.
// Important here is that node uses Relay-class and calls
// gw.messageAvailable() frequently. This sketch should not
// sleep.
#include <Relay.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
Relay gw(9,10);
void setup()
{
Serial.begin(BAUD_RATE); // Used to write debug info
gw.begin(RADIO_ID);
}
void loop()
{
if (gw.messageAvailable()) {
// Incoming message for this node...
}
}
@@ -0,0 +1,71 @@
// Example showing how to create an atuator for a servo.
// Connect red to +5V, Black or brown to GND and the last cable to Digital pin 3.
// The servo consumes much power and should probably have its own powersource.'
// The arduino might spontanally restart if too much power is used (happend
// to me when servo tried to pass the extreme positions = full load).
#include <Sensor.h>
#include <SPI.h>
#include <EEPROM.h>
#include <Servo.h>
#include <RF24.h>
#define SERVO_DIGITAL_OUT_PIN 3
#define SERVO_MIN 30 // Fine tune your servos min. 0-180
#define SERVO_MAX 180 // Fine tune your servos max. 0-180
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define CHILD_ID 10 // Id of the sensor child
Sensor gw(9, 10);
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created Sensor gw(9,10);
void setup()
{
Serial.begin(BAUD_RATE);
gw.begin(RADIO_ID);
myservo.attach(SERVO_DIGITAL_OUT_PIN);
// Register all sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID, S_COVER);
// Fetch servo state at startup
gw.getStatus(CHILD_ID, V_DIMMER);
setRelayStatus(gw.getMessage()); // Wait here until status message arrive from gw
}
void loop()
{
if (gw.messageAvailable()) {
// New messsage from gw
message_s message = gw.getMessage();
setRelayStatus(message);
}
}
void setRelayStatus(message_s message) {
if (message.header.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE
int val = atoi(message.data);
myservo.write(SERVO_MIN + (SERVO_MAX-SERVO_MIN)/100 * val); // sets the servo position 0-180
// Write some debug info
Serial.print("Servo changed. new state: ");
Serial.println(val);
} else if (message.header.type==V_UP) {
Serial.println("Servo UP command");
myservo.write(SERVO_MAX);
gw.sendVariable(CHILD_ID, V_DIMMER, 100);
} else if (message.header.type==V_DOWN) {
Serial.println("Servo DOWN command");
myservo.write(SERVO_MIN);
gw.sendVariable(CHILD_ID, V_DIMMER, 0);
} else if (message.header.type==V_STOP) {
Serial.println("Servo STOP command");
// Servo is pretty fast.. don't think we have time to send a stop
}
}
@@ -0,0 +1,47 @@
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define DIGITAL_INPUT_SOIL_SENSOR 3 // Digital input did you attach your soil sensor.
#define INTERRUPT DIGITAL_INPUT_SOIL_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 0 // Id of the sensor child
Sensor gw(9, 10);
Sleep sleep;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
pinMode(DIGITAL_INPUT_SOIL_SENSOR, INPUT); // sets the soil sensor digital pin as input
// Register all sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID, S_MOTION);
}
int lastSoilValue = -1;
void loop()
{
// Read digital soil value
int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR); // 1 = Not triggered, 0 = In soil with water
if (soilValue != lastSoilValue) {
Serial.println(soilValue);
gw.sendVariable(CHILD_ID, V_TRIPPED, soilValue==0?"1":"0"); // Send the inverse to gw as tripped should be when no water in soil
lastSoilValue = soilValue;
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepInterrupt(INTERRUPT,CHANGE);
}
@@ -0,0 +1,68 @@
// Example sketch showing how to request time from vera
// and how to set a cutom variable on a sensor
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
#include <Time.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define CHILD_ID 0 // Id of the sensor child
Sensor gw(9, 10);
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
// Register any sensortype. This example we just create a motion sensor.
gw.sendSensorPresentation(CHILD_ID, S_MOTION);
setSyncProvider(getTime);
setSyncInterval(3600); // get time from gw every hour
}
unsigned long getTime() {
return gw.getTime(); // Vera is time provider
}
void loop()
{
// long time = gw.getTime();
// setTime(time);
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
// Ok now send time back to gw and store in in Variable1
// of the motion sensor device
gw.sendVariable(CHILD_ID, V_VAR1, minute());
// wait 10 seconds...
delay(10000);
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
@@ -0,0 +1,151 @@
//
// Use this sensor to measure volume and flow of your house watermeter.
// You need to set the correct pulsefactor of your meter (pulses per m3).
// The sensor starts by fetching current volume reading from gateway (VAR 1).
// Reports both volume and flow back to gateway.
//
// Unfortunately millis() won't increment when the Arduino is in
// sleepmode. So we cannot make this sensor sleep if we also want
// to calculate/report flow.
//
// Sensor on pin 3
//
//
//
#include <Relay.h>
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
// Set RADIO_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a RADIO_ID for you.
#define RADIO_ID AUTO
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
#define PULSE_FACTOR 1000 // Nummber of blinks per m3 of your meter (One rotation/liter)
#define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
#define MAX_FLOW 40 // Max flow (l/min) value to report. This filetrs outliers.
unsigned long SEND_FREQUENCY = 20; // Minimum time between send (in seconds). We don't want to spam the gateway.
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 5 // Id of the sensor child
Sensor gw(9,10);
//enable sleep
Sleep sleep;
double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter
volatile unsigned long pulseCount = 0;
volatile unsigned long lastBlink = 0;
volatile double flow = 0;
unsigned long oldPulseCount = 0;
unsigned long newBlink = 0;
double oldflow = 0;
double volume;
double oldvolume;
unsigned long lastSend;
unsigned long lastPulse;
unsigned long currentTime;
boolean metric;
void setup()
{
Serial.begin(BAUD_RATE); // Used to type in characters
gw.begin(RADIO_ID);
// Register this device as Waterflow sensor
gw.sendSensorPresentation(CHILD_ID, S_WATER);
// Fetch last known pulse count value from gw
pulseCount = oldPulseCount = atol(gw.getStatus(CHILD_ID, V_VAR1));
//Serial.print("Last pulse count from gw:");
//Serial.println(pulseCount);
attachInterrupt(INTERRUPT, onPulse, RISING);
lastSend = millis();
}
void loop()
{
currentTime = millis();
// Only send values at a maximum frequency or woken up from sleep
if (SLEEP_MODE || currentTime - lastSend > 1000*SEND_FREQUENCY) {
// New flow value has been calculated
if (!SLEEP_MODE && flow != oldflow) {
// Check that we dont get unresonable large flow value.
// could hapen when long wraps or false interrupt triggered
if (flow<((unsigned long)MAX_FLOW)) {
gw.sendVariable(CHILD_ID, V_FLOW, flow, 2); // Send flow value to gw
}
//Serial.print("l/min:");
//Serial.println(flow);
oldflow = flow;
}
// No Pulse count in 2min
//Serial.print("currentTime");
//Serial.println(currentTime);
//Serial.print("lastPulse");
//Serial.println(lastPulse);
if(currentTime - lastPulse > 120000){
flow = 0;
}
// Pulse count has changed
if (pulseCount != oldPulseCount) {
gw.sendVariable(CHILD_ID, V_VAR1, pulseCount); // Send volumevalue to gw VAR1
double volume = ((double)pulseCount/((double)PULSE_FACTOR));
oldPulseCount = pulseCount;
//Serial.print("Pulse count:");
//Serial.println(pulseCount);
if (volume != oldvolume) {
gw.sendVariable(CHILD_ID, V_VOLUME, volume, 3); // Send volume value to gw
//Serial.print("m3:");
//Serial.println(volume, 3);
oldvolume = volume;
}
}
lastSend = currentTime;
}
if (SLEEP_MODE) {
delay(300); //delay to allow serial to fully print before sleep
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SEND_FREQUENCY * 1000); //sleep for: sleepTime
}
}
void onPulse()
{
if (!SLEEP_MODE) {
unsigned long newBlink = micros();
unsigned long interval = newBlink-lastBlink;
lastPulse = millis();
if (interval<500000L) {
// Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)
return;
}
flow = (60000000.0 /interval) / ppl;
lastBlink = newBlink;
//Serial.println(flow, 4);
}
pulseCount++;
}