Dear all,
first of all I want to thank you for contributing to this excellent forum.
Without it I would not have gotten so far with my automated greenhouse project.
Now, I want to document my project progress and need some help to tackle the last two issues I have:
- Connection/stability problems: Before I implemented the watchdog function (shown below) the device used to go offline in the Blynk app after a random time period (sometimes after 30 min, sometimes after day or more). After implementation of the watch dog I have restarts instead. I still want to determine if its a software issue or a hardware issue (power, antenna …).
Things that I have tried:
- Connect the step down converter to VIN/GND and attach a LiPo to the battery connector (problem remains)
- Reduce the sketch to the bare minimun, but still gsm connection via blynk.begin (problem remains)
- the sketch with gsm/blynk removed works flaweless over several days
- Webhook to openweathermap.org (The final goal is to automatically close the windows when a storm comes up).
Things that I have tried:
- There are a lot of examples in the forum for ESP8266 using Arduino_JSON.h and ESP8266HTTPClient.h. However those libs don’t work for my board.
- I found a working sketchfor my board (that neither uses a dedicated HTTPClient nor JSON module) but it does not uses blynk.begin to establish the gsm connection and I cant figure out how to combine it with by sketch. Any insights?
My Hardware:
- Arduino MKR GSM 1400 (powered by a 12V car batter via a small Small step down converter attached battery connector of the MKR GSM 1400 )
- GSM Antenna
- 2 Grove DHT11 modules
- 4 Channel relay (channel 1 and 2 connceted to two linear actuator that work as window openers; channel 3 controlls a magnetic valve to water the plants)
Software:
This is my current sketch:
// Set Blynk hertbeat interval (in seconds)
#define BLYNK_HEARTBdEAT 60
#define BLYNK_TEMPLATE_ID "XXX"
#define BLYNK_DEVICE_NAME "XXX"
#define BLYNK_AUTH_TOKEN "XXX"
#include "arduino_secrets.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = SECRET_AUTH_TOKEN;
#define BLYNK_PRINT Serial
#include <MKRGSM.h>
#include <BlynkSimpleMKRGSM.h>
#include "DHT.h"
#include <multi_channel_relay.h>
#include <WDTZero.h>
WDTZero MyWatchDoggy; // Define WDT
Multi_Channel_Relay relay;
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// Your access credentials.
// Set password to "" for open networks.
char pin[] = SECRET_PINNUMBER;
char apn[] = SECRET_GPRS_APN;
char user[] = SECRET_GPRS_LOGIN;
char pass[] = SECRET_GPRS_PASSWORD;
// ----------For DHT-------------
#define DHT1PIN 3 // Digital pin connected to the indoor DHT sensor
#define DHT2PIN 4 // Digital pin connected to the outdoor DHT sensor
#define DHT1TYPE DHT11 // DHT 11
#define DHT2TYPE DHT11 // DHT 11
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
BlynkTimer timer;
int latchButton;
int latchFlag;
//for connection/uptime managment
int DeviceLED = 6; // Define internal LED - will be used to indicate connection status
int ReCnctFlag; // Reconnection Flag
int ReCnctCount = 0; // Reconnection counter
// function to brake the window motor
void brake()
{
Serial.println("WindowOpener: Braking");
relay.turn_off_channel(1);
relay.turn_off_channel(2); //Deactivate both relays to brake the motor
}
// sequence to open the window
void openWindow()
{
timer.setTimeout(2000L, brake); // wait 2 seconds - potential cool down for relay than brake motor
timer.setTimeout(4000L, []() // wait further 2 seconds (i.e. 4 seconds in total) than initiate opening
{
Serial.println("WindowOpener: Opening sequence in progress");
relay.turn_on_channel(1);
relay.turn_off_channel(2); //Activate the relay one direction, they must be different to move the motor - forward
});
timer.setTimeout(29000L, brake); // wait further 25 seconds (i.e. 29 seconds in total) to complete opening than brake motor
}
// sequence to close the window
void closeWindow()
{
timer.setTimeout(2000L, brake); // wait 2 seconds - potential cool down for relay than brake motor
timer.setTimeout(4000L, []() // wait further 2 seconds (i.e. 4 seconds in total) than initiate closing
{
Serial.println("WindowOpener: Closing sequence in progress");
relay.turn_on_channel(2);
relay.turn_off_channel(1); //Activate the relay one direction, they must be different to move the motor - backward
});
timer.setTimeout(29000L, brake); // wait further 25 seconds (i.e. 29 seconds in total) to complete closing than brake motor
}
//===== Latching button attached to V1 =====
BLYNK_WRITE(V1) { // Button Widget set as switch
latchButton = param.asInt();
if (latchButton == 1 && latchFlag == 0) {
latchFlag = 1; // Keeps from allowing button press more then once while relay activated
Blynk.setProperty(V1, "isDisabled", true); // Keeps from allowing the widget button press more then once while relay activated
// ----- Start your timed thing here
openWindow();
// -----
timer.setTimeout(29000L, []() { // Timed Lambda Function - Latching Button release after 29 seconds
latchFlag = 0; // resets to allow next interaction
Blynk.setProperty(V1, "isDisabled", false);
}); // END Timer Function
} else {
if (latchButton == 0 && latchFlag == 0) {
latchFlag = 1; // Keeps from allowing button press more then once while relay activated
Blynk.setProperty(V1, "isDisabled", true); // Keeps from allowing the widget button press more then once while relay activated
// ----- Start your timed thing here
closeWindow();
// -----
timer.setTimeout(29000L, []() { // Timed Lambda Function - Latching Button release after 29 seconds
latchFlag = 0; // resets to allow next interaction
Blynk.setProperty(V1, "isDisabled", false);
}); // END Timer Function
}
}
}
// DHT sensors send
void sendSensor()
{
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
if (isnan(h1) || isnan(t1) || isnan(h2) || isnan(t2)) {
Serial.println("Failed to read from DHT sensor(s)!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Serial.println("Reading DHT sensor(s)");
Blynk.virtualWrite(V5, t1);
Blynk.virtualWrite(V6, h1);
Blynk.virtualWrite(V7, t2);
Blynk.virtualWrite(V8, h2);
}
// function called on shut down by watchdog
void myshutdown()
{
Serial.print("\nWe gonna shut down ! ...");
}
void setup()
{
int t = 20; //Initialize serial and wait for port to open, max 10 seconds
Serial.begin(9600);
while (!Serial) {
delay(500);
if ( (t--) == 0 ) break;
}
Serial.print("Setup Soft Watchdog at 32S interval");
MyWatchDoggy.attachShutdown(myshutdown);
MyWatchDoggy.setup(WDT_SOFTCYCLE32S); // initialize WDT-softcounter refesh cycle on 32sec interval
// Here your Arduino connects to the Blynk, through begin you can set all the access credential required to establish a connection with the app
Blynk.begin(auth, gsmAccess, gprs, client, pin, apn, user, pass);
dht1.begin();
dht2.begin();
// Set I2C address and start relay
relay.begin(0x11);
// Setup DHT sendSensor function to be called every 100 seconds
timer.setInterval(100000L, sendSensor);
// UpTime function to be called every minute
timer.setInterval(60000L, UpTime);
// UpTimeBlink function to be called every 1 seconds
timer.setInterval(1000L, UpTimeBlink);
// start with windows closed
closeWindow();
}
BLYNK_CONNECTED() {
Serial.println("Connected");
ReCnctCount = 0;
}
void UpTime() {
Blynk.virtualWrite(V0, millis() / 60000); // Send UpTime minutes to App
Serial.print("UpTime: ");
Serial.println(millis() / 60000); // Send UpTime seconds to Serial
Serial.print("minute(s)");
}
void UpTimeBlink() {
digitalWrite(DeviceLED, !digitalRead(DeviceLED)); // Blink onboard LED
}
void loop()
{
MyWatchDoggy.clear(); // refresh wdt - before it loops
//this start the blynk utilities and make run the iteration with the app
timer.run();
if (Blynk.connected()) { // If connected run as normal
Blynk.run();
} else if (ReCnctFlag == 0) { // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
ReCnctFlag = 1; // Set reconnection Flag
Serial.println("Starting reconnection timer in 30 seconds...");
timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function
ReCnctFlag = 0; // Reset reconnection Flag
ReCnctCount++; // Increment reconnection Counter
Serial.print("Attempting reconnection #");
Serial.println(ReCnctCount);
Blynk.connect(); // Try to reconnect to the server
}); // END Timer Function
}
}
Thank you for reading.