Hello, have arduino uno + esp8266. Can't read from thingspeak, however writing data to thingspeak works. I got "Failed to read from ThingSpeak". Thanks in advance
#include <SoftwareSerial.h>
#include <DHT.h>
#include <ThingSpeak.h>
#include <ArduinoJson.h>
#include <TFT.h>
#include <SPI.h>
#define RX 2 // ESP8266 RX pin
#define TX 3 // ESP8266 TX pin
String AP = ""; // AP NAME
String PASS = ""; // AP PASSWORD
String API = ""; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
unsigned long READ_CHANNEL_ID = 111111;
const char* READ_API_KEY = "";
SoftwareSerial esp8266(RX, TX);
#define cs 10 // cs pin
#define dc 9 // dc pin
#define rst 8 // reset pin
TFT TFTscreen = TFT(cs, dc, rst); // set pins for display function
DHT dht(7, DHT11); // set pins for Sensor function
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
pinMode(8, INPUT);
dht.begin(); // run Sensor
TFTscreen.begin(); // run display
TFTscreen.background(0, 0, 0); // Clear display
TFTscreen.setTextSize(2); // set type
}
void loop() {
int randomGeneratedValue = random(0, 100);
// it doesn't
readFromThingSpeak(READ_CHANNEL_ID, READ_API_KEY);
// it works
// sendToThingSpeak(randomGeneratedValue);
// Wait for a few seconds
delay(5000);
}
void readFromThingSpeak(unsigned long channelID, const char* apiKey) {
String url = "/channels/" + String(channelID) + "/fields/1.json?api_key=" + apiKey;
Serial.println("Reading from ThingSpeak: " + url);
sendCommand("AT+CIPMUX=1", 5, "OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(url.length() + 4), 4, ">");
esp8266.println("GET " + url + " HTTP/1.1");
delay(1500);
sendCommand("AT+CIPCLOSE=0", 5, "OK");
String response = esp8266.readStringUntil('\n');
Serial.print("length: " + String(response.length()))
Serial.print("content: " + content)
if (response.indexOf("OK") != -1) {
String json = esp8266.readStringUntil('\n');
DynamicJsonDocument doc(1024);
deserializeJson(doc, json);
int value = doc["field1"];
Serial.println("Value from ThingSpeak: " + String(value));
TFTscreen.print("Value: ");
TFTscreen.print(value);
} else {
Serial.println("Failed to read from ThingSpeak");
}
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". ");
Serial.print(command);
Serial.println(" ");
Serial.println(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);
if(esp8266.find(readReplay))
{
found = true;
break;
}
countTimeCommand++;
}
Serial.print(esp8266.readString());
if(found == true)
{
Serial.println("Response is OK");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Response is fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
delay(1000);
}
// void sendToThingSpeak(float data) {
// String url = "GET /update?api_key=" + API + "&" + field + "=" + String(data);
// Serial.println("Sending to ThingSpeak: " + url);
// sendCommand("AT+CIPMUX=1", 5, "OK");
// sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
// sendCommand("AT+CIPSEND=0," + String(url.length() + 4), 4, ">");
// esp8266.println(url);
// delay(1500);
// sendCommand("AT+CIPCLOSE=0", 5, "OK");
// }