Instantly share code, notes, and snippets.

Embed
What would you like to do?
/***************************************************
Adafruit MQTT Library ESP8266 Example,
hacked up for fun by Factor Mystic
https://factormystic.net/blog/a-fun-little-mqtt-program-for-the-esp8266
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
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_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
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
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println(F("minimal MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
uint32_t x = 0;
const char* const lines[] = {
"Hi.",
"Target acquired.",
"Dispensing product.",
"Firing.",
"Hello friend.",
"Gotcha.",
"There you are.",
"I see you.",
"Hellooo.",
"Searching.",
"Canvassing.",
"Sentry mode activated.",
"Is anyone there?",
"Could you come over here?",
"Coming through.",
"Excuse me.",
"Sorry.",
"My fault.",
"Ohh.",
"Hello."
"Deploying.",
"Preparing to dispense product.",
"Activated.",
"There you are.",
"Who's there?",
"Ahahaa.",
"Critical error.",
"Sorry, we're closed.",
"Shutting down.",
"I don't blame you.",
"I don't hate you.",
"Whyyyy.",
"No hard feelings",
"Aiaiaiaiaiai.",
"Hey.",
"Hey hey hey.",
"Put me down.",
"Woahhh.",
"Illegal operation.",
"Who are you?",
"Hey.",
"Please put me down.",
"Help.",
"Uh oh.",
"Goodbye.",
"Sleep mode activated.",
"Your business is appreciated.",
"Hibernating.",
"Goodnight.",
"Resting.",
"Nap time.",
"Are you still there?",
"Target lost.",
"Can I help you?",
"Searching.",
"Hey, it's me!",
"Don't shoot!",
"Stop shooting!",
"Aah.",
"Self test error.",
"Unknown error.",
"Malfunctioning.",
"Aaaah.",
"Ow ow ow ow owww.",
"Why?",
"I did everything you asked!",
"I don't understand!",
"I'm fine!",
"AHHHHHHHHH!",
"Wheeeeee-OH NO!",
"Why.",
"Target Acquired!",
"Hello?",
"Whee!",
"Wheeeeeeee!",
"I'm afraid of heights!",
"Noooooo!",
"See you soon!",
"I'm scared!",
"Hooray!",
"Glorious freedom!",
"I'm flyinnnng!",
"Goodbye!",
"Come closer.",
"Something's wrong...",
"What are you doing?",
"Failure...",
"Ow!",
"Owww!",
"Owwwww!",
"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.",
"Arrrgh!",
"Owwww!",
"Can't breathe...",
"Excuse me, you're squishing me.",
"Um, Hello?",
"Help! Being squished!",
"Hello?",
"Excuse me?",
"Hello?",
"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.",
"Noted.",
"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?",
"Yeah.",
"Hurt me once, shame on me. Hurt me twice...",
"Dang!",
"Agggh...",
"Crap!",
"Rgggh!",
"Crap!",
"Dang!",
"Dang.",
"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.",
"Okay!",
"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!",
"Oh.",
"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!",
"Die!",
"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.",
"Fantastic.",
"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...",
"Nooooooooo...",
"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",
"Nonononononono!",
"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?",
"Yeah.",
"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'?",
"Um.",
"Crap.",
"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.",
"Ahh!",
"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.
MQTT_connect();
Serial.print(F("\nPublishing x: "));
Serial.print(x);
Serial.print("...");
// about to send = led on
digitalWrite(LED_BUILTIN, LOW);
if (!test_esp_pub.publish(lines[x])) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// pause a bit so the led doesn't flash too quickly
delay(50);
// send complete = led off
digitalWrite(LED_BUILTIN, HIGH);
delay(3000);
//x++;
x = random(0, 300);
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}
// 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()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
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