Skip to content

Commit e42f309

Browse files
committed
Add Arduino Yun example.
1 parent 2572130 commit e42f309

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Adafruit MQTT Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_MQTT_Library.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_MQTT_Library)
22

33
Arduino library for MQTT support, including access to Adafruit IO. Works with
4-
the Adafruit CC3000, FONA, ESP8266 Arduino platforms, and anything that supports
4+
the Adafruit CC3000, FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports
55
Arduino's Client interface (like Ethernet shield).
66

77
See included examples for how to use the library to access an MQTT service to

examples/mqtt_yun/mqtt_yun.ino

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/***************************************************
2+
Adafruit MQTT Library Arduino Yun Example
3+
4+
Make sure your Arduino Yun is connected to a WiFi access point which
5+
has internet access. Also note this sketch uses the Console class
6+
for debug output so make sure to connect to the Yun over WiFi and
7+
open the serial monitor to see the console output.
8+
9+
Works great with the Arduino Yun:
10+
----> https://www.adafruit.com/products/1498
11+
12+
Adafruit invests time and resources providing this open source code,
13+
please support Adafruit and open-source hardware by purchasing
14+
products from Adafruit!
15+
16+
Written by Tony DiCola for Adafruit Industries.
17+
MIT license, all text above must be included in any redistribution
18+
****************************************************/
19+
#include <Bridge.h>
20+
#include <Console.h>
21+
#include <YunClient.h>
22+
#include "Adafruit_MQTT.h"
23+
#include "Adafruit_MQTT_Client.h"
24+
25+
26+
/************************* Adafruit.io Setup *********************************/
27+
28+
#define AIO_SERVER "io.adafruit.com"
29+
#define AIO_SERVERPORT 1883
30+
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
31+
#define AIO_KEY "...your AIO key..."
32+
33+
/************ Global State (you don't need to change this!) ******************/
34+
35+
// Create a YunClient instance to communicate using the Yun's brighe & Linux OS.
36+
YunClient client;
37+
38+
// Store the MQTT server, client ID, username, and password in flash memory.
39+
// This is required for using the Adafruit MQTT library.
40+
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
41+
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
42+
// was compiled (so this should be unique across multiple devices for a user,
43+
// alternatively you can manually set this to a GUID or other random value).
44+
const char MQTT_CLIENTID[] PROGMEM = AIO_KEY __DATE__ __TIME__;
45+
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
46+
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
47+
48+
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
49+
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
50+
51+
/****************************** Feeds ***************************************/
52+
53+
// Setup a feed called 'photocell' for publishing.
54+
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
55+
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
56+
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
57+
58+
// Setup a feed called 'onoff' for subscribing to changes.
59+
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
60+
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
61+
62+
/*************************** Sketch Code ************************************/
63+
64+
void setup() {
65+
Bridge.begin();
66+
Console.begin();
67+
Console.println(F("Adafruit MQTT demo"));
68+
69+
// Setup MQTT subscription for onoff feed.
70+
mqtt.subscribe(&onoffbutton);
71+
}
72+
73+
uint32_t x=0;
74+
75+
void loop() {
76+
// Ensure the connection to the MQTT server is alive (this will make the first
77+
// connection and automatically reconnect when disconnected). See the MQTT_connect
78+
// function definition further below.
79+
MQTT_connect();
80+
81+
// Try to ping the MQTT server
82+
/*
83+
if (! mqtt.ping(3) ) {
84+
// MQTT pings failed, lets reconnect
85+
Console.println("Ping fail!");
86+
}
87+
*/
88+
89+
// this is our 'wait for incoming subscription packets' busy subloop
90+
Adafruit_MQTT_Subscribe *subscription;
91+
while ((subscription = mqtt.readSubscription(1000))) {
92+
if (subscription == &onoffbutton) {
93+
Console.print(F("Got: "));
94+
Console.println((char *)onoffbutton.lastread);
95+
}
96+
}
97+
98+
// Now we can publish stuff!
99+
Console.print(F("\nSending photocell val "));
100+
Console.print(x);
101+
Console.print("...");
102+
if (! photocell.publish(x++)) {
103+
Console.println(F("Failed"));
104+
} else {
105+
Console.println(F("OK!"));
106+
}
107+
108+
delay(1000);
109+
}
110+
111+
// Function to connect and reconnect as necessary to the MQTT server.
112+
// Should be called in the loop function and it will take care if connecting.
113+
void MQTT_connect() {
114+
int8_t ret;
115+
116+
// Stop if already connected.
117+
if (mqtt.connected()) {
118+
return;
119+
}
120+
121+
Console.print("Connecting to MQTT... ");
122+
123+
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
124+
Console.println(mqtt.connectErrorString(ret));
125+
Console.println("Retrying MQTT connection in 5 seconds...");
126+
mqtt.disconnect();
127+
delay(5000); // wait 5 seconds
128+
}
129+
Console.println("MQTT Connected!");
130+
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name=Adafruit MQTT Library
2-
version=0.10.0
2+
version=0.11.0
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
5-
sentence=MQTT library that supports the CC3000, FONA, ESP8266, and generic Arduino Client hardware.
5+
sentence=MQTT library that supports the CC3000, FONA, ESP8266, Yun, and generic Arduino Client hardware.
66
paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics.
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_MQTT_Library

0 commit comments

Comments
 (0)