About String(char array) and Terminal on Arduino and Blynk

hi all:
i m working a project by Arduino and Blynk.i want to get String from Terminal,then i ll use this String.
some code:

WidgetTerminal terminal(V0);
char *pinValueV0 = “”;

BLYNK_WRITE(V0)
{
pinValueV0 = param.asStr();
}

when i use the pinValueV0 in loop(),i found the string has a error which from terminal-V0 .
and i check the param.asStr(),and pinValueV0 in BLYNK_WRITE(V0) by Serial.println,its OK
but pinValueV0 in loop() has error about last string always.

give me hands

@gaosongChina if you are struggling with character arrays and pointers then perhaps just try Strings and convert the String to a usable format.

BLYNK_WRITE(V0)
{
    String pinValueV0 = param.asStr();
}

It depends what datatype you are looking for to allocate to pinValueV0 but toInt() will work if you want an integer and toFloat() for a Float / Double.

You can also use atof() and atoi() etc if you want to continue using character arrays.

In actual factual you might not need to do any conversion as you can do:

BLYNK_WRITE(V0)
{
    String pinValueV0 = param.asStr();
    if(pinValueV0 == "1"){
       // do something
    }
   else{
       // do something different
   }
}

Remembering of course to define pinValueV0 as a global variable if you want to use it outside of the BLYNK_WRITE(V0) function.

Thanks,@Costas . it is
i want to achieve a function :inputing something by Terminal,and showing by LCD
so converting format is improper
and i find something:
code:

WidgetTerminal terminal(V0);
char *pinValueV0 = "";
BLYNK_WRITE(V0)
{
  menu_redraw_required = 1;
  pinValueV0 = param.asStr();
  Serial.println("param.asStr()=");
  Serial.println(param.asStr());
  Serial.println("pinValueV0[WRITEV]=");
  Serial.println(pinValueV0);
}
void loop()
{
  if (  menu_redraw_required != 0 ) {
      Serial.println("pinValueV0[LOOP]=");
      Serial.println(pinValueV0);
    menu_redraw_required = 0;
  }

  Blynk.run();
}

then i input “hi” and get it in serial monitor:

:sob::sob::sob:

@gaosongChina I can’t get your code to compile but I have a sketch below that reads from Terminal and prints to the LCD. Serial Monitor looks like this and app shows details as expected:

 Starting
[304] Connecting to MTN WIFI 19996
[1304] Connected to WiFi
[1304] IP: 192.168.10.195
[1304] Blynk v0.3.9 on Arduino
[5001] Connecting to 192.168.10.229:8442
[5181] Ready (ping: 9ms).
param.asStr()= hello
pinValueV0[WRITEV]= hello
pinValueV0[LOOP]= hello
param.asStr()= Costas here
pinValueV0[WRITEV]= Costas here
pinValueV0[LOOP]= Costas here

Sketch used is:

//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxxxxxxx";
char ssid[] = "MTN WIFI 19996";
char pass[] = "xxxxxxxxxxxxx";
char server[] = "blynk-cloud.com";

WidgetLCD lcd(V1);
WidgetTerminal terminal(V0);

String pinValueV0;
int menu_redraw_required = 0;

BLYNK_WRITE(V0)  // Terminal
{
  menu_redraw_required = 1;
  pinValueV0 = param.asStr();
  Serial.print("param.asStr()= ");
  Serial.println(param.asStr());
  Serial.print("pinValueV0[WRITEV]= ");
  Serial.println(pinValueV0);
}

void setup()
{
  Serial.begin(115200);
  Serial.println("\nStarting");
  Blynk.begin(auth, ssid, pass, server);
  while (Blynk.connect() == false) { // try to connect to server for 10 seconds
  } 
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(0, 0, "  Connected to"); 
  lcd.print(0, 1, "     server"); 
}

void loop()
{
  if (  menu_redraw_required != 0 ) {
    Serial.print("pinValueV0[LOOP]= ");
    Serial.println(pinValueV0);
    menu_redraw_required = 0;
    lcd.clear();
    lcd.print(0 , 0, pinValueV0);
  }  
  Blynk.run();
}

Notes:

  1. In a simple sketch like this the loop() is OK but in practise you should use SimpleTimer at say 300ms to 500ms intervals to check the status of the menu_redraw_required flag.
  2. If you really need a char array use toCharArray() function with the syntax of string.toCharArray(buf, len)
  3. If I am missing something please explain in detail what you want to do.
1 Like

@Costas i solved it by your hand and myself !:grin:
1.my lcd is real,not virtual.and i use drawStr() which need char array.
so i use toCharArray() fuction to make a new array for drawStr().because the array size is dynamic.There are other problems to show the string
2.so i use print() instead of drawStr() .Fuction print() can use String directly. Solved!!
3.Now i learned the difference between drawStr() and print(),however i recognized they are same exactly。
4.i dont understand why 300~500ms delay is required to check the status of the menu_redraw_required flag
:innocent:

@gaosongChina it doesn’t have to be 300ms to 500ms, it could be 10ms if you NEED it to be that frequent. The point is it should be done with a timer rather than in the main loop(). As you expand your sketch if you have much more than Blynk.run() and timer.run() in the loop() it will start to fail.

I hadn’t realised you were using a physical LCD that requires a particular format.