Use arduino NodeMCU to send HTTP GET request and the return value is abnormal

0

I am a beginner who is new to NodeMCU. I am using Arduino ESP8266. After the WiFi is successfully connected, I make a GET request through the API. When the value is returned, the HTTP Response status is 200 OK, but the return is not in JSON format. Data, but "HTTP Response code: 200"

Exception (28):
epc1=0x40207b64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x007a1200 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffd60 end: 3fffffc0 offset: 0190
3ffffef0: 00000000 4bc6a7f0 80cac083 00000000
3fffff00: 00000000 000000c8 00000289 00000001
3fffff10: 00000000 0000000a 40100200 3ffee5ec
3fffff20: 3ffee51c 3ffee510 3ffee570 402054f8
3fffff30: 0289028f 80442352 3fffff60 40205881
3fffff40: 3ffee51c 3ffee510 3ffee570 402058dd
3fffff50: 3fffdad0 3ffee510 3ffee570 4020261a
3fffff60: 00000000 00000000 80fee500 402058c0
3fffff70: 00000000 3ffee520 3ffee570 4020590c
3fffff80: 00000000 00000000 00000001 40100154
3fffff90: 3fffdad0 00000000 3ffee5ac 3ffee5ec
3fffffa0: 3fffdad0 00000000 3ffee5ac 40206c5c
3fffffb0: feefeffe feefeffe 3ffe8500 40100bc1
<<<stack<<<

So I hope someone can help me out, thanks!

The following is my code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>

const char* ssid = "216-2.4G";
const char* password = "iwantwifi";

//Your Domain name with URL path or IP address with path
const char* serverName = "http://192.168.1.11:3001/api/students";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

String sensorReadings;
//float sensorReadingsArr[3];


void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    Serial.print(WiFi.status());
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if (WiFi.status() == WL_CONNECTED) {

      sensorReadings = httpGETRequest(serverName);
      Serial.println(sensorReadings);
      JSONVar myObject = JSON.parse(sensorReadings);

      // JSON.typeof(jsonVar) can be used to get the type of the var
      if (JSON.typeof(myObject) == "undefined") {
        Serial.println("Parsing input failed!");
        return;
      }

      Serial.print("JSON object = ");
      Serial.println(myObject);

      // myObject.keys() can be used to get an array of all the keys in the object
      JSONVar keys = myObject.keys();

      for (int i = 0; i < keys.length(); i++) {
        JSONVar value = myObject[keys[i]];
        Serial.print(keys[i]);
        Serial.print(" = ");
        Serial.println(value);
      }
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

String httpGETRequest(const char* serverName) {
  HTTPClient http;

  // Your IP address with path or Domain name with URL path
  http.begin(serverName);

  // Send HTTP POST request
  int httpResponseCode = http.GET();

  String payload = "{}";

  if (httpResponseCode > 0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
}
exception
arduino
http-get
arduino-esp8266
asked on Stack Overflow Aug 16, 2020 by Steven • edited Aug 17, 2020 by gre_gor

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0