简体   繁体   中英

ESP32 panics every other HTTP POST request

As the title suggests, my ESP32 panics every other HTTP POST request. For the first one it works fine, wuth the second one I get a 400 error, the ESP panics, and then restarts itself.

This wouldn't be this much of an issue if the ESP didn't power any other things, however, it does. The result is that the sensors it powers don't work properly and give wrong data. Below I have included the error code:

Verbonden met WiFi, met Ip adres: myip
Distance: 35 cm
Temperatuur: 19.70
Luchtvochtigheid: 74.00
Grondvochtigheid: 4095
Lichsterkte sensor 1: 432.00
Lichsterkte sensor 2: 288.00
Lichsterkte sensor 3: 0.00
HTTP Response code: 200
Test: {"Log":{}}
Distance: 35 cm
Temperatuur: 19.70
Luchtvochtigheid: 74.00
Grondvochtigheid: 4095
Lichsterkte sensor 1: 480.00
Lichsterkte sensor 2: 290.00
Lichsterkte sensor 3: 0.00
HTTP Response code: 400
Test: "Missing values"
Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0xc000e82e  PS      : 0x00060430  A0      : 0x800d3002  A1      : 0x3ffb1d90
A2      : 0x3ffb1f10  A3      : 0xc000e82e  A4      : 0x00000001  A5      : 0x0000ff00
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x8015dd02  A9      : 0x3ffc11dc
A10     : 0x3ffba818  A11     : 0x3f401763  A12     : 0x00000002  A13     : 0x0000ff00  
A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x0000000a  EXCCAUSE: 0x00000014
EXCVADDR: 0xc000e82c  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff

Backtrace: 0x4000e82e:0x3ffb1d90 0x400d2fff:0x3ffb1db0 0x400d3071:0x3ffb1dd0 0x400d1c4e:0x3ffb1df0 
0x400d6625:0x3ffb1fb0 0x40088f69:0x3ffb1fd0

Rebooting...
ets Jun  8 2016 00:22:57

My C++ code is as follows:

// Includen van alle benodigde libraries
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <DHT.h>

// Definen van alle datapins van alle componenten
#define DHTPIN 14
#define DHTTYPE DHT11
#define echoPin 26
#define grondPin 33
#define trigPin 27
#define lichtPin1 32
#define lichtPin2 35 
#define lichtPin3 34
//#define redPin 2
//#define greenPin 4

// magic number handlers
const int procentHulpwaarde = -1;
const int nulWaarde = 0;
const int potDiepte = 31;
const int grensWaardepostUpdate = 10;
const int procentRekenwaarde = 100;
const int sensorRekenwaarde = 4095;
const int timerDelaywaarde = 1000;

// Declaratie variabeles
String strluchtvochtigheid;
String strtemperatuur;
String strgrondvochtigheid;
String strlichtsterkte1;
String strlichtsterkte2;
String strlichtsterkte3;
String strwaterniveau;

double dblluchtvochtigheid = nulWaarde;
double dbltemperatuur = nulWaarde;
double lichtSterkte1;
double lichtSterkte2;
double lichtSterkte3;

int distance;
int sensorValueGrond; 
int postCounter;

long duration;
unsigned long lastTime = nulWaarde;
unsigned long timerDelay = timerDelaywaarde;

// Aanroepen van benodigdheden voor miscellaneous functies binnen de code
DHT dht(DHTPIN, DHTTYPE);
StaticJsonDocument<200> doc;

// Declaratie van WiFi netwerken die gebruikt kunnen worden

//const char* ssid = "12connect";
//const char* password = "";

// Declaratie van de eindbestemming
const char* serverName = "my target";

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

  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  //pinMode(redPin, OUTPUT);
  //pinMode(greenPin, OUTPUT);

  dht.begin();

  WiFi.begin(ssid, password);
  Serial.println("Connecting");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Verbonden met WiFi, met Ip adres: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  // Natuurkundige berekening voor de hoeveelheid water in het reservoir
  distance = duration * 0.034 / 2; 

  if ((millis() - lastTime) > timerDelay) {

    if (WiFi.status()== WL_CONNECTED) {

      //Lees alle sensoren uit

      // DHT sensor
      dblluchtvochtigheid = dht.readHumidity();
      dbltemperatuur = dht.readTemperature();

      // Grondvochtigheids sensor
      sensorValueGrond = analogRead(grondPin); 

      // Ultrasonic sensor
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      // Natuurkundige berekening voor de afstand
      distance = duration * 0.034 / 2; 

      // Lichtsterkte sensoren
      lichtSterkte1 = analogRead(lichtPin1);
      lichtSterkte2 = analogRead(lichtPin2);
      lichtSterkte3 = analogRead(lichtPin3);

      // Print alle waardes uit (voor debugging)
      Serial.print("Distance: ");
      Serial.print(distance);
      Serial.println(" cm");
      Serial.print("Temperatuur: ");
      Serial.println(dbltemperatuur);
      Serial.print("Luchtvochtigheid: ");
      Serial.println(dblluchtvochtigheid);
      Serial.print("Grondvochtigheid: ");
      Serial.println(sensorValueGrond);
      Serial.print("Lichtsterkte sensor 1: ");
      Serial.println(lichtSterkte1);
      Serial.print("Lichtsterkte sensor 2: ");
      Serial.println(lichtSterkte2);
      Serial.print("Lichtsterkte sensor 3: ");
      Serial.println(lichtSterkte3);

      HTTPClient http;
      http.begin(serverName);

      //procentuele omrekening van alle getallen
      sensorValueGrond = ((((sensorValueGrond  - sensorRekenwaarde) * 
  procentHulpwaarde) * procentRekenwaarde) / sensorRekenwaarde);
      distance = ((distance * procentRekenwaarde) / potDiepte) - 
  procentRekenwaarde;
      lichtSterkte1 = (lichtSterkte1 * procentRekenwaarde) / sensorRekenwaarde;
      lichtSterkte2 = (lichtSterkte2 * procentRekenwaarde) / sensorRekenwaarde;
      lichtSterkte3 = (lichtSterkte3 * procentRekenwaarde) / sensorRekenwaarde;

  
      // Omzetten van alle getallen naar strings, zo kunnen ze worden meegegeven in de JSON string
      strtemperatuur = strtemperatuur + dbltemperatuur;
      strluchtvochtigheid = strluchtvochtigheid + dblluchtvochtigheid;
      strwaterniveau = strwaterniveau + distance;
  strgrondvochtigheid = sensorValueGrond + strgrondvochtigheid;
      strlichtsterkte1 = strlichtsterkte1 + lichtSterkte1;
      strlichtsterkte2 = strlichtsterkte2 + lichtSterkte2;
      strlichtsterkte3 = strlichtsterkte3 + lichtSterkte3;

  
      // Toevoegen van headers aan de HTTP Post, zo kunnen we gebruik maken van gebruikersnamen en wachtwoorden
      http.addHeader("Content-Type", "application/json");
      http.addHeader("X-AUTH-TOKEN", "TEST");
      http.addHeader("X-AUTH-ID", "wajdhlawkjhdlawjkdhawkjdh");
      http.addHeader("Temperatuur", strtemperatuur);
      http.addHeader("Luchtvochtigheid", strluchtvochtigheid);
      http.addHeader("Grondvochtigheid", strgrondvochtigheid);
      http.addHeader("Waterniveau", strwaterniveau);
      http.addHeader("Lichtsterkte1", strlichtsterkte1);
      http.addHeader("Lichtsterkte2", strlichtsterkte2);
      http.addHeader("Lichststerkte3", strlichtsterkte3);
  
      // HTTP Post
      int httpResponseCode = http.POST("{\"Temperatuur\" : "+strtemperatuur+" , 
  \"Luchtvochtigheid\" : "+strluchtvochtigheid+" , \"Grondvochtigheid\" : 
  "+strgrondvochtigheid+" , \"Waterniveau\" : "+strwaterniveau+" , 
  \"Lichtsterkte1\" : "+strlichtsterkte1+" , \"Lichtsterkte2\" : 
  "+strlichtsterkte2+" , \"Lichtsterkte3\" : "+strlichtsterkte3+"}");
      String response = http.getString();
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      Serial.print("Test: ");
      Serial.println(response);
    
      // Schoon alles op
  
      strtemperatuur = "";
      strluchtvochtigheid = "";
      strgrondvochtigheid = "";
      strlichtsterkte1 = "";
      strwaterniveau = "";
      strlichtsterkte1  = "";
      postCounter = nulWaarde;
      http.end();
    } else {
      Serial.println("WiFi Disconnected");
    }   
    lastTime = millis();
    }
  } 
}

Does anyone know how to solve this?

Try declaring HTTPClient http globally just once, and take it out of the loop() . Recreating it over and over again could be messing with your heap which may already be Swiss cheese anyway because you use the String class so much.

Speaking of which: it would be better to compose most of your String s only once, and not every time in the loop, where you could just add the measurements to prefabricated String s.

Sterkte!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM