serial-portcommunicationesp8266nodemcu

Nodemcu Exception (9) problem because of serial communication


I have an UI and a mainboard. They are communicated with UART protocol. I added a basic code to my mainboard to send "ADC1=xxxx ADC2=yyyy" data. I want to catch only this data from my mainboard but there are countless values which is sent from UI->MB or MB->UI. As a short ı want to catch only "ADC1=xxxx ADC2=yyyy" as xxxx and yyyy integers or strings and able to write it an basic html page in nodemcu with below code.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

int incomingByte = 0;
char deneme[] = "";
const char *delp;
const char *delp2;
/*Put your SSID & Password*/
const char* ssid = "ssid";  // Enter SSID here
const char* password = "pass";  //Enter Password here
ESP8266WebServer server(80);
float Temperature;
float Humidity;
 
void setup() {
  Serial.begin(9600);
  delay(100);
  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());

  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

}
void loop() {

  if (Serial.available()) // now we have at least one character in the Serial input buffer
  {

String incomingStr = Serial.readStringUntil('\n');
incomingStr.toCharArray(deneme,32);
 Serial.println(deneme);
 delp = strtok (deneme,"-");
 delp2 = strtok (NULL,"-");
 Serial.println(delp);
 Serial.println(delp2);

 }
server.handleClient();
 
delay(500);

}

void handle_OnConnect() {
  server.send(200, "text/html", SendHTML(Temperature,Humidity)); 
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float Temperaturestat,float Humiditystat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>ESP8266 Weather Report</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="<script>\n"; 
  ptr +="setInterval(loadDoc,200);\n";
  ptr +="function loadDoc() {\n";
  ptr +="var xhttp = new XMLHttpRequest();\n";
  ptr +="xhttp.onreadystatechange = function() {\n";
  ptr +="if (this.readyState == 4 && this.status == 200) {\n";
  ptr +="document.getElementById(\"webpage\").innerHTML =this.responseText}\n";
  ptr +="};\n";
  ptr +="xhttp.open(\"GET\", \"/\", true);\n";
  ptr +="xhttp.send();\n";
  ptr +="}\n";
  ptr +="</script>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<div id=\"webpage\">\n";
  ptr +="<h1>ESP8266 NodeMCU Weather Report</h1>\n";
  
  ptr +="<p>Temperature: ";
  ptr +=delp;
  ptr +=" NTC</p>";
  ptr +="<p>Humidity: ";
  ptr +=delp2;
  ptr +=" Heatsink</p>";
  
  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}

Result of code is an disappointment, ı get error which is named as exception(9). I am not sure what is the problem. For me, possible reasons;


Solution

  • I think there's problem with

    char deneme[] = ""; 
    

    also whith use of strtok. I change declaration and remove blocking delay in loop : try this code : (I assume you receive "0xFF S ? ADC1=1189 ADC2=1508 S ? 0xFF S ?" in serial buffer)

    EDIT: enhanced analyse of received data : must use strtok_r as you need two level of token search in array.

            //char deneme[] = ""; remove this line
            unsigned long tw;
            
    void loop() {
      if (millis() - tw > 500) {  //read serial each 0.5 sec
        if (Serial.available()) // now we have at least one character in the Serial input buffer
        {
          String incomingStr = Serial.readStringUntil('\n');
          //I test this with
          //String incomingStr = "0xFF S ? ADC1=1189 ADC2=1508 S ? 0xFF S ?";
    
          if (incomingStr.indexOf("ADC1") > -1 || incomingStr.indexOf("ADC2") > -1) {  //if serial data contains ADC1 or ADC2
            char deneme[incomingStr.length() + 1];
            incomingStr.toCharArray(deneme, incomingStr.length() + 1);
            Serial.println(deneme);           //return "0xFF S ? ADC1=1189 ADC2=1508 S ? 0xFF S ?"
            //use strtok_r function as we use two instance nested
            //create contexts for strtok_r function 
            char* t1 = deneme; //must assign any value, char* t1; alone fail to compile
            char* t2 = deneme; //idem
    
            char* token = strtok_r (deneme, " ", &t1);   //return "0xFF", "S", "ADC1=1189", etc...
            while ( token != NULL ) {
              char* token2 = strtok_r(token, "=", &t2);    //return null,null,null,ADC1,ADC2 etc...
              if (token2 != NULL ) {
                if (String(token2) == "ADC1") delp = strtok_r (NULL, "=", &t2); //return 1189
                if (String(token2) == "ADC2") delp2 = strtok_r (NULL, "=", &t2); //return 1508
              }
              token = strtok_r ( NULL, " ", &t1);
            }
            Serial.print("delp = ");Serial.println(delp);
            Serial.print("delp2 = ");Serial.println(delp2);
            tw = millis();
          }
        }
        //scan http requests each loop
        server.handleClient();
      }
    }