Categories
Development

Input data from the Blynk app to hardware

We will check how to read input data from the user. The code used is very simple … basically Hello World in Arduino … yes, blinking the LED.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "auth_data";
char ssid[] = "ssid_wifi";
char pass[] = "password_wifi";

int led_pin = 2;
int delay_time = 1000;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(led_pin, OUTPUT);
}

void loop() {
  Blynk.run();
  BLYNK_LOG("on");
  digitalWrite(led_pin, HIGH);
  delay(delay_time);
  BLYNK_LOG("off");      
  digitalWrite(led_pin, LOW);
  delay(delay_time);

}

We just added some logging, but you can really ignore that if you want.

Anyway, let’s create an app now. We will be using the text input widget.

In settings (just click on the widget) you need to select virtual pin for the output. We are using V0, but you can really use whichever you want (just change the code accordingly).

Let’s just add reading data from the app

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "2uTzRbOYroqcm5-I_835c5PfiS6SW0h0";
char ssid[] = "corega5";
char pass[] = "milky12345";

int led_pin = 2;
int delay_time = 1000;

BLYNK_WRITE(V0) //writing to V0 from input test
{
  delay_time = param.asInt(); 
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(led_pin, OUTPUT);
}

void loop() {
  Blynk.run();
  BLYNK_LOG("on");
  digitalWrite(led_pin, HIGH);
  delay(delay_time);
  BLYNK_LOG("off");      
  digitalWrite(led_pin, LOW);
  delay(delay_time);

}

As you can see we just added BLYNK_WRITE and are changing the variable for the delay.

That’s it. Really simple.

Categories
Development

Blink LED with Blynk via USB port

Let’s write code on Arduino Uno

For this project you will need an Arduino Uno board, a way to program it and an application Blynk on your phone.

Initial code was just a regular Arduino code to see that I didn’t make a mistake. It is rather trivial with this project, but it is good to be consistent and really test the basic code, so you will know the code is working.

void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);  
}

Since we don’t have any wireless connection to the Arduino Uno we will be using USB connection for that.

After checking the documentation on the Blynk page we need to install the Blynk library first.

And we also need to create a button in the Blink project on the phone.

Open the Blynk app and click on “New Project”. The project name will be “Blink” since we are blinking the LED on the Arduino Uno board. From the CHOOSE DEVICE list select Arduino UNO and for connection type USB. Theme is not important.

When you finish clik CREATE button and the Auth token is sent to the mail address you used to register.

You can see in the documentation page regarding USB connection that you need to use some scripts. Depending on which operating system you are using the extension is .bat or .sh.

There is an error in the example on the above mentioned page in the command Blynk.begin: The command should be Blynk.begin(Serial, auth); and not Blynk.begin(auth, Serial);

The script to start on the computer is located in the directory called scripts inside the Blynk library directory. It is called blynk-ser.sh on Mac or Linux and blynk-ser.bat on the Windows machine.

I tested the script on Mac machine with an Arduino Uno copy (using CH340 driver) and latest operating system (Catalina).

When starting the script it offered me port /dev/tty.usbserial-1410 but running the script with that port I always got a Resource busy message.

After some searching on the internet I got nothing. I was sure there is a “problem” with socat code, but wasn’t sure what and why. Anyway after playing with only socat I found out that the device /dev/cu.wchusbserial1410 gets me somewhere, but only if I start the script as a root. So the command:

sudo ./blynk-ser.sh -c /dev/cu.wchusbserial1410

was a success.

Categories
Development

Blink LED with Blynk on ESP32

Let’s write code on ESP32

For this project you will need an ESP32 board, a way to program it and an application Blynk on your phone.

Initial code was just a regular Arduino code to see that I didn’t make a mistake. It is rather trivial with this project, but it is good to be consistent and really test the basic code, so you will know the code is working.

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(2, HIGH);
  delay(1000);
  digitalWrite(2, LOW);
  delay(1000);  
}

After checking the documentation on the Blynk page we need to install the Blynk library first.

And we also need to create a button in the Blink project on the phone.

Open the Blynk app and click on “New Project”. The project name will be “Blink” since we are blinking the LED on the ESP32 board. From the CHOOSE DEVICE list select ESP32 Dev Board and for connection type Wi-Fi. Theme is not important.

When you finish clik CREATE button and the Auth token is sent to the mail address you used to register.

Let’s add the button to the project from the list and click on it to modify it.

We will use the gp2 digital pin, since this is the pin to which blue LED is connected.

Don’t forget to select SWITCH mode of the button.

That’s it.

Let’s program the ESP32 with new code now.

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "auth_code_sent_via_email";
char ssid[] = "ssid_of_your_wifi";
char pass[] = "password_for_your_wifi";

void setup() {
  Blynk.begin(auth, ssid, pass);
}

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

Upload the code to the ESP32 and run the project in Blynk app by clicking on Play button in right upper corner. It changes to STOP button.

Next to the STOP button will be a small icon of the board and if there is no connection the red circle with exclamation point will be shown.

No connection to the board

Check if the board is connected, that you have programmed it, that ssid and password are correct, that auth code is also correct.

After successful connection to the board you can test the code by pressing the button and checking the LED on the board.