Serial.read() and comparing data, send to widget

Hi

I am trying to send some command to Blynk app through ESP.
I have attached another microcontroller with ESP, which sends data serially to ESP. ESP reads the data and compare the value and sends the hardcode command to blynk app. But it’s not working somehow. Here is code excerpt-

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

char auth[] = "fada16aba9104046b40948144ca518a1"; 
int Auto_bit
char ch = 0;
  
void setup()
 {
    Serial.begin(9600);
    Blynk.begin(auth, "1233", "12345");
 }

BLYNK_WRITE(V0)
{
  Auto_bit = param.asInt();
}


void loop()
{
  Blynk.run();
  if (Serial.available() > 0) 
  {
    char ch = Serial.read(); 
     if(ch == 'A')
      {
      Blynk.virtualWrite(V0, HIGH);
      }
    }
}

Hey! Welcome to the forum. In the Arduino IDE, use [CTR-T] to auto format your code so it is more readable. Also, use code tags </> here on the forum, so your code looks like this:

char ch = 0;

void setup() {
  
  Serial.begin(9600);
  Blynk.begin(auth, "1233", "12345");
}

void loop() {
  
  Blynk.run();
  
  if (Serial.available() > 0) {
    
    char ch = Serial.read();
    
    if (ch == 'A') {
      
      Blynk.virtualWrite(V0, HIGH);
    }
  }
}

Is this all of your code? There is no auth initialized before setup, and I don’t even see any Blynk libraries being called (i’m not sure if it’s needed in your case but it’s just an observation.)

Sorry for poor format of the code. The code does have authorisation code, libraries all. It compiles well.

Can I take a look at the full code? You can just insert “auth” where the code is if you don’t want to share it.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "fada16aba9104046b40948144ca518a1"; 
int Auto_bit
char ch = 0;
  
void setup()
 {
    Serial.begin(9600);
    Blynk.begin(auth, "1233", "12345");
 }

BLYNK_WRITE(V0)
{
  Auto_bit = param.asInt();
}


void loop()
{
  Blynk.run();
  if (Serial.available() > 0) 
  {
    char ch = Serial.read(); 
     if(ch == 'A')
      {
      Blynk.virtualWrite(V0, HIGH);
      }
    }
}

What widget do you have on V0?

Double check that, and try using this code, OP:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "fada16aba9104046b40948144ca518a1";
int Auto_bit
char ch = 0;

void setup() {
  
  Serial.begin(9600);
  Blynk.begin(auth, "1233", "12345");
  
}

BLYNK_WRITE(V0) {
  
  Auto_bit = param.asInt();

  if (Serial.available() > 0) {
    
    char ch = Serial.read();
    
    if (ch == 'A') {
      
      Blynk.virtualWrite(V0, HIGH)
  
}


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

Also, check this link out for more info on serial input basics.

2 Likes

@anuj.mattoo I can highly recommend the details in the link provided by @marcofusco

I learnt almost all my serial data stuff from the original 2014 thread written by Robin2 on the Arduino forum.

The Widget is simple button. Tried this code but it doesn’t work somehow. I am sending data to ESP via bluetooth module attached to it. When I send A from bluetooth, I can’t see widget state ON. The project is in running mode and connected to ESP.

Have you tried the examples on the Arduino page?

I use example 3, not sure how Bluetooth works but standard serial for temperature transfers is as follows:

Sending device:

  ESPserial.write("<");  // start marker
  ESPserial.write(outstr);  // e.g "23.7"
  ESPserial.write(">");   // end marker

Receiving device:

  tempC = atof(receivedChars);   // array to float conversion
  Blynk.virtualWrite(V1, tempC);  // writes 23.7 to Blynk Value Display

In the receiving device you need to call void recvWithStartEndMarkers() and showNewData() in your loop or with SimpleTimer.