How to Send Data to Thingspeak without Sensors

In this post we are going see how to send data to Thingspeak without any sensors, this tutorial is aimed at beginners who are about to make their first IoT project. In this post we will see two tutorials one using generic ESP8266 and another using NodeMCU.

We will see:

  • How to setup Thingspeak account to receive data.
  • Installing ESP8266 board package to Arduino IDE.
  • Sending random data to Thingspeak using NodeMCU.
  • Program code for NodeMCU.
  • Sending random data to Thingspeak using generic ESP8266.
  • How to upload code to generic ESP8266.
  • Program code for generic ESP8266 and Arduino.
  • Result / output.

Let’s dive in shall we?

How to setup Thingspeak account to receive data?

You need to setup your Thingspeak account correctly only then the sent data will be update on your receiving channel / field on Thingspeak.

First things first, you need to create a Thingspeak account, if you haven’t done yet please click here and fill up your appropriate credentials and signup.

  • Click new channel and do the following:
  • Go to “Channel Settings” tab and name the channel and field as shown above and check the box beside field 1.
  • Scroll down and click “save channel”.

Write API key:

API stands for Application Programing Interface, here the API is a string of alphanumeric code through which you can write or read data from Thingspeak. Here we need the “Write API key” which you can get from “API Keys” tab. You need to paste your API key in the appropriate part of the given code.

Keep your “write API” key confidential if you don’t want others to write data on your channel.

Take note of your Channel ID, you need to enter this in the given code as well.

Installing ESP8266 package to Arduino IDE

To write and compile code for ESP8266 or NodeMCU on Arduino IDE, you need to install ESP8266 package. You can install the package by following the given steps below:

  • Now go to Tools > Board > Boards Manager, which is at top of all options.
  • Now a window will pop-up and type ESP8266 and click install:

Installation may take 5 minutes or so, once the installation is complete, you can write and compile code for boards like generic ESP8266, NodeMCU and several other Wi-Fi boards.

Send Data to Thingspeak without Sensors Using NodeMCU

Sending data to Thingspeak without sensor using NodeMCU

To send data to Thingspeak using NodeMCU without any sensor, all you just need is a micro-USB cable and a NodeMCU as hardware.

We will be generating random number in the code from 0 to 100 and this random number will be sent to Thingspeak. In this way a beginner can send data and see the values live on Thingspeak channel / field immediately without the need of any sensors.

The given code can also be used for testing a NodeMCU’s Wi-Fi functionality by generating random numeric data and sending it to a server like Thingspeak.     

How to upload code to NodeMCU?

  • Select Tools > Board > NodeMCU 1.0
  • Select Tools > Upload speed to 115200 (baud rate)
  • Select the correct COM port.
  • Press and hold the flash button and press reset button once, now release the flash button.
  • Click upload.

Only after pressing the flash and reset buttons in the mentioned sequence the program gets uploaded to NodeMCU.

On successful code upload you will see this:

Code uploaded to ESP8266
Code uploaded to ESP8266

(Or this)

Program uploaded to ESP8266
Program uploaded to ESP8266

If you failed to upload the code to NodeMCU, you did not press the buttons in the mentioned sequence or you did not select the correct COM port or the correct board or baud rate (115200).

Download Thingspeak library: click here

Program Code for NodeMCU:

// ----------(c) Electronics-Project-Hub -----------//
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
float value = 0;
int x = 0;

//----------- Enter you Wi-Fi Details---------//
char ssid[] = "xxxxxxx";   // your network SSID (name)
char pass[] = "YYYYYYY";   // your network password
//-------------------------------------------//

//----------- Channel Details -------------//
unsigned long Channel_ID = 0000000; // Channel ID
const int Field_number = 1; // Don't change
const char * WriteAPIKey = "ABZDEFG"; // Your write API Key
// ----------------------------------------//

WiFiClient  client;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
}

void loop()
{
  internet();
  get_value();
  upload();
}

void upload()
{
  internet();
  x = ThingSpeak.writeField(Channel_ID, Field_number, value, WriteAPIKey);
  if (x == 200)Serial.println("Data Updated.");
  if (x != 200)
  {
    Serial.println("Data upload failed, retrying....");
    delay(15000);
    upload();
  }
}

void internet()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      Serial.print(".");
      delay(5000);
    }
    Serial.println("\nConnected.");
  }
}

void get_value()
{
 value = random(0, 100);
 Serial.println("---------------------");
 Serial.print("Next Value:");
 Serial.println(value);
 delay(15000);
}
// ----------(c) Electronics-Project-Hub -----------//

You need to enter your Wi-Fi’s SSID and password here:

//----------- Enter you Wi-Fi Details---------//
char ssid[] = "xxxxxxx";   // your network SSID 
char pass[] = "YYYYYYY";   // your network password
//-------------------------------------------//

You need to enter your channel details here:

//----------- Channel Details -------------//
unsigned long Channel_ID = 0000000; // Channel ID
const int Field_number = 1; // Don't change
const char * WriteAPIKey = "ABZDEFG"; // Your write API Key
// ----------------------------------------//

Thingspeak output for NodeMCU:

Data on Thingspeak

Serial monitor: Select baud rate 115200

Send Data to Thingspeak without Sensor Using Generic ESP8266

Generic ESP8266 overview:

Generic ESP8266 comes in a small PCB package with 8 pins, it has its own CPU, RAM, Flash memory, 2 GPIO pins and it has UART (Serial) communication through which it communicates with other microcontrollers.

Like Arduino we need to upload a program code to make this board functional, but this board doesn’t come with a USB port that connects to a PC, fortunately the code can be uploaded using an Arduino board or using a FTDI. In this tutorial we will be using an Arduino board to program this generic ESP8266.

ESP8266
ESP8266

General specification:

  • Operating voltage 3.3V.
  • Tensilica L10632 bit CPU clocked at 80MHz.
  • Current consumption 100mA.
  • 512KB flash memory.
  • GPIO output current 12mA.      
  • Supports UART communication.
  • Deep sleep current consumption less than 10 uA.

You will be surprised that on paper the raw processing power of this board is more than many Arduino boards. Generic ESP8266 can be used as standalone or as a slave device with a (master) microcontroller like Arduino, PIC etc. Since this board has only 2 GPIO pins, not many things can get done when made standalone, so we will be using this as a slave device with other microcontroller (Arduino).     

Pin diagram of generic ESP8266:

ESP8266 Pin diagram
ESP8266 Pin diagram
Pin Diagram of ESP8266
Pin Diagram of ESP8266

Block diagram, how we are going to upload data to Thingspeak without sensor:

Send data to Thingspeak - Block diagram

In the beginning of this topic we mentioned that generic ESP8266 need a program code to make it functional, so we will be uploading a program code to generic ESP8266 which enables communication to Thingspeak server.

Arduino board will generate random numbers between 0 and 100 and pass the values via UART / serial pins of generic ESP8266. The ESP8266 will connect to internet and update the values on Thingspeak.

A point worth noting is that we need two program codes, one for generic ESP8266 and another code for Arduino to generate random number.

How to upload code to generic ESP8266 using Arduino?

As we mentioned earlier the ESP8266 does not come with a USB port through which a program code can be uploaded, so we need to connect an external programming device like FTDI which is basically a USB to serial converter and vice-versa.

Since Arduino board has built-in USB to serial converter chip which is used for programming the microcontroller IC, we can make use of it instead of spending some bucks on FTDI converter. 

You need to wire up the Arduino board and ESP8266 as shown below:

Circuit diagram for uploading code to ESP8266
Circuit diagram for uploading code to ESP8266

NOTE 1: You need to remove the microcontroller IC from the Arduino board to accomplish this step otherwise you’ll get upload errors.

NOTE 2: Use only 3.3V, 5V can kill the ESP8266.

Uploading code to ESP8266
Uploading code to ESP8266

Now follow these steps:

  • Copy the given code for generic ESP8266 and paste it on Arduino IDE.
  • Select Tools > Boards > Generic ESP8266 Module.
  • Select Tools > Upload speed > 115200 (Baud rate).
  • Select the correct COM port.
  • Press and hold the flash button press the reset button once and release the flash button.
  • Click upload.

Once the code gets uploaded successfully you’ll see this:

Code uploaded to ESP8266
Code uploaded to ESP8266

(Or this)

Program uploaded to ESP8266
Program uploaded to ESP8266

If you failed to upload, try again and please check for wiring errors and press the buttons in the mentioned sequence.  

Download Thingspeak library: click here

Program code for generic ESP8266

// ----------(c) Electronics-project-hub-------- //
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

//------- WI-FI details ----------//
char ssid[] = "XXXXXXX"; //SSID here
char pass[] = "YYYYYYY"; // Passowrd here
//--------------------------------//

//----------- Channel details ----------------//
unsigned long Channel_ID = 0000000; // Channel ID
const int Field_Number = 1; // Don't change
const char * myWriteAPIKey = "ABCEDFG"; //Your write API key
//-------------------------------------------//

String value = "";
int x;
WiFiClient  client;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
  internet();
}

void loop()
{
  internet();
  if (Serial.available() > 0)
  {
    while (Serial.available() > 0)
    {
      int inChar = Serial.read();
      value += (char)inChar;
    }
  }
  upload();
}

void internet()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
  }
}

void upload()
{
  x = ThingSpeak.writeField(Channel_ID, Field_Number, value, myWriteAPIKey);
  if (x != 200)
  {
    delay(15000);
    upload();
  }
  value = "";
}
// ----------(c) Electronics-project-hub-------- //

You need to enter your Wi-Fi SSID and password here:

//----------- Enter you Wi-Fi Details---------//
char ssid[] = "XXXXXXX";   // your network SSID (name)
char pass[] = "YYYYYYY";   // your network password
//-------------------------------------------//

You need to enter your channel details here:

//----------- Channel Details -------------//
unsigned long Channel_ID = 0000000; // Channel ID
const int Field_number = 1; // Don't change
const char * WriteAPIKey = "ABZDEFG"; // Your write API Key
// ----------------------------------------//

Rewire the ESP8266 and Arduino as shown below:

Send data to Thingspeak without sensors

NOTE: Insert the microcontroller IC back to the socket of Arduino board.

Using the above (final) circuit we are going to send data (random number) to Thingspeak, with the above circuit set-up upload the below given code to Arduino.

Program code for Arduino 

//---------(c) Electronics-project-hub.com ----------//
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
float value;
void setup()
{
  Serial.begin(9600); // Serial Monitor baud rate
  mySerial.begin(115200); // Baud rate for ESP8266
}

void loop()
{
  value = random(0, 100); // Generating random value 0 to 100.
  mySerial.println(value); // Sending random value to ESP8266.
  Serial.println(value);
  delay(5000);
}
//---------(c) Electronics-project-hub.com ----------//

Result on Thingspeak:

Data on Thingspeak

Serial monitor: To open serial monitor select baud rate 9600.

Why all the values on serial monitor are NOT uploaded to Thingspeak?

One thing you will notice is that not all the values generated / shown on serial monitor is uploaded to Thingspeak, this is because Thingspeak server has a limit on how much data it can update in a given time.

Another reason is that communication between ESP8266 and Arduino is asynchronous. Arduino simply blasts some random values periodically to ESP8266 and ESP8266 accepts new data only after it successfully uploaded its current data (that’s how the code was written for the generic ESP8266 module).

If you have any questions ask us in the comment, you will get a guaranteed reply from us.

Avatar photo

My nick name is blogthor, I am a professional electronics engineer specialized in Embedded System. I am a experienced programmer and electronics hardware developer. I am the founder of this website, I am also a hobbyist, DIYer and a constant learner. I love to solve your technical queries via comment section.