I am new to this forum as I thought to get some expert opinion being a newbie in this field.
I have following program running on my ESP8266 (ESP-12F) which uses Hardware & Software serials for flashing/ serial printing & communicating with an inverter, respectively.
#include "thingProperties.h"
#include <SoftwareSerial.h>
SoftwareSerial SUART(12, 13); //SRX = DPin-4, STX = DPin-5
const byte numChars = 127;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
boolean newData = false;
char *string, *found;
int gsValues[30] = {0};
unsigned long previousMillis = 0;
const long interval = 5000; //milliseconds
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(2400);
SUART.begin(2400);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
}
void loop() {
ArduinoCloud.update();
// Your code here
if (ArduinoCloud.connected())
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
SUART.print("^P005GS\r");
}
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
//showParsedData();
newData = false;
aco_apwr = gsValues[5];
gd_volt = gsValues[0]/10;
inv_temp = gsValues[13];
pv1_pwr = gsValues[16];
pv1_volt = (int)gsValues[18]/10.0;
//delay(5000);
}
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '^';
char endMarker = '\r';
char rc;
while (SUART.available() > 0 && newData == false) {
rc = SUART.read();
//Serial.print(rc);
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
Serial.println("E_detected");
}
}
else if (rc == startMarker) {
recvInProgress = true;
Serial.println("S_detected");
}
}
}
//============
void parseData() { // split the data into its parts
string = strdup(tempChars);
char tmp[4];
//printf("Original string: '%s'\n",string);
found = strsep(&string,",");
//printf("%s\n",found);
int j = 0;
for (int i = 0; i <= strlen(found); i++)
{
if (i >= 4)
{
tmp[j] = found[i];
j++;
}
}
gsValues[0] = atoi(tmp);
unsigned int idx = 1;
while( (found = strsep(&string,",")) != NULL )
{
gsValues[idx] = atoi(found);
//Serial.println(found);
//Serial.println(gsValues[idx]);
idx++;
}
}
void showParsedData() {
//Serial.println(messageFromPC);
for (int i = 0; i <=29; i++)
Serial.println(gsValues[i]);
}
The ESP connects and transmits perfectly but randomly throws Exception (0) and does not resets/ connects to cloud again. I have tried changing code, attaching device again and ssid keys in cloud etc. But, unable get rid of this issue.
Please, suggest what I may be doing wrong or try to resolve the said issue.
Thanks for the reply.
This is what I'm getting now and it keeps on retrying.
S_detected
E_detected
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
TimeServiceClass::getRemoteTime cannot get time from NTP, fallback on connection handler
TimeServiceClass::getRemoteTime cannot get time from connection handler
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
I have come to know that if I disconnect the device i.e. serial is not available then it is kept connected. But, when I connect back it randomly disconnects. So, there is some issue with my Serial functions handling text interpretation etc.
I am getting this error and it never gets connected again...
S_detected
E_detected
ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost
Disconnected from Arduino IoT Cloud
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -1
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
The ESP Exception Decoder requires access to the .elf file that is generated through the sketch compilation. That file is not available when the sketch is compiled via Arduino Cloud Editor, because the file is on the Arduino Cloud server. So unfortunately it is not possible to use the decoder on this stack trace.
It would be possible if you uploaded the Thing sketch to the board using Arduino IDE, then decoded the stack trace that is produced when the board is running the program uploaded via Arduino IDE. In this case, Arduino IDE saves the .elf file to the hard drive on your computer, so it is available for the exception decoder to find.
You must copy the entire content from Serial Monitor. You missed the end of the output so the decoder couldn't recognize it.
There is a bug in the Arduino IDE Serial Monitor that makes it so you can only select the text that is visible in Serial Monitor. If you scroll the panel while selecting, the text that is out of view is unselected. So make sure to resize the Serial Monitor panel to make all the content visible before selecting and copying it. You can resize the Serial Monitor panel by hovering the mouse pointer over the border between the Serial Monitor panel and the editor panel of the Arduino IDE window. You will see the mouse pointer change to the resize pointer. Click and drag the border up to increase the size of the Serial Monitor panel.