Reading data from private channel using suggested mathlab code. I get a read error ...
Show older comments
I am in the process of building a small ESP8266 driven data screen thingy thing ... that would show some data from a private thingspeak channel. In recognition of a need for security I wish to use SSL.
I have adjusted the Mathlab code to suit my situation, but for some reason I keep getting an error 400 which is a bad request from the client side (I.e. Me not thingspeak)
I have read and re-read my code and would now like a fresh pair of eyes to spot the obvious error.
/*
ReadMultipleFields
Hardware: ESP8266 based boards
*/
#define TS_ENABLE_SSL // For HTTPS SSL connection
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include "secrets.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)
WiFiClientSecure client;
// setup channel variables
unsigned long chl111 = SECRET_CH_ID;
const char * chl111_api = SECRET_READ_APIKEY_COUNTER;
unsigned int counterFieldNumber = 0;
int statusCode = 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
void setup() {
Serial.begin(115200); // Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
if(fingerprint!=NULL){
client.setFingerprint(fingerprint);
}
else{
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.print(".");
delay(5000);
}
Serial.println("\nConnected");
}
}
void loop() {
// 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);
if(statusCode == 200)
{
// Fetch the stored data
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);
*/
}
else{
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
Serial.println();
delay(10000); // no need to fetch too often
4 Comments
John D Smith
on 29 Aug 2021
John D Smith
on 23 Nov 2024
Walter Roberson
on 23 Nov 2024
SHA1 has been forbidden for all but root certificates since no later than 2017. https://serverfault.com/questions/837994/why-are-ca-root-certificates-all-sha-1-signed-since-sha-1-is-deprecated
John D Smith
on 24 Nov 2024
Accepted Answer
More Answers (0)
Communities
More Answers in the ThingSpeak Community
Categories
Find more on Write Data to Channel in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!