Widget reading analog pin does not resume function after power off

Hello,

I am not sure if this is a Blynk error or a user error. My apologies if I have mis-classified it.

Context in a nutshell:

Capacitative moisture sensor on Arduino Yun analog pin 0 (A0). On power off/on, all other functions in my app resume after the Yun reboots. The analog pin reading stubbornly remains at 1023.

Naturally, I checked my hardware connections. Nothing made the pin reading budge.

However, I want to the app (iOS) and temporarily reassigned the Labeled Value widget that was reading A0 to the null pin. I immediately reset it to A0 and the sensor began reading correctly.

My code is pasted below. Many thanks if someone can point out if I am making a user error or if I have identified an actual bug.

Mitch

#include <Bridge.h>
#include <BlynkSimpleYun.h>
#include <SimpleTimer.h>

char auth[] = "yourMom";

// Initialize Blynk server routines
SimpleTimer timer;

// Physical Pin mappings
#define H2O_Sensor_Physical_Pin A0
#define Pump_Relay_Physical_Pin 10
#define H2O_Sensor_Power_Pin     7

// Virtual Pin Mappings
#define PumpRelayVPin      V0
#define MoistureSensorVPin V1
#define PumpCheckVPin      V2
#define TableWidgetVPin    V4

// Other key user variables
const int Timer_Interval = 2000L; // timer interval in milliseconds
bool isFirstConnect = true;       // used to keep device from syncing every connection
int pumpState;                    // declare here to give pump state global scope 
int lastPumpState;                // dont' bother to log pump state if it hasn't changed
int MoistureSensorValue;
int rowIndex = 0;                 // used to determine most recently added row

void PerformPeriodicEvents () {
  ReadMoistureSensor();
  CheckPumpStatus();
} // End PerformPeriodicEvents block

void CheckPumpStatus() {
  String stateLabel = "Pump ";
  String moistureLabel = "H2O %";
  moistureLabel.concat(MoistureSensorValue);
  int uptime    = millis()/1000;
  
 if (pumpState==1) {
    stateLabel.concat("ON @ ");
    stateLabel.concat(uptime); 
  } else {
    stateLabel.concat("OFF @ ");
    stateLabel.concat(uptime);
  } // end label conditional
  
  if(pumpState==1) {
    Blynk.virtualWrite(PumpCheckVPin, 1);
  } else {
    Blynk.virtualWrite(PumpCheckVPin, 0);
  }
  
  if (pumpState!= lastPumpState) {
    Blynk.virtualWrite(TableWidgetVPin, "add", rowIndex, stateLabel, moistureLabel); // Use uptime as part of state label
    Blynk.virtualWrite(TableWidgetVPin, "pick", rowIndex);                           // Highlight most recent row
  } // end pump state logging block

  lastPumpState = pumpState;
  
  rowIndex++;

} // end CheckPumpStatus block

void ReadMoistureSensor() { // DFRobot capacitative moisture sensor 
  // Turn on digital pin used to power sensor
    digitalWrite(H2O_Sensor_Power_Pin, HIGH);
    // give the pin and sensor time to stabilize before reading, it is a capacitative sensor of unknown and constantly varying capacitance 
    // I empirically found the minimum time needed to get a reading consistently identical to the always-powered reading under the whole range of expected conditions
    delay(200); 
  // Read analog sensor and remap value to invert and scale 0-100
    MoistureSensorValue = map(analogRead(H2O_Sensor_Physical_Pin), 270, 460, 100, 0);
  // Write sensor value to virtual pin
    Blynk.virtualWrite(MoistureSensorVPin, MoistureSensorValue);
  // Turn off digital pin used to power sensor
    digitalWrite(H2O_Sensor_Power_Pin, LOW);
} // End ReadMoistureSensor block


BLYNK_WRITE(V0) {  // V0 maps to a digital pin for pump relay control
  pumpState = param.asInt();
  if(pumpState==1) {
    digitalWrite(Pump_Relay_Physical_Pin, HIGH);
  } else {
    digitalWrite(Pump_Relay_Physical_Pin, LOW);
  }   
} // End BLYNK_WRITE(V0) block


BLYNK_CONNECTED() { 
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  } // end isFirstConnect conditional
} // End BLYNK_CONNECTED block

void setup() {
 
  pinMode(Pump_Relay_Physical_Pin, OUTPUT);                  // Set pin modes
  pinMode(PumpCheckVPin, OUTPUT);
  pinMode(H2O_Sensor_Power_Pin, OUTPUT);
  pinMode(H2O_Sensor_Physical_Pin, INPUT_PULLUP);

  digitalWrite(H2O_Sensor_Power_Pin, HIGH);                  // Set initial pin states

  Blynk.virtualWrite(TableWidgetVPin, "clr");                // Clear table widget on start up
  
  Blynk.begin(auth);                                         // Initialize Blynk Server Connection
  while (Blynk.connect() == false) {  }                      // wait til connect
  Blynk.syncAll();                                           // Sync Blynk app with server
  timer.setInterval(Timer_Interval, PerformPeriodicEvents);  // initialize SimpleTimer instance
  
} // End setup block


void loop() {
  Blynk.run();
  timer.run();               // run simpleTimer instance
} // End loop block

Take this whole line out… it has essentially turned your Analog pin A0 into a Digital pin waiting for a LOW input

1 Like

Thanks for putting eyes on this. I’m curious why it worked at all then.

Thanks again,

Mitch