ESP8266 NodeMCU Publish Sensor Readings to ThingSpeak (easiest way)

In this guide, you’ll learn how to send sensor readings with the ESP8266 NodeMCU board to ThingSpeak. For demonstration purposes, we’ll use a BME280 sensor, but you can easily modify the examples to use any other sensor. The ESP8266 board will be programmed using the Arduino core.

ESP8266 NodeMCU Publish Sensor Readings to ThingSpeak easiest way Arduino IDE Core

ThingSpeak allows you to publish your sensor readings to their website and plot them in charts with timestamps. Then, you can access your readings from anywhere in the world.

We have a similar tutorial for the ESP32 board: ESP32 Publish Sensor Readings to ThingSpeak (easiest way)

Project Overview

There are many ways to send sensor readings to ThingSpeak. In this tutorial, we’ll use one of the easiest waysโ€”using the thingspeak-arduino library. This library provides methods to easily publish sensor readings to single fields or multiple fields. You can check the library examples on its GitHub page.

To exemplify, we’ll use the BME280 sensor, but you can use any other sensor (you need to modify the code).

Recommended reading: ESP8266 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity).

We’ll cover how to publish to a single field and how to publish to multiple fields.

Preparing Arduino IDE

For this tutorial we’ll program the ESP8266 using the Arduino core. So, make sure you have the ESP8266 add-on installed in your Arduino IDE:

If you want to program the ESP8266 using VS Code with the PlatformIO extension, follow the next tutorial instead:

Installing the ThingSpeak Library

To send sensor readings to ThingSpeak, we’ll use the thingspeak-arduino library. You can install this library through the Arduino Library Manager. Go to Sketch > Include Library > Manage Libraries… and search for “ThingSpeak” in the Library Manager. Install the ThingSpeak library by MathWorks.

Install ThingSpeak Library Arduino IDE

Installing BME280 Libraries

As mentioned previously, we’ll publish sensor readings from a BME280 sensor. So, you also need to install the libraries to interface with the BME280 sensor.

You can install the libraries using the Arduino Library Manager. Go to Sketch Include Library > Manage Libraries and search for the library name.

Installing Libraries (VS Code + PlatformIO)

If you’re using VS Code with the PlatformIO extension, copy the following to the platformio.ini file to include the libraries.

lib_deps = mathworks/ThingSpeak@^2.0.0
            adafruit/Adafruit Unified Sensor @ ^1.1.4
            adafruit/Adafruit BME280 Library @ ^2.1.2  

Building the Circuit

To exemplify how to send data to ThingSpeak, we’ll send sensor readings from a BME280 sensor. So, you need to wire a BME280 sensor to your ESP8266.

Parts Required

To complete this tutorial you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic Diagram

Weโ€™re going to use I2C communication with the BME280 sensor module. For that, wire the sensor to the default ESP8266 SCL (GPIO 5) and SDA (GPIO 4) pins, as shown in the following schematic diagram.

ESP8266 NodeMCU BME280 Sensor Temperature Humidity Pressure Wiring Diagram Circuit

Recommended reading: ESP8266 Pinout Reference: Which GPIO pins should you use?

ThingSpeak – Getting Started

Go to ThingSpeak an click the โ€œGet Started For Freeโ€ button to create a new account. This account is linked to a Mathworks account. So, if you already have a Mathworks account, you should log in with that account.

Creating New Channel

After your account is ready, sign in, open the โ€œChannelsโ€ tab and select “My Channels“.

ThingSpeak Getting Started my channels tab

Press the โ€œNew Channelโ€ button to create a new channel.

ESP32 ESP8266 NodeMCU ThingSpeak Create New Channel

Type a name for your channel and add a description. In this example, we’ll just publish temperature. If you want to publish multiple readings (like humidity and pressure), you can enable more fields.

ThingSpeak Edit Add New Channel Settings Configuration

Click the Save Channel button to create and save your channel.

ESP32 ESP8266 NodeMCU ThingSpeak Save New Channel

Customizing Chart

The chart can be customized, go to your Private View tab and click on the edit icon.

ThingSpeak Customize Chart Private View ThingSpeak

You can give a title to your chart, customize the background color, x and y axis, and much more.

ThingSpeak field 1 chart options save settings

When youโ€™re done, press the โ€œSaveโ€ button.

API Key

To send values from the ESP8266 to ThingSpeak, you need the Write API Key. Open the โ€œAPI Keysโ€ tab and copy the Write API Key to a safe place because youโ€™ll need it in a moment.

Thingspeak Write API Key generate new key

ESP8266 Publish Sensor Readings to ThingSpeak – Code

Copy the following code to your Arduino IDE (or to the main.cpp file if you’re using PlatformIO).

/*
  Adapted from WriteSingleField Example from ThingSpeak Library (Mathworks)
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-thingspeak-publish-arduino/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

const char* ssid = "REPLACE_WITH_YOUR_SSID";   // your network SSID (name) 
const char* password = "REPLACE_WITH_YOUR_PASSWORD";   // your network password

WiFiClient  client;

unsigned long myChannelNumber = X;
const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX";

// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;

// Variable to hold temperature readings
float temperatureC;
//uncomment if you want to get temperature in Fahrenheit
//float temperatureF;

// Create a sensor object
Adafruit_BME280 bme; //BME280 connect to ESP8266 I2C (GPIO 4 = SDA, GPIO 5 = SCL)

void initBME(){
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
}

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

void loop() {
  if ((millis() - lastTime) > timerDelay) {
    
    // Connect or reconnect to WiFi
    if(WiFi.status() != WL_CONNECTED){
      Serial.print("Attempting to connect");
      while(WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, password); 
        delay(5000);     
      } 
      Serial.println("\nConnected.");
    }

    // Get a new temperature reading
    temperatureC = bme.readTemperature();
    Serial.print("Temperature (ยบC): ");
    Serial.println(temperatureC);
    
    //uncomment if you want to get temperature in Fahrenheit
    /*temperatureF = 1.8 * bme.readTemperature() + 32;
    Serial.print("Temperature (ยบF): ");
    Serial.println(temperatureF);*/
    
    
    // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
    // pieces of information in a channel.  Here, we write to field 1.
    int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureC, myWriteAPIKey);
    //uncomment if you want to get temperature in Fahrenheit
    //int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureF, myWriteAPIKey);

    if(x == 200){
      Serial.println("Channel update successful.");
    }
    else{
      Serial.println("Problem updating channel. HTTP error code " + String(x));
    }
    lastTime = millis();
  }
}

View raw code

To make the code work, you need to insert your network credentials in the following variables:

const char* ssid = "REPLACE_WITH_YOUR_SSID";   
const char* password = "REPLACE_WITH_YOUR_PASSWORD";  

You need to insert the number of the channel that you’re publishing to. If you only have one channel created in ThingSpeak, the channel number is 1. Otherwise, you can see the number of the channel on the Private View tab.

unsigned long myChannelNumber = 1;

Finally, you need to insert the Write API key you’ve gotten from the previous steps:

const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX";

How the Code Works

First, you need to include the necessary libraries.

#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

Insert your network credentials in the following variables:

const char* ssid = "REPLACE_WITH_YOUR_SSID";   
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; 

Create a Wi-Fi client to connect to ThingSpeak.

WiFiClient client;

Insert your channel number as well as your write API key:

unsigned long myChannelNumber = 1;
const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX";

In the timerDelay variable insert how frequently you want to publish readings. In this case, we’re publishing readings every 30 seconds (30000 milliseconds). You can change this delay time if you want.

unsigned long lastTime = 0;
unsigned long timerDelay = 30000;

The temperatureC variable holds the temperature value in Celsius degrees.

float temperatureC;

If you want to get the temperature in Fahrenheit degrees, uncomment the following line.

//float temperatureF;

Create an Adafruit_BME280 object called bme on the default ESP8266 pins.

Adafruit_BME280 bme; //BME280 connect to ESP8266 I2C (GPIO 4 = SDA, GPIO 5 = SCL)

The initBME() function initializes the BME280 sensor.

void initBME(){
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
}

In the setup(), initialize the Serial Monitor:

Serial.begin(115200);  //Initialize serial

Initialize the BME280 sensor.

initBME();

Set the ESP8266 as a Wi-Fi station:

WiFi.mode(WIFI_STA);

Initialize ThingSpeak:

ThingSpeak.begin(client);  // Initialize ThingSpeak

In the loop(), connect or reconnect to Wi-Fi in case the connection was lost:

// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
  Serial.print("Attempting to connect");
  while(WiFi.status() != WL_CONNECTED){
    WiFi.begin(ssid, password); 
    delay(5000);     
  } 
  Serial.println("\nConnected.");
}

Get a new temperature reading and print it in the Serial Monitor:

temperatureC = bme.readTemperature();

Uncomment the following lines if you want to get the temperature in Fahrenheit degrees.

/*temperatureF = 1.8 * bme.readTemperature() + 32;
Serial.print("Temperature (ยบC): ");
Serial.println(temperatureF);*/

Finally, write to ThingSpeak. You can use the writeField() method that accepts as arguments:

  • the channel number;
  • the field number (in our case, we just have one field);
  • the value you want to publish (temperatureC or temperatureF);
  • your write API key.

This function returns the code 200 if it has succeeded in publishing the readings.

int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureC, myWriteAPIKey);
if(x == 200){
  Serial.println("Channel update successful.");
}
else{
  Serial.println("Problem updating channel. HTTP error code " + String(x));
}

If you want to publish your readings in Fahrenheit degrees, uncomment the following line in the code:

//int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureF, myWriteAPIKey);

Demonstration

After inserting your network credentials, channel number and API key, upload the code to your board.

Open the Serial Monitor at a baud rate of 115200, and press the on-board RST button. After 30 seconds, it should connect to Wi-Fi and start publishing the readings to ThingSpeak.

Publish Temperature Readings ThingSpeak ESP8266 NodeMCU Serial Monitor

Go to your ThingSpeak account to the channel you’ve just created, and you’ll see the temperature readings being published and plotted on the chart.

ThingSpeak Chart BME280 Readings ESP8266 NodeMCU

Now, you can get access to those readings from anywhere in the world by accessing your ThingSpeak account.

Sending Multiple Fields (Temperature, Humidity, and Pressure)

In this section, you’ll learn how to send multiple fieldsโ€”this is sending more than one value at a timeโ€”we’ll send temperature, humidity, and pressure readings.

Enable Multiple Fields – ThingSpeak

First, you need to create more fields in your ThingSpeak account. This is simple. You need to go to your Channel Settings and add as many fields as you want. In this case, we’ve added two more fields, one for the humidity and another for the pressure.

Thingspeak add new fields channel settings change

Finally, save the channelโ€”click the Save Channel button.

ESP32 ESP8266 NodeMCU ThingSpeak Save New Channel

Now, if you go to the Private View tab, you should have three charts. Edit the newly created charts with a title and axis labels.

ESP8266 Write Multiple Fields to ThingSpeak – Code

The following code sends multiple fields to ThingSpeak (temperature, humidity, and pressure from the BME280 sensor).

/*
  Adapted from WriteSingleField Example from ThingSpeak Library (Mathworks)
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-thingspeak-publish-arduino/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

const char* ssid = "REPLACE_WITH_YOUR_SSID";   // your network SSID (name) 
const char* password = "REPLACE_WITH_YOUR_PASSWORD";   // your network password

WiFiClient  client;

unsigned long myChannelNumber = X;
const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX";

// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;

// Variable to hold temperature readings
float temperatureC;
float humidity;
float pressure;
//uncomment if you want to get temperature in Fahrenheit
//float temperatureF;
String myStatus = "";

// Create a sensor object
Adafruit_BME280 bme; //BME280 connect to ESP8266 I2C (GPIO 4 = SDA, GPIO 5 = SCL)

void initBME(){
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
}

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

void loop() {
  if ((millis() - lastTime) > timerDelay) {
    
    // Connect or reconnect to WiFi
    if(WiFi.status() != WL_CONNECTED){
      Serial.print("Attempting to connect");
      while(WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, password); 
        delay(5000);     
      } 
      Serial.println("\nConnected.");
    }

    // Get a new temperature reading
    temperatureC = bme.readTemperature();
    Serial.print("Temperature (ยบC): ");
    Serial.println(temperatureC);
    humidity = bme.readHumidity();
    Serial.print("Humidity (%): ");
    Serial.println(humidity);
    pressure = bme.readPressure() / 100.0F;
    Serial.print("Pressure (hPa): ");
    Serial.println(pressure);
    
    //uncomment if you want to get temperature in Fahrenheit
    /*temperatureF = 1.8 * bme.readTemperature() + 32;
    Serial.print("Temperature (ยบF): ");
    Serial.println(temperatureF);*/

    // set the fields with the values
    ThingSpeak.setField(1, temperatureC);
    //ThingSpeak.setField(1, temperatureF);
    ThingSpeak.setField(2, humidity);
    ThingSpeak.setField(3, pressure);

    
    // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
    // pieces of information in a channel.  Here, we write to field 1.
    int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

    if(x == 200){
      Serial.println("Channel update successful.");
    }
    else{
      Serial.println("Problem updating channel. HTTP error code " + String(x));
    }
    lastTime = millis();
  }
}

View raw code

This code is very similar to the previous one, but sends multiple fields. Let’s take a look at the relevant parts for this example.

Frist, create variables to hold the sensor readings:

float temperatureC;
float humidity;
float pressure;

In the loop(), get new temperature, humidity and pressure readings:

// Get a new temperature reading
temperatureC = bme.readTemperature();
Serial.print("Temperature (ยบC): ");
Serial.println(temperatureC);
humidity = bme.readHumidity();
Serial.print("Humidity (%): ");
Serial.println(humidity);
pressure = bme.readPressure() / 100.0F;
Serial.print("Pressure (hPa): ");
Serial.println(pressure);

You need to assign a value to each field. If you’ve created the fields as we did, the first field corresponds to the temperature, the second to the humidity, and the third to the pressure. The following lines assign the corresponding values to each field using the setField() methodโ€”it accepts as arguments the field number and the value:

// set the fields with the values
ThingSpeak.setField(1, temperatureC);
//ThingSpeak.setField(1, temperatureC);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, pressure);

Finally, use the writeFields() method and set as arguments the channel number and the write API key:

int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

Demonstration

Upload the previous code to your boardโ€”don’t forget to insert your network credentials, Write API Key and channel number. After uploading, it should connect successfully and send the readigns:

ESP8266 NodeMCU Write Multiple Fields ThingSpeak Serial Monitor

If you go to your ThingSpeak account, under Private View, you can see three charts with the sensor readings.

ESP8266 NodeMCU Publish Send Sensor Readings to ThingSpeak Demonstration easiest way

Wrapping Up

In this tutorial, you’ve learned how to publish readings from a BME280 sensor to ThingSpeak using the ESP8266 and the arduino-thingspeak library. You can change the examples to send readings from any other sensors or data from any other source. We have tutorials for the most popular sensors:

You can visualize the sensor readings from anywhere by logging in to your ThingSpeak account.

The library used throughout this tutorial provides several examples that may be usefulโ€”check the examples here or in your Arduino IDE go to File > Examples > ThingSpeak, and you’ll have several examples.

Learn more about the ESP8266 with our resources:

Thanks for reading.



Learn how to build a home automation system and weโ€™ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB databaseย DOWNLOAD ยป
Learn how to build a home automation system and weโ€™ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB databaseย DOWNLOAD ยป

Enjoyed this project? Stay updated by subscribing our newsletter!

21 thoughts on “ESP8266 NodeMCU Publish Sensor Readings to ThingSpeak (easiest way)”

  1. HEY, thanks for posting this project i have made the same project and there is some error and daly in the data uploding. could your please check the error. i am dropping the link in website coloumn.

    Reply
  2. Hello Rui,

    Great article! I previously implemented your older articles using the DHT11 and DS18B20 sensors in a web server and they worked flawlessly. I was unhappy with the DHT11, but only because that sensor had such a huge offset. The offset was way outside of the accuracy listed in the sensors spec sheet, but it was a good exercise. I moved on to the DS18B20 sensor and the accuracy is much better.

    I’ve just implemented your project using the BME280 sensor, but instead, I modified your code to read the DS18B20, instead.

    Everything works flawlessly, and ThingSpeak reports the data (my channel is: 1418992).

    Even so, I have a question about the code. Under the definitions section, you have:
    “unsigned long myChannelNumber = X;”.

    In your explanation of the code, you stated that if I only have one channel created in ThingSpeak, the channel number is “1”. First, is what you call myChannelNumber the same as my ThingSpeak channel ID? If so, then in the code, I would assign 1418992 to that variable (which I have done and this seems to work). The statement about it being “1” if I only have one channel is confusing because I do in fact, only have one channel in ThingSpeak, and it’s not “1” if “channel” is the same as ThingSpeak’s “Channel ID”.

    Again, thanks for a great tutorial. I follow you as much as I can now that I’m semi-retired.

    Best,
    James

    Reply
    • Hi James.
      Thanks for your comment.

      In my understanding, the channel is the number under the “MATLAB Visualization” button when you open your channel.
      However, each channel has a unique channel ID. So, I guess that number may also work.

      Regards,
      Sara

      Reply
    • Hello James and Sara;
      From one semi-retired to another, thanks for your post.
      I’m using an Wemos D1 ESP8266 and after inserting the Channel ID in X, it works fine for me.
      Thanks for the tutorial and post itself.
      Best regards,
      Francisco.

      Reply
  3. Great article, and I’ve implemented it. ThingSpeak is great for this stuff. However, I have a somewhat tangential, yet related question.

    Is it possible to have ThingSpeak display a running count of single-bit events?

    For example, suppose I wish to count the number of “hits” from a PIR motion sensor without adding code in my NodeMCU to handle it.

    Do you know if ThingSpeak is capable of this? After all, it does allow one to configure a data window with number of samples, timing, etc., but I cannot make it display a running tally.

    Thanks,
    James

    Reply
      • Thanks for your reply.

        It looks like I’ll need to implement a software counter and then feed the data from the counter, as a “channel” to ThingSpeak. I was hoping to be lazy ๐Ÿ™‚, but that makes more sense anyway because of the frequent delays between changes in data values and ThingSpeak’s display of that data.
        Thanks again,
        James

        Reply
  4. Hi Guys,
    Thank you for this great project.
    I have tried to adapt the code for the DHT22 sensor and I am gett in a compilation error of
    “exit status 1
    Error compiling for board Generic ESP8266 Module.”

    Reply
      • Hi Sara, forgive my basic knowledge. This is my first attempt at altering code for a specific purpose.
        I think this error message suggests that the error is in line 40 or 51?

        40 | #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

        | ^~~~~

        C:\Users\User\OneDrive\Documents\Arduino\These work\dht22sensor_chart\dht22sensor_chart.ino:51:1: note: in expansion of macro ‘DHTTYPE’

        51 | DHTTYPE DHT22; //DHTTYPE DHT22 connect to ESP8266 I2C (GPIO 4 = SDA, GPIO 5 = SCL)

        | ^~~~~~~

        Using library ESP8266WiFi at version 1.0 in folder: C:\Users\User\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266WiFi

        Using library ThingSpeak at version 2.0.1 in folder: C:\Users\User\OneDrive\Documents\Arduino\libraries\ThingSpeak

        Using library DHT-sensor-library-master at version 1.3.10 in folder: C:\Users\User\OneDrive\Documents\Arduino\libraries\DHT-sensor-library-master

        Using library Adafruit_Sensor-master at version 1.1.4 in folder: C:\Users\User\OneDrive\Documents\Arduino\libraries\Adafruit_Sensor-master

        exit status 1

        Error compiling for board Generic ESP8266 Module.

        Reply
  5. Hello,

    Great tutorial, so informative.
    Do you know have any course/tutorial for bulk write/update of data on thingspeak?
    As an example, the temperature reads the data every 5 min, but to save battery, it sends the data every 1hour via Bulk write/Post on Thingspeak.
    Cheers,
    Matthew

    Reply
  6. I think I’ve set everything up right but I still get this error message, any help would be appreciated. ‘invalid use of incomplete type ‘class setup()::WiFiClient’

    Reply
  7. Hi,
    I am making project where I am using multiple sensors, arduino uno r3 and Nodemcu esp8266. I want to read data from the sensors using arduino and upload the data to thingspeak channel through Nocemcu esp8266. But I’m having a trouble to post the accurate data to thingspeake. Please help me out here with proper coding. Here I’m uploading my codes for arduino and nodemcu esp8266.

    ๐€๐ซ๐๐ฎ๐ข๐ง๐จ ๐‚๐จ๐๐ž:
    #include <SoftwareSerial.h>
    #include “DHT.h”
    #include <Wire.h>
    #include <SPI.h>
    #define DHTPIN 7
    #define DHTTYPE DHT11
    DHT dht(DHTPIN, DHTTYPE);
    SoftwareSerial nodemcu(3, 2); // Rx = 8 & Tx = 9
    String myString;

    void setup() {
    dht.begin();
    Serial.begin(9600);
    nodemcu.begin(9600);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    float h = dht.readHumidity();
    Serial.print(F(“Humidity: “));
    Serial.println(h);
    nodemcu.println(h);

    float t = dht.readTemperature();
    Serial.print(F(“Temperature: “));
    Serial.println(t);
    nodemcu.println(t);
    // myString = String(“coming from arduino: “) + “\n” + String(“Humidity: “) + String(h) + “\n” + String(“Temperature: “) + String(t) + “\n”;
    delay(3000);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ๐๐จ๐๐ž๐ฆ๐œ๐ฎ ๐ž๐ฌ๐ฉ๐Ÿ–๐Ÿ๐Ÿ”๐Ÿ” ๐‚๐จ๐๐ž:
    #include <SoftwareSerial.h>
    #include <ESP8266WiFi.h>
    #include <Wire.h>

    String apiKey = “*******************”; // Enter your Write API key here
    const char *ssid = “xxxxxxx”; // Enter your WiFi Name
    const char *pass = “yyyyyyy”; // Enter your WiFi Password
    const char *server = “api.thingspeak.com”;
    WiFiClient client;
    String myString; // complete message from Arduino, which consists of sensors data
    float h,t;

    void setup() {
    // Debug console
    Serial.begin(9600);
    Serial.println(“Connecting to “);
    Serial.println(ssid);

    //connect to your local wi-fi network
    WiFi.begin(ssid, pass);

    //check wi-fi is connected to wi-fi network
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(“.”);
    }
    Serial.println(“”);
    Serial.println(“WiFi connected”);
    Serial.print(“IP Address: “);
    delay(5000);
    Serial.println(WiFi.localIP());
    delay(5000);
    }

    void loop() {
    h = Serial.read();
    // myString=myString+h;
    // Serial.println(h);

    t = Serial.read();
    // myString=myString+t;
    // Serial.println(t);

    if (client.connect(server, 80)) {

    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(h);
    postStr += "\r\n\r\n";

    postStr += "&field2=";
    postStr += String(t);
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);

    }
    client.stop();
    Serial.println(“Waiting…”);
    delay(3000);
    }

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.