Hardware: ESP8266 based boards
#define TS_ENABLE_SSL // For HTTPS SSL connection
#include <WiFiClientSecure.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
// setup channel variables
unsigned long chl111 = SECRET_CH_ID;
const char * chl111_api = SECRET_READ_APIKEY_COUNTER;
unsigned int counterFieldNumber = 0;
long int field[4] = {1,2,3,4};
// Fingerprint check, make sure that the certificate has not expired.
const char * fingerprint = SECRET_SHA1_FINGERPRINT; // use SECRET_SHA1_FINGERPRINT for fingerprint check
Serial.begin(115200); // Initialize serial
; // wait for serial port to connect. Needed for Leonardo native USB port only
client.setFingerprint(fingerprint);
client.setInsecure(); // To perform a simple SSL Encryption
ThingSpeak.begin(client); // Initialize ThingSpeak
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.println("\nConnected");
// Read and store all the latest field values, location coordinates, status message, and created-at timestamp
// use ThingSpeak.readMultipleFields(channelNumber, readAPIKey) for private channels
statusCode = ThingSpeak.readMultipleFields(chl111,chl111_api);
int calcTemp = ThingSpeak.getFieldAsInt(field[1]);// Field 1
int mBarRaw = ThingSpeak.getFieldAsInt(field[2]); // Field 2
int mBarMSL = ThingSpeak.getFieldAsInt(field[3]); // Field 3
int calcAlt = ThingSpeak.getFieldAsInt(field[4]); // Field 4
/* String statusMessage = ThingSpeak.getStatus(); // Status message
String latitude = ThingSpeak.getLatitude(); // Latitude
String longitude = ThingSpeak.getLongitude(); // Longitude
String elevation = ThingSpeak.getElevation(); // Elevation
String createdAt = ThingSpeak.getCreatedAt(); // Created-at timestamp
Serial.println("Calculation Temp): " + String(calcTemp));
Serial.println("Raw Air Pressure: " + String(mBarRaw));
Serial.println("Adjust Air Pressure to MSL " + String(mBarMSL));
Serial.println("Altitude (M): " + String(calcAlt));
/* Serial.println("Status Message, if any: " + statusMessage);
Serial.println("Latitude, if any (+ve is North, -ve is South): " + latitude);
Serial.println("Longitude, if any (+ve is East, -ve is West): " + longitude);
Serial.println("Elevation, if any (meters above sea level): " + elevation);
Serial.println("Created at, if any (YYYY-MM-DD hh:mm:ss): " + createdAt);
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
delay(10000); // no need to fetch too often