Display only one sensor (of several) attached to one device

thank you Pete. I’ll test it and if there’s any problems send you the data as before.
Jeff

Hi Pete:
Program compiles fine. Now the buttons work, but they ONLY display the offsets ( 0, -4,-8, -12). The attached LCD display that use to work and display the correct depth, now doesn’t work at all.

Here’s the data from the serial moniter.

15:55:28.819 -> [2139] Connecting to kuggle's_condo_2
15:55:28.865 -> [2140] Connected to WiFi
15:55:28.865 -> [2140] IP: 192.168.0.37
15:55:28.912 -> [2140] 
15:55:28.912 ->     ___  __          __
15:55:28.959 ->    / _ )/ /_ _____  / /__
15:55:28.959 ->   / _  / / // / _ \/  '_/
15:55:29.006 ->  /____/_/\_, /_//_/_/\_\
15:55:29.006 ->         /___/ v1.0.0 on NodeMCU
15:55:29.053 -> 
15:55:29.053 -> [2256] Connecting to blynk-cloud.com:80
15:55:29.330 -> [2650] Ready (ping: 230ms).
15:55:29.377 -> connected
15:55:29.471 -> Blynk run 1
15:55:29.471 -> Selected tank = 0
15:55:29.471 -> Actual reading = 0
15:55:29.518 -> Adjustment factor = 0
15:55:29.518 -> Adjusted reading = 0
15:55:29.566 -> Blynk run 2
15:55:29.566 -> Blynk run 3
15:55:29.752 -> [3069] Disconnected
15:55:29.752 -> blynk disconnected
15:55:46.938 -> [20255] Connecting to blynk-cloud.com:80
15:55:47.217 -> [20536] Ready (ping: 109ms).
15:55:47.263 -> connected
15:55:47.357 -> Blynk run 1
15:55:47.357 -> Selected tank = 4
15:55:47.357 -> Actual reading = 0
15:55:47.403 -> Adjustment factor = 12
15:55:47.403 -> Adjusted reading = -12
15:55:47.450 -> Blynk run 2
15:55:47.450 -> Blynk run 3
15:55:47.820 -> [21141] Disconnected
15:55:47.820 -> blynk disconnected
15:56:04.922 -> [38255] Connecting to blynk-cloud.com:80
15:56:05.342 -> [38677] Ready (ping: 227ms).
15:56:05.436 -> connected
15:56:05.483 -> Blynk run 1
15:56:05.483 -> Selected tank = 1
15:56:05.530 -> Actual reading = 0
15:56:05.530 -> Adjustment factor = 0
15:56:05.576 -> Adjusted reading = 0
15:56:05.576 -> Blynk run 2
15:56:05.576 -> Blynk run 3
15:56:05.715 -> [39061] Disconnected
15:56:05.762 -> blynk disconnected
15:56:22.952 -> [56255] Connecting to blynk-cloud.com:80
15:56:23.370 -> [56673] Ready (ping: 202ms).
15:56:23.417 -> connected
15:56:23.463 -> Blynk run 1
15:56:23.510 -> Selected tank = 2
15:56:23.510 -> Actual reading = 0
15:56:23.557 -> Adjustment factor = 4
15:56:23.557 -> Adjusted reading = -4
15:56:23.603 -> Blynk run 2
15:56:23.603 -> Blynk run 3
15:56:23.743 -> [57083] Disconnected
15:56:23.790 -> blynk disconnected
15:56:40.918 -> [74255] Connecting to blynk-cloud.com:80
15:56:41.293 -> [74617] Ready (ping: 226ms).
15:56:41.339 -> connected
15:56:41.433 -> Blynk run 1
15:56:41.433 -> Selected tank = 2
15:56:41.479 -> Actual reading = 0
15:56:41.479 -> Adjustment factor = 4
15:56:41.526 -> Adjusted reading = -4
15:56:41.526 -> Blynk run 2
15:56:41.526 -> Blynk run 3
15:56:41.899 -> [75208] Disconnected
15:56:41.899 -> blynk disconnected
15:56:58.917 -> [92255] Connecting to blynk-cloud.com:80
15:56:59.289 -> [92620] Ready (ping: 208ms).
15:56:59.383 -> connected
15:56:59.429 -> Blynk run 1
15:56:59.429 -> Selected tank = 2
15:56:59.475 -> Actual reading = 0
15:56:59.475 -> Adjustment factor = 4
15:56:59.521 -> Adjusted reading = -4
15:56:59.521 -> Blynk run 2
15:56:59.567 -> Blynk run 3
15:56:59.707 -> [93023] Disconnected
15:56:59.707 -> blynk disconnected

and here’s the sketch

/*
 * 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

int adjustment;
int adjusted_reading;
int tank_to_read;

const int ledPin = D1; //warning light that water has dropped beyone preset level
char auth[] = "xxxxxxxxxxxxxxxxxxxxx"; //Enter the Auth code which was send by Blink
char ssid[] = "xxxxxxxxxxxxxx";  //Enter your WIFI Name
char pass[] = "xxxxxxxxxx";  //Enter your WIFI Password

BlynkTimer timer;

void setup()
{
timer.setInterval(1000L,take_a_reading); // Existing timer
timer.setInterval(180000L,send_to_blynk); // send data to Blynk every 3 minutes (180,000 m/s)
  
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 take_a_reading()
{
int packetSize = LoRa.parsePacket();
int reading = LoRa.parseInt();
  
   adjusted_reading = (reading - adjustment);
 
  Serial.print("Selected tank = ");
  Serial.println(tank_to_read);
  Serial.print("Actual reading = ");
  Serial.println(reading);
  Serial.print("Adjustment factor = ");
  Serial.println(adjustment);
  Serial.print("Adjusted reading = ");
  Serial.println(adjusted_reading);
   
  if (packetSize)
  {
    lcd.setCursor(0,0); //First line
    lcd.print("water is down");
    lcd.setCursor(0,1); //Second line
    lcd.print(adjusted_reading);
    lcd.print(" inches"); 
   } 
}
void send_to_blynk()

 {
  Serial.println ("Connecting to blynk");
  Blynk.connect();
  // the remainder of the processing should be done
  // in the BLYNK_CONNECTED function
}

BLYNK_CONNECTED()
{
  Serial.println ("connected");
  Blynk.syncVirtual(V1); // force the server to send the latest value for the segmented switch
  Blynk.run();
  Serial.println ("Blynk run 1");
  take_a_reading(); // take a reading from the tank indicated by the segmented switch
  Blynk.run();
  Serial.println ("Blynk run 2");   
  Blynk.virtualWrite(V0, adjusted_reading); // To Display Widget
  Blynk.run();
  Serial.println ("Blynk run 3"); 
  Blynk.disconnect();
  Serial.println ("blynk disconnected");  
}

void loop()
{
timer.run();
  }
  
BLYNK_WRITE(V1)
{
 tank_to_read = param.asInt();
 switch (tank_to_read)
  {
    case 1:
   {   
     adjustment = 0;
     break;      
   }

    case 2:
   {   
     adjustment = 4;
     break;      
   }

    case 3:
   {   
     adjustment = 8;
     break;      
   }

    case 4:
   {   
     adjustment = 12;
     break;      
   }
}
 }
        

It appears that none of your functions are now being called by the timers, but I’m not sure why.

Looking through your sketch, I’m confused about what is connected to each of your NodeMCU’s pins. Youre using a combination og GPIO numbers and D numbers, which confuses things, plus you have a pin you’re calling dio0 (presumably meant to represent digital input output 0) which is assigned to GPIO2.

Can you provide a wiring diagram, or at least a list of what is connected to each pin (stick with the screen-printed “D” numbers for this).

Also, it seems that you are using the legacy app and legacy cloud servers, so you should be using Blynk library version 0.6.1
Please downgrade to this in the IDE.

Can you provide me with a link to the LoRa library that you’re using, or explain how you installed it and what version you are using?

Pete.

Hi Pete:
Thanks for devoting so much time to helping me get this working. Hopefully the solution will be helpful to others as well.

The first thing I did, was roll back the Blynk library. It would have been nice if that solved all the problems, but it didn’t.

Here is the schematic of my setup:


The background of the schematic is explained in the Hackster article:

The lora library I’m using is LoRa by Sandeep Mistry, version 0.7.2
If you need anything else that will help let me know.
Thanks.
Jeff

I am now VERY confused!

Your wiring diagram shows the MCU as being a Nano.

Pete.

Sorry, Pete. That was the transmitting ciricuit. Here’s the receiver circuit.
Jeff

So, judging by your diagram, the NodeMCU connections seem to be as follows…

D0 (GPIO16) → RST Pin on LoRa Rx module
D1 (GPIO5) → LED on U3 connector?
D2 (GPIO4) → DIO0 Pin on LoRa Rx Module
D3 (GPIO0) → SDA Pin on OLED Display
D4 (GPIO2) → SCL Pin on OLED Display
D5 (GPIO14) → SCK pin on LoRA Rx module
D6 (GPIO12) → MISO pin on LoRA Rx module
D7 (GPIO13) → MOSI pin on LoRA Rx module
D8 (GPIO15) → nSS pin on LoRA Rx module

However, your mixing and matching of GPIO pin numbers and “D” pin references appears to have led to some confusion.

Your #define dio0 2 line of code should read #define dio0 4 if your wiring diagram is correct, as the DIO0 Pin on LoRa Rx Module is shown as being connected to pin D2 (GPIO4).

Can you confirm that your wiring diagram does actually represent the physical reality, and if it does then change the #define dio0 2 line of code to #define dio0 4 and re-test.

Pete.

Hi Pete:
Here is a picture of the actual PCB showing the actual connections.


I changed the line of code to
#define dio0 2 to dio0 4
It makes no difference.
It’s funny that the board (circuit) has worked fine without glitches on all other of the various sketches up to now. It’s only when we started adding in the BlynkTImer that problems have surfaced. I would think if my wiring was out it would have shown up before.
I’m open to try anything at this point.
Jeff

Looking at the link you provided, the error existed there too, so you’ve simply replicated it.
But, if you’d stuck to using GPIO numbers throughout, and have removed the the confusing DHT wiring data at the beginning of your code, you may have spotted the error.

I’ll download the LoRa library you’re using and see if I can figure out exactly what’s happening.

Pete.

Thanks, Pete. Any idea why the problem is only showing up now?

It would appear that it’s not the cause of the problem, but I want to eliminate all of the issues before looking at alternative approaches.

Pete.

when you find out I’ll flag it for the folks on the website I got it from.

Okay, I’ve done some testing and figured-out a solution.
We needed a way to prevent the code from executing until the values had been synchronised from the segmented switch, so I did that with a flag called sync_completed.

I’ve also made some changes that remove the blocking Blynk.begin command that would halt all code execution if you are unable to connect to either WiFi or Blynk, and removes the issue of using the Blynk.connect in an undocumented way.

The code now manages the WiFi connection manually (as opposed to as part of the Blynk connection process) and checks this connection once per minute.

I’ve also cleaned-up the pin numbering, with GPIO numbers used throughout, and comments that give the corresponding “D” numbers.

I’ve made a few other changes…

If the device can’t connect to the LoRa receiver it will restart the ESP rather than going into an endless loop of doing nothing.
The Wire.pins command has been replaced with Wire.begin, as the .pins command is no deprecated.

Things I have doubts about…
Your take_a_reading function has some code within the if (packetSize) logical test. I assume that this test means that a packet has been received from the LoRa device.
If this is the case then I assume that this code should also be inside the if (packetSize) section of the function…

  adjusted_reading = (reading - adjustment);

  Serial.print("Selected tank = ");
  Serial.println(tank_to_read);
  Serial.print("Actual reading = ");
  Serial.println(reading);
  Serial.print("Adjustment factor = ");
  Serial.println(adjustment);
  Serial.print("Adjusted reading = ");
  Serial.println(adjusted_reading);

what do you think?

Also, the LCD output may benefit from being amended to show the tank number, and if this shows as zero then you know that no synchronisation with Blynk has taken place and therefore the reading for whichever tank is currently in use is not having the adjustment factor applied.
The display could also be used to show errors such as no WiFi or no Blynk connection.
I’ll leave it up to you to deal with that.

Here’s the updated code…

/*
   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 0.6.1 (if usimg Legacy or latest if using IoT): https://github.com/blynkkk/blynk-library

  Conncetions:
  D0 (GPIO16) -> RST Pin on LoRa Rx module
  D1 (GPIO5) -> LED on U3 connector?
  D2 (GPIO4) -> DIO0 Pin on LoRa Rx Module
  D3 (GPIO0) -> SDA Pin on OLED Display
  D4 (GPIO2) -> SCL Pin on OLED Display
  D5 (GPIO14) -> SCK pin on LoRA Rx module
  D6 (GPIO12) -> MISO pin on LoRA Rx module
  D7 (GPIO13) -> MOSI pin on LoRA Rx module
  D8 (GPIO15) -> nSS pin on LoRA Rx module

*/
//#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 0 // GPIO0  = D3 - redefining I2C pins as D1 is needed by LoRa unit
#define I2C_SCL 2 // GPIO2  = D4

#define ss 15     // GPIO15 = D1
#define rst 16    // GPIO16 = D0
#define dio0 4    // GPIO4  = D2

int adjustment;
int adjusted_reading;
int tank_to_read;

const int ledPin = 5; // GPIO5  = D1 - warning light that water has dropped beyond preset level

char auth[] = "xxxxxxxxxxxxxxxxxxxxx"; //Enter the Auth code which was send by Blink
char ssid[] = "xxxxxxxxxxxxxx";  //Enter your WIFI Name
char pass[] = "xxxxxxxxxx";  //Enter your WIFI Password

int wifi_connect_count = 0;          // Keep track of how many times we've tried to connect to the WiFi
int wifi_connect_max_retries = 20;   // Specify how many attempts we will have at connecting to the WiFi
bool sync_completed = false;         // Flag to track when BLYNK_WRITE(V1) has completed

BlynkTimer timer;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Wire.begin(I2C_SDA, I2C_SCL); // Updated from deprecated wire.pins(sda,scl) command

  Serial.begin(74880); // Native baud rate for the NodemCU

    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);

    Serial.println("LoRa Receiver ");
    LoRa.setPins(ss, rst, dio0);
    if (!LoRa.begin(433E6))
    {
      Serial.println("Starting LoRa failed!");
      delay(2000);
      Serial.println("Restarting ESP...");
      ESP.restart(); // Restart the ESP
    }
    Serial.println("LoRa Started");
    lcd.clear();
    lcd.print("Waiting");
    LoRa.setSpreadingFactor(10);
    LoRa.setSignalBandwidth(62.5E3);
    LoRa.crc();

  WiFi_Connect();
  Blynk.config(auth);
  send_to_blynk();  // Connect to Blynk then sync the segmented switch at startup

  timer.setInterval(1000L, take_a_reading); // Existing timer
  timer.setInterval(60000L, check_connections); // Check that we have a WiFi connection every 1 minute
  timer.setInterval(180000L, send_to_blynk); // send data to Blynk every 3 minutes (180,000 m/s)
}

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


void take_a_reading()
{
  int packetSize = LoRa.parsePacket();
  int reading = LoRa.parseInt();

  adjusted_reading = (reading - adjustment);

  Serial.print("Selected tank = ");
  Serial.println(tank_to_read);
  Serial.print("Actual reading = ");
  Serial.println(reading);
  Serial.print("Adjustment factor = ");
  Serial.println(adjustment);
  Serial.print("Adjusted reading = ");
  Serial.println(adjusted_reading);

  if (packetSize)
  {
    lcd.setCursor(0, 0); //First line
    lcd.print("water is down");
    lcd.setCursor(0, 1); //Second line
    lcd.print(adjusted_reading);
    lcd.print(" inches");
  }
}

void send_to_blynk()
{
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println ("Connecting to Blynk");
    Blynk.connect();
    if (Blynk.connected())
    {
      Serial.println ("Connected");
      sync_completed = false;

      Blynk.syncVirtual(V1); // force the server to send the latest value for the segmented switch

      while (!sync_completed) // Wait until BLYNK_WRITE(V1) has completed
      {
        Blynk.run();
      }
      take_a_reading(); // take a reading from the tank indicated by the segmented switch
      Blynk.virtualWrite(V0, adjusted_reading); // To Display Widget
      Blynk.run();
      Blynk.disconnect();
      Serial.println ("Blynk Disconnected");
    }
    else
    {
      Serial.println (">>>>>>>>>>>> Unable to connect to Blynk <<<<<<<<<<<<");
    }
  }
  else
  {
    // we get here if we there is no WiFi connection
    Serial.println (">>>>>>>>>>>> No WiFi Connection, so Blynk connection not attempted <<<<<<<<<<<<");
  }
}

BLYNK_WRITE(V1) // Segmented switch to select tank
{
  Serial.println("BLYNK_WRITE(V1) Executed");
  tank_to_read = param.asInt();
  switch (tank_to_read)
  {
    case 1:
      {
        adjustment = 0;
        sync_completed = true;
        break;
      }

    case 2:
      {
        adjustment = 4;
        sync_completed = true;
        break;
      }

    case 3:
      {
        adjustment = 8;
        sync_completed = true;
        break;
      }

    case 4:
      {
        adjustment = 12;
        sync_completed = true;
        break;
      }
  }
}

void check_connections()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    WiFi_Connect();
  }
  else
  {
    Serial.println(F(">>>> WiFi checked - all good <<<<"));
  }
}

void WiFi_Connect() // New functon to handle the connectuon to the WiFi network
{
  wifi_connect_count=0; // reset the counter
  Serial.println(F("Connecting to WiFi"));

  if (WiFi.status() != WL_CONNECTED)
  {
    WiFi.begin(ssid, pass); // connect to the network
  }
  while (WiFi.status() != WL_CONNECTED  && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attemps allowed
  {
    delay(500);
    wifi_connect_count++;
    Serial.print(F("WiFi connection - attempt number "));
    Serial.println(wifi_connect_count);
  }

  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.mode(WIFI_STA);
    Serial.println(F("Wi-Fi CONNECTED"));
    Serial.println();
  }
  else
  {
    Serial.println(F(">>>>>>>>>>>> Unable to connect to WiFi <<<<<<<<<<<<"));
  }
}

Pete.

2 Likes

Thanks for a massive amount of work, Pete. I’ll test it later this morning and let you know how it works.
Jeff

Hi Pete:
I tried your program. I get an error:

‘LoRa’ was not declared in this scope

I tried moving :

adjusted_reading = (reading - adjustment);

  Serial.print("Selected tank = ");
  Serial.println(tank_to_read);
  Serial.print("Actual reading = ");
  Serial.println(reading);
  Serial.print("Adjustment factor = ");
  Serial.println(adjustment);
  Serial.print("Adjusted reading = ");
  Serial.println(adjusted_reading);

into the itop of if (packetSize) to see if it make a difference - it doesn’t I still get the same error. Here’s the code with the addition you suggested:

/*
   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 0.6.1 (if usimg Legacy or latest if using IoT): https://github.com/blynkkk/blynk-library

  Conncetions:
  D0 (GPIO16) -> RST Pin on LoRa Rx module
  D1 (GPIO5) -> LED on U3 connector?
  D2 (GPIO4) -> DIO0 Pin on LoRa Rx Module
  D3 (GPIO0) -> SDA Pin on OLED Display
  D4 (GPIO2) -> SCL Pin on OLED Display
  D5 (GPIO14) -> SCK pin on LoRA Rx module
  D6 (GPIO12) -> MISO pin on LoRA Rx module
  D7 (GPIO13) -> MOSI pin on LoRA Rx module
  D8 (GPIO15) -> nSS pin on LoRA Rx module

*/
//#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 0 // GPIO0  = D3 - redefining I2C pins as D1 is needed by LoRa unit
#define I2C_SCL 2 // GPIO2  = D4

#define ss 15     // GPIO15 = D1
#define rst 16    // GPIO16 = D0
#define dio0 4    // GPIO4  = D2

int adjustment;
int adjusted_reading;
int tank_to_read;

const int ledPin = 5; // GPIO5  = D1 - warning light that water has dropped beyond preset level

char auth[] = "kuggle's_condo_2"; //Enter the Auth code which was send by Blink
char ssid[] = "jakesnak";  //Enter your WIFI Name
char pass[] = "ZFiawkv0a-UnC_tXeO3Ir0n_FRhEXy1v";  //Enter your WIFI Password

int wifi_connect_count = 0;          // Keep track of how many times we've tried to connect to the WiFi
int wifi_connect_max_retries = 20;   // Specify how many attempts we will have at connecting to the WiFi
bool sync_completed = false;         // Flag to track when BLYNK_WRITE(V1) has completed

BlynkTimer timer;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Wire.begin(I2C_SDA, I2C_SCL); // Updated from deprecated wire.pins(sda,scl) command

  Serial.begin(74880); // Native baud rate for the NodemCU

    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);

    Serial.println("LoRa Receiver ");
    LoRa.setPins(ss, rst, dio0);
    if (!LoRa.begin(433E6))
    {
      Serial.println("Starting LoRa failed!");
      delay(2000);
      Serial.println("Restarting ESP...");
      ESP.restart(); // Restart the ESP
    }
    Serial.println("LoRa Started");
    lcd.clear();
    lcd.print("Waiting");
    LoRa.setSpreadingFactor(10);
    LoRa.setSignalBandwidth(62.5E3);
    LoRa.crc();

  WiFi_Connect();
  Blynk.config(auth);
  send_to_blynk();  // Connect to Blynk then sync the segmented switch at startup

  timer.setInterval(1000L, take_a_reading); // Existing timer
  timer.setInterval(60000L, check_connections); // Check that we have a WiFi connection every 1 minute
  timer.setInterval(180000L, send_to_blynk); // send data to Blynk every 3 minutes (180,000 m/s)
}

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


void take_a_reading()
{
  int packetSize = LoRa.parsePacket();
  int reading = LoRa.parseInt();

  adjusted_reading = (reading - adjustment);

  Serial.print("Selected tank = ");
  Serial.println(tank_to_read);
  Serial.print("Actual reading = ");
  Serial.println(reading);
  Serial.print("Adjustment factor = ");
  Serial.println(adjustment);
  Serial.print("Adjusted reading = ");
  Serial.println(adjusted_reading);

  if (packetSize)
  {
    adjusted_reading = (reading - adjustment);

  Serial.print("Selected tank = ");
  Serial.println(tank_to_read);
  Serial.print("Actual reading = ");
  Serial.println(reading);
  Serial.print("Adjustment factor = ");
  Serial.println(adjustment);
  Serial.print("Adjusted reading = ");
  Serial.println(adjusted_reading);
    lcd.setCursor(0, 0); //First line
    lcd.print("water is down");
    lcd.setCursor(0, 1); //Second line
    lcd.print(adjusted_reading);
    lcd.print(" inches");
  }
}

void send_to_blynk()
{
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println ("Connecting to Blynk");
    Blynk.connect();
    if (Blynk.connected())
    {
      Serial.println ("Connected");
      sync_completed = false;

      Blynk.syncVirtual(V1); // force the server to send the latest value for the segmented switch

      while (!sync_completed) // Wait until BLYNK_WRITE(V1) has completed
      {
        Blynk.run();
      }
      take_a_reading(); // take a reading from the tank indicated by the segmented switch
      Blynk.virtualWrite(V0, adjusted_reading); // To Display Widget
      Blynk.run();
      Blynk.disconnect();
      Serial.println ("Blynk Disconnected");
    }
    else
    {
      Serial.println (">>>>>>>>>>>> Unable to connect to Blynk <<<<<<<<<<<<");
    }
  }
  else
  {
    // we get here if we there is no WiFi connection
    Serial.println (">>>>>>>>>>>> No WiFi Connection, so Blynk connection not attempted <<<<<<<<<<<<");
  }
}

BLYNK_WRITE(V1) // Segmented switch to select tank
{
  Serial.println("BLYNK_WRITE(V1) Executed");
  tank_to_read = param.asInt();
  switch (tank_to_read)
  {
    case 1:
      {
        adjustment = 0;
        sync_completed = true;
        break;
      }

    case 2:
      {
        adjustment = 4;
        sync_completed = true;
        break;
      }

    case 3:
      {
        adjustment = 8;
        sync_completed = true;
        break;
      }

    case 4:
      {
        adjustment = 12;
        sync_completed = true;
        break;
      }
  }
}

void check_connections()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    WiFi_Connect();
  }
  else
  {
    Serial.println(F(">>>> WiFi checked - all good <<<<"));
  }
}

void WiFi_Connect() // New functon to handle the connectuon to the WiFi network
{
  wifi_connect_count=0; // reset the counter
  Serial.println(F("Connecting to WiFi"));

  if (WiFi.status() != WL_CONNECTED)
  {
    WiFi.begin(ssid, pass); // connect to the network
  }
  while (WiFi.status() != WL_CONNECTED  && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attemps allowed
  {
    delay(500);
    wifi_connect_count++;
    Serial.print(F("WiFi connection - attempt number "));
    Serial.println(wifi_connect_count);
  }

  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.mode(WIFI_STA);
    Serial.println(F("Wi-Fi CONNECTED"));
    Serial.println();
  }
  else
  {
    Serial.println(F(">>>>>>>>>>>> Unable to connect to WiFi <<<<<<<<<<<<"));
  }
}

Suggestions?
Jeff

Apologies, I commented-out the LoRa code for my testing, forgot to un-comment the #include for the library.

Pete.

Just caught it and was about to email you.

Hi Pete:
I had an error on my part. The program compiles, but we’re back to the old problem. It goes one round and then goes to zero.
Jeff

When you say “Added the additional code” did you take my code and add your WiFi credentials, password and Auth token (and un-comment the //#include <LoRa.h> line) or did you do it a different way?

Pete.

Here is what I’m using. I’ve xxxd out my personal stuff.

/*
   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 0.6.1 (if usimg Legacy or latest if using IoT): https://github.com/blynkkk/blynk-library

  Conncetions:
  D0 (GPIO16) -> RST Pin on LoRa Rx module
  D1 (GPIO5) -> LED on U3 connector?
  D2 (GPIO4) -> DIO0 Pin on LoRa Rx Module
  D3 (GPIO0) -> SDA Pin on OLED Display
  D4 (GPIO2) -> SCL Pin on OLED Display
  D5 (GPIO14) -> SCK pin on LoRA Rx module
  D6 (GPIO12) -> MISO pin on LoRA Rx module
  D7 (GPIO13) -> MOSI pin on LoRA Rx module
  D8 (GPIO15) -> nSS pin on LoRA Rx module

*/
#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 0 // GPIO0  = D3 - redefining I2C pins as D1 is needed by LoRa unit
#define I2C_SCL 2 // GPIO2  = D4

#define ss 15     // GPIO15 = D1
#define rst 16    // GPIO16 = D0
#define dio0 4    // GPIO4  = D2

int adjustment;
int adjusted_reading;
int tank_to_read;

const int ledPin = 5; // GPIO5  = D1 - warning light that water has dropped beyond preset level

char auth[] = "ZFiawkv0a-UnC_tXeO3Ir0n_FRhEXy1v"; //Enter the Auth code which was send by Blink
char ssid[] = "kuggle's_condo_2";  //Enter your WIFI Name
char pass[] = "jakesnak";  //Enter your WIFI Password

int wifi_connect_count = 0;          // Keep track of how many times we've tried to connect to the WiFi
int wifi_connect_max_retries = 20;   // Specify how many attempts we will have at connecting to the WiFi
bool sync_completed = false;         // Flag to track when BLYNK_WRITE(V1) has completed

BlynkTimer timer;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Wire.begin(I2C_SDA, I2C_SCL); // Updated from deprecated wire.pins(sda,scl) command

  Serial.begin(74880); // Native baud rate for the NodemCU

    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);

    Serial.println("LoRa Receiver ");
    LoRa.setPins(ss, rst, dio0);
    if (!LoRa.begin(433E6))
    {
      Serial.println("Starting LoRa failed!");
      delay(2000);
      Serial.println("Restarting ESP...");
      ESP.restart(); // Restart the ESP
    }
    Serial.println("LoRa Started");
    lcd.clear();
    lcd.print("Waiting");
    LoRa.setSpreadingFactor(10);
    LoRa.setSignalBandwidth(62.5E3);
    LoRa.crc();

  WiFi_Connect();
  Blynk.config(auth);
  send_to_blynk();  // Connect to Blynk then sync the segmented switch at startup

  timer.setInterval(1000L, take_a_reading); // Existing timer
  timer.setInterval(60000L, check_connections); // Check that we have a WiFi connection every 1 minute
  timer.setInterval(180000L, send_to_blynk); // send data to Blynk every 3 minutes (180,000 m/s)
}

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


void take_a_reading()
{
  int packetSize = LoRa.parsePacket();
  int reading = LoRa.parseInt();

  adjusted_reading = (reading - adjustment);

  

  if (packetSize)
  {
    adjusted_reading = (reading - adjustment);

Serial.print("Selected tank = ");
  Serial.println(tank_to_read);
  Serial.print("Actual reading = ");
  Serial.println(reading);
  Serial.print("Adjustment factor = ");
  Serial.println(adjustment);
  Serial.print("Adjusted reading = ");
  Serial.println(adjusted_reading);


  
    lcd.setCursor(0, 0); //First line
    lcd.print("water is down");
    lcd.setCursor(0, 1); //Second line
    lcd.print(adjusted_reading);
    lcd.print(" inches");
  }
}

void send_to_blynk()
{
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println ("Connecting to Blynk");
    Blynk.connect();
    if (Blynk.connected())
    {
      Serial.println ("Connected");
      sync_completed = false;

      Blynk.syncVirtual(V1); // force the server to send the latest value for the segmented switch

      while (!sync_completed) // Wait until BLYNK_WRITE(V1) has completed
      {
        Blynk.run();
      }
      take_a_reading(); // take a reading from the tank indicated by the segmented switch
      Blynk.virtualWrite(V0, adjusted_reading); // To Display Widget
      Blynk.run();
      Blynk.disconnect();
      Serial.println ("Blynk Disconnected");
    }
    else
    {
      Serial.println (">>>>>>>>>>>> Unable to connect to Blynk <<<<<<<<<<<<");
    }
  }
  else
  {
    // we get here if we there is no WiFi connection
    Serial.println (">>>>>>>>>>>> No WiFi Connection, so Blynk connection not attempted <<<<<<<<<<<<");
  }
}

BLYNK_WRITE(V1) // Segmented switch to select tank
{
  Serial.println("BLYNK_WRITE(V1) Executed");
  tank_to_read = param.asInt();
  switch (tank_to_read)
  {
    case 1:
      {
        adjustment = 0;
        sync_completed = true;
        break;
      }

    case 2:
      {
        adjustment = 4;
        sync_completed = true;
        break;
      }

    case 3:
      {
        adjustment = 8;
        sync_completed = true;
        break;
      }

    case 4:
      {
        adjustment = 12;
        sync_completed = true;
        break;
      }
  }
}

void check_connections()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    WiFi_Connect();
  }
  else
  {
    Serial.println(F(">>>> WiFi checked - all good <<<<"));
  }
}

void WiFi_Connect() // New functon to handle the connectuon to the WiFi network
{
  wifi_connect_count=0; // reset the counter
  Serial.println(F("Connecting to WiFi"));

  if (WiFi.status() != WL_CONNECTED)
  {
    WiFi.begin(ssid, pass); // connect to the network
  }
  while (WiFi.status() != WL_CONNECTED  && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attemps allowed
  {
    delay(500);
    wifi_connect_count++;
    Serial.print(F("WiFi connection - attempt number "));
    Serial.println(wifi_connect_count);
  }

  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.mode(WIFI_STA);
    Serial.println(F("Wi-Fi CONNECTED"));
    Serial.println();
  }
  else
  {
    Serial.println(F(">>>>>>>>>>>> Unable to connect to WiFi <<<<<<<<<<<<"));
  }
}