ESP8266 IOT Using Arduino and ThingSpeak

Introduction: ESP8266 IOT Using Arduino and ThingSpeak

About: Electronics Component - PCB [Design, Printing, Inserting] - Electronic Programming

Internet of Things (IoT) is a concept that aims to expand the benefits of connected internet connectivity continuously -the ability to share data, remote control, and etc, as well as on objects in the real world. For example, food, electronics, collectibles, any equipment, including living things that are all connected to local and global networks through embedded and active sensors.

Here is an example program of Arduino IoT using ESP8266 Wireless Module -how to send data in the form of random value, which can be monitored from anywhere by utilizing the free IOT service provider -ThingSpeak.

Step 1: Materials You Need

You will need:

  1. ESP-8266 Wireless Module
  2. SFE Duino
  3. Male to Female Jumper Wires

Step 2: Setup

Connect SFE Duino and ESP-8266 Wirelss Module with jumper wires as schematic above.

Step 3: Connect With ThingSpeak

Once you've connected the materials, visit ThingSpeak and create an account there. And then, create a new channel.

Step 4: Code

After that, you can upload the code below and make sure to fill the data as yours -start from API key and Field name, and also Access Point setting which will be connected to ESP-8266 Wireless Module (AP and Password).

#include
#define DEBUG true
#define RX 2
#define TX 3

String HOST = "api.thingspeak.com";
String PORT = "80";
String AP = "AP NAME";
String PASS = "AP PASSWORD";


String API = "CJZ1G46URZWEGRVR";
String field = "field1";

int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;

SoftwareSerial esp8266(RX,TX);
void setup() {

Serial.begin(9600);
esp8266.begin(115200);

sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=""+ AP +"",""+ PASS +""",15,"OK");
countTrueCommand = 0;

}

void loop() {

String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
switch(countTrueCommand) {

case 0: sendCommand("AT",5,"OK");break;
case 1: sendCommand("AT+RST",10,"invalid");break;
case 2: sendCommand("AT+CIPMUX=1",5,"OK"); break;
case 3: sendCommand("AT+CIPSTART=0,"TCP","+ HOST +","+ PORT,15,"OK"); break;
case 4: sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">"); break;
case 5: esp8266.println(getData);delay(1500);countTrueCommand++; break;
case 6: sendCommand("AT+CIPCLOSE=0",5,"OK"); break;
case 7:

Serial.println(valSensor);
Serial.print(getData);
Serial.print(",");
Serial.println(getData.length());
valSensor = random(100000); // random value, change with sensor value if using sensor
countTrueCommand = 0;
delay(10000);
break;

}

}
void sendCommand(String command, int maxTime, char readReplay[]) {

Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))

{

esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{

found = true;
break;

}
countTimeCommand++;

}
if(found == true)
{

Serial.println("OYI");

countTrueCommand++;
countTimeCommand = 0;

}
if(found == false)
{

Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;

}
found = false;

}

Step 5: Monitor It!

After you upload it, make sure that the Wifi you used connected with internet connection. Then, open serial monitoring to see AT Command data. Data which sent is a random data. You can change it with data of sensor or etc. Here is the value of monitoring result after many data processing.

Be the First to Share

    Recommendations

    • STEM Contest

      STEM Contest
    • Role Playing Game Challenge

      Role Playing Game Challenge
    • CNC Contest

      CNC Contest

    16 Comments

    0
    bwengyeduncan.bd
    bwengyeduncan.bd

    Reply 5 months ago

    Note that the wifi module uses 3.3v TTL logic so connection to 5v will damage it

    0
    AarohanS1
    AarohanS1

    Question 9 months ago

    This setup works for me. Is it possible to add wifimanager into it? Kindly share how to do that.

    0
    DePaul_250
    DePaul_250

    Question 10 months ago on Step 5

    I am working on IoT using ESP8266 Node MCU and some cloud dashboards,
    I need help about connecting NodeMCU on Wifi with no password
    _#include <ESP8266WiFi.h>


    const char *ssid = "Your wifi Network name";

    const char *pass = "Network password";_

    0
    SharadD3
    SharadD3

    Question 1 year ago

    my arduino ide is showing error for "sendCommand" how should I fix this problem?

    Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Uno"
    C:\Users\Dasari\AppData\Local\Temp\arduino_modified_sketch_505484\Sserial.ino: In function 'void setup()':
    Blink:33:39: error: 'send_command' was not declared in this scope
    send_command("AT+RST\r\n",2000,DEBUG);
    ^
    exit status 1
    'send_command' was not declared in this scope
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

    0
    sayeda24
    sayeda24

    1 year ago

    this code is not working ,getting fail error
    can you post right code


    0
    93ahire
    93ahire

    3 years ago

    /*this is 100% working program just change your AP name pass and things speak password*/

    #include <SoftwareSerial.h>

    #define DEBUG true

    #define RX 8

    #define TX 7

    String HOST = "api.thingspeak.com";

    String PORT = "80";

    String AP = "XXXX";//your AP name

    String PASS = "XXXX";\\your AP password

    String API = "XxxXPF**V6**OEIL";//your API

    String field = "field1";

    int countTrueCommand;

    int countTimeCommand;

    boolean found = false;

    int valSensor = 1;

    SoftwareSerial esp8266(RX,TX);

    void setup() {

    Serial.begin(9600);

    esp8266.begin(115200);

    Serial.println("AT+CWJAP=\""+AP+"\",\""+PASS+"\"");

    sendCommand("AT",5,"OK");

    sendCommand("AT+CWMODE=1",5,"OK");

    sendCommand("AT+CWJAP=\""+AP+"\",\""+PASS+"\"",15,"OK");

    countTrueCommand = 0;

    }

    void loop() {

    String readDat="GET https://api.thingspeak.com/channels/9/feeds.json?...

    String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);

    switch(countTrueCommand) {

    case 0: sendCommand("AT",5,"OK");break;

    case 1: sendCommand("AT+RST",10,"OK");break;

    case 2: sendCommand("AT+CIPMUX=1",5,"OK"); break;

    case 3: sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK"); break;

    case 4: sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">"); break;

    case 5: esp8266.println(getData);delay(1500);countTrueCommand++; break;

    case 6: sendCommand("AT+CIPCLOSE=0",5,"OK"); break;

    case 7:

    Serial.println(valSensor);

    Serial.print(getData);

    Serial.print(",");

    Serial.println(getData.length());

    valSensor = random(100000); // random value, change with sensor value if using sensor

    countTrueCommand = 0;

    delay(10000);

    break;

    }

    }

    void sendCommand(String command, int maxTime, char readReplay[]) {

    Serial.print(countTrueCommand);

    Serial.print(". at command => ");

    Serial.print(command);

    Serial.print(" ");

    while(countTimeCommand < (maxTime*1))

    {

    esp8266.println(command);//at+cipsend

    if(esp8266.find(readReplay))//ok

    {

    found = true;

    break;

    }

    countTimeCommand++;

    }

    if(found == true)

    {

    Serial.println("OYI");

    countTrueCommand++;

    countTimeCommand = 0;

    }

    if(found == false)

    {

    Serial.println("Fail");

    countTrueCommand = 0;

    countTimeCommand = 0;

    }

    found = false;

    }

    0
    RuodiW
    RuodiW

    Reply 3 years ago

    after running this program it is working, but always fail at the AT phase, and shows 0. at command => AT Fail, how to fix this problem?

    1523132079.png
    0
    sayeda24
    sayeda24

    Reply 1 year ago

    I got same error ,Mr.100% working can you reply ,posting wrong code

    0
    Krusher99
    Krusher99

    Reply 2 years ago

    Hi 93ahire, I am struggling with your code to the effect that it compiles and uploads to my esp8266 but fails to connect to my access point. what can I use to troubleshoot/ get error messages.

    0
    annesha
    annesha

    Question 2 years ago on Step 4

    If i need to check the connection status of the WiFi what should i do?

    0
    Jeff Quek
    Jeff Quek

    2 years ago

    Hi, can I replace the random(10000) with flow sensor value? how to replace it? I tried, it show in serial monitor but no upload in the thingspeak. Please advise. Thank you.

    0
    DinkarJ
    DinkarJ

    3 years ago

    Error in the code can be corrected as follows--

    in line 1 :

    #include <SoftwareSerial.h>

    and change the code at case 3 as :

    case 3: sendCommand("AT+CIPSTART=0,TCP,"+ HOST +","+ PORT,15,"OK"); break;

    0
    humidif
    humidif

    Question 3 years ago on Step 5

    What is the baud rate to be fixed

    0
    kishanr19
    kishanr19

    Question 3 years ago on Step 4

    While compiling, i get this error

    expected ')' before 'TCP'

    After that, I added '+' to front and back of TCP. Then It gave

    'TCP' not found in this scope.

    0
    harshkat
    harshkat

    3 years ago

    some error in the given code