Reading data from private channel using suggested mathlab code. I get a read error ...

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 comentarios

Oh dear oh dearie me ...
Caught by the old O and 0 in the API key. I rather stupidly typed the read api into secrets rather than copy and paste from my browser.
Possibly a good addition to help files would be that very suggestion ... I have been a clutz before and had this happen. Reassuring I had the code kinda right though :-) I have posted this as a comment that it might help others.
Great result however ...
Connected
Calculation Temp): 989
Raw Air Pressure: 1010
Adjust Air Pressure to MSL 176
Altitude (M):
*/
Which is fine, however in my paniced attempt at getting it to read I changed the field numbers, and blocked out possible error lines.... Here is the intended result I was looking for.
Connected
Calculation Temp): 8
Raw Air Pressure: 989
Adjust Air Pressure to MSL 1010
Altitude (M): 176
Status Message, if any:
Latitude, if any (+ve is North, -ve is South):
Longitude, if any (+ve is East, -ve is West):
Elevation, if any (meters above sea level):
Created at, if any (YYYY-MM-DD hh:mm:ss): 2021-08-29T00:41:43Z
The fun thing to do now is build a code to feed this data into a database of my own, that is used to trigger a frost machine in my vege garden.
All fun and games in lockdown :-)
What has happened to Sha1 security ... I am not a close follower of the news on thingspeak ... has there been a change in the way ssl is done?.
I have lost faith in the world therefore the world is no longer trusted :-) ... thanks for the reply.

Iniciar sesión para comentar.

 Respuesta aceptada

See comment on my question above ... was a typing error in the read api key.
Yes I know eliminate the obvious first then ask questions ...

Más respuestas (0)

Comunidades de usuarios

Más respuestas en  ThingSpeak Community

Categorías

Más información sobre Write Data to Channel en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Ag. de 2021

Comentada:

el 24 de Nov. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by