Use boards with WiFi

ESP8266/ESP32 and installation instructions using Arduino IDE Boards Manager

The ESP8266 is a low-cost Wi-Fi microchip with full TCP/IP stack and microcontroller capability produced by Shanghai-based Chinese manufacturer Espressif Systems [source: https://en.wikipedia.org/wiki/ESP8266]. The chip is usually embedded on a board such as the NodeMCU (see picture below) to make it easier to use. Most of ESP8266-based boards are compatible with Arduino IDE provided that you install the required addons. The successor to these microcontroller chips is the ESP32.

Again, many boards based on the ESP32 are available. Below is the Heltec ESP32 with a small OLED screen and also LoRa radio module that we will see later on.

Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).

You can refer for futher information on Arduino core for ESP8286 WiFi chip on github page for ESP8266.

As there are many boards based on both ESP8266 or ESP32, you can refer to the wikipedia page to try to look for your board.

Connecting a simple analog sensor to the ESP8266/ESP32

Sensor Pins Arduino Pins
VDD/VCC 5V
GND GND
OUTPUT A0

It is known that both ESP8266 and ESP32 have "bad" analog-to digital converter. So here the analog sensor is just for demonstration purpose. It is better for accuracy to get a digital sensor, such as the DHT22.

Brief description of the code

In the example below, we briefly explain setting up the wifi and establishing the connection. If the connection is successful, we measure the temperature. The temperature value is then uploaded to a ThingSpeak channel.

We will briefly explain the steps used in the example below to use the WiFi module.

If using ESP8266, use ESP98266WiFi.h header file. In case you are using ESP32, use the WiFi.h

 
#if defined ESP8266 || defined ARDUINO_ESP8266_ESP01
include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif

Define the network SSID and password, here for demonstration purpose, I usually use my smartphone as access point. Note that it is not possible to use a WiFi that requires pop-up windows or a complex authentication mechanism.

 
char* ssid = "iPhoneD";
char* password = "hello123";

Define the wifi client being used

 
WiFiClient espClient;

Then we define a dedicated function to setup the WiFi. setup_wifi() mainly defines the connection to the WiFi network using the SSID and the password, and then check if the WiFi is successfully connected.

void setup_wifi() {
  WiFi.begin(ssid, password);
}

In setup(), in addition to define the input pins we mainly call setup_wifi() function to connect to WiFi.

void setup() {
  delay(3000); 
  Serial.begin(38400);

  // for the temperature sensor 
  pinMode(TEMP_PIN_READ, INPUT);
  
  // Print a start message 
  Serial.println(F("Simple Temp WiFi+ThingSpeak demo"));  

  setup_wifi();
}

In loop(), we first check if the WiFi is connected. If is not connected, we attempt to connect to the WiFi again.

 
 if (WiFi.status() != WL_CONNECTED) {

    unsigned long start=millis();    
    while (WiFi.status() != WL_CONNECTED && millis()-start<15000) {
        WiFi.begin(ssid, password);
     }
 }

Then, if WiFi is connected and connection to the wifi client is established, then we upload to the ThingSpeak channel using a REST API.

 
 if (WiFi.status() == WL_CONNECTED && espClient.connect(server,80)) { 
    String postStr = apiKey;
    postStr +="&field3=";
    postStr += String(temp);
    postStr += "\r\n\r\n";
          
    espClient.print("POST /update HTTP/1.1\n");
    espClient.print("Host: api.thingspeak.com\n");
    espClient.print("Connection: close\n");
    espClient.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
    espClient.print("Content-Type: application/x-www-form-urlencoded\n");
    espClient.print("Content-Length: ");
    espClient.print(postStr.length());
    espClient.print("\n\n");
    espClient.print(postStr);
          
    Serial.println("Upload to Thingspeak");
           
    espClient.stop();    
 } 

Complete example

Here is the complete working and tested example of the an Arduino board with WiFi. Data is uploaded to the ThingSpeak channel 66794: https://thingspeak.com/channels/66794 on field 3.

  	
	    
// if you have an ESP8266 based board
#define ESP8266

#if defined ESP8266 || defined ARDUINO_ESP8266_ESP01
include <ESP8266WiFi.h>
// else if you have an ESP32
#else
#include <WiFi.h>
#endif

// Update these with values suitable for your network.
char* ssid = "iPhoneD";
char* password = "hello123";

// Test LoRa gateway channel, channel ID 66794
String apiKey = "SGSH52UGPVAUYG3S";
// To upload on thingspeak
const char* server = "api.thingspeak.com";

WiFiClient espClient;

#define TEMP_PIN_READ  A0 //will be pin 36 on the Heltec 
#define TEMP_SCALE  5000.0

// setting up the wifi
void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  unsigned long start=millis();
  
  while (WiFi.status() != WL_CONNECTED && millis()-start<15000) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  if (WiFi.status() == WL_CONNECTED) {
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
  }
  else {
      Serial.println("WiFi not connected");          
  }
} 

void setup() {
  delay(3000); 
  Serial.begin(38400);

  // for the temperature sensor 
  pinMode(TEMP_PIN_READ, INPUT);
  
  // Print a start message 
  Serial.println(F("Simple Temp WiFi+ThingSpeak demo"));  

  setup_wifi();
}

void loop() {

    if (WiFi.status() != WL_CONNECTED) {

        unsigned long start=millis();    
        while (WiFi.status() != WL_CONNECTED && millis()-start<15000) {
          Serial.print("Attempting to connect to WPA SSID: ");
          Serial.println(ssid);
          // Connect to WPA/WPA2 network
          WiFi.begin(ssid, password);
          delay(500);
        }
    }

    if (WiFi.status() == WL_CONNECTED) {
        Serial.println("Connected to AP");   
    }
    else  {
        Serial.println("WiFi not connected");        
    }

    int value = analogRead(TEMP_PIN_READ);
    Serial.print(F("Reading "));
    Serial.println(value);

    float temp;
  
    // change here how the temperature should be computed depending on your sensor type 
    temp = (value*TEMP_SCALE/1024.0)/10;
  
    if (isnan(temp)) {
      Serial.println("Failed to read from Temp sensor!");
    }
    else {
        Serial.print("Temperature: ");
        Serial.print(temp);
        Serial.println("Temperature: ");
             
      // "184.106.153.149" or api.thingspeak.com
      if (WiFi.status() == WL_CONNECTED && espClient.connect(server,80)) { 
          String postStr = apiKey;
          postStr +="&field3=";
          postStr += String(temp);
          postStr += "\r\n\r\n";
          
          espClient.print("POST /update HTTP/1.1\n");
          espClient.print("Host: api.thingspeak.com\n");
          espClient.print("Connection: close\n");
          espClient.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
          espClient.print("Content-Type: application/x-www-form-urlencoded\n");
          espClient.print("Content-Length: ");
          espClient.print(postStr.length());
          espClient.print("\n\n");
          espClient.print(postStr);
          
          Serial.println("Upload to Thingspeak");
           
          espClient.stop();    
        }      
    }
      
    delay(10000);  
 }                  

The raw source of the sketch example is visible here.

Enjoy!