Tag Archives: Arduino

MySensors Relay Sketch

Using an array rather than the incremental value that is now published on the mysensors.org website. This array method was used in the older version of the relay sketch before the upgrade to version 2.0. In order for upgrading from 1.5 to 2.0 for the relay boxes I’ve build it is far easier to stick to the the array method as most have an single relay board with eight relays on it which doesn’t fit in the current sketch any more.

/*
 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 *
 * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
 * Copyright (C) 2013-2019 Sensnology AB
 * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 *******************************
 *
 * REVISION HISTORY
 * Version 1.0 - Henrik Ekblad
 *
 * DESCRIPTION
 * Example sketch showing how to control physical relays.
 * This example will remember relay state after power failure.
 * http://www.mysensors.org/build/relay
 *
 *******************************
 *
 * UPDATES
 * Version 1.1 - Eugene Dullaard
 *
 * DESCRIPTION
 * This version uses an array of defined PINs like the v1 example
 * sketch did, instead of a startpin and counting from there.
 * The example is currently setup for Arduino Nano with 12 Relays
 * configured. Debugging and repeater is turned off.
 */

// Enable debug prints to serial monitor
//#define MY_DEBUG

// Enable and select radio type attached
#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95

// Enable repeater functionality for this node
//#define MY_REPEATER_FEATURE

#include <MySensors.h>;

/* Just adjust the number of relays to your need, the Maximum is twelve
 * There is no need to adjust the amount in the RELAY variable inless
 * you want to change the order in pins as used in your project.
 * As a reminder pins A6 and A7 are analog only and cannot be used.
 */
 
const int RELAY[] = {3, 4, 5, 6, 7, 8, A0, A1, A2, A3, A4, A5}; // I/O pins for the relays
#define NUMBER_OF_RELAYS 12 // Total number of attached relays
#define RELAY_ON 0  // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay


void before()
{
    for (int sensor=1, pin=0; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(RELAY[pin], OUTPUT);
        // Set relay to last known state (using eeprom storage)
        digitalWrite(RELAY[pin], loadState(sensor)?RELAY_ON:RELAY_OFF);
    }
}

void setup()
{

}

void presentation()
{
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Relay", "1.1");

    for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
        // Register all sensors to gw (they will be created as child devices)
        present(sensor, S_BINARY);
    }
}


void loop()
{

}

void receive(const MyMessage &message)
{
    // We only expect one type of message from controller. But we better check anyway.
    if (message.getType()==V_STATUS) {
        // Change relay state
        digitalWrite(RELAY[message.getSensor()-1], message.getBool()?RELAY_ON:RELAY_OFF);
        // Store state in eeprom
        saveState(message.getSensor(), message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.getSensor());
        Serial.print(", New status: ");
        Serial.println(message.getBool());
    }
}

ATTiny85 One Button Timer

For if you have a need. Single switch button with three LEDs to indicate the amount of time you want to set. In the configuration you can set the time per unit (20 minutes in the code below). After turning it on, the three LEDs should start to burn so to check they work, then when pressing the button it will start the time. You can at anytime adjust the time which will then be reset to the amount of time you want. If you want to decrease the time you have to cycle until the end after which it will go back to the lowest setting. ButtonTimerTotal times works in the same way as binary, given the value in the code is 20, the first LED is 20, the second 40(2x) and the last 80(4x). Now by pressing the button you can choose between 20 to 140 minutes of delay before it switches off the timer.  There is also a random time function which is activated when all LEDs are turned off, this will pick a random time between 40(2*) and 140(7*). As the amount of i/o is limited I’ve used an ATTiny85 rather than a complete Arduino, by modifying which pins it is using the code can be used on an Arduino as well.

// Button Timer 1.0 for ATTiny85
// Update 1.1 : Added 0 (no LEDS) as a random timer with a pre-defined minimum and maximum of 7

// Control a device through relay or optocoupler for a set period of time.
// Time can be adjusted during operation, cycle through all option before
// returning to lower values again, following a binary like display.

// Global Initialization

int Timer = 20;       // Default timer value (lowest and incremental factor) = X
int TimerInc = Timer; // Incremental Timer value to calculate Timer changes
int TimeLED[3];       // LEDs indicating time (X, X*2, X*4)
int TimeLEDSP = 0;    // First of the three Pins for the TimeLEDs,
int TimeInSecs;       // Timer expiration counter
int TimerButton = 3;  // Button to change time
int TimerRelay = 4;   // Relay that is on for duration of Timer
int RandomOn = 0;	  // 0 = Random off; 1 = Random on
int RandomMin = 40;   // Minimum random time in minutes

void setup() {
  int SetupTMP;       // Temp Var within Setup
  int SetupLoop = 1;  // Loop Var within Setup

  // Mapping PINs and setting them to either input or output
  for (SetupTMP = 0; SetupTMP < 3; SetupTMP++) {
    TimeLED[SetupTMP] = SetupTMP + TimeLEDSP;
    pinMode(TimeLED[SetupTMP], OUTPUT);
  }
  pinMode(TimerButton, INPUT);
  pinMode(TimerRelay, OUTPUT);
  
  //seed random number generator with noise from an unconnected pin
  randomSeed(analogRead(A0));

}

void loop() {
  int MainTMP;        // Temp Var within Main
  TimeInSecs = 60 * Timer;
  
  // Test Procedure, run all lights wait for TimerButton push, after push TimerRelay turns on
  for (MainTMP = 0; MainTMP < 3; MainTMP++) {
    digitalWrite(TimeLED[MainTMP], HIGH);
  }
  MainTMP = 1;
  
  while (MainTMP == 1) {
    delay(1000);
    if (digitalRead(TimerButton) == HIGH) {MainTMP = 0;}
  }

  for (MainTMP = 0; MainTMP < 3; MainTMP++) {
    digitalWrite(TimeLED[MainTMP], LOW);
  }
  digitalWrite(TimerRelay, HIGH);

  // Main Loop, wait until time runs out or button is pressed to increase time (one sec interval)
  while (TimeInSecs > 0) {

    delay(1000);        // 1 Second delay
    TimeInSecs--;
    
    // Button is pressed, add and recalculate time, if max is reached go to 0/random value
    if (digitalRead(TimerButton) == HIGH) {
	  if (RandomOn == 1) {
	    Timer = 0;
		RandomOn = 0;
	  }
      Timer += TimerInc;
      if (Timer > (TimerInc * 7)) {Timer = 0;}
      if (Timer > 0) {TimeInSecs = 60 * Timer;}
	  else { 
	    TimeInSecs = (random(RandomMin, (7 * Timer)) * 60);
		RandomOn = 1;
	  }
    }

    // Show Time through LEDs
    MainTMP = Timer;
    if (MainTMP >= (TimerInc * 4)) {
      digitalWrite(TimeLED[2], HIGH);
      MainTMP -= (TimerInc * 4);
    }
    else {
      digitalWrite(TimeLED[2], LOW);
    }
    if (MainTMP >= (TimerInc * 2)) {
      digitalWrite(TimeLED[1], HIGH);
      MainTMP -= (TimerInc * 2);
    }
    else {
      digitalWrite(TimeLED[1], LOW);
    }
    if (MainTMP >= TimerInc) {
      digitalWrite(TimeLED[0], HIGH);
      MainTMP -= TimerInc;
    }
    else {
      digitalWrite(TimeLED[0], LOW);
    }
    
  }

  // Turn relay off after timer has expired
  digitalWrite(TimerRelay, LOW);

}

One Button Timer Build

My own build on a little bit of perfboard and put together with a relay in the smallest box I had lying around.

Testing FC-28 Hygrometer with Arduino

A simple sketch to check what the values are that are recorded on the analog pins of one or more FC-28 Hygrometer(s), it might work with any other sensor that delivers analog output as well. In respect to the Hygrometer you might record the values it gives when dry and when soaked/wet. Then you can use those to calculate the trigger values for your own sketch. These units tend to corrode which does affect the values produced so this sketch can be used to recalculate them.

fc28fc28sketch

//Sketch to test for analog hygrometer values with FC28 Hygrometer
//for one or more FC28 units. Output is visible through serial monitor.

//This code is free to use.
//Author : Eugene Dullaard
//https://eugene.dullaard.nl/?p=690

//Setting variables
int analogStartPin = 0;  //Analog Start Pin, usually 0 for A0.
int units = 3;           //Amount of FC28 units to test,
                         //these units need to be connected on
                         //the analog ports following StartPin.
//temp variables
int x;
int y;

void setup (){
  Serial.begin(9600);
  Serial.println("Soil moisture sensor input");
}

void loop ()
{
  Serial.println();

  for (x = analogStartPin; x < analogStartPin + units; x++) {
    y=analogRead(x);
    Serial.print("Analog "); Serial.print(x); Serial.print(":");
    Serial.println(y);
  }

  delay (1000);
}