Arduino – Deep Focus https://fazals.ddns.net/ Embrace it! Mon, 17 Aug 2020 07:36:06 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.1 https://fazals.ddns.net/wp-content/uploads/2020/08/react-native-300-150x150.png Arduino – Deep Focus https://fazals.ddns.net/ 32 32 173186184 Blynk OTA on NodeMCU, ESP8266, ESP32 and more | PlatformIO | Blynk OTA simplified. https://fazals.ddns.net/blynk-ota-arduino-nodemcu-esps/ Sat, 20 Jun 2020 08:53:41 +0000 https://fazals.ddns.net/?p=2217 Blynk OTA on NodeMCU, ESP8266, ESP32 and more | PlatformIO | Blynk OTA simplified. Read More »

]]>
We all know that the Blynk app together with tiny micro controllers is an amazing combination for IoT. NodeMCU, ESP8266, ESP32 and many other Arduino boards are compatible with the Blynk platform. With such large support, Blynk OTA would be a over whelming feature. The code uploading and testing of the device is simple when in the prototype stage. But, when you have a completed the design and development and have no access to the serial port over the USB, you can no longer upload new codes and updates to the devices without breaking them apart.

But is it possible to have the Blynk code updated the over the air (OTA)?

Well officially, the Blynk Platform doesn’t support the OTA updates. They do have it, but with “0” compatibility, making it of no use. They might develop it in the future, but the wait is never over.

Don’t have Arduino IDE, download Arduino IDE from their official website.

The solution for Blynk OTA over WiFi

Do you know that the Arduino OTA library, helps in uploading the code to any device over the network. Well if you don’t. Yeah, it does, using the TCP/IP protocol.

The Arduino OTA code

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#ifndef STASSID
#define STASSID "your-ssid"    //put your WiFi SSID (Name)
#define STAPSK  "your-password"    //Set your WiFi Password
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

void yourSetup(){
  //put your code that need to run once
}

void yourLoop(){
  //put the code that needs to run continuously
}

void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);

  // Hostname defaults to esp8266-[ChipID]
  // ArduinoOTA.setHostname("myesp8266");

  // No authentication by default
  // ArduinoOTA.setPassword("admin");

  // Password can be set with it's md5 value as well
  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  yourSetup();
}

void loop() {
  ArduinoOTA.handle();
  yourLoop();
}

The code is exactly same as provided in the examples in the Arduino IDE with slight modification, it’s simple and easy to understand. Once uploaded to your NodeMCU or any ESP, your device’s network port will show up in the Arduino IDE as below.

Network port on Arduino IDE

What’s next? Adding Blynk code to the OTA code.

You can see there are 2 functions at the starting of the code. This is where your main program code will be. The functions void yourSetup() and void yourLoop() will word just like the void loop() and the void setup() functions.

void yourSetup(){
  //put your code that need to run once
}

void yourLoop(){
  //put the code that needs to run continuously
}

Now we need to add the Blynk code. First we need to import the Blynk header file, then add the credentials, and then copy paste the Blynk setup and loop’s code into our setup and loop. The complete code is given below.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#include <BlynkSimpleEsp8266.h>
char auth[] = "Your_auth_token_here";

#ifndef STASSID
#define STASSID "Your_WiFi_SSID_here"
#define STAPSK "Your_WiFi_Password_here"
#endif

const char *ssid = STASSID;
const char *password = STAPSK;

void yourSetup()
{
  Blynk.begin(auth, ssid, password);
}

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

void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  ArduinoOTA.setHostname("NodeMCU");

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
    {
      type = "sketch";
    }
    else
    { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR)
    {
      Serial.println("Auth Failed");
    }
    else if (error == OTA_BEGIN_ERROR)
    {
      Serial.println("Begin Failed");
    }
    else if (error == OTA_CONNECT_ERROR)
    {
      Serial.println("Connect Failed");
    }
    else if (error == OTA_RECEIVE_ERROR)
    {
      Serial.println("Receive Failed");
    }
    else if (error == OTA_END_ERROR)
    {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  yourSetup();
}

void loop()
{
  ArduinoOTA.handle();
  yourLoop();
}

That’s it, you now have your Blynk code running on your edge device with OTA – Over The Air updates capability. Add or remove or manipulate the code in the yourSetup and the yourLoop part, you will have your OTA capability.

Having problem uploading the code? check out the FAQs

My network port is not showing up on the Arduino IDE

The Arduino IDE does have a problem detecting and working with the network ports. If your code is uploaded and the device is connected to WiFi and the network port is not being shown, restart your IDE.

If the port is not being shown even after restarting, I suggest you to shift over to PlatformIO on Visual Studio Code for micro controller programming. It has a lot better coding and debugging experience. Or check out this post if you want to setup your own custom Blynk server.

Blynk OTA

The most simple, easy to use and 100% working OTA code for Blynk with NodeMCU, ESP8266, ESP32 and so on.

Operating System: Windows 10, Linux, Mac OS, OSX

Application Category: IoT, Arduino

]]>
2217
PlatformIO for Arduino, ESP8266, ESP32 & more with VSCode | Coding super simplified https://fazals.ddns.net/platformio-arduino-with-vscode/ Thu, 18 Jun 2020 17:04:54 +0000 https://fazals.ddns.net/?p=2189 PlatformIO for Arduino, ESP8266, ESP32 & more with VSCode | Coding super simplified Read More »

]]>
The Arduino IDE is a wonderful tool to program your Arduino Boards, the ESPs as well as a few ARM micro-controller boards. It allows you to write your code and upload it to your micro controllers.

But if you have ever used or seen someone using VSCode or the Visual Studio Code from Microsoft. I can assure you that, you would never ever want to use Arduino IDE ever again. PlatformIO with VS Code is a perfect combination for programming your micro controller boards, be it Arduino UNO, Arduino Mega, NodeMCU, or the ESPs.

Downloading and setting up Visual Studio Code

Step 1:

In order to use VSCode for programming your micro controller boards, you need to install the Platform IO Extension for VSCode.
So first of all you need to download Visual Studio Code from it official website. It’s a free and open source software and I personally use it for every program I code. Be it Python, HTML, CSS, C, or the Lua Script. So step 1 is to download VS Code, and then install it. There are different version available for Linux, Windows and Mac.

Step 2:

So now, that you are done downloading and install VS Code. You need to install the Platform IO extension for VS Code.

Procedure to install and enable Platform IO in VS Code for Arduino

1. Open up the Visual Studio Code Application

As soon as the installation is done. Open the Visual Studio Code application. The VS Code can not only be used for Arduino, but also for Python, HTML, CSS, JS, Java, and what ever programming language there is.

2. Navigate to the “Extensions” menu in the side bar on the left and search for PlatformIO

Open the Extensions menu from the side bar, or use the shortcut “Ctrl+Shift+x”. Next you will need to search for “PlatformIO” in the search box and click on the tiny install button.

Just wait for a few minutes for the PlatformIO extension to install and then restart the VS Code application. Now, you are ready to use it to program your micro controllers.

Importing Libraries and Boards

Remember, you had to manually add 3rd party boards like the NodeMCUs or ESPs in the Arduino IDE from the preferences settings in the IDE. Well, you no more need to do that unnecessary thing.

We will use the basic Arduino UNO to show you how to get things done. We will make a simple LED blinking project.

Step 1:

After your PlatformIO extension is installed, you should be able to see an Alien icon just below the extensions icon in the left sidebar. Clicking on it should take you to the PlatfromIO Home in VS Code.
Click on the New Project button, a new window will appear.

Step 2:

Fill in the Name of your project.
Select the Board from the list of boards.
And then click Finish.
It usually takes a few minutes to download the essentials for that particular board and then you are good to go.

In the Arduino IDE your code was saved in files with and .ino extension. But in here, it will be saved as .cpp. Yeah, thats the file extension for C++. Don’t worry, you need not learn C++ for this. All the syntax and coding style remains same as in the Arduino IDE. But there are a few tiny changes to be made.

Understands file structure in VS Code.

First of all, lets have a look at the files in our project.

As it can be seen, there are a number of files in project UNO’s workspace.
The main file that contains all of your code is the main.cpp file.
Another file of importance is the last file platformio.ini. This file is used for configuring all the various parameters for communicating with the board, for example the baud rate. Well will cover that later.

Now, click on the file main.cpp and you should see a few lines of code already written for you. Just like in the Arduino IDE.

#include <Arduino.h>

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

You can see that there is a new line in the code, just before your void setup().

#include <Arduino.h>

This is all the difference in the code you write in the Arduino IDE and the PlatformIO IDE. Just write the code to blink the onboard LED, or use the one below, and then click on the upload button at the bottom of the application. And that should work like a charm.👌

#include <Arduino.h>
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Hope you learnt something new. Would you also like to know what’s Blynk and how to use it? Or may be learn how to setup your own website for free.

See you around 😎 Do care to share and comment.

]]>
2189