Skip to content

Commit

Permalink
Minor update for MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
M-ichae-l committed Jul 4, 2023
1 parent ccdf823 commit 6dfd170
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void setup() {

//Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ PubSubClient client(wifiClient);
void reconnect() {
// Loop until we're reconnected
while (!(client.connected())) {
Serial.print("\nAttempting MQTT connection...");
Serial.print("\r\nAttempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");
Expand All @@ -71,7 +71,7 @@ void setup() {

//Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void setup() {

//Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ char subscribeTopic[] = "inTopic";
WiFiSSLClient wifiClient;
PubSubClient client(wifiClient);

#define MQTT_TLS_SERVER_AUTH 0

/* Mosquitto Root CA can be download here:
* https://test.mosquitto.org/
*/
Expand Down Expand Up @@ -51,6 +53,7 @@ char* rootCABuff = \
"m/XriWr/Cq4h/JfB7NTsezVslgkBaoU=\n" \
"-----END CERTIFICATE-----\n";

#if MQTT_TLS_SERVER_AUTH
char* certificateBuff = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDjTCCAnWgAwIBAgIBADANBgkqhkiG9w0BAQsFADCBkDELMAkGA1UEBhMCR0Ix\n" \
Expand All @@ -76,7 +79,7 @@ char* certificateBuff = \
"-----END CERTIFICATE-----\n";

char* privateKeyBuff = \
"-----BEGIN RSA PRIVATE KEY-----\n" \
"-----BEGIN PRIVATE KEY-----\n" \
"MIIEpAIBAAKCAQEAmog1ck7TyKdDw/943tfGLvwY/rz6DCZkosOQCYEZhjMSjX4M\n" \
"BNcCU/rhwm8EP+1uYZfHrdsijalMuSUGUOi9Y1t+SqzYLvvgLi8+h3LIGiGvqqhs\n" \
"97iyWNu1WfWEMqBxjRVAI7/+u3b/1Ztkz9UBN7+/y4jGLTsaW4IRQ5qCwt58ov+Q\n" \
Expand All @@ -102,7 +105,8 @@ char* privateKeyBuff = \
"mlEBWNsCgYBAsNfSFgDnSTcsSkfx/dYSXaQmKuoEDCztWjx+lDii3nvkI1HigM4P\n" \
"Q0fJ6v8y/ivow9zRYuO+k8VN5TRe7ml3VsKYk6rCX80MuKR9oOPvwyuoJ6Bi/51h\n" \
"6keVd5li5TtenbNLgDZxKTelJtDO8zSf1Fix/UnbN1kRM8ka+yAflw==\n" \
"-----END RSA PRIVATE KEY-----\n";
"-----END PRIVATE KEY-----\n";
#endif

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Expand All @@ -117,7 +121,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
void reconnect() {
// Loop until we're reconnected
while (!(client.connected())) {
Serial.println("Attempting MQTT connection...");
Serial.println("\r\nAttempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");
Expand All @@ -136,37 +140,37 @@ void reconnect() {
}

void setup() {
Serial.begin(115200);
Serial.begin(115200);

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);
delay(1000);
}

while (status != WL_CONNECTED) {
Serial.print("\r\n 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);
delay(1000);
}

client.setKeepAlive(keepAliveTimer);

/*/
wifiClient.setRootCA((unsigned char*)rootCABuff);
wifiClient.setClientCertificate((unsigned char*)certificateBuff, (unsigned char*)privateKeyBuff);
client.setServer(mqttServer, 8884);
/*/
wifiClient.setRootCA((unsigned char*)rootCABuff);
client.setServer(mqttServer, 8883);

client.setCallback(callback);

// Allow the hardware to sort itself out
delay(1500);
client.setKeepAlive(keepAliveTimer);

#if MQTT_TLS_SERVER_AUTH
wifiClient.setRootCA((unsigned char*)rootCABuff);
wifiClient.setClientCertificate((unsigned char*)certificateBuff, (unsigned char*)privateKeyBuff);
client.setServer(mqttServer, 8884);
#else
wifiClient.setRootCA((unsigned char*)rootCABuff);
client.setServer(mqttServer, 8883);
#endif

client.setCallback(callback);

// Allow the hardware to sort itself out
delay(1500);
}

void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
delay(1000);
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
delay(1000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ maintainer=Realtek <[email protected]>
sentence=A client library for MQTT messaging.
paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.
category=Communication
url=http://www.amebaiot.com/ameba-arduino-peripherals-examples/
url=http://www.amebaiot.com
architectures=AmebaD
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
if (!connected()) {
int result = 0;


if(_client->connected()) {
result = 1;
} else {
Expand Down

0 comments on commit 6dfd170

Please sign in to comment.