Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Adafruit MQTT Library ESP8266 Example,
hacked up for fun by Factor Mystic
Must use ESP8266 Arduino from:
Works great with Adafruit's Huzzah ESP board & Feather
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID "ssid"
#define WLAN_PASS "pasword"
#define AIO_SERVER "ip of pc running mqtt broker"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME ""
#define AIO_KEY ""
WiFiClient client;
Adafruit_MQTT_Publish test_esp_pub = Adafruit_MQTT_Publish(&mqtt, "test/esp");
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
// Initialize the LED_BUILTIN pin as an output
Serial.println(F("minimal MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
while (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connected");
Serial.println("IP address: ");
uint32_t x = 0;
const char* const lines[] = {
"Target acquired.",
"Dispensing product.",
"Hello friend.",
"There you are.",
"I see you.",
"Sentry mode activated.",
"Is anyone there?",
"Could you come over here?",
"Coming through.",
"Excuse me.",
"My fault.",
"Preparing to dispense product.",
"There you are.",
"Who's there?",
"Critical error.",
"Sorry, we're closed.",
"Shutting down.",
"I don't blame you.",
"I don't hate you.",
"No hard feelings",
"Hey hey hey.",
"Put me down.",
"Illegal operation.",
"Who are you?",
"Please put me down.",
"Uh oh.",
"Sleep mode activated.",
"Your business is appreciated.",
"Nap time.",
"Are you still there?",
"Target lost.",
"Can I help you?",
"Hey, it's me!",
"Don't shoot!",
"Stop shooting!",
"Self test error.",
"Unknown error.",
"Ow ow ow ow owww.",
"I did everything you asked!",
"I don't understand!",
"I'm fine!",
"Wheeeeee-OH NO!",
"Target Acquired!",
"I'm afraid of heights!",
"See you soon!",
"I'm scared!",
"Glorious freedom!",
"I'm flyinnnng!",
"Come closer.",
"Something's wrong...",
"What are you doing?",
"It burns.",
"I'm on fire.",
"I'm on fire ow.",
"Please stop.",
"You've made your point.",
"Okay, you win.",
"This is not good.",
"Can't breathe...",
"Excuse me, you're squishing me.",
"Um, Hello?",
"Help! Being squished!",
"Excuse me?",
"I'm different...",
"Thanks anyway...",
"Take me with you...",
"Good shot!",
"Well done!",
"I need backup!",
"You have excellent aim!",
"I never liked her.",
"These things happen.",
"That was nobody's fault.",
"She was provoking you.",
"Oh dear.",
"Oh my!",
"I blame myself.",
"He probably deserved it.",
"I saw it. It was an accident.",
"She's probably okay.",
"But I need to protect the humans!",
"We're back!",
"Hi there!",
"Back again!",
"Hi again!",
"Well, hello, stranger!",
"Hello there!",
"This time is OUR time.",
"This time...",
"Second time's a charm...",
"Howdy, stranger! We're back!",
"Annnnd we're back!",
"Hi there!",
"Back, and deadlier than ever!",
"Thought you'd seen the last of us, didn't ya?",
"Hurt me once, shame on me. Hurt me twice...",
"I thought we fixed that.",
"Awwwww, come on.",
"What's a guy gotta got to do to get some bullets around here?",
"Ah for Pete's sake!",
"Nope. Still can't see.",
"Yep. Still blind.",
"Click click click! Still defective!",
"Yeah, that's right! Still defective!",
"Yeah! Still not working.",
"Yeah! Non-lethal as ever.",
"Can't. See. A. Thing.",
"Absolutely no improvement.",
"Not getting better with age!",
"This is just getting embarrassing.",
"Alright, you can go.",
"[sigh] Don't tell anyone about this.",
"Dang! Alright, if anyone asks, I killed you.",
"Hey, be a sport lady and just tell him I killed you.",
"Well played.",
"Well, I tried. Best of Luck!",
"Best of luck, lady!",
"Hey, safe travels, there.",
"Hey, nice potato!",
"Like the potato!",
"Hey, thanks so much!",
"Don't be a stranger!",
"It's been a pleasure!",
"Take me with you? Please?",
"Hey, give 'em hell, sweetheart!",
"That was fun, wasn't it?",
"Well, you bested me this time.",
"Touche, young lady, touche.",
"It's my big chance!",
"I won't let you down!",
"You won't regret this!",
"Now it's broken turret's time to shine!",
"Get ready for it!",
"You asked for this!",
"Here it comes, pal!",
"Locked and loaded!",
"Ha ha! Gotcha right where I want ya!",
"Ah, crap.",
"Agggh, not again!",
"Ohhh, not now!",
"I'm fired, aren't I?",
"This is trouble.",
"Oh, this ain't good.",
"Watch out, pal!",
"Agggh! I'm on fire!",
"I can't see a thing. What just happened? Better open fire! [click click click click] Dang!",
"Oh thank god. You saved my bacon, pal. Where we going? Is this a jailbreak? I can't see a thing.",
"Yeah! Let's do this!",
"Yeah, alright.",
"No, wait, wait!",
"Hey, hold on now WHOA WHOA WHOA!",
"Oh, this is ridiculous!",
"Oh, come on!",
"What are you doing no no no no no!",
"Oh, this is ridiculous!",
"Well, I tried.",
"Well, can't win 'em all...",
"You can't fire me I quit!",
"Oh, come on, you guys!",
"Oh, this is just PERFECT!",
"Oh, come ON!",
"Aw, come on!",
"This isn't fair!",
"Give me another channnnce!",
"No no wait wait wait-AGGGHHHH",
"Ah, come on!",
"Hey, hold on whoa whoa whoa whoa!",
"Hey wait wait wait wait wait wait!",
"No no, no no, no no, whoa whoa, no, whoa!",
"Where we going? What are we doing? Agh!",
"Hey, where are we going-NOOOOOOOOOOOOO!",
"You messed with the wrong turret!",
"Heh heh heh heh...",
"Moo hoo ha ha ha ha...",
"Yeah! Ha ha ha!",
"Heh heh heh...",
"Really? Alright.",
"Yes indeed. Yes indeed.",
"Alright. Your funeral, pal.",
"So... we're all supposed to be blind, then, right? It's not just me? Alright, fantastic.",
"I, uh... don't have any bullets. Are you gonna give me any bullets? Are the bullets up there?",
"Where do I get my gun?",
"Do, uh.... we get some eyes at some point?",
"Yeah, I'm uh... I'm a bad man!",
"I, uh... I am a bad man.",
"Well, I gave it everything I could. Can't ask for more than that.",
"Uhhhh... Blam! Blam blam blam! I'm not defective!",
"Well, I did my best.",
"Oh no, I'm one of the bad ones, aren't I?",
"Hey! Squeaky-voice! Gimme some of your bullets!",
"Can I get some bullets here? Anybody got bullets?",
"Anyone got any bullets?",
"Well, I tried.",
"Uhhh, no bullets. Sorry.",
"No can do.",
"Uhhh, no bullets. Sorry.",
"Hey! Squeaky-voice! How'd I do?",
"YEAH! What's happening?",
"Yeah, I GOT it, didn't I?",
"Did I hit it? I hit it, didn't I?",
"Yeah. Clickety click click. Right on the money.",
"Shootin' blanks every time, ALL the time.",
"Yeah, that's right. Little bullseye.",
"What'd I hit?",
"Well, can't win 'em all.",
"How was that?",
"Standing down.",
"I'm trouble.",
"Watch and learn, everybody. Watch and learn.",
"Alright, check THIS out.",
"Alright, stand back, everybody.",
"Time to watch the master.",
"Watch and learn, everyone. Watch and learn.",
"So what am I, uh, supposed to do here?",
"Identify yourself or I will shoot.",
"Yeah, how ya doin'.",
"Pleased to meetcha.",
"Yeah, what?",
"Yeah! Let's do this!",
"Who said that?",
"Yeah, what?",
"Put me in the game, coach!",
"Hello. Hello. Hell—Aw, crap...",
"Yeah, hey, how ya doin'?",
"I'm gonna make you proud!",
"Yeah, how ya doin'?",
"Uh, yeah, what?",
"I'm not defective!",
"I'm different...",
"Thank you.",
"Get mad!",
"Don't make lemonade!",
"Prometheus was punished by the gods for giving the gift of knowledge to man. He was cast into the bowels of the earth and pecked by birds.",
"It won't be enough.",
"The answer is beneath us.",
"Her name is Caroline.",
"That's all I can say.",
"Remember that!",
"Don't forget!",
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
Serial.print(F("\nPublishing x: "));
// about to send = led on
digitalWrite(LED_BUILTIN, LOW);
if (!test_esp_pub.publish(lines[x])) {
} else {
// pause a bit so the led doesn't flash too quickly
// send complete = led off
digitalWrite(LED_BUILTIN, HIGH);
x = random(0, 300);
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
if(! {
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println("Retrying MQTT connection in 5 seconds...");
delay(5000); // wait 5 seconds
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
Serial.println("MQTT Connected!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment