Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt_basic does not work when combined with serial communication in loop() #245

Open
1 task done
autonomouscitizen opened this issue Sep 2, 2024 · 2 comments
Open
1 task done
Labels
no-issue-activity pending It is a feature/bug-fix request, currently on hold

Comments

@autonomouscitizen
Copy link

autonomouscitizen commented Sep 2, 2024

Boards

BW16

External Hardware

Teensy 4.1 communication via UART

Hardware Configuration

nothing else attached to BW16

Version

latest dev (checkout manually)

IDE Name

Arudino IDE 2.32.

Operating System

Windows 10

Auto Flash Mode

Disable

Erase All Flash Memory (4MB)

Disable

Standard Lib

Arduino_STD_PRINTF

Upload Speed

1500000

Description

The basic mqtt example sketch works as expected. Software serial communication between Teensy 4.1 and BW16 works as expected. However, the two do not work in combination. When trying to implement software serial communication between Teensy 4.1 and BW16 in the loop() function, the mqtt functionality experiences hangups that are not resolvable. Sometimes readouts on the serial monitor will suddenly stop, with no error messages printed. All functionality of the device also stops. Other times there occurs an infinite loop in which I get the readouts; “[INFO] Create socket successfully” and then “[ERROR] Connect to server failed” then [INFO] [ard_socket.c][send_data] err = 0" “failed, rc = 4” and repeats this cycle infinitely.

This does not occur if I remove the software serial communication portion of the code. I tried to implement a watchdog system reset when the reconnect() function fails to connect, however it appears that the watchdog functionality does not execute. Thus the device frequently enters a non-functioning state that can only be resolved with a physical reset, which renders this device completely useless for operation in a remote location.

this problem was detected earlier and reported as "closed" despite the issue still occurring: #75

Sketch

#include "sys_api.h"

#include <WiFi.h>
#include <PubSubClient.h>

#include <SoftwareSerial.h>

SoftwareSerial mySerial(PB2, PB1); // RX, TX

char c;
String dataIn;


char ssid[] = "xxxxx";      
char pass[] = "xxxxx";  
int status = WL_IDLE_STATUS;        

int publishCount = 0;
int characterCount = 0;
int characterCount2 = 0;


char mqttServer[]     = "test.mosquitto.org";
char clientId[]       = "amebaClient";
char publishTopic[]   = "server";
char publishPayload[] = "initialize";
char subscribeTopic[] = "Input";
char key[10] = {0};

void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    for (unsigned int i = 0; i < length; i++) {
        Serial.print((char)(payload[i]));
    }
    Serial.println();
}

WiFiClient wifiClient;
PubSubClient client(wifiClient);
void reconnect() {
    // Loop until we're reconnected
    while (!(client.connected())) {
        Serial.print("\r\nAttempting MQTT connection...");
        // Attempt to connect
        if (client.connect(clientId)) {
            Serial.println("connected");
            //Once connected, publish an announcement and resubscribe
            client.publish(publishTopic, publishPayload);
            client.subscribe(subscribeTopic);
        } else {
            Serial.println("failed, rc=");
            Serial.print(client.state());
            Serial.println(" wait 5 seconds");
            delay(5000);
        }
    }
    
}



void setup() {
    //Initialize serial and wait for port to open:
    Serial.begin(115200);
    mySerial.begin(115200);
    //Attempt to connect to WiFi network
    while (status != WL_CONNECTED) {
        //Serial.print("\r\nAttempting to connect to SSID: ");
        //Serial.println(ssid);
        // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
        status = WiFi.begin(ssid, pass);

        // wait 10 seconds for connection:
        delay(10000);
        
    }

    client.setServer(mqttServer, 1883);
    client.setCallback(callback);
    
    //Allow Hardware to sort itself out
    delay(1500);
    
}


void loop() 
{

  while (mySerial.available()>0) 
  {
    c = mySerial.read();

    if (c == '\n') {break;}
    else {dataIn+=c;}
  }

  if (c=='\n')
  {
    Serial.println(dataIn);
   
    int str_len = dataIn.length() + 1;
    dataIn.toCharArray(key, str_len);
            
    if (client.connect(clientId)) 
    {
      client.publish(publishTopic, key);  
    }
            
    c=0;
    dataIn="";
  }

   
  if (!(client.connected())) 
  {
    //reconnect();
    sys_reset();
  }
  client.loop();
   
  
}

Error/Debug Message

Sometimes readouts on the serial monitor will suddenly stop, with no error messages printed. All functionality of the device also stops. Other times there occurs an infinite loop in which I get the readouts; “[INFO] Create socket successfully” and then “[ERROR] Connect to server failed” then [INFO] [ard_socket.c][send_data] err = 0" “failed, rc = 4” and repeats this cycle infinitely.

Other times i get: 
RTL8721D[Driver]: no beacon for a long time, disconnect or roaming
12:38:41.420 -> dissconn reason code: 65535
12:38:41.420 -> connected stage, loss beacon
12:38:41.420 -> 

12:39:49.457 -> Attempting MQTT connection...
12:39:49.457 -> [INFO]server_drv.cpp:  start_client
12:39:49.457 -> [INFO] Create socket successfully
12:39:49.457 -> 
12:40:07.496 ->  [ERROR] Connect to server failed
12:40:07.497 -> 

12:40:07.497 -> [INFO] [ard_socket.c][send_data] err = 1583184219

Reproduce remarks

No response

I have checked online documentation, FAQ, GitHub Wiki and existing/closed issues.

  • I confirm I have checked online documentation, FAQ, GitHub Wiki and existing/closed issues.
@autonomouscitizen autonomouscitizen added the pending It is a feature/bug-fix request, currently on hold label Sep 2, 2024
Copy link

github-actions bot commented Sep 2, 2024

Hello, hope this message finds you well. Congrats to your first Issue! We will review it as soon as possiable. Feel free to have a look at https://www.amebaiot.com/en/ameba-arduino-summary/ for more information

Copy link

This issue is stale because it has been open for 14 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-issue-activity pending It is a feature/bug-fix request, currently on hold
Projects
None yet
Development

No branches or pull requests

1 participant