Categories
3d printer Development

3d printer

There is this store in Austria (also in Slovenia, but the item was available only in Austria) called Hofer (Aldi in some countries) and they were selling 3d printer for 200 EUR which is pretty budget oriented so I’ve bought one. It says BALCO on the printer and apparently it’s a printer from an Australian company. Have to admit it was one of the best buys lately.

Categories
Development

Reading data from ESP32 to Blynk app

Let’s see how to get data from the ESP32 board.

This time we will not bother with testing on the ESP32 since it would be just blinking the LED every 1 second and we already did it.

So let’s just create a new project with virtual LED. The steps are similar to what we did in our first project, but we have to select LED from widget box selection and name it V0

virtual LED setup with the name V0
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "blynk_auth_token";
char ssid[] = "ssid_name";
char pass[] = "wifi_password";

WidgetLED led(V0);

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

void loop() {
  Blynk.run();
  led.on();
  delay(1000);
  led.off();
  delay(1000);
}

The idea is to blink the virtual LED in the mobile application from the ESP32 application. Delay should be 1 second.

Program ESP32 and run the project on the mobile.

We can see how the virtual LED is blinking, but the delay between blinks is far from 1 second. So what is happening?

Here is one important lesson we need to know. The communication between application on the mobile and ESP32 is not direct, but through Blynk cloud which means ESP32 is sending data to the cloud and Blynk application is reading this data. In between some delays appear due to traffic on the internet and it is really hard to predict how much delay will there be between blinks.

Categories
Development

Distance with Raspberry Pi

This one can be found all over the internet, but I wanted to have it here, just for the reference. And if it comes handy to someone – double win.

Anyway – the sensor is ultrasonic HC-SR04. And it works like that:

  1. start the timer
  2. send the sound in direction you want to measure the distance
  3. measure the time when the sound comes back (an echo)
  4. calculate the distance

The code is

def check_distance():
    GPIO.output(TRIG, False)
    time.sleep(0.5)
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)
    while  GPIO.input(ECHO) == 0:
      pulse_start = time.time()

    while GPIO.input(ECHO) == 1:
      pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 171500 
    distance = round(distance, 2)
    return distance

This one is a function in Python returning the distance. You need to define TRIG for the transmitter and ECHO for the receiver.

Why are we multiplying with 171500?

Since the speed of sound is 343 m/s and the sound needs to travel double the distance from the sensor to the obstacle we need to half the calculated distance (from the formula v=d/t). We get 171.5 m/s, but since we want the result in millimetres the result has to be multiplied by 1000 (you know – 1m = 100cm = 1000mm) .

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.

Categories
Development

How to use Blynk environment

Let’s learn how to use Blynk environment to control different hardware platforms, for example Arduino. First you need to install the application on the Android or iOS device.

Initial installation

After installation you also need to register so you can get the data properly routed to your page and not to somebody else.

There is also an option to run your own server, but I recommend that you start with an account on Blynk server, just to test that everything is working as it should.

You will also receive an email with links to different services about Blynk (libraries, examples).

Initial screen of the Blynk application

Arduino and Blynk

Since the Arduino environment is rather easy to start with, I will start with this and the library is the same for all the environments. So basically – how to turn the Arduino Uno embedded LED on or off.

Empty project page

Is Blynk free?

Basically yes, but there is a caveat. You are using the app by expending the energy. You get 2000 units of energy after you create new project and the list of widgets you can add is shown with their energy “value”.

List of widgets for opened project

It means you lower available energy by adding different elements to it.

What happens when I run out of energy?

You can purchase more energy by clicking on the +Add button on the right side of the where you get a list of prices for different Energy packs (levels of energy).

Price list of energy packs