Day 20- IOT using Blynk App


1. How to make Account on Blynk Cloud
2. Make Templets, data stream, and design web dashboard.
3. Arduino code for 2ch iot LED
4. Ardino code for 4ch Relay iot  Home Automation
5. Control Servo Moter uisng Blynk.





To control an LED over the internet using the Blynk app, 

you'll need to follow these steps:


### Requirements:

1. **Blynk App** (Install it on your smartphone from the App Store or Google Play).

2. **Microcontroller** (e.g., Arduino, ESP8266, ESP32).

3. **LED** (with appropriate resistors).

4. **Wi-Fi Network**.

5. **Blynk Library** for the microcontroller.


### Step-by-Step Guide


#### Step 1: Setting up Blynk App

1. **Download and Install Blynk**:

   - Go to the App Store (iOS) or Google Play (Android).

   - Install the **Blynk** app.


2. **Create a Blynk Account**:

   - Open the app and create an account or log in.


3. **Create a New Project**:

   - Tap on **"Create New Project"**.

   - Choose a name for your project (e.g., "LED Control").

   - Select your hardware model (for example, "ESP8266" or "Arduino").

   - Select the connection type (usually **WiFi**).

   - Tap **Create**.


4. **Get the Auth Token**:

   - After creating the project, Blynk will generate an **Auth Token**. This is a unique identifier used to connect your hardware to the Blynk server.

   - Email this token to yourself, or copy it for later use.


5. **Add Widgets**:

   - Add a **Button widget** to the app. This button will control the LED.

   - Configure the button:

     - Set it to output on a virtual pin (e.g., **V1** for virtual pin 1).

     - Set the mode to **Switch** or **Push** depending on how you want it to work.


#### Step 2: Setup Microcontroller (Arduino / ESP8266 / ESP32)


1. **Install Blynk Library**:

   - Open Arduino IDE and install the Blynk library if you haven’t already.

   - Go to **Sketch > Include Library > Manage Libraries**.

   - In the Library Manager, search for "Blynk" and install the latest version.


2. **Install Board Support (if needed)**:

   - For ESP8266 or ESP32, you'll need to install the board definitions.

   - Go to **File > Preferences**, and in the "Additional Boards Manager URLs" field, add:

     - For ESP8266: `http://arduino.esp8266.com/stable/package_esp8266com_index.json`

     - For ESP32: `https://dl.espressif.com/dl/package_esp32_index.json`

   - Then go to **Tools > Board > Boards Manager**, search for **ESP8266** or **ESP32**, and install it.


3. **Connect the LED**:

   - Connect the positive leg of the LED to a digital output pin (for example, **D5** or **GPIO14**).

      **D8** or **GPIO15**)

   - Connect the negative leg of the LED to **GND**.





4. **Arduino Sketch** (Example Code for ESP8266):

   Here is the basic code that connects your microcontroller to the Blynk app and controls the LED over the internet.

///////////////////////////////////////////////////////////////////////////////////


#define BLYNK_TEMPLATE_ID "*****************" // paste the copied details from blynk cloud
#define BLYNK_TEMPLATE_NAME "***************"
#define BLYNK_AUTH_TOKEN "******************"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int led1=14;
int led2=15;

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "******"; //your hotspot name
char pass[] = "******"; //your hotspot password name

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

}

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

BLYNK_WRITE(V1){
  int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
  digitalWrite(led1,pinValue);
}

BLYNK_WRITE(V2){
  int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
  digitalWrite(led2,pinValue);
}


////////////////////////////////////////////////////////////////////////////////////


### Explanation:

- **`Blynk.begin()`**: This function initializes the Blynk connection to the app using the Auth Token, Wi-Fi credentials (SSID and password).

- **`BLYNK_WRITE(V1)`**: This function listens for button presses from the Blynk app on virtual pin V1. When the button is pressed, it turns the LED on (HIGH), and when the button is released, it turns the LED off (LOW).

- **`digitalWrite()`**: This function is used to turn the LED on or off based on the button press.


#### Step 3: Upload the Code to the Microcontroller

- Select the correct board (e.g., **NodeMCU 1.0** or **ESP8266** under **Tools > Board**).

- Choose the correct COM port.

- Upload the code to the microcontroller.


#### Step 4: Test the Setup

1. Once the code is uploaded, open the **Serial Monitor** in the Arduino IDE.

2. You should see a message indicating that the ESP8266 is connected to the Wi-Fi network and the Blynk server.

3. Open the **Blynk App** and press the button widget you added. It should turn the LED on and off based on the button's state.


### Troubleshooting:

- Ensure that your microcontroller has internet access and is connected to the correct Wi-Fi network.

- Double-check your Auth Token.

- Ensure that the correct pin is used for the LED in the code and the hardware setup.

  

Once everything is set up, you can control the LED from anywhere in the world using the Blynk app!



Now upgrade the Blynk for 4 ch Relay module, upgrade the Arudino Code for same


//////////////////////////////////////////////////////////////////////////////////////////

How to Control Servo moter using Blynk

To control a servo motor using Blynk over IoT (Internet of Things), you can use an Arduino, an ESP8266 or ESP32 (for Wi-Fi), and the Blynk mobile app. The following is a step-by-step guide, including the code and setup:

Requirements:

  1. Hardware:
    • Arduino (e.g., Uno, Nano) or ESP32/ESP8266 (for IoT functionality)
    • Servo Motor (e.g., SG90)
    • Jumper Wires
    • Breadboard (optional)
    • Power supply for the servo motor (if needed)
  2. Software:
    • Arduino IDE (to program the board)
    • Blynk mobile app (available for iOS/Android)
    • Blynk Library for Arduino IDE

Steps:

1. Install the Blynk Library:

  • In Arduino IDE, go to Sketch > Include Library > Manage Libraries.
  • Search for "Blynk" and click "Install".

2. Set up the Blynk App:

  • Download the Blynk app from the App Store or Google Play.
  • Create a new project in the app.
  • Select your board type (e.g., ESP8266 or ESP32).
  • Blynk will generate an Auth Token. Copy this token, as you'll need it in the Arduino code.

3. Wiring:





  • Connect the servo motor to the Arduino (or ESP32/ESP8266):
    • Servo Signal Pin to one of the PWM-capable pins (e.g., D5 on ESP8266, GPIO 13 on ESP32).
    • Servo VCC to 5V (or external power if required).
    • Servo GND to GND.

4. Arduino Code:

Servo Motor Control With Blynk

#define BLYNK_TEMPLATE_ID ".................."
#define BLYNK_TEMPLATE_NAME "........................"
#define BLYNK_AUTH_TOKEN "............................."


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include<Servo.h>
Servo servo1, servo2;

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "WiFi Username";
char pass[] = "WiFi Password";

BLYNK_WRITE(V0)
{
  int s0 = param.asInt();
  servo1.write(s0);
  Blynk.virtualWrite(V2, s0);
}

BLYNK_WRITE(V1)
{
  int s1 = param.asInt();
  servo2.write(s1);
  Blynk.virtualWrite(V3, s1);
}


void setup()
{
  Serial.begin(9600);
  servo1.attach(D2); //Connect to Pin D2 in NodeMCU
  servo2.attach(D3); //Connect to Pin D3 in NodeMCU
  Blynk.begin(auth, ssid, pass);//Splash screen delay
  delay(2000);
}

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

Explanation of the Code:

  1. Libraries:

    • BlynkSimpleEsp8266.h or BlynkSimpleEsp32.h: These libraries are used to connect the Arduino (ESP8266 or ESP32) to the Blynk platform via Wi-Fi.
    • Servo.h: This library is used to control the servo motor.
  2. Wi-Fi Credentials and Auth Token:

    • The auth[] variable stores the Blynk authentication token that you received from the Blynk app.
    • The ssid[] and pass[] store your Wi-Fi credentials.
  3. Servo Control:

    • The servo motor is controlled by reading the slider value (0 to 180 degrees) from the Blynk app. The BLYNK_WRITE(VPIN) function listens for changes in the slider (virtual pin V1) and updates the servo angle.
  4. Main Loop:

    • The Blynk.run() function keeps the communication with the Blynk server alive and constantly checks for updates (like slider movements).

5. Upload the Code:

  • Open the Arduino IDE, select the correct board and port (for ESP8266/ESP32), and upload the code.

6. Control the Servo:

  • Open the Blynk app on your phone and create a Slider widget linked to virtual pin V1 (or the pin you specified in the code).
  • Slide the slider to control the servo motor over the internet.

Troubleshooting:

  • If the servo doesn't work, check the power supply. Servo motors may require more current than what the Arduino can supply, so use an external power source.
  • Ensure your Wi-Fi credentials and Blynk token are correct.
  • Test the servo locally without Blynk to ensure it's working properly with a simple code.

By following these steps, you should be able to control the servo motor via the Blynk app over the internet using your Arduino or ESP board.

///////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment

Arduion Uno pin out