Labeled value widget - cannot display string or char variable

Hello everyone!

I encounter a strange problem.
I am using an ESP32 wifi to connect to a server, where the esp32 extracts some data from a mysql database with the help of a php file (these are details, not very important)… Anyway, basically I read the http response using the httpclient library. The response is in a String variable. These data are from a weatherstation, like: temperature, humidity, pressure, windspeed and wind direction.

These values I convert them to float, and succesfully display them in Blynk using labeled value widget.
The problem is with the Wind Direction data, This cannot be converted to float or int or whatever, because this data is character based, like: N, NE, NNE, S, SE, etc. So this is kept in a String. Printing the string in Serial monitor, works fine. but when I try to print it in blynk, using a virtual pin, (like all other data) it shows just “…” The value is not present in value widget.
What seems to be the problem with this string?
Bellow is the code I used to get the string from server, and display in blynk widget on port V22.

PS: If I make a custom string, for example: String MyString = “Test”. This string is succesfully displayed in blynk widget as “Test” as it should. Wich means that it is a strange problem with that kind of string that httpclient produce, that is incompatible with blynk widget… or what can I say?..

Any help would be apreciated. (if you need the full code, I will give it, but it’s preety big)

Thank you!
Adrian.

(...)
String wswd;

http.begin("http://... path to the php file.... "); //Specify the URL
        httpCode = http.GET();  
        wswd = http.getString();
        Serial.println(wswd);
      http.end();

(...)
Blynk.virtualWrite(V22, wswd);

Seeing the full function at least would be useful.

Pete.


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <HTTPClient.h>
#include <WifiFHD.h> //wifi credential file

BlynkTimer get_ws;
BlynkTimer timer;
BlynkTimer Mytimer;
WidgetRTC rtc;


char auth[] = "-------";

#define Bucket_Size 0.3           // rain bucket size  (0.3mm)

volatile unsigned long contactTime;    // Timer to manage any contact bounce in interrupt routine

unsigned long intervalrain1h=3600000;    // 1h timer for reset rain1h counter.
unsigned long previousMillisrain1h=0; // 1h timer for reset rain1h counter.

int releu = 13; // rain senzor relay
const int butonmanual = 12; //buton manual ON/OFF la pin 12

//variabile 

int tipCount1h, tipCount24h, resetrain;
float rain1h, rain24h;


unsigned long time24h; 
unsigned long time48h; 



int precip24h, precip48h;
int buttonpressed;

int iora, iminut, isecunda, izi, iluna, ian;
int senzor, starereleu;
int yy, cyear, cmonth, cday, chour, cminute, cseconds, httpCode;
 

char memwstemp[4];
char memwsws[4];
char memwswd[4];

int memwshum;
float memwspressure;
char memwstempchar[5];
char memwswschar[5];
String memwswdchar;
char rain1hchar[5];
char rain24hchar[5];
String wswd;


char wd[5];
float fwstemp, fwsws;


String wstemp;
String wshum;
String wspressure;
String wsws;




String valsenzor;

// Digital clock display of the time    // functia de clock.
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String ora = String(hour());
  String minut = String(minute());
  String secunda = String(second());
  String zi = String(day());
  String luna = String(month());
  String an = String(year());

 iora = ora.toInt();
 iminut = minut.toInt();
 isecunda = secunda.toInt();
 izi = zi.toInt();
 iluna = luna.toInt();
 ian = an.toInt();
  
 }

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}


void IRAM_ATTR get_rain() {   

    if((millis() - contactTime) > 30 ) { // debounce of sensor signal 
     tipCount1h += 1;
     tipCount24h += 1;
    contactTime = millis();  
    }
}

void setup() {
 
  Serial.begin(9600);
  pinMode(33, INPUT_PULLUP); // senzor ploaie la D33 (intrerupt pin).
  attachInterrupt(digitalPinToInterrupt(33), get_rain, FALLING);
  pinMode(releu, OUTPUT); // releu senzor ploaie 
  pinMode(butonmanual, OUTPUT);

  digitalWrite(butonmanual, HIGH);
  
  setSyncInterval(10 * 60); // set rtc Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
  timer.setInterval(10000L, clockDisplay);
  get_ws.setInterval(60000L, get_ws_data);
  
 // conectarea la wifi ....

  WiFi.mode(WIFI_STA);
  WiFi.setSleep(false);
  WiFi.enableSTA(true);
  WiFi.setAutoConnect(true);
  WiFi.setAutoReconnect(true);
 
  Blynk.begin(auth, ssid, pass, "myserver", 8088);  // !
  Mytimer.setInterval(1000L, MyFunction);   

}



BLYNK_WRITE(V6) {  // Button Widget on V13 set to SWITCH mode
  if (param.asInt() == 1) {  // If button state is 1 (HIGH) then...
     resetrain = 1;
     } else {  // If button state is 0 (LOW) then...
    resetrain = 0;
  }
 }
 void MyFunction() 
{
          
         Blynk.virtualWrite(V0, rain1hchar);
         Blynk.virtualWrite(V1, rain24hchar);
         Blynk.virtualWrite(V2, time24h);
         Blynk.virtualWrite(V3, time48h);
         Blynk.virtualWrite(V10, memwstempchar);
         Blynk.virtualWrite(V11, memwswschar);
         Blynk.virtualWrite(V22, wswd);
         Blynk.virtualWrite(V8, wshum);
         Blynk.virtualWrite(V9, wspressure);
         Blynk.virtualWrite(V13, valsenzor);

    }

void get_ws_data()
{
        HTTPClient http;
      
        http.begin("http://home.adrianc.ro/weatherstation/readtemp.php"); //Specify the URL
        httpCode = http.GET();    
        wstemp = http.getString();
        Serial.println(wstemp);
      http.end();
   fwstemp = wstemp.toFloat() ;
   dtostrf(fwstemp, 4, 1, memwstempchar); 


     
    http.begin("http://home.adrianc.ro/weatherstation/readhum.php"); //Specify the URL
        httpCode = http.GET();  
        wshum = http.getString();
       Serial.println(wshum);
      http.end();

    
    http.begin("http://home.adrianc.ro/weatherstation/readpressure.php"); //Specify the URL
        httpCode = http.GET();  
        wspressure = http.getString();
        Serial.println(wspressure);
      http.end();


      
    http.begin("http://home.adrianc.ro/weatherstation/readws.php"); //Specify the URL
        httpCode = http.GET();  
        wsws = http.getString();
        Serial.println(wsws);
      http.end();
       fwsws = wsws.toFloat() ;
    dtostrf(fwsws, 4, 1, memwswschar); 

    
    http.begin("http://home.adrianc.ro/weatherstation/readwd.php"); //Specify the URL
        httpCode = http.GET();  
        wswd = http.getString();
        Serial.println(wswd);
      http.end();
      
       
}

void loop()
{
  Blynk.run();
  Mytimer.run();  // Goes in the void loop() so the BlynkTimer library can run.
  timer.run();
  get_ws.run();
}

I ran into the “…” display in Blynk when the text was too many characters to fit into the widget. Have you tried making the widget larger (wider) ?

1 Like

I just maximized as large as I can the widget window, but still “…” display. It should be max 3 characters. In serial monitor it’s ok… I don’t see any spaces or weird characters…

Off topic, but you don’t need all of these timers.

One timer object can handle up to 16 separate timer instances. Having three separate instances uses three times more memory and processing time.

Have you tried trimming your string, or querying it’s length before sending it to Blynk?

Pete.

I understand, I am sure that my code is not perfect, in terms of optimising memory and stuff, but ESP32 has plenty of it, basically the job that ESP has to do is to count the interrupts triggered by a rain gauge, and control a relay accordingly, to cut down / or not, the 24V AC that powers solenoid for irrigation system. (Missing a part of code that is irelevant) Togheter with all this, I wanted to display weather station data in the same blynk project as the rain info, relay state and all that stuff!


Ok, going back to the problem, sincerely I do not know how to trim the string, nor quering its lenght…? Can you tell me how to do this?

Thank you!

https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/trim/

https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/length/

Pete.

Nothing changed, unfortunately…

Hmm… I just now saw that the results from server, has blank spaces before and after the value, as you can see in the serial output. Hmmm… For some reason this is not an impediment for string to float function to work… but in this case strange things happens. Trim function must cut these blank spaces…why it doesen’t? or how can I eliminate them?

Ohhh snap!

It works, after trim. My bad! I had an old buff variable (from the previous attempts) in the “BlynkvirtualWrite” command. I changed again with the correct one (the trimmed one) and it works.

I spend hours researching on this, converting to char array, and all other crazy stuff, all the solution did not fixed the problem! Yours does!!
Thank you very very much for the solution!

Adrian

1 Like

Good, glad it’s fixed the problem (and you’ve learned some new string manipulation commands).

Now, just to humour me, would you make these changes to your timers…

Change this:

To this:

BlynkTimer timer;

Your timer declarations in void setup from these:

timer.setInterval(10000L, clockDisplay);
get_ws.setInterval(60000L, get_ws_data);
Mytimer.setInterval(1000L, MyFunction); 

to these:

timer.setInterval(10000L, clockDisplay);
timer.setInterval(60000L, get_ws_data);
timer.setInterval(1000L, MyFunction);

And your void loop from this:

void loop()
{
  Blynk.run();
  Mytimer.run();  // Goes in the void loop() so the BlynkTimer library can run.
  timer.run();
  get_ws.run();
}

to this:

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

Pete.

Yes Sir! You have all the rights to suggest and make modifications to my sketch :slight_smile: therefore, I did all the changes you suggest. Here is the full code of the project:

Thanks again!


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <HTTPClient.h>
#include <WifiFHD.h>

BlynkTimer timer;
WidgetRTC rtc;


char auth[] = "xxxxxxxxxxxxxxx";

#define Bucket_Size 0.3           // rain bucket size  (0.3mm) - cat ai masurat tu la un click

volatile unsigned long contactTime;    // Timer to manage any contact bounce in interrupt routine

unsigned long intervalrain1h=3600000;    // 1h timer for reset rain1h counter.
unsigned long previousMillisrain1h=0; // 1h timer for reset rain1h counter.

int releu = 13; // pin releu senzor ploaie.
const int butonmanual = 12; //buton manual ON/OFF la pin 12

//variabile 

int tipCount1h, tipCount24h, manualrequest, resetrain;
float rain1h, rain24h;

unsigned long counter24h;
unsigned long counter48h;
unsigned long milisec24h = 86400000;
unsigned long milisec48h = 172800000;
unsigned long difcontor24h; 
unsigned long diftimer24h;
unsigned long time24h; 
unsigned long difcontor48h; 
unsigned long diftimer48h;
unsigned long time48h; 
unsigned long contor24h;
unsigned long contor48h;


int precip24h, precip48h;
int buttonpressed;

int iora, iminut, isecunda, izi, iluna, ian;
int senzor, starereleu;
int yy, cyear, cmonth, cday, chour, cminute, cseconds, httpCode;
 

char memwstemp[4];
char memwsws[4];
char memwswd[4];

int memwshum;
float memwspressure;
char memwstempchar[5];
char memwswschar[5];
String memwswdchar;
char rain1hchar[5];
char rain24hchar[5];
String wswd;

char buf[5];

float fwstemp, fwsws;


String wstemp;
String wshum;
String wspressure;
String wsws;




String valsenzor;

// Digital clock display of the time    // functia de clock.
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String ora = String(hour());
  String minut = String(minute());
  String secunda = String(second());
  String zi = String(day());
  String luna = String(month());
  String an = String(year());

 iora = ora.toInt();
 iminut = minut.toInt();
 isecunda = secunda.toInt();
 izi = zi.toInt();
 iluna = luna.toInt();
 ian = an.toInt();
  
 }

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}


void IRAM_ATTR get_rain() {   // functia care incrementeaza variabilele tipcount1h si tipcount24h atunci cand face click parghia 

    if((millis() - contactTime) > 30 ) { // debounce of sensor signal 
     tipCount1h += 1;
     tipCount24h += 1;
    contactTime = millis();  
    }
}

void setup() {
 
  Serial.begin(9600);
  pinMode(33, INPUT_PULLUP); // senzor ploaie la D33 (intrerupt pin).
  attachInterrupt(digitalPinToInterrupt(33), get_rain, FALLING);
  pinMode(releu, OUTPUT); // releu senzor ploaie 
  pinMode(butonmanual, OUTPUT);

  digitalWrite(butonmanual, HIGH);
  
  setSyncInterval(10 * 60); // set rtc Sync interval in seconds (10 minutes)

  // Display digital clock every 10 seconds
 timer.setInterval(10000L, clockDisplay);
 timer.setInterval(60000L, get_ws_data);
 timer.setInterval(1000L, MyFunction);
  
 // conectarea la wifi ....

  WiFi.mode(WIFI_STA);
  WiFi.setSleep(false);
  WiFi.enableSTA(true);
  WiFi.setAutoConnect(true);
  WiFi.setAutoReconnect(true);
 
  Blynk.begin(auth, ssid, pass, "myserver", 8088);  // !!!!! change that for your Blynk server in cloud!!
  

}

 BLYNK_WRITE(V4) {  
  if (param.asInt() == 1) { 
    digitalWrite(releu, LOW);
    buttonpressed = 3;
     } else {  
    digitalWrite(releu, HIGH);
    buttonpressed = 2;
  }
 }

BLYNK_WRITE(V6) {  
  if (param.asInt() == 1) {  
     resetrain = 1;
     } else {  // If button state is 0 (LOW) then...
    resetrain = 0;
  }
 }
 void MyFunction() 
{

      rain1h = tipCount1h * Bucket_Size;    //calculate 1h rain.
      rain24h = tipCount24h * Bucket_Size;  // calculate 24h rain.

    if ((unsigned long)(millis() - previousMillisrain1h) >= intervalrain1h) {   // reset 1h rain
        previousMillisrain1h = millis();
        tipCount1h = 0;
       }
       

     if ((iora == 23 && iminut == 59))    // reset rain at the end of the day
    {
       tipCount1h = 0;
       tipCount24h = 0;
  }

 
 // conditions for rain volume high, not to start irrigation


     if ((rain24h >=3.3) && (rain24h < 7)) {
      precip24h = 1;
           }
      
      if (rain24h >=7) {
        precip48h = 1;
              }

      
  
       
if (precip24h == 1)
  {
     counter24h = counter24h + 1;
     senzor = 1;
  }
        
        if (precip48h == 1)
  {
     counter48h = counter48h + 1;
     senzor = 1;
  }



 if ((counter24h >= 2) && (counter24h <10))
  {
    contor24h = millis();
  }
    if (counter24h > 1)
    {
    difcontor24h = millis() - contor24h;
    diftimer24h = milisec24h - difcontor24h;
    time24h = diftimer24h / 60000;
    valsenzor = "ON";
    }
    
    if (time24h < 0) {
    
    counter24h = 0;
    precip24h = 0;
    senzor = 0;
    difcontor24h = 0;
    diftimer24h = 0;
    time24h = 0;
    }


  
 if ((counter48h >= 2) && (counter48h <10)) 
  {
    contor48h = millis();
  }
  if (counter48h > 1)
    {
    difcontor48h = millis() - contor48h;
    diftimer48h = milisec48h - difcontor48h;
    time48h = diftimer48h / 60000;
    valsenzor = "ON";
   }
    
    if (time48h < 0) {
    
    counter48h = 0;
    precip48h = 0;
    senzor = 0;
    difcontor48h = 0;
    diftimer48h = 0;
    time48h = 0;
     }

     if ((counter48h != 0) ||  (counter24h !=0)) valsenzor = "OFF";
     
 
   if ((buttonpressed == 3) || (buttonpressed == 0))
 {
      if (senzor == 1) digitalWrite(releu, HIGH);
      if (senzor == 0) digitalWrite(releu, LOW);
     
 }

 
 if (buttonpressed == 2) digitalWrite(releu, HIGH);

 
     dtostrf(rain1h, 4, 1, rain1hchar); 
     dtostrf(rain24h, 4, 1, rain24hchar); 

 if (resetrain == 1)    // reset rain from manual reset rain button
    {
       tipCount1h = 0;
       tipCount24h = 0;
        counter24h = 0;
        precip24h = 0;
        senzor = 0;
        difcontor24h = 0;
        diftimer24h = 0;
        time24h = 0;

       counter48h = 0;
       precip48h = 0;
       senzor = 0;
       difcontor48h = 0;
       diftimer48h = 0;
       time48h = 0;
   
  }

         
         Blynk.virtualWrite(V0, rain1hchar);
         Blynk.virtualWrite(V1, rain24hchar);
         Blynk.virtualWrite(V2, time24h);
         Blynk.virtualWrite(V3, time48h);
         Blynk.virtualWrite(V10, memwstempchar);
         Blynk.virtualWrite(V11, memwswschar);
         Blynk.virtualWrite(V22, wswd);
         Blynk.virtualWrite(V8, wshum);
         Blynk.virtualWrite(V9, wspressure);
         Blynk.virtualWrite(V13, valsenzor);

      starereleu = digitalRead(releu);

   if (starereleu == HIGH) Blynk.virtualWrite(V4, LOW); // send back to Blynk relay state
   if (starereleu == LOW) Blynk.virtualWrite(V4, HIGH); // send back to Blynk relay state    
   if (starereleu == HIGH) Blynk.virtualWrite(V5, LOW); // send back to Blynk relay state
   if (starereleu == LOW) Blynk.virtualWrite(V5, HIGH);  // send back to Blynk relay state
}

void get_ws_data()
{
        HTTPClient http;
      
        http.begin("http://home.adrianc.ro/weatherstation/readtemp.php"); //Specify the URL
        httpCode = http.GET();    
        wstemp = http.getString();
        Serial.println(wstemp);
      http.end();
   fwstemp = wstemp.toFloat() ;
   dtostrf(fwstemp, 4, 1, memwstempchar); 


     
    http.begin("http://home.adrianc.ro/weatherstation/readhum.php"); //Specify the URL
        httpCode = http.GET();  
        wshum = http.getString();
       Serial.println(wshum);
      http.end();

    
    http.begin("http://home.adrianc.ro/weatherstation/readpressure.php"); //Specify the URL
        httpCode = http.GET();  
        wspressure = http.getString();
        Serial.println(wspressure);
      http.end();

      
    http.begin("http://home.adrianc.ro/weatherstation/readws.php"); //Specify the URL
        httpCode = http.GET();  
        wsws = http.getString();
        Serial.println(wsws);
      http.end();
       fwsws = wsws.toFloat() ;
    dtostrf(fwsws, 4, 1, memwswschar); 


    
    http.begin("http://home.adrianc.ro/weatherstation/readwd.php"); //Specify the URL
        httpCode = http.GET();  
        wswd = http.getString();
        Serial.println(wswd);
      http.end();
      wswd.trim();

}

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

Thanks, Pete. This little timer tutorial was gold for me.
.

1 Like