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

조회 수: 13 (최근 30일)
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
John D Smith
John D Smith 2024년 11월 24일
I have lost faith in the world therefore the world is no longer trusted :-) ... thanks for the reply.

댓글을 달려면 로그인하십시오.

채택된 답변

John D Smith
John D Smith 2021년 8월 29일
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 ...

추가 답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Write Data to Channel에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by