Workshop Goals
- Learn how to setup and use the Arduino MKR1000 board
- Learn how to connect the board to the adafruit.io service with the Adafruit MQTT library
- Learn how to connect your adafruit.io to other online services (like Twitter, Instagram, Google etc.) using IFTTT
- Make an Internet of Things thing that has absolutely no reason to be on the internet
Setup
There are some steps we need to first do to get to the actual fun part.
Install Arduino IDE and the Necessary Libraries
- Download and install the Arduino IDE (https://www.arduino.cc/en/Main/Software)
- Open the Arduino Boards Manager (Tools—>Board—>Boards Manager)
- Search for Arduino SAMD Boards and install the latest version (the description should mention the MKR1000 board)
- Open the Library Manager (Sketch—>Include Library—>Library Manager)
- Install the Adafruit MQTT Library
- Install the ArduinoHTTPCLient library
Setup the MKR1000 Board
If you are taking part in the Arduino Day workshop, you can skip this step. This has already been done.
- Solder the header pins to the MKR1000 board, so you can easily plug it to the breadboard
- Follow the instructions how to update the Firmware and update the SSL certificates
Adafruit.io and IFTTT
You will need an account for two services for this workshop. Adafruit IO allows us to stream, log, and interact with our data. IFTTT allows us to connect that data to other services.
- Create an Adafruit account https://accounts.adafruit.com/users/sign_up
- Log in with the account to Adafruit IO https://io.adafruit.com/
- Also register to IFTTT https://ifttt.com/
/*************************************************** Adafruit MQTT Library WINC1500 Example Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. MIT license, all text above must be included in any redistribution ****************************************************/ #include <SPI.h> #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include <WiFI.h> char ssid[] = "aalto open"; // your network SSID (name) char pass[] = ""; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; /************************* Adafruit.io Setup *********************************/ #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "your_id" #define AIO_KEY "your_key" /************ Global State (you don't need to change this!) ******************/ //Set up the wifi client WiFiClient client; Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); // You don't need to change anything below this line! #define halt(s) { Serial.println(F( s )); while(1); } /****************************** Feeds ***************************************/ // Setup a feed called 'photocell' for publishing. // Notice MQTT paths for AIO follow the form: /feeds/ Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell"); // Setup a feed called 'onoff' for subscribing to changes. Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff"); /*************************** Sketch Code ************************************/ #define LEDPIN 6 void setup() { while (!Serial); Serial.begin(115200); Serial.println(F("Adafruit MQTT demo for WINC1500")); // Initialise the Client Serial.print(F("\nInit the WiFi module...")); // check for the presence of the breakout if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WINC1500 not present"); // don't continue: while (true); } Serial.println("ATWINC OK!"); pinMode(LEDPIN, OUTPUT); mqtt.subscribe(&onoffbutton); } uint32_t x=0; 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(); // this is our 'wait for incoming subscription packets' busy subloop Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))) { if (subscription == &onoffbutton) { Serial.print(F("Got: ")); Serial.println((char *)onoffbutton.lastread); if (0 == strcmp((char *)onoffbutton.lastread, "OFF")) { digitalWrite(LEDPIN, LOW); } if (0 == strcmp((char *)onoffbutton.lastread, "ON")) { digitalWrite(LEDPIN, HIGH); } } } // Now we can publish stuff! Serial.print(F("\nSending photocell val ")); Serial.print(x); Serial.print("..."); if (! photocell.publish(x++)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } } // 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; // attempt to connect to Wifi network: while (WiFi.status() != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid); // wait 10 seconds for connection: uint8_t timeout = 10; while (timeout && (WiFi.status() != WL_CONNECTED)) { timeout--; delay(1000); } } // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); 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 } Serial.println("MQTT Connected!"); }