Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Bethsy
#82885 Hello everyone, I'm working with an ESP8266 (NodeMCU), in Arduino IDE, I'm sensing power consumption of different devices and sending that data as a Json file to a web server, in order to not spam the server, I'm creating an array with 60 Json objects where the data is kept. I tried to use a For cycle in order to create the Jsons objects but it showed at Serial Monitor an issue called "Exception (2)" which reset the ESP8266, I made a research and I found that it happens when the flash memory is overwritten, commonly by Wifi functions like Wifi.begin() and the solution was implemented Wifi.Persistent(false). It didn't work out for me, so I implement other strategies like created Json object by object, even though it seems to work at first when I created the fifteenth object the Exception(2) appears again and also the message "Connection Refused". Some people fixed this issue by replacing "localhost" with the direct IP address but with the Herokuapp, the server that I'm working with, it does not allow you to use it directly. Finally, I have read a lot of comments in different forums that it's a problem related to memory but I don't know how to fix it.

Here's my code:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

String bearer = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3N1ZWRBdCI6MTU2MDQ4NjQ5NS45OTQ5MzUsImlzc3VlciI6I";
String Host = "http://efihome.herokuapp.com/api/logs";

int httpCode;


const char* ID = "abcdefg";
const char* PASS = "12345678";

HTTPClient http;  //Objeto de clase HTTPClient

const int capacity = JSON_ARRAY_SIZE(30) + 30 * JSON_OBJECT_SIZE(10);
StaticJsonDocument<capacity> doc;
JsonObject obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10,
           obj11, obj12, obj13, obj14, obj15, obj16, obj17, obj18, obj19, obj20,
           obj21, obj22, obj23, obj24, obj25, obj26, obj27, obj28, obj29, obj30,
           obj31, obj32, obj33, obj34, obj35, obj36, obj37, obj38, obj39, obj40,
           obj41, obj42, obj43, obj44, obj45, obj46, obj47, obj48, obj49, obj50;

float current[60] = {1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7,
                     6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8,
                     5.0, 6.7, 6.2, 6.6, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2,
                     6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 1.5, 6.8, 5.0,
                     6.7, 6.2, 6.6, 1.5, 6.8, 5.0, 6.7, 6.2, 6.6, 6.6};
double date = 1560456080;
String deviceID = "97367434-CA04-4FC7-A8BA-703DCFCBA081";
float power[60] = {20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8,
                   15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6,
                   10.0, 9.8, 15.2, 20.6, 15.2, 20.5, 25.6, 10.0, 9.8, 15.2,
                   20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 20.5, 25.6, 10.0,
                   9.8, 15.2, 20.6, 20.5, 25.6, 10.0, 9.8, 15.2, 20.6, 15.2};
float FactorPower[60] = {0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0,
                         0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8,
                         0.9, 1.0, 0.95, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8,
                         0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8, 0.9, 1.0,
                         0.95, 0.8, 0.7, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8,
                         0.9, 1.0, 0.95, 0.8, 0.9, 1.0, 0.95, 0.8, 0.7, 0.8};
float voltage[60] = {117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5,
                     119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5,
                     118.9, 118.5, 119.2, 117.8, 116.8, 117.5, 120.5, 118.9, 118.5, 119.2,
                     117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 117.5, 120.5, 118.9,
                     118.5, 119.2, 117.8, 117.5, 120.5, 118.9, 118.5, 119.2, 117.8, 116.8};

void setup() {
  Serial.begin(9600);
  //  WiFi.persistent(false);
  WiFi.begin(ID, PASS);  //Inicia la conexión con la red Wi-Fi.

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }

  Serial.println("WiFi Conectado");
 
  GETServidor();
}

void loop() {
  // put your main code here, to run repeatedly:

}

void GETServidor() {
  CreateObj(obj1, 0);
  CreateObj(obj2, 1); 
  CreateObj(obj3, 2);
  CreateObj(obj4, 3);
  CreateObj(obj5, 4);
  CreateObj(obj6, 5);
  CreateObj(obj7, 6);
  CreateObj(obj8, 7);
  CreateObj(obj9, 8);
  CreateObj(obj10, 9);
  CreateObj(obj11, 10);
  CreateObj(obj12, 11);
  CreateObj(obj13, 12);
  CreateObj(obj14, 13);
  CreateObj(obj15, 14);
  CreateObj(obj16, 15);
  CreateObj(obj17, 16);
  CreateObj(obj18, 17);
  CreateObj(obj19, 18);
  CreateObj(obj20, 19);
  CreateObj(obj21, 20);
  CreateObj(obj22, 21);
  CreateObj(obj23, 22);
  CreateObj(obj24, 23);
  CreateObj(obj25, 24);
  CreateObj(obj26, 25);
  CreateObj(obj27, 26);
  CreateObj(obj28, 27);
  CreateObj(obj29, 28);
  CreateObj(obj30, 29);
  CreateObj(obj31, 30);
  CreateObj(obj32, 31); 
  CreateObj(obj33, 32);
  CreateObj(obj34, 33);
  CreateObj(obj35, 34);
  CreateObj(obj36, 35);
  CreateObj(obj37, 36);
  CreateObj(obj38, 37);
  CreateObj(obj39, 38);
  CreateObj(obj40, 39);
  CreateObj(obj41, 40);
  CreateObj(obj42, 41); 
  CreateObj(obj43, 42);
  CreateObj(obj44, 43);
  CreateObj(obj45, 44);
  CreateObj(obj46, 45);
  CreateObj(obj47, 46);
  CreateObj(obj48, 47);
  CreateObj(obj49, 48);
  CreateObj(obj50, 49);
 
  // Each array until , = 185
  char rootBuffer[185*50];

  serializeJsonPretty(doc, rootBuffer);
  //Serial.println(rootBuffer);

  if (http.begin(Host)) {
    http.addHeader("Accept", "application/json");
    http.addHeader("Authorization", "Bearer " + bearer);
    http.addHeader("Content-Type", "application/json");
    httpCode = http.POST(rootBuffer);

    if (httpCode > 0) {
      Serial.printf("[HTTP] POST... code: %d\n", httpCode);
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    }
    else {
      Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
//      String payload = http.getString();
//      Serial.println(payload);
    }

    http.end();
  } else {
    Serial.println("Fallo en conexión con servidor");
  }
  //===================================================================================
  //===================================================================================
  //===================================================================================

}

void CreateObj(JsonObject obj, int i) {
  obj = doc.createNestedObject();
  obj["current"] = current[i];
  obj["date"] = date;
  obj["device_id"] = deviceID;
  obj["power"] = power[i];
  obj["factor_power"] = FactorPower[i];
  obj["voltage"] = voltage[i];
}