Comments (13)
Hi,
I found a reason for the disconnection,
When using UART and MQTT in the program at the same time, it will lead to irregular disconnection.
I hope there will be a solution to make it work.
Thank you.
from ambd_arduino.
Hi @Brian9871,
I have tried to test the MQTT and I don't see any disconnection issue. So I'm not sure if there's an issue with your code
from ambd_arduino.
Hi @daphwl ,
The disconnection happens in about 5-10 minutes, when my UART transmits at 5hz.
from ambd_arduino.
Are you using the Arduino SDK MQTT example?
from ambd_arduino.
Yes, I use the Arduino SDK MQTT example, and add serial function to it.
there will be a problem of disconnection, I attach my code here.
/*
Basic MQTT example
This sketch demonstrates the basic capabilities of the library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
*/
#include <WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
// Update these with values suitable for your network.
char ssid[] = "CJS_5G"; // your network SSID (name)
char pass[] = "77777777"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char mqttServer[] = "192.168.137.1";
char clientId[] = "amebaClient";
char publishTopic[] = "outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";
/* UART */
SoftwareSerial mySerial(PB2, PB1); // RX, TX
void callback(char* topic, byte* payload, unsigned int length) {
mySerial.print("Message arrived [");
mySerial.print(topic);
mySerial.print("] ");
for (int i = 0; i < length; i++) {
mySerial.print((char)(payload[i]));
}
mySerial.println();
}
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void reconnect() {
// Loop until we're reconnected
while (!(client.connected())) {
mySerial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
mySerial.println("connected");
} else {
mySerial.print("failed, rc=");
mySerial.print(client.state());
mySerial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
mySerial.begin(1000000);
while (status != WL_CONNECTED) {
mySerial.print("Attempting to connect to SSID: ");
mySerial.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 the hardware to sort itself out
delay(1500);
}
long nowTime = 0;
long lastTime = 0;
void loop() {
nowTime = millis();
if (nowTime > (lastTime + 200))
{
mySerial.write(0x84);
client.publish(publishTopic, publishPayload);
lastTime = nowTime;
}
client.loop();
if (!(client.connected())) {
reconnect();
}
}
from ambd_arduino.
Hi,
I have taken a look at your code. Saw that you have changed all the Serial
into mySerial
. I would recommend that you test out the default MQTT example code that we have in Arduino SDK first and double-check if it is working because I have tested that MQTT example and it working on my side. Thank you.
from ambd_arduino.
Hi,
I have tried the Serial in this program, and still have the problem.
and it happened in 5-15min.
Can you run this code for at least twenty minutes and let me know if there is any disconnection?
Thank you very much.
/*
Basic MQTT example
This sketch demonstrates the basic capabilities of the library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
*/
#include <WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
char ssid[] = "CJS_5G"; // your network SSID (name)
char pass[] = "77777777"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char mqttServer[] = "192.168.137.1";
char clientId[] = "amebaClient";
char publishTopic[] = "outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (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("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
while (status != WL_CONNECTED) {
Serial.print("Attempting 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 the hardware to sort itself out
delay(1500);
}
long nowTime = 0;
long lastTime = 0;
void loop() {
nowTime = millis();
if (nowTime > (lastTime + 200))
{
Serial.write(0x84);
client.publish(publishTopic, publishPayload);
lastTime = nowTime;
}
client.loop();
if (!(client.connected())) {
reconnect();
}
}
from ambd_arduino.
@Brian9871
Hi, are you able to connect the default example? Without any editing. Arduino_package/hardware/libraries/MQTTClient/examples/MQTT_Basic/MQTT_Basic.ino
from ambd_arduino.
Hi, @ambiot ,
But in my project, I need to use TX_0 and RX_0 to received sensor data.
Is it possible to achieve the myserial program I provided above?
from ambd_arduino.
TX_0
https://www.amebaiot.com/wp-content/uploads/2021/10/bw16/2-2.png
there is no TX_0 or RX_0
from ambd_arduino.
@ambiot The TX_0 and RX_0 means that Serial_TX and Serial_RX.
If you have the BW16 kit, you can see TX_0 and RX_0 written on it...
All in all, I hope to use Serial RX/TX to receive data and upload it through MQTT.
However, at present, when SoftSerial and MQTT are used at the same time, there will be occasional disconnections.
The code just uses example and SoftSerial to cause problems. I hope someone can provide relevant assistance.
Thanks.
from ambd_arduino.
#75 (comment)
I hope someone can confirm first to see if there is any problem with this code, but this only integrates the MQTT Example and the SoftSerial Example.
This code structure is also very simple, but there will be a disconnection problem in 5-20 minutes.
from ambd_arduino.
Issue fixed with #101
from ambd_arduino.
Related Issues (20)
- Pinout LED blue (BW16 RTL8720DN) HOT 3
- mDNS service discovery HOT 3
- [MicroPython feature request] WiFi scan improve. HOT 3
- Compatibility issue: AMB26(RTL8720DF) and NORA-W30(RTL8720DF) share same #defined(BOARD_RTL8720DF). HOT 3
- Permission denied when compiling on Linux HOT 4
- Compile fails on Windows if space exists in path (eg username) HOT 4
- [Question] How to install amb-D board using release .zip file? HOT 2
- Send raw ieee80211 data HOT 3
- Expose WiFi channel in WiFi.scanNetworks() and update example to showcase usage HOT 5
- I confirm I have checked existing/closed list of issues and the Contributing Guide.
- Spelling mistakes (GitHub Action Spell Check) HOT 4
- AMB25 and AMB26 upload issues HOT 2
- Documentation/instructions/support files for how to rebuild the libraries in "variants/common_libs" (source in system) HOT 6
- Error compiling for board Ai-Thinker BW16 (RTL8720DN) HOT 3
- UDP.h is missing in core HOT 2
- The SPI.h file error HOT 3
- BW16 MQTT can't use ArduinoJSON.h HOT 2
- Would it be possible to add enterprise wifi (PEAP-MSCHAPv2) support HOT 2
- SPI dataMode not updating correctly in SPIClass::beginTransaction() HOT 3
- WS2812B fails with multiple calls to show() HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ambd_arduino.