BLYNK
BLYNK.IO       📲 GETTING STARTED       đź“— DOCS       âť“HELP CENTER       👉 SKETCH BUILDER

Widget in blynk display zero value

hello, i hope someone can help answering this. for my circuit, i transfer the sensor’s data from arduino mega 2560 to nodemcu by serial communication. then , i connect nodemcu through wifi to blynk so it can display the data for monitoring for users. all the datas was shown on the serial monitor on both arduino and nodemcu. the problem is that the widgets in blynk app show zero value, eventhough it have connected to the nodemcu ( i saw on top where it says test online ).

Blockquote

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>


char auth[] = "SfuZoO2g1UDb6pQ3_raGY_Kixuc4UCse";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "********@unifi";
char pass[] = "*******";

BlynkTimer timer;

String myString; // complete message from arduino, which consists of snesors data
char rdata; // received characters

int firstVal, secondVal,thirdVal; // sensors 
// This function sends Arduino's up time every second to Virtual Pin (1).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000);
  
}



void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

    timer.setInterval(1000L,sensorvalue1); 
     timer.setInterval(2000L,sensorvalue2); 

}

void loop()
{
   if (Serial.available() == 0 ) 
   {
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
   }
   
  if (Serial.available() > 0 ) 
  {
    rdata = Serial.read(); 
    myString = myString+ rdata; 
   // Serial.print(rdata);
    if( rdata == '\n')
    {

String l = getValue(myString, ',', 0); // humidity
String m = getValue(myString, ',', 1); // temperature
 


firstVal = l.toInt(); // humidity
secondVal = m.toInt(); // temperature


  myString = "";
// end new code
    }
  }

}

void sensorvalue1()
{
int sdata = firstVal; // humidity value
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, sdata);

}
void sensorvalue2()
{
int sdata = secondVal; // temperature value
  if (sdata > 25 )
  {
    Blynk.notify("temperature exceeded!!!"); 
  }

  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, sdata);

}

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Blockquote

this is my code for nodemcu, i figured the problem may be coming from here so im posting only this one. the other one is used for arduino. is there any help or suggestion of maybe why it display zero value?

@ana0998 please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

okay done :slight_smile:

A few questions…

  1. Why aren’t you connecting the sensors directly to the NodeMCU instead of to the Arduino?

  2. If you have to use the Arduino, why aren’t you using the NodeMCU (or even an ESP-01) as a WiFi modem using AT firmware and the Blynk simpleshield library?

  3. is your timer widget attached to pin V1? if so then this should be a value or labelled value widget instead.

  4. Why are your Blynk.run and timer.run inside an if` statement?

  5. Why are you using so many variables (l, firstVal and sdata) to handle the same piece of information?

  6. Why don’t you have the Notify widget in your app when you are using notifications?

  7. Why don’t you have a flag to indicate that an over temperature notification has already been sent, so that you only receive constant notifications when the device is over temperature, and so that you don’t exceed the Blynk limitation of one notification every 5 seconds?

  8. What version of the Blynk library are you using?

Pete.

answers…

  1. i connect them through arduino because there a few more analog sensors that i want to connect after this. this one i used one sensor only because i want to see how it works

  2. i am not sure about this since i learn this code from internet, do you mean this one ?

 WiFi.enableInsecureWEP(true);
  WiFi.begin(ssid, pass);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP()); 
  1. okay , i’ll change to value widget. yes, it connect to v1

  2. since i am still new to iot and blynk, so i learnt how to do this code from internet. this is what was written from the page i learnt this code " If this means state, if no data has been obtained by the Nodemcu module, simply continue to run Blynk app.run and timer.run functions. While the other condition means that if the Nodemcu module has received data from the Arduino, only read the Nodemcu module and add the characters obtained with the mystring variable to make a complete message."

  3. as to variables used , firstVal was used to indicate the data that was passed from arduino to nodemcu. sdata was used to indicate as the data to be written in blynk. if you have suggestion, i cah change it.

  4. oh thank you for mentioning that . okay i’ll add them.

  5. oh, im still new so im not sure how to use or to add a flag. i dont realised i need one of those :sweat_smile:

  6. this one,

thank you for answering , i hope with this informations you can help me how i can proceed from here

No, I mean the code you see when you use the Sketch Builder and choose Arduino Mega as your board type and ESP8266 WiFi Shield as your connection type.
In that scenario, you have no code running on your ESP8266, just the AT firmware (which you would need to flash to your ESP device using a utility).

Yes, use one global variable.

Search the forum for “notifications” and “flag” and you’ll find examples.

Pete.

use serialTransfer library to share variable between mcu

hye, thank you for sharing . i have uploaded into arduino libraries and the code still work the same, and blynk did not show any value except zero :persevere:

hye pete , so i have tried the code from sketch builder and choose the connection as you preferred,

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Z_Biv_WFNTu8hJ9jGpIoIfJZNPdlNoAY";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "rajaamireda@unifi";
char pass[] = "******";

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(7, 8); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 38400

ESP8266 wifi(&EspSerial);

#define DHTPIN A1          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

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

and it come out like this on seriol monitor

why does the esp is not responding ?? , i have connect them through rx tx connection :thinking:

i hope you can help through this

Which AT firmware file did you upload to the NodeMCC, and what upload tool did you use?
When you set the serial monitor baud rate to 74880 and connect to the NodeMCU alone, then type “AT” (without the quotes) in the input line at the top of the serial monitor and hit “send” what response do you get?

Also, why are you still using SoftwareSerial when you are using the Mega?

Pete.

I have a few questions:

Can the right values be seen using the Serial Monitor (via the Serial.print() - function) ?
Its always a good idea to track, what is working and what not using this as a debug technique.

So something like:

Serial.print("This is the sensor data: ");     
Serial.println(sdata);

can be helpful.

If this is working, can you hit the rectagular symbol top right on the blynk app?
this would show us, what pins these widgets are connected to.

So please make sure, that those are using virtualPin V5 and V6 :slight_smile: