Created
January 7, 2018 17:15
-
-
Save iampaulidrobo/386bed2e1789ca148a4c703ed41dc530 to your computer and use it in GitHub Desktop.
Adafruit MQTT connection using Node MCU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code by Piyush Tailor | |
#include "config.h" | |
/************************ Example Starts Here *******************************/ | |
// digital pin 5 | |
#define LED_PIN 13 | |
// set up the 'digital' feed | |
AdafruitIO_Feed *LED1 = io.feed("LED1"); | |
void setup() { | |
// set led pin as a digital output | |
pinMode(LED_PIN, OUTPUT); | |
// start the serial connection | |
Serial.begin(115200); | |
// wait for serial monitor to open | |
while(! Serial); | |
Serial.println(); | |
// connect to io.adafruit.com | |
Serial.print("Connecting to Adafruit IO"); | |
io.connect(); | |
// set up a message handler for the 'digital' feed. | |
// the handleMessage function (defined below) | |
// will be called whenever a message is | |
// received from adafruit io. | |
LED1->onMessage(handleMessage); | |
// wait for a connection | |
while(io.status() < AIO_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
// we are connected | |
Serial.println(); | |
Serial.println(io.statusText()); | |
} | |
void loop() { | |
// io.run(); is required for all sketches. | |
// it should always be present at the top of your loop | |
// function. it keeps the client connected to | |
// io.adafruit.com, and processes any incoming data. | |
io.run(); | |
} | |
// this function is called whenever an 'digital' feed message | |
// is received from Adafruit IO. it was attached to | |
// the 'digital' feed in the setup() function above. | |
void handleMessage(AdafruitIO_Data *data) { | |
Serial.print("received <- "); | |
if(data->toPinLevel() == HIGH) | |
Serial.println("HIGH"); | |
else | |
Serial.println("LOW"); | |
// write the current state to the led | |
digitalWrite(LED_PIN, data->toPinLevel()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment