Random Analog Numbers in Blynk App

I’m using a BH1750 light sensor, I’ve got the sensor reading correctly through the Serial Monitor but when I try to read those same results over a virtual pin (V2) from the analog read on Blynk, what should be a value similar to “153” comes up on my Blynk App screen (on V2) as “1328”, it just keeps bouncing with big swings. Do I need to reference something else other than A5?

I’m just grasping at straws here. I’m trying to get a valid, recognizable number to report using V2 to the Blynk Analog widget. I’m seeing on my Analog Widget in Blynk numbers like 1749 that should be 123 for the real LUX value of the sensor. The reason I guessed A5 might be applicable was because the device has:

Vcc => Vcc
Gnd => Gnd
SCL => D1
SDA => D0
ADD => A5 (HEX address)

Thank you … props to @freddog2020 for the GD Code.

(code below compiled on Particle Web IDE)


// This #include statement was automatically added by the Particle IDE.
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"

// This #include statement was automatically added by the Particle IDE.
#include "BH1750Lib/BH1750Lib.h"


char auth[] = "b2784b3b3c59??94f8752709100fd";

// door 1 sensor and control
const int magSwitch1 = A3;
const int relaySwitch1 = D6;
// door 2 sensor and control
const int magSwitch2 = A4;
const int relaySwitch2 = D7;

// door 1 status
int magStatus1 = 0;
int ledStatus1 = 0;
// door 2 status
int magStatus2 = 0;
int ledStatus2 = 0;

// fan controls
const int fan1 = D4;
const int fan2 = D5;

// Lumens "LUX" Light Sensor
BH1750Lib lightMeter;
int luxRead = A5;
float luxTemp;

bool result = Blynk.connected();

// timeout in milliseconds
SparkCorePolledTimer updateTimer(1000);

void OnTimer(void) {
    sendDoorStatus();
}

void setup() {
    Serial.begin(9600);
    lightMeter.begin(BH1750LIB_MODE_CONTINUOUSHIGHRES);    
    Blynk.begin(auth);
    while (Blynk.connect() == false) {
        // Wait until connected
    }

    pinMode(relaySwitch1, OUTPUT);
    pinMode(relaySwitch2, OUTPUT);
    pinMode(magSwitch1, INPUT_PULLDOWN);
    pinMode(magSwitch2, INPUT_PULLDOWN);
    pinMode(fan1, OUTPUT);
    pinMode(fan2, OUTPUT);
    
// The next two lines shutdown the relay so when the power bumps your doors don't pop open. 
  digitalWrite( 6 , LOW ); //Double Garage Door Switch
  digitalWrite( 7 , LOW ); //Single Garage Door Switch
  digitalWrite( 0 , HIGH ); //Whole House Fan #1 D0
  digitalWrite( 1 , HIGH ); //Whole House Fan #2 D1

    updateTimer.SetCallback(OnTimer);
    Serial.println("Running...");
}

void sendDoorStatus() {
    Blynk.virtualWrite(V0, ledStatus1);
    Blynk.virtualWrite(V1, ledStatus2);
    Blynk.virtualWrite(V2, luxTemp);

}

void loop() {
    Blynk.run();
    // Process LUX Light Meter
    uint16_t lux = lightMeter.lightLevel();
    Serial.print("Light: ");
    Serial.print(lux);
    Serial.println(" lx");
    delay(2000);
    
    luxTemp = analogRead(luxRead);
    
    //constantly monitor the door magnetic switch status (garage door open or closed)
    magStatus1 = digitalRead(magSwitch1);
    magStatus2 = digitalRead(magSwitch2);

    if (magStatus1 == LOW) {
        ledStatus1 = 1023; // 100% brightness
        //Serial.println("LED1: high");
    } else {
        ledStatus1 = 1;
        //Serial.println("LED1: low");
    }
    if (magStatus2 == LOW) {
        ledStatus2 = 1023;
    } else {
        ledStatus2 = 1;
    }

    updateTimer.Update();
}

First off, you code is not exactly recommended practice. The 2000ms delay will mess up your program in the end. It’s recommended to use the SimpleTimer Library for these kinds of things. Your loop() would look like this:

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

void someTimedThingEveryTwoSecconds();
{
  CodeToBeExectuedEveryTwoSeconds;
}

This would look like the basics. Blynk can’t handle delays because it needs to stay connected to the cloud service. I see you are using some sort of timer to send info to Blynk, this is the way to go. I’d recommend trying to implement that for your light meter too. :slight_smile: