Now that I’ve got the segmented program running nicely I’m starting to add the other features that (with a great deal of help) I worked out earlier. One of them was a timer. This was necessitated because in our remote condition we share an internet connection. In my early test version last summer there were complaints that my system was using too much data. That was because the sensor was connected to Blynk 24/7.
I really didn’t need constant monitoring, so I incorporated a millis program that would connect to the internet briefly every ten minutes. That appears to cut down on the internet use a lot. I’ll attach the program at the bottom of this message.
Now that I have the segmented switch program I want to incorporate that timer into the program. Since there is no limit on how often the transmitter and the receiver communicate I don’t see any need in incorporating the timer into the void take_a_reading() loop. My thoughts are it should be incorporated into the BLYNK_WRITE(V1). I guess my question is can it be applied once to cover the four cases, or does it have to be added to each case? Or is there a more elegant way of incorporating this?
/*
* For this example you'll need the following library:
* *
* 1) Arduino-LiquidCrystal-I2C-library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
*
* 2) Blynk Library: https://github.com/blynkkk/blynk-library
*
* Conncetions:
* D1 -> SCL of I2C LCD
* D2 -> SDA of I2C LCD
* D3 -> Out of DHT11/22
*
*/
#include <LoRa.h>
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define I2C_SDA D3 //redefining I2C pins as D1 is needed by LoRa unit
#define I2C_SCL D4
#define ss 15
#define rst 16
#define dio0 2
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long interval = 1000;
const int ledPin = D1; //warning light that water has dropped beyone preset level
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Enter the Auth code which was send by Blink
char ssid[] = "xxxxxxxxxx"; //Enter your WIFI Name
char pass[] = "xxxxxxxxxx"; //Enter your WIFI Password
void setup()
{
pinMode(ledPin,OUTPUT);
Wire.pins(D3, D4);
//Serial.begin(9600);
lcd.begin(); // iInit the LCD for 16 chars 2 lines
lcd.backlight(); // Turn on the backligt (try lcd.noBaklight() to turn it off)
delay (1000);
Blynk.begin(auth, ssid, pass);
Serial.println("LoRa Sender");
LoRa.setPins(ss, rst, dio0);
if (!LoRa.begin(433E6))
{
Serial.println("Starting LoRa failed!");
delay(100);
while (1);
}
Serial.println("LoRa Started");
lcd.clear();
lcd.print("Waiting");
LoRa.setSpreadingFactor(10);
LoRa.setSignalBandwidth(62.5E3);
LoRa.crc();
}
void loop()
{
// try to parse packet
int packetSize = LoRa.parsePacket();
int targetDistance = LoRa.parseInt();
if (packetSize)
{
//Serial.print("distance is ");
// Serial.print(targetDistance);
// Serial.print(" inches ");
// Serial.println(" ");
lcd.setCursor(0,0); //First line
lcd.print("water is down");
lcd.setCursor(0,1); //Second line
lcd.print((targetDistance - 4));
lcd.print(" inches");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Blynk.connect();
Blynk.run(); // Initiates Blynk
Blynk.virtualWrite(V1,(targetDistance - 4));
interval = 10 * 1000UL;
}
else {
Blynk.disconnect();
interval = 10 * 60 * 1000UL;
}
delay(1000);
{
if ((targetDistance - 4) <= 25 )
{
digitalWrite(ledPin,LOW);
}
else
{
digitalWrite(ledPin,HIGH);
}
}
}
}