Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

fetch - Arduino-ESP8266 GET request though ThingHTTP does not show up on Serial monitor

I'm brand new to Arduino coding. I'm trying to fetch weather conditions from a website using ESP8266 and thingspeak.com (ThingHTTP). I have written Arduino code to send AT commands to ESP8266 and display its responses on the Serial monitor. Here is my code.

#include <SoftwareSerial.h>

#define ESP8266_rxPin 2
#define ESP8266_txPin 3

// URL
const char URL_current_temp= "GET https://api.thingspeak.com/apps/thinghttp/send_request?api_key=A6QD2DQTCSIMUETK HTTP/1.0

";

SoftwareSerial ESP8266(ESP8266_rxPin, ESP8266_txPin);// rx tx

char espBuffer[1024] = {0};
int readCount = 0;
long startTime = millis();

void setup() {
  //Pin Modes for ESP TX/RX
  pinMode(ESP8266_rxPin, INPUT);
  pinMode(ESP8266_txPin, OUTPUT);
  ESP8266.begin(9600);
  ESP8266.listen();//not needed unless using other software serial instances
  Serial.begin(9600);
  delay(5000);//delay before kicking things off
  
  //Setup ESP8266

  //AT
  Serial.println("Sending an AT command...");
  ESP8266.print("AT
");
  delay(3000);
  while (ESP8266.available()){
  String inData1 = ESP8266.readStringUntil('
');
  Serial.println("Got reponse from ESP8266: " + inData1);
  }
  //AT+RST
  Serial.println("");
  Serial.println("Reseting ESP8266...");
  ESP8266.print("AT+RST
");
  delay(8000);
  while (ESP8266.available()){
  String inData2 = ESP8266.readStringUntil('
');
  Serial.println("Got reponse from ESP8266: " + inData2);
  }
  //AT+CWMODE
  Serial.println("");
  Serial.println("Setting CWMODE...");
  ESP8266.print("AT+CWMODE=1
");
  delay(3000);
  while (ESP8266.available()){
  String inData3 = ESP8266.readStringUntil('
');
  Serial.println("Got reponse from ESP8266: " + inData3);
  }
  //AT+CWJAP
  Serial.println("");
  Serial.println("Connecting to WiFi...");
  ESP8266.print("AT+CWJAP="ARTEM","ARTEM123"
");
  delay(8000);
  while (ESP8266.available()){
  String inData4 = ESP8266.readStringUntil('
');
  Serial.println("Got reponse from ESP8266: " + inData4);
  }
  //AT+CIPMUX
  Serial.println("");
  Serial.println("Setting CIPMUX...");
  ESP8266.print("AT+CIPMUX=1
");
  delay(3000);
  while (ESP8266.available()){
  String inData5 = ESP8266.readStringUntil('
');
  Serial.println("Got reponse from ESP8266: " + inData5);
  }
  
}//void setup()

void loop() {
  //Connecting to thingspeak
  Serial.println("");
  Serial.println("Connecting to host...");
  ESP8266.print("AT+CIPSTART=0,"TCP","api.thingspeak.com",80
");
delay(6000);
  while (ESP8266.available()){
  String inData6 = ESP8266.readStringUntil('
');
  Serial.println("Got reponse from ESP8266: " + inData6);
  }

  //AT+CIPSEND
  Serial.println("");
  Serial.println("Setting message length...");
  ESP8266.print("AT+CIPSEND=0,100
");
delay(4000);
  while (ESP8266.available()){
  String inData7 = ESP8266.readStringUntil('
');
  Serial.println("Got reponse from ESP8266: " + inData7);
  }
  
  //Sending request
  Serial.println("");
  Serial.println("Sending request...");
  ESP8266.print("GET https://api.thingspeak.com/apps/thinghttp/send_request?api_key=A6QD2DQTCSIMUETK HTTP/1.0

");

  while (millis() - startTime < 8000) { // Run for at least 8 seconds 
  // Check to make sure we don't exceed espBuffer's boundaries
  if (ESP8266.available() > readCount + sizeof espBuffer - 1) 
    break;
  readCount += ESP8266.readBytes(espBuffer + readCount, ESP8266.available());
}

Serial.println(espBuffer); 

delay(15000); 
}//void loop()

When I try fetching weather data directly through ESP8266 (bypassing Arduino) I get desired response on Serial monitor, but when I use Arduino to send commands, I get response from ESP8266 for everything except for the GET request. Here is my Serial monitor response. Can someone please help me figure out what is wrong? Arduino Serial monitor response from ESP8266

question from:https://stackoverflow.com/questions/65623069/arduino-esp8266-get-request-though-thinghttp-does-not-show-up-on-serial-monitor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...