HOW TO CONNECT ESP32 TO IOT IN 5 MINUTES

HOW TO CONNECT ESP32 TO IOT IN 5 MINUTES

INTRODUCTION:

Can we make our project to talk with the internet? How to connect our esp32 with the internet? Can we stream data from it? Is it safe to store data in the cloud server?

Adafruit IO  is a platform designed to stream, log, and interact with our data. Adafruit IO is a cloud server that can be used by anybody who wants to log his data with the internet. With Adafruit IO we can use any device which can send data over the internet. Adafruit IO keeps our data private and secure will never send it anywhere.

HOW TO CONNECT OUR ESP32 WITH IOT IN 5 MINS:

 This video will show how to send the data to Adafruit IO  using ESP32

HOW TO INSTALL THE ADAFRUIT IO LIBRARY:

STEP1: In the Arduino ide <sketch<include library<manage library  

STEP2: Enter adafruit.io Arduino library to search box and then install

STEP3: Dependencies for library< install all

Adafruit IO Setup:

To Create a new account on adafruit io:

STEP1: Sign to create an account

STEP2: Navigate to adafruit io Dashboard

STEP3: Create a dashboard to visualize and interact with the data being sent between our esp32 board and Arduino io.

STEP4: Click Dashboard button<Name your dashboard< to create

STEP5: To turn ON/OFF our led add toggle button for it

Create new block< select toggle block<feed name< create<next step

led feed< next step< button ON as 1<button OFF as 0<create block

STEP6: To display button pressed data from board to adafruit io

Create new block<gauge<feed name<create<next step

Block tittle<gauge min value 0<gauge max value 1<create block

HARDWARE PREPARATION:

For this video we need:

  1. ESP32
  2. LED
  3. PUSH BUTTON
  4. USB CABLE
  5. BREAD BOARD
  6. JUMPER WIRE

CONNECTION TO BE MADE:

CODE TO BE USED:

This code is used to send data to adafruit io to switch ON/OFF the LED through the button.
Using Adafruit IO_led_btn code:
Code:
// Adafruit IO Publish & Subscribe, Digital Input and Output Example
//
// Adafruit invests time and resources in providing this open-source code.
// Please support Adafruit and open-source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Modified by Brent Rubell for Adafruit Industries
// Copyright (c) 2020 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"

/************************ Example Starts Here *******************************/

// Button Pin
#define BUTTON_PIN 5

// LED Pin
#define LED_PIN 4

// button state
bool btn_state = false;
bool prv_btn_state = false;

// set up the 'led' feed
AdafruitIO_Feed *led = io.feed("led");

// set up the 'button' feed
AdafruitIO_Feed *button = io.feed("button");

void setup() {

  // set button pin as an input
  pinMode(BUTTON_PIN, INPUT);

  // set LED pin as an output
  pinMode(LED_PIN, OUTPUT);

  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  while(! Serial);

  Serial.print("Connecting to Adafruit IO");

  // connect to io.adafruit.com
  io.connect();

  // set up a message handler for the count feed.
  // the handleMessage function (defined below)
  // will be called whenever a message is
  // received from adafruit io.
  led->onMessage(handleMessage);

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());
  led->get();

}

void loop() {

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

  // grab the btn_state state of the button.
  if(digitalRead(BUTTON_PIN) == LOW)
    btn_state = false;
  else
    btn_state = true;

  // return if the btn state hasn't changed
  if(btn_state == prv_btn_state)
    return;

  // save the btn_state state to the 'button' feed on adafruit io
  Serial.print("sending button -> "); Serial.println(btn_state);
  button->save(btn_state);

  // store last button state
  prv_btn_state = btn_state;

}

// this function is called whenever a 'led' message
// is received from Adafruit IO. it was attached to
// the counter feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {
  Serial.print("received <- ");

  if(data->toPinLevel() == HIGH)
    Serial.println("HIGH");
  else
    Serial.println("LOW");

  digitalWrite(LED_PIN, data->toPinLevel());
}

Config.h

/************************ Adafruit IO Config *******************************/

// visit io.adafruit.com if you need to create an account,
// or if you need your Adafruit IO key.
#define IO_USERNAME "---- your username here ----"
#define IO_KEY "----your IO key here -----"


/******************************* WIFI **************************************/

// the AdafruitIO_WiFi client will work with the following boards:
//   - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
//   - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
//   - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
//   - Feather M0 WiFi -> https://www.adafruit.com/products/3010
//   - Feather WICED -> https://www.adafruit.com/products/3056
//   - Adafruit PyPortal -> https://www.adafruit.com/product/4116
//   - Adafruit Metro M4 Express AirLift Lite ->
//   https://www.adafruit.com/product/4000
//   - Adafruit AirLift Breakout -> https://www.adafruit.com/product/4201

#define WIFI_SSID "robocircuits"
#define WIFI_PASS "robocircuits"

// uncomment the following line if you are using airlift
//#define USE_AIRLIFT

// uncomment the following line if you are using winc1500
// #define USE_WINC1500

// comment out the following lines if you are using fona or ethernet
#include "AdafruitIO_WiFi.h"

#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) ||         \
    defined(ADAFRUIT_PYPORTAL)
// Configure the pins used for the ESP32 connection
#if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
// Don't change the names of these #define's! they match the variant ones
#define SPIWIFI SPI
#define SPIWIFI_SS 10  // Chip select pin
#define SPIWIFI_ACK 9  // a.k.a BUSY or READY pin
#define ESP32_RESETN 6 // Reset pin
#define ESP32_GPIO0 -1 // Not connected
#endif
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS,
                   SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);
#else
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
#endif
/******************************* FONA **************************************/

// the AdafruitIO_FONA client will work with the following boards:
//   - Feather 32u4 FONA -> https://www.adafruit.com/product/3027

// uncomment the following two lines for 32u4 FONA,
// and comment out the AdafruitIO_WiFi client in the WIFI section
// #include "AdafruitIO_FONA.h"
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);

/**************************** ETHERNET ************************************/

// the AdafruitIO_Ethernet client will work with the following boards:
//   - Ethernet FeatherWing -> https://www.adafruit.com/products/3201

// uncomment the following two lines for ethernet,
// and comment out the AdafruitIO_WiFi client in the WIFI section
// #include "AdafruitIO_Ethernet.h"
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);
 

TO UPLOAD THE CODE:

PRESS<RESET<EN

THANK YOU…

Leave a Reply