Irrigation relay with timer - esp8266 - WIP

Hi,
I needed to control an electric valve that turns on and off my (still to be built) drip irrigation system.
I have ran into a lot of limitations with the current build of Blynk for ios and arduino/esp but from what the Blynk guys said, a lot of stuff will be fixed in the new ios version (hurry up Apple).
Just few things that annoyed me/couldn t get to work properly, quite possibly because of me missing stuff (any pointers welcome):
-you can t seem to be able to write the state of a button the same way you can write the state of a led widget (i turn off the relay, i turn off the widget led, i d like to turn off the button as well)
-the experimental virtualRead seems to do what s needed but it resets the position of the thumb on the slider widgets
-can t configure the size of led/button/slider (ie, i d like a large led because i don t have anything else to fill the space with, or a 3/4 screen width slider
-blynk lib manages the wifi - i have my own lib for that

Here s how it looks so far


and here is the code

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social groups:              http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example runs directly on ESP8266 chip.
 *
 * You need to install this for ESP8266 development:
 *   https://github.com/esp8266/Arduino
 *
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define BLYNK_EXPERIMENTAL

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <EEPROM.h>
#include <ESP8266mDNS.h>
#include <WifiManager.h>

#include <SimpleTimer.h>


WiFiManager wifi(0);

SimpleTimer timer;
int timerId;

boolean started = false;
long wateringStart = 0;
long wateringLength = 0;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "BLYNK AUTH CODE";

void setup()
{

  Serial.begin(115200);
  wifi.autoConnect("Blynk");
  String ssid = wifi.getSSID();
  String pass = wifi.getPassword();

  Blynk.begin(auth, ssid.c_str(), pass.c_str());
  //Blynk.begin(auth, ssid.c_str(), "");

  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);

  timer.setInterval(1000, heartBeat);

  Serial.println("done setup");
}


void loop()
{
  Blynk.run();
  timer.run();
  if (!started && Blynk.connected()) {
    started = true;
    for (int i = 0; i < 7; i++) {
      Blynk.virtualWrite(1, i % 2);
      delay(200);
    }
  }
}

void heartBeat() {
  if ( wateringLength != 0 ) {
    if ((wateringStart + wateringLength - millis()) < 1000) {
      //wateringLength = 0;
      Serial.println("done watering");
      digitalWrite(5, LOW);
      wateringStart = 0;
      int i = digitalRead(5);
      Blynk.virtualWrite(1, i);
    }
  }
}

// This takes the value of virtual pin 0 in blynk program...  if it is 1 turns on, else off...
BLYNK_WRITE(0) {
  int a = param.asInt();
  if (a == 0) {
    digitalWrite(5, LOW);

    wateringStart = 0;
  } else {
    digitalWrite(5, HIGH);
    if (wateringStart == 0) {
      wateringStart = millis();
      //Blynk.virtualRead(6);
    }
  }
  Blynk.virtualWrite(1, a);
  Serial.println("write");
}

//// This publishes the int power back to the blynk program...

BLYNK_READ(2) {
  //Serial.println("read");

  long uptime = millis() / 1000 / 60;
  Blynk.virtualWrite(2, uptime);
}

BLYNK_WRITE(3) {
  int a = param.asInt();
  if (a != 0) {
    Serial.print("refresh");
    int i = digitalRead(5);
    Blynk.virtualWrite(1, i);
  }
}

BLYNK_READ(4) {
  //Serial.println("read watering interval");

  long watering = 0;
  if ( wateringStart != 0 ) {
    watering = (millis() - wateringStart) / 1000 ;
  }

  Blynk.virtualWrite(4, watering);
}

//read timer left
BLYNK_READ(5) {
  //Serial.println("read");

  long remaining = 0;
  if ( wateringLength != 0 && wateringStart != 0) {
    remaining = (wateringStart + wateringLength - millis()) / 1000 ;
  } else {
    remaining = wateringLength / 1000;
  }

  Blynk.virtualWrite(5, remaining);
}


BLYNK_WRITE(6) {
  //Serial.println("write watering length");
  long a = param.asLong();
  Serial.print("write legth");
  wateringLength = a * 1000;
}


BLYNK_WRITE(20) {
  int a = param.asInt();
  if (a == 0) {
    Serial.println("reset");
    ESP.reset();
  }
}
1 Like

Hi,
great idea and good work. i tried to run your code on my ESP-01 module but i get a lot of errors when compiling. Which version of the Arduino ESP8266 are you using? I have the Arduino 1.6.1 installed.
I get message s like:
irrigation_relay.ino: In function ‘void loop()’:
irrigation_relay.ino:75:25: error: ‘class BlynkWifi’ has no member named ‘connected’
irrigation_relay.ino: In function ‘void BlynkWidgetWrite6(BlynkReq&, const BlynkParam&)’:
irrigation_relay.ino:161:18: error: ‘const class BlynkParam’ has no member named ‘asLong’

What am i doing wrog?
Thanks for a short reply.

HI,
It looks like you need the latest Blynk lib, the one that has the connected function.

I am using the latest arduino 1.6.4 with the esp support installed through Boards Manager.

Also, the libraries i have added, i ve just checked out from github straight in my libraries folder

and my own GitHub - tzapu/WiFiManager: ESP8266 WiFi Connection manager with web captive portal

I hope this helps

First of all thanks a lot for your reply. I did exactly as you suggested. I downloaded Arduino 1.6.4. Installed the libs as you proposed into the libraries folder. and compiled the sketch. Let us say i have fewer errors but there are some as:

In file included from /Users/voyager/Documents/Arduino ESP8266 BLYNK/libraries/blynk/BlynkSimpleEsp8266.h:15:0,
from irrigation_relay.ino:29:
/Users/voyager/Documents/Arduino ESP8266 BLYNK/libraries/blynk/BlynkApiArduino.h:44:6: warning: always_inline function might not be inlinable [-Wattributes]
void BlynkApi::processCmd(const void* buff, size_t len)
^
In file included from /Users/voyager/Documents/Arduino ESP8266 BLYNK/libraries/blynk/BlynkSimpleEsp8266.h:16:0,
from irrigation_relay.ino:29:
/Users/voyager/Documents/Arduino ESP8266 BLYNK/libraries/blynk/Blynk/BlynkProtocol.h:161:6: warning: always_inline function might not be inlinable [-Wattributes]
void BlynkProtocol::processInput(void)
^

Der Sketch verwendet 218.760 Bytes (41%) des Programmspeicherplatzes. Das Maximum sind 524.288 Bytes.

Well i still need some help if possible.

Thanks

BTW i use the Generic ESP8266 Module and i am on OS X Yosemite

hi, those seem to be just warnings, the sketch should work if uploaded to the ESP

another annoying thing that will probably be updated soon
-can t reuse pins between multiple widgets. i d like to have both a button and the new timer trigger (and update the state in app, either automatically or i can do it in code once i can write button states) for the same virtual pin. i guess actually, what matters is the ability to control widget states from the arduino side, then the rest can be easily sorted by just using a common set of functions

played with timer support now that the new ios version is out

push notifications on the mobile side are not implemented yet, sure would of been nice

Okay, i downloaded to my ESP after creating a new project. I changed the auth token to mine and started the module. This is what i get:

AutoConnect
Reading EEPROM SSID
SSID:
��������������������������������
Reading EEPROM Password
Password:
����������������������������������������������������������������
Waiting for Wifi to connect
.
.
.

Could not connect to WiFi
Started Soft Access Point

WiFi connected
0.0.0.0
192.168.4.1
mDNS responder started
Server started

Do wifi.getSSID(); and wifi.getPassword(); fetch the values from the EEPROM ?These functions are not mentioned in Arduino ESP8266. I think these values are the ones from my Wlan. On what addresses are they stored and what is their length? 32 for said and 64 for pass?

Got the ssid and password storage. Stored ssid @ 0, len 32 and pass at 33 len 64 in EPROM. Program now reads my values and runs up to ‘done setup’ and then nothing. I’ll go on trying.

hi,
wifi.getSSID(); and wifi.getPassword(); are actually part of my wifimanager library.

when you first started it it read random values from eeprom and started an access point ‘Blynk’ that you could connect to and configure the wifi from the web interface
There s a short (still) description of how it works here: https://github.com/tzapu/WiFiManager

You could bypass the wifi library complelty and jsut pass your wifi and ssid hardcoded to the blynk begin function, or try to reconfigure it using the web portal it starts. be sure to also setup your auth token.

cheers

Hi,
how reliable is your hard- and software. I programmed a RCSwitch system using your module as the base. It works normally for a maximum of two or three days and then it stops. I’ve tried it with several ESP modules, but i always get the same result. Is it BLYNK or the ESPs? I am running a same RCSwitch system based on the NETIO App and that is running perfect.
Or do you have some ideas?

Not reliable at all it seems, i m away for this week but will experiment more when back

@voyager

It is hard to say. Investigation required. I think @vshymanskyy is already working on it, as ESP support was added just recently it may have some problems, but it could also be a hardware issue. Regarding Arduino boards and shields - they are working perfectly during long term run.

@Dimitry
Reliability
Longterm Tests with my ESP modules running programs based on Arduino IDE, NodeMcu and Sming showed that the hardware is running fine. So there must be some disturbence in the Blynk modules. Major problem is that the Blynk module looses the connection too often while the other systems remain stable.

@voyager

could you share your code? minimum required block in order to reproduce.

@Dmitriy this seems to do it for me:
-1 button on V3 and 1 led widget on V1
-mash the refresh button about 5 times and it goes into something like:

[43387] Sent 0/12
refresh
[43389] Connecting to cloud.blynk.cc:8442
[43811] Ready!
refresh
refresh

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x40100000, len 28780, room 16 
tail 12
chksum 0x08
ho 0 tail 12 room 4
load 0x3ffe8000, len 1564, room 12 
tail 0
chksum 0xdb
load 0x3ffe8620, len 3340, room 8 
tail 4
chksum 0x5c
csum 0x5c
{$[244] Blynk v0.2.2-beta

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include "Config.h"


boolean lit = false;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

void setup()
{

  Serial.begin(115200);



  Blynk.begin(auth, ssid.c_str(), pass.c_str());
  Serial.println("done setup");
}


void loop()
{
  Blynk.run();
}



//REFRESH BUTTON v3
BLYNK_WRITE(3) {
  lit = !lit;
  Blynk.virtualWrite(1, lit);
  Serial.println("refresh");
}

this is just a simple sample, in a more complicated project what i think happens is when the ios app/project starts it may be triggering too many read and writes which leads to a similar situation to this: sent x/12…watch doog restart

source was changed to a new level.

Guys, thank you all for your feedback. We are working already on ESP8266 issues. We will update you when we’ll fix it.

@tzapulica, @voyager, @Minc, …
Please check again the latest master branch: https://github.com/blynkkk/blynk-library
Ensure you have the latest build of ESP8266 for Arduino IDE (I’ve got 1.6.4-835-g77d77e8).
Stability should have improved (For the ESP8266 standalone application).

If the module is rebooting - check it’s power supply. A stable source of 3.3v 500mA is recommended.

BTW, I successfully run PWM (checked pins 0 and 2), HC-SR04, Servo with ESP8266 and of course, Blynk :wink:

1 Like

@vhymanskyy
since i downloaded arduino 1.6.4 and the new blynk-library my prog is not working any more. No sign of life on the serial port. Cannot get the serial output to work and the ESP-01 is not starting any sketch compiled with the new system. Blynk with Arduino boards work.