esp8266 crashes when sending json object and resets

896 Views Asked by At

Hallo I am trying to send a JSON object useing the arduinoJson library. Everything works fine when it is a small json object, but when it gets larger the esp8266 crashes and resets. I can see when debugging that it creates the json object, but when it sends it useing ajax POST methode it crashes. In the picture you can see the Serial monitor and you can also see that the json object gets created but when it sends it then the esp8266 crashes and reboots Serial debug. I believe the problems is something to do with this line of code Possible problem

The code on the esp8266 looks like this.

#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <WiFiClientSecure.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <Wire.h>
#include "max6675.h"
#include <UniversalTelegramBot.h>
#define BOTtoken "1863133375:AAFE3m83AFQYaRjqOas2Kr-DB5fhbONCZ80"
#define CHAT_ID "-599306428"


const int thermoCS = D8;
const int thermoCLK = D5;
int x = 0;
int r[] = {1,1,1,1,1};
int lk;

int TEMP1[121];
int TEMP2[121];
int TEMP3[121];
int TEMP4[121];
int TEMP5[121];

MAX6675 thermocouple_TEMP1(thermoCLK, thermoCS, D7);
MAX6675 thermocouple_TEMP2(thermoCLK, thermoCS, D6);
MAX6675 thermocouple_TEMP3(thermoCLK, thermoCS, D2);
MAX6675 thermocouple_TEMP4(thermoCLK, thermoCS, D1);
MAX6675 thermocouple_TEMP5(thermoCLK, thermoCS, D0); // esp8266 skal bruge D0, hvor ESP32 skal bruge 0
WiFiClientSecure client;
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secure_client;
UniversalTelegramBot bot(BOTtoken, secure_client);
// Replace with your network credentials
const char* ssid = "Telenor646209_EXT";
const char* password = "";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String temp5() {
  float f = abs(thermocouple_TEMP5.readCelsius()) ;
    return String(f);
    client.stop();
}
String temp4() {
  float d = abs(thermocouple_TEMP4.readCelsius()) ;
    return String(d);
    client.stop();
}
String temp3() {
  float s = abs(thermocouple_TEMP3.readCelsius()) ;
    return String(s);
    client.stop();
}
String temp2() {
  float a = abs(thermocouple_TEMP2.readCelsius());
    return String(a);
    client.stop();
}

String temp1() {
  float m = abs(thermocouple_TEMP1.readCelsius());
    return String(m);
    client.stop();
}
String TEMP() {
  String Json;
  StaticJsonDocument<54000> doc;
JsonArray Temp1 = doc.createNestedArray("Temp1");
JsonArray Temp2 = doc.createNestedArray("Temp2");
JsonArray Temp3 = doc.createNestedArray("Temp3");
JsonArray Temp4 = doc.createNestedArray("Temp4");
JsonArray Temp5 = doc.createNestedArray("Temp5");
  for (int i = 0; i <= x-1; i++) {
  Temp1.add(TEMP1[i]);
  Temp2.add(TEMP2[i]);
  Temp3.add(TEMP3[i]);
  Temp4.add(TEMP4[i]);
  Temp5.add(TEMP5[i]);
  }
  serializeJson(doc, Json);
  Serial.println(Json);
    return Json;
    client.stop();
}


void setup(){
  // Serial port for debugging purposes
  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  secure_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
  Serial.begin(9600);
  // Initialize SPIFFS
  if(!SPIFFS.begin()){
    //Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    //Serial.println("Connecting to WiFi..");
  }
  Serial.println(WiFi.localIP());
  // Print ESP32 Local IP Address
  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html");
  });
  server.on("/temp1", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temp1().c_str());
  });
  server.on("/temp2", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temp2().c_str());
  });
  server.on("/temp3", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temp3().c_str());
  });
  server.on("/temp4", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temp4().c_str());
  });
  server.on("/temp5", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temp5().c_str());
  });
  server.on("/TEMP", HTTP_POST, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", TEMP().c_str());
  });

  // Start server
  server.begin();
    //bot.sendMessage(CHAT_ID, "EPS8266_fyr starter", "");
}
 
void loop(){
 TEMP1[x] = x;//abs(thermocouple_TEMP1.readCelsius()); 
 TEMP2[x] = x;//abs(thermocouple_TEMP2.readCelsius());
 TEMP3[x] = x;//abs(thermocouple_TEMP3.readCelsius());
 TEMP4[x] = x;//abs(thermocouple_TEMP4.readCelsius());
 TEMP5[x] = x;//abs(thermocouple_TEMP5.readCelsius());
  //Serial.print("TEMP værdier:");
  //Serial.print(x);
  //Serial.print(" TEMP1: ");
  Serial.print(TEMP1[x]);
  //Serial.print(" TEMP2: ");
  Serial.print(TEMP2[x]);
 //Serial.print(" TEMP3: ");
  Serial.print(TEMP3[x]);
  //Serial.print(" TEMP4: ");
  Serial.print(TEMP4[x]);
  //Serial.print(" TEMP5: ");
  Serial.println(TEMP5[x]);
  x += 1;
  if(TEMP1[x] >= 35){
  //bot.sendMessage(CHAT_ID, "Nedsfaldsrørs temperatur er over 35°C", "");
 }
  if(x == 120){  
    x=119;
    if(x>118) {
      for (int antal = 0; antal <= 118; antal++){
         TEMP1[antal] = TEMP1[antal+1]; 
         TEMP2[antal] = TEMP2[antal+1];
         TEMP3[antal] = TEMP3[antal+1];
         TEMP4[antal] = TEMP4[antal+1]; 
         TEMP5[antal] = TEMP5[antal+1];  
      }
  
    }
  }
  delay(1000);
}

The last part of my code is HTML/JAVASCRIPT and it looks like this.

<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://code.highcharts.com/highcharts.js"></script>
  <style>
    h2 {
      font-family: Arial;
      font-size: 2.5rem;
      text-align: center;

    }
  </style>
</head>
<body>
  <h2>DASH BOARD</h2>
  <div id="chart-temperature" class="container"></div>
  <p id="demo"></p>
  <p id="demo2"></p>
</body>
<script>
var Json;
var d = new Date();
var h = d.getHours();
var c = d.getMinutes();
var chartT = new Highcharts.Chart({
  chart:{
    renderTo : 'chart-temperature',
    type: 'line',
    zoomType: 'x',
    panning: true,
    panKey: 'shift'
  },
  tooltip: {
  dateTimeLabelFormats: {
    millisecond: "%A, %b %e, %H:%M"
  }
},
  title: { text: 'temperature' },
  series: [{
    name: 'TEMP1',
    showInLegend: true,
    connectNulls: true,
    data: [],
    color: '#FF0000'
  }, {
     name: 'TEMP2',
     connectNulls: true,
    data:[],
    color: '#4572A7'
  },
  {
     name: 'TEMP3',
     connectNulls: true,
    data:[],
    color: '#000000'
  },
  {
     name: 'TEMP4',
     connectNulls: true,
    data:[],
    color: '#0000FF'
  },
  {
     name: 'TEMP5',
     connectNulls: true,
    data:[],
    color: '#6600FF'
  }],

  plotOptions: {
    line: { animation: false,
      dataLabels: { enabled: true }

    },
  },
  xAxis: { type: 'datetime',
    dateTimeLabelFormats: { second: '%H:%M:%S' },
    min: Date.UTC(0,0,0,h,c-120,0), tickInterval: 30*60*1000, max: Date.UTC(0,0,0,h,c,0)
  },
  yAxis: {
    title: { text: 'Temperature (Celsius)' },
    //title: { text: 'Temperature (Fahrenheit)' }
  },
  credits: { enabled: false }
});
Highcharts.setOptions({
    time: {
        useUTC: true
    }
});
function loadall(){
  var m;
  var tid;
  var y1;
  var y;
  var just;
    var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      if(chartT.series[0].data.length == 0){
        y1 = this.responseText;
        y = JSON.parse(y1);
        just = y.Temp1.length;
      for (let i = 0; i < just; i++) {
         tid = Date.UTC(0,0,0,h,(c-i),0);
            chartT.series[0].addPoint([tid, y.Temp1[just-i-1]], true, false, false);
            chartT.series[1].addPoint([tid, y.Temp2[just-i-1]], true, false, false);
            chartT.series[2].addPoint([tid, y.Temp3[just-i-1]], true, false, false);
            chartT.series[3].addPoint([tid, y.Temp4[just-i-1]], true, false, false);
            chartT.series[4].addPoint([tid, y.Temp5[just-i-1]], true, false, false);
          }
           }
            }
             }
    xhttp.open("POST", "TEMP", true);
    xhttp.send();
  }
  function Load(serie,sensor,tid){
    var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        var y = parseFloat(this.responseText);
    if (this.readyState == 4 && this.status == 200) {
            chartT.series[serie].addPoint([tid, y], true, true, false);
          }
           }
    xhttp.open("GET", sensor, false);
    xhttp.send();
  }
  loadall();
  setInterval(function(){
    d = new Date();
    h = d.getHours();
    c = d.getMinutes();
    chartT.xAxis[0].update({
        max: Date.UTC(0,0,0,h,c,0),
        min: Date.UTC(0,0,0,h,c-120,0)
    });
    Load(0,"temp1",Date.UTC(0,0,0,h,c,0));
    Load(1,"temp2",Date.UTC(0,0,0,h,c,0));
    Load(2,"temp3",Date.UTC(0,0,0,h,c,0));
    Load(3,"temp4",Date.UTC(0,0,0,h,c,0));
    Load(4,"temp5",Date.UTC(0,0,0,h,c,0));
  }, 60000);

</script>
</html>
1

There are 1 best solutions below

2
Andreas Ellehoej On

I found the problem that casued the reset and it was the esp8266's watchdog that reseted it after 6 seconds. This means i have to rewrite my program to either save the json array in the SPIFFS or send it in smaller parts.

The fix is to upgrade to ESP32 because the ESP8266 is not fast enough