Skip to content

Commit 4b32850

Browse files
authored
Merge pull request #153 from brentru/add-pyportal-mqtts
Add PyPortal MQTTS Example
2 parents 0687c95 + 2b3892a commit 4b32850

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/***************************************************
2+
Adafruit MQTT Library PyPortal Adafruit IO SSL/TLS Example
3+
4+
Must use the latest version of nina-fw from:
5+
https://github.com/adafruit/nina-fw
6+
7+
Adafruit PyPortal: https://www.adafruit.com/product/4116
8+
9+
Adafruit invests time and resources providing this open source code,
10+
please support Adafruit and open-source hardware by purchasing
11+
products from Adafruit!
12+
13+
Written by Brent Rubell for Adafruit Industries.
14+
MIT license, all text above must be included in any redistribution
15+
****************************************************/
16+
#include <WiFiNINA.h>
17+
#include <SPI.h>
18+
#include "Adafruit_MQTT.h"
19+
#include "Adafruit_MQTT_Client.h"
20+
21+
/************************* WiFi Access Point *********************************/
22+
23+
#define WLAN_SSID "WLAN_SSID"
24+
#define WLAN_PASS "WLAN_PASSWORD"
25+
int keyIndex = 0; // your network key Index number (needed only for WEP)
26+
27+
int status = WL_IDLE_STATUS;
28+
/************************* Adafruit.io Setup *********************************/
29+
30+
#define AIO_SERVER "io.adafruit.com"
31+
// Using port 8883 for MQTTS
32+
#define AIO_SERVERPORT 8883
33+
// Adafruit IO Account Configuration
34+
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
35+
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
36+
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
37+
38+
/************ Global State (you don't need to change this!) ******************/
39+
40+
// WiFiSSLClient for SSL/TLS support
41+
WiFiSSLClient client;
42+
43+
// Setup the MQTT client class by WLAN_PASSing in the WiFi client and MQTT server and login details.
44+
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
45+
/****************************** Feeds ***************************************/
46+
47+
// Setup a feed called 'test' for publishing.
48+
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
49+
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
50+
51+
/*************************** Sketch Code ************************************/
52+
53+
void setup()
54+
{
55+
//Initialize serial and wait for port to open:
56+
Serial.begin(115200);
57+
while (!Serial)
58+
{
59+
; // wait for serial port to connect. Needed for native USB port only
60+
}
61+
62+
// check for the WiFi module:
63+
if (WiFi.status() == WL_NO_MODULE)
64+
{
65+
Serial.println("Communication with WiFi module failed!");
66+
// don't continue
67+
while (true)
68+
;
69+
}
70+
71+
String fv = WiFi.firmwareVersion();
72+
if (fv < "1.0.0")
73+
{
74+
Serial.println("Please upgrade the firmware");
75+
}
76+
77+
// attempt to connect to WiFi network:
78+
while (status != WL_CONNECTED)
79+
{
80+
Serial.print("Attempting to connect to WLAN_SSID: ");
81+
Serial.println(WLAN_SSID);
82+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
83+
status = WiFi.begin(WLAN_SSID, WLAN_PASS);
84+
85+
// wait 10 seconds for connection:
86+
delay(10000);
87+
}
88+
Serial.println("Connected to wifi");
89+
printWiFiStatus();
90+
}
91+
92+
uint32_t x = 0;
93+
94+
void loop()
95+
{
96+
// Ensure the connection to the MQTT server is alive (this will make the first
97+
// connection and automatically reconnect when disconnected). See the MQTT_connect
98+
// function definition further below.
99+
MQTT_connect();
100+
101+
// Now we can publish stuff!
102+
Serial.print(F("\nSending val "));
103+
Serial.print(x);
104+
Serial.print(F(" to test feed..."));
105+
if (!test.publish(x++))
106+
{
107+
Serial.println(F("Failed"));
108+
}
109+
else
110+
{
111+
Serial.println(F("OK!"));
112+
}
113+
114+
// wait a couple seconds to avoid rate limit
115+
delay(2000);
116+
}
117+
118+
// Function to connect and reconnect as necessary to the MQTT server.
119+
// Should be called in the loop function and it will take care if connecting.
120+
void MQTT_connect()
121+
{
122+
int8_t ret;
123+
124+
// Stop if already connected.
125+
if (mqtt.connected())
126+
{
127+
return;
128+
}
129+
130+
Serial.print("Connecting to MQTT... ");
131+
132+
uint8_t retries = 3;
133+
while ((ret = mqtt.connect()) != 0)
134+
{ // connect will return 0 for connected
135+
Serial.println(mqtt.connectErrorString(ret));
136+
Serial.println("Retrying MQTT connection in 5 seconds...");
137+
mqtt.disconnect();
138+
delay(5000); // wait 5 seconds
139+
retries--;
140+
if (retries == 0)
141+
{
142+
// basically die and wait for WDT to reset me
143+
while (1)
144+
;
145+
}
146+
}
147+
148+
Serial.println("MQTT Connected!");
149+
}
150+
151+
void printWiFiStatus()
152+
{
153+
// print the SSID of the network you're attached to:
154+
Serial.print("SSID: ");
155+
Serial.println(WiFi.SSID());
156+
157+
// print your board's IP address:
158+
IPAddress ip = WiFi.localIP();
159+
Serial.print("IP Address: ");
160+
Serial.println(ip);
161+
162+
// print the received signal strength:
163+
long rssi = WiFi.RSSI();
164+
Serial.print("signal strength (RSSI):");
165+
Serial.print(rssi);
166+
Serial.println(" dBm");
167+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit MQTT Library
2-
version=0.20.4
2+
version=1.0.0
33
author=Adafruit
44
maintainer=Adafruit <info@adafruit.com>
55
sentence=MQTT library that supports the FONA, ESP8266, Yun, and generic Arduino Client hardware.

0 commit comments

Comments
 (0)