So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By GoodMorningJack
#88172 Hello everyone,
If I post here today it's because I ran out of ideas to solve a problem I face on my project. Basically, I'm building a webserver that streams real-time data from various sensors from an arduino and displays it in real-time graphs.
My development board is a RobotDyn Arduino Mega R3 with ESP8266 built-in like this one :
Image

For you to understand how it works :
  • The ATmega 2560 gets data form sensors and sends it (in JSON format) through its serial 3 port as fast as possible.
  • The ESP8266 is set as an access point, so I am able to connect to it like any other WiFi hotspot, and the webpages files are saved into its flash memory and access via SPIFFS
  • From there, the ESP8266 is able to intercept this data by reading its serial port and plot it in live graphs, through an xmlhttprequest, every 100ms.

I started this project a few weeks ago and everything worked fine for a while. I was able to get data and plot it live, without any problem (despite small freezes of the page every now and then). But a few days ago, the ESP began to act strangely. The webpages took more and more time to load until it wouldn't even load anymore at some point. Sometimes I'm even unable to connect to the module.

What I do not understand is the reason for this problem to appear. Everything worked fine and it suddenly began to act erratically without me changing the ESP or ATmega program. I was just working on the actual webpages, changing some colors here and there.

Here's what I tried to solve the issue but did not helped :
  • I tried to erase the ESP firmware and flash the module with blank files or the last ESP8266 firmware, did not helped
  • I tried to get everything working by uploading my programs on another board, but faced the same issue so I concluded that the problem may not be the board but probably came from my programs.
  • I tried to change the ESP IP address and gateway, but the problem persisted
  • I tried to load another webpage, very simple this time, just printing "Hello World" and it sometimes worked, but after a insanely long load time
  • I tried to load older versions of my program saved automatically in my cloud file manager, but there was still the same problem, so maybe the issue also came from the board after all ?

Right now I'm just out of ideas to solve this issue. I have no error message printing anywhere so I do not even know where the problem could potentially come from, but I suspect that it come from the ESP Module (the arduino on the other and just work as expected, no issue here).

Of course, here is the program loaded inside the ESP8266 :
Code: Select all#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <FS.h>

const byte numChars = 150;    // Maximum message length
char json[numChars];    // Recieved data container

static byte ndx = 0;              // Array position
const char startMarker = '{';     // Start of data marker
const char endMarker = '}';       // End of data marker

boolean messageReady = false;
boolean recvInProgress = false;
 
const char* ssid = "TEST_Stand";     // Arduino access point SSID
const char* password = "testStand_2020";   // Arduino access point password

IPAddress staticIP(192,168,4,1);      // Server IP address
IPAddress gateway(192,168,4,1);       // Server gateway address
IPAddress subnet(255,255,255,0);      // Server subnet address

WiFiClient client;                // Init Client
ESP8266WebServer server(80);      // Init Server on default port 80

void setup(){
  Serial.begin(115200);     // Begin Serial communication
  delay(10);                // Wait
 
  if(!SPIFFS.begin()){      // Initialize SPIFFS
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
  WiFi.softAP(ssid, password);                    // Start the access point
  WiFi.softAPConfig(staticIP, gateway, subnet);   // Config the access point
  delay(100);                                     // Wait
 
  Serial.print("Access Point \"");
  Serial.print(ssid);
  Serial.println("\" started ...");

  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP());
 
  server.onNotFound([]() {                                // If the client requests any URI
    if (!handleFileRead(server.uri()))                    // send it if it exists
      server.send(404, "text/plain", "404: Not Found");   // otherwise, respond with a 404 (Not Found) error
  });

  server.on("/data",getData);   // Get the incomming sensor data
  server.begin();               // Server start
 
}

void loop(){
  server.handleClient();      // Handle client
}


void getData() {                                                // Read the Serial and get data
  while (Serial.available() > 0 && messageReady == false) {     // Read the Atmega2560 response over serial com
    char c = Serial.read();
      if (recvInProgress == true){
        if (c != endMarker) {
          json[ndx] = c;
          ndx++;
          if (ndx >= numChars)
            ndx = numChars - 1;
        }
        else {
         json[ndx] = '}';       // Terminate the json
         json[ndx+1] = '\0';    // Terminate the string
         ndx = 0;
         recvInProgress = false;
         messageReady = true;
        }
      }
    else if (c == startMarker) {
            json[ndx] = c;
            ndx++;
            recvInProgress = true;
        }
  }
  if (messageReady == true) {
    server.send(200, "text/plain", json);     // Send data to the server as plain text
    messageReady = false;
    memset(json, 0, sizeof(json));            // Clear the buffer for next transmission
  }
}

String getContentType(String filename) {                  // convert the file extension to the MIME type
  if (filename.endsWith(".html")) return "text/html";
  else if(filename.endsWith(".png")) return "image/png";
  else if (filename.endsWith(".css")) return "text/css";
  else if (filename.endsWith(".js")) return "text/javascript";
  else if (filename.endsWith(".ttf")) return "font/ttf";
  return "text/plain";
}

bool handleFileRead(String path) {                      // send the right file to the client (if it exists)
  if (path.endsWith("/")) path += "opus.html";          // If a folder is requested, send the index file (OPUS.html)
  String contentType = getContentType(path);            // Get the MIME type
  if (SPIFFS.exists(path)) {    // If the file exists
    File file = SPIFFS.open(path, "r");                      // Open it
    int siz = file.size();
    server.sendHeader("Content-Length", (String)(siz));
    server.sendHeader("Cache-Control", "max-age=2628000, public"); // cache for 30 days
    size_t sent = server.streamFile(file, contentType); // And send it to the client
    file.close();                                       // Then close the file again
    return true;
  }
  return false;                                         // If the file doesn't exist, return false
}


Thanks a lot for the time you spent reading this topic and maybe trying to find a solution, any help would be greatly appreciated.
User avatar
By GoodMorningJack
#88193 Okay so I think I've found a solution and I'll just post it here if anyone stumble on a similar issue. I just changed the WiFi channel of my ESP8266 module from 1 to 6, since something (mabye another WiFi router went on) was apparently disturbing the data exchange.
Code: Select allWiFi.softAP(ssid, password, 11)