BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Weather station blynk 2.0

project:
it is a that in theory was supposed to be simple but i ran into many errors and as i never managed to use blynk 2.0 sucesfully so … here is my proyect is based on an esp 8266 with a dht 11 and a bmp 180 (I know I could have used a bmp 280 but in my country I can’t find them) everything is powered with a 18650 2200mah battery that is recharged using solar and a tp4056 and the voltage monitor with 2 resistors (100K) and the light with a ldr and a 3.3k resistor seen that the esp 8266 has are an analog input I used a multiplexer (cd4051be) of which I use only 2 inputs B, C are connected to ground p.s. the code is based on examples.

connections:
dht pin D4
bmp pin D1, D2
mux out pin A0
mux A pin D5

problem: my code does not seem to work I do not know if they are syntax errors or blynk adgent that is used wrongly I am totally open to suggestions I am infinitely thankful to anyone who can help me.
if you need any informaction ask.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define USE_NODE_MCU_BOARD

#include <Adafruit_BMP085.h>
#include "DHT.h"
int analogInPin  = A0;    // Analog input pin
int sensorValue; 
#define DHTpin 2 // D4 on the nodemcu ESP8266
#define DHTTYPE DHT11
DHT dht(DHTpin,DHTTYPE);
#define MUX_A D5 //pin mux
float calibration = 0.33;


//#define APP_DEBUG

#include "BlynkEdgent.h"

Adafruit_BMP085 bmp;

void setup()
{ Serial.begin(115200);
  pinMode(MUX_A, OUTPUT);
  }
  void changeMux(int a){
  digitalWrite(MUX_A, a);
  
  dht.begin();
  bmp.begin();
  
  Serial.println("Serial begin");
  BlynkEdgent.begin();
  delay(2000);
}

void loop() {
BlynkEdgent.run();

//luminosità
  float voltage;
  float light;

  changeMux(LOW);
  delay (1000);
  sensorValue = analogRead(analogInPin);
  voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration); //multiply by two as voltage divider network is 100K & 100K Resistor
  Serial.print("Analog Value = ");
  Serial.print(sensorValue);
  Serial.print("\t Output Voltage = ");
  Serial.print(voltage);
  delay(1000);
  
  changeMux(HIGH);
  delay (1000);
  light = analogRead(analogInPin / 1023.0f);
  Serial.print("ligt = ");
  Serial.print(light);
  delay (1000);
  
 
  
//altri sensori
   float humidity = dht.readHumidity();
   Serial.print("humidity ");
    Serial.println(humidity);
  
   float temperature = bmp.readTemperature();
   Serial.print("temperature ");
    Serial.println(temperature);
   
   float pressure = bmp.readPressure();
   Serial.print("Pressure ");
    Serial.println(pressure);
   
   Blynk.virtualWrite(V0, voltage); //luminosità
   Blynk.virtualWrite(V1, temperature); //temperatura
   Blynk.virtualWrite(V2, humidity); //humidità
   Blynk.virtualWrite(V3, pressure); //pressione
   Blynk.virtualWrite(V4, light); //luce
   
   
   delay(2000);
    ESP.deepSleep(60000000);
    
}

If there are syntax errors then the compilation will fail.

You should read this:

You are using GPIO0(D2) and GPIO2 (D4) which are also used by Edgent.
Do you really need to use Edgent? The only thing you gain is dynamic provisioning and Blynk.Air, but Blynk.Air won’t work with deepsleep.

The code structure with Blynk and deepsleep can be quite tricky. Throw-in the complexity of Edgent and it gets a lot more complex.

Pete.

1 Like

So should I remove the edgent and check the correct board? Anyway when I tried the code the led wasn’t blinking and the serial monitor was putting out some random characters i tried changing the bound rate it it had the same effect and when I tried seeing if my multiplexer was working i got the same voltage so pretty strange… what i should do i read all of your article when I get back to the pc I’ll check the board.

I’m really new to coding

Racola.

What do you mean by this?
I was simply asking if you needed the Edgent functionality, as it’s not the best starting point for many projects.

Are you referring to the sketch you posted at the beginning of the topic?

Are you talking about when you tried to provision the device?

That would be a good idea, if you’re going to stick with Edgent, but you seem to be starting from the wrong place. Begin by answering my question about which Edgent features you wish to use.

Pete.

1 Like

In reality as long as I can get data from the esp i don’t need fancy features so i tink i don’t need it as you said blynk.air won’t work but i got pretty easy access on the device so…

Yes the sketch above

I didn’t try to provision the device i uploaded the code and checked the serial monitor

It won’t connect to Blynk with Edgent without going through the provisioning process, but that’s irrelevant if you’re going to scrap Edgent and go for a simpler setup.
I’d suggest you start with the Sketch Builder DHT and get that working, then add-in the code for the other sensors and connect them one at a time and test at each stage.

Pete.

1 Like

That sounds great i didn’t know you could do this I’ll give it a try so… i get the template for de dht ( the one blynk made) and add de sensors gradually but i got a couple of questions: the template that i get will work whit blynk 2.0? Because it still has the option for the old 32 digit code but also the New stuff. And if I can hard code the wifi credentials or i can still use the app to put them?

Also thank you so much for the time that your loosing for a fool like me😅

Yes

Do you mean the Auth token?
If so, then Blynk IoT also uses Auth tokens. With Edgent these are provisioned dynamically, if you don’t use Edgent then when you create a device from your template then you’ll be given the Auth token along with the template ID and device name to copy/paste at the top of your sketch.

You hard-code them if you aren’t using Edgent.

Pete.

1 Like

Ok I will try all of that stuff

Again thank you so much for the help

hi again I followed the advice he gave me and now everything seems to work: at least the sensors but the voltage dividers do not seem to work well in short they do not give very accurate values ​​and I have already calibrated them I do not know if it is for the ADC of the esp 8266 from the last time I added another voltage divider for the solar panel and built a pcb so it can’t be for the bradboard any advice?

P.S. how to i put code here i forgot

What does this mean?

Triple backticks…
```

Pete.

Sorry if i wasn’t clear… so basically Frome the last time I had only one resistor devider for the battery voltage but now i have 2 one for the battery and one for the solar panel (so i can truck the voltage of the solar panel and the battery) i still have the same multiplexer setup i only added one control pin so i can switch 4 lines (I’m using 3 battery solar and lidar) the code has been working quite well I’m getting all the readings but… When I calibrated the resistors in the code 3/10 I’m getting correct values but the 7/10 are values a bit or a lot off by what i measure from my multimeter and for the voltage source I’m using a battery so… No voltage swings Yeah that’s my question: do you know why are my values shifting?

Maybe you should post your latest sketch, a schematic, details of what you are seeing in your serial monitor or Blynk, and what your multimeter shows.

Pete.

so here is my letest code; unfortunatly i can’t put a wiring diagram but i can explain it and put some photos

if you take a look the cd4051 datasheet or search for pin out … i got INH, Vee, Vss, C to ground of the board; A to pin D5 on the mcu and B to D6; Vdd is coneccted to vin on the mcu which is at 4.2 volt of the battery
the inputs i am using are the 0,1,2: 0 is for the ldr which i am not very interested in accuracy, 1 is used for the battery voltage is a voltage divider 100k and 100k so i get half the voltage, 2 is the same thing except that I use it to monitor the voltage of the solar panel which keeps the battery charged. it’s nothing special but when I see the values ​​in the serial monitor they keep changing and fluctuating quite a bit sometimes they have the right value,

i put the: for (int x = 0; x <10; x ++)
{

function to get better accuracy but it doesn’t seem to work I don’t know if the problem is the same ground

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <Adafruit_BMP085.h>

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define MUX_A D5
#define MUX_B D6
#define MUX_C D7

int analogInPin  = A0;    // Analog input pin
int sensorValue;
int sensor;
float calibration1 = 0.43;
float calibration2 = 0.28 ;

float light; 
float voltage;
float solar;

Adafruit_BMP085 bmp;

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "";
char pass[] = "";


void setup()
{
  // Debug console
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
                                                                   // You can also specify server:
                                                                   //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
                                                                   //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  dht.begin();
  bmp.begin();
  
  pinMode(MUX_A, OUTPUT);
  pinMode(MUX_B, OUTPUT);     
  pinMode(MUX_C, OUTPUT);     
  }
  
  void changeMux(int c, int b, int a){
  digitalWrite(MUX_A, a);
  digitalWrite(MUX_B, b);
  digitalWrite(MUX_C, c);
  }
  


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


void send(){
  delay(1000);
  
  changeMux(LOW, LOW, LOW);
  delay(2000);
  
  light = analogRead(analogInPin / 1023.0f);
  Serial.print("ligt = ");
  Serial.print(light);
  delay (1000);
  
  for (int x = 0; x < 10; x++)
  {

  changeMux(LOW, LOW, HIGH);
  delay(3000);
  
  sensorValue = analogRead(analogInPin);
  voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration1);//multiply by two as voltage divider network is 100K & 100K Resistor
  Serial.print("Analog Value = ");
  Serial.print(sensorValue);
  Serial.print("\t Output Voltage = ");
  Serial.print(voltage);
  delay(2000);
 
  changeMux(LOW, HIGH, LOW);
  delay(3000);
  sensor = analogRead(analogInPin);
  solar = (((sensor * 3.3) / 1024) * 2 + calibration2);//multiply by two as voltage divider network is 100K & 100K Resistor
  Serial.print("Analog Value = ");
  Serial.print(sensor);
  Serial.print("\t Output Voltage = ");
  Serial.print(solar);
  delay(2000);
  }

  float h = dht.readHumidity();
  
  float t = bmp.readTemperature();
   
  float p = bmp.readPressure();

 
  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V2, h);
  Blynk.virtualWrite(V3, p);
  Blynk.virtualWrite(V4, voltage);
  Blynk.virtualWrite(V5, light);
  Blynk.virtualWrite(V6, solar);


  delay(3000);
  ESP.deepSleep(9e+8);
}

P.S. the code works really well besides the accuracy error

this call a function with a lot off delay :scream:
Never use delay with blynk

1 Like

i’m not using blynk edgent so it shuld be fine

You can’t use any delay with blynk, with or without blynk egent !
You have to use timers instead.
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

void loop(){
  Blynk.run();
  Timer.run();
}
1 Like

oright but i do i make a 1 sec timer? i get it i take away that ilne of code and aff timer.run but then? still the accuracy problem

look @PeteKnight tutorial

1 Like

sorry i didn’t see the link before i will try correct the code but still the voltage accuracy idk why but how do i solve it? (read the cuple posts before if you haven’t) and for the multiplexer to work when i switch the signals there has to be a time for the mux to do it’s things