After some hours of sleep and refactoring everything, now I think I've managed to get everything working.
I have some problem wrapping my head around "forceTransmit" command and how it works.
Also, millis() is also something I have to learn
Here is the code if anyone finds it interesting.
What it does is measure soil moisture every hour and sends info (normal forkthingie).
Also reading every 30 seconds for temperature and humidity (si7021)
If humidity changes by 1 or temperature by 0.2 it will send info.
If no info has been sent it will force send info every 20 minutes.
3 Relays are there to power water pumps and a LED strip.
i have now semi-learned the use of functions, which is fun
cheers!
/**
* 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-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/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
//Network
#define MY_NODE_ID 62
#define MY_PARENT_NODE_ID 0
#define MY_PARENT_NODE_IS_STATIC
#define MY_RADIO_NRF24
//Includes
#include <SPI.h>
#include <MySensors.h>
#include <SI7021.h>
#include <RunningAverage.h>
//Define Relay
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_2 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_3 5 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 3 // 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
#define RELEASE "1.4"
//Define Moisture things
#define AVERAGES 2
// Child sensor ID's
#define CHILD_ID_TEMP 5
#define CHILD_ID_HUM 6
#define CHILD_ID_MOISTURE 7
// How many milli seconds between each measurement of temp and humidity
#define MEASURE_INTERVAL 30000
// FORCE_TRANSMIT_INTERVAL, this number of times of wakeup, the sensor is forced to report all values to the controller
#define FORCE_TRANSMIT_INTERVAL 2000 // 20minutes
// HUMI_TRANSMIT_THRESHOLD tells how much the humidity should have changed since last time it was transmitted. Likewise with
// TEMP_TRANSMIT_THRESHOLD for temperature threshold.
#define HUMI_TRANSMIT_THRESHOLD 1.0
#define TEMP_TRANSMIT_THRESHOLD 0.2
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
#define STABILIZATION_TIME 1000 // Let the sensor stabilize before reading
SI7021 humiditySensor;
// Sensor messages
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgMoist(CHILD_ID_MOISTURE, V_HUM);
MyMessage msgR1(RELAY_1, V_STATUS);
MyMessage msgR2(RELAY_2, V_STATUS);
// Global settings
const unsigned long tUpdateTemp = 5000; // update interval
unsigned long t0;
const unsigned long tUpdateMoist = 360000; // update interval
unsigned long t1;
int measureCount = 0;
boolean isMetric = true;
boolean highfreq = true;
boolean transmission_occured = false;
// Storage of old measurements
float lastTemperature = -100;
int lastHumidity = -100;
int oldMoistureLevel = -1;
//Change direction on moisture
byte direction = 0;
//Moisture sensor pins
RunningAverage raHum(AVERAGES);
const int SENSOR_ANALOG_PINS[] = {A0, A1}; // Sensor is connected to these two pins. Avoid A3 if using ATSHA204. A6 and A7 cannot be used because they don't have pullups.
void before()
{
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++)
{
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void setup()
{
Serial.begin(115200);
humiditySensor.begin();
isMetric = getControllerConfig().isMetric;
Serial.print(F("isMetric: ")); Serial.println(isMetric);
raHum.clear();
t0=millis();
t1=millis();
//sendTempHumidityMeasurements(true);
//sendMoistureMeasurements();
delay(250);
for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++)
{
pinMode(SENSOR_ANALOG_PINS[i], OUTPUT);
digitalWrite(SENSOR_ANALOG_PINS[i], LOW);
}
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("RelayTempHumMoist", "1.0");
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++)
{
// Register all sensors to gw (they will be created as child devices)
present(sensor, S_BINARY,"Relays");
}
present(CHILD_ID_TEMP,S_TEMP,"Temperature");
present(CHILD_ID_HUM,S_HUM,"Humidity");
present(CHILD_ID_MOISTURE, S_HUM,"Moisture");
}
void loop()
{
measureCount ++;
bool forceTransmit = false;
transmission_occured = false;
if (measureCount > FORCE_TRANSMIT_INTERVAL)
{
forceTransmit = true;
measureCount = 0;
Serial.print(F("inne i loopen :"));
}
if ((millis() - t1) > tUpdateMoist) sendMoistureMeasurements();
sendTempHumidityMeasurements(forceTransmit);
}
void sendTempHumidityMeasurements(bool force)
{
wait(MEASURE_INTERVAL);
bool tx = force;
si7021_env data = humiditySensor.getHumidityAndTemperature();
raHum.addValue(data.humidityPercent);
float diffTemp = abs(lastTemperature - (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths)/100.0);
float diffHum = abs(lastHumidity - raHum.getAverage());
Serial.print(F("TempDiff :"));Serial.println(diffTemp);
Serial.print(F("HumDiff :"));Serial.println(diffHum);
t0 = millis();
if (isnan(diffHum)) tx = true;
if (diffTemp > TEMP_TRANSMIT_THRESHOLD) tx = true;
if (diffHum > HUMI_TRANSMIT_THRESHOLD) tx = true;
if (tx)
{
measureCount = 0;
float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths) / 100.0;
int humidity = data.humidityPercent;
Serial.print("T: ");Serial.println(temperature);
Serial.print("H: ");Serial.println(humidity);
send(msgTemp.set(temperature,1));
send(msgHum.set(humidity));
lastTemperature = temperature;
lastHumidity = humidity;
transmission_occured = true;
}
}
void sendMoistureMeasurements()
{
pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor
analogRead(SENSOR_ANALOG_PINS[direction]);// Read once to let the ADC capacitor start charging
sleep(STABILIZATION_TIME);
int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PINS[direction]));
// Turn off the sensor to conserve battery and minimize corrosion
pinMode(SENSOR_ANALOG_PINS[direction], OUTPUT);
digitalWrite(SENSOR_ANALOG_PINS[direction], LOW);
direction = (direction + 1) % 2; // Make direction alternate between 0 and 1 to reverse polarity which reduces corrosion
// Always send moisture information so the controller sees that the node is alive
// Send rolling average of 2 samples to get rid of the "ripple" produced by different resistance in the internal pull-up resistors
// See http://forum.mysensors.org/topic/2147/office-plant-monitoring/55 for more information
if (oldMoistureLevel == -1)
{ // First reading, save value
oldMoistureLevel = moistureLevel;
}
send(msgMoist.set((moistureLevel + oldMoistureLevel + 0.5) / 2 / 10.23, 1));
oldMoistureLevel = moistureLevel;
t1 = millis();
}
void receive(const MyMessage &message)
{
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_STATUS)
{
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}```