The term Internet of Things (IoT) is somewhat fuzzy but gets thrown around a lot – at the end of the day, it’s really just the idea of connecting things to the Internet and each other. This tutorial will cover how to do the former – connecting things (i.e. your Arduino board) to the Internet. By the end of this tutorial, you will be able to connect your Arduino to the Internet and publish photoresistor sensor data onto the Adafruit IO system.
*This tutorial is adapted from Adafruit tutorials. (Part I + Part II)
WHAT YOU NEED:
- ADAFRUIT ATWINC1500 WIFI BREAKOUT (GET IT HERE)
- ARDUINO UNO R3
- BREADBOARD
- SOLDERING KIT
- PHOTORESISTOR
- 10K OHM RESISTOR
- A BUNCH OF JUMPER WIRES
PART I | CONNECT TO THE INTERNET
1.1 ATWINC1500 WIFI BREAKOUT BOARD | ASSEMBLY
a) Insert the header strip onto your breadboard – long pins down (you may also choose to snap off the header strip to the length to match that of the breakout board)
b) Place the breakout board over the pins so that the short pins poke through.
c) Set up your soldering kit (for instructions on how to solder, read adafruit’s awesome guide to soldering or watch this video, brought to you by, yes, adafruit again.
d) Solder all the pins to corresponding breakout pads. Check your solder joints. (if you messed up, dw – watch adafruit’s desoldering video)
1.2 ATWINC1500 WIFI BREAKOUT BOARD | WIRING
*wiring/pin assignments assuming you are using Arduino UNO
- Vin – connect this to 5V
- GND – connect to common ground
- SCK – Connect to SPI clock. (pin #13)
- MISO – Connect to SPI MISO. (pin #12)
- MOSI – Connect to SPI MOSI. (pin #11)
- CS – Connect to any digital I/O pin (pin #8 by default)
- EN – connect this to 5V (if you’re using UNO)
- IRQ – Connect to any digital I/O pin, preferably one with an interrupt capability. (pin #7 by default)
- RST – Connect to any digital I/O pin. (pin #4 by default)
1.3 ATWINC1500 WIFI BREAKOUT BOARD | CODE TO SET UP AND TEST CONNECTION TO THE INTERNET
a) Download and install the WiFi101 library.
b) Open Arduino IDE and load File/Examples/WiFi101/CheckWifi101FirmwareVersion sketch
c) Add the line of code below to the beginning of void setup()
1 2 |
//Configure pins for Adafruit ATWINC1500 Breakout WiFi.setPins(8,7,4); |
d) Upload to Arduino, open up serial monitor, set baud rate to 9600 baud. You should able to see your firmware version. If you get Check result: NOT PASSED, hang in there. Else, skip to f).
e) Follow this tutorial to update your firmware to get your ATWINC1500 firmware back in sync with newer versions of the WiFi101 library.
***Note: If your firmware is still out of date even after following the steps in the tutorial above, download the latest (as of 11/11/17) release of the WiFi-Firmware-Updater-Plugin from this link, unpack the zip archive into tools folder – create one if it doesn’t exist yet – within your Arduino folder. It should look something like /Arduino/tools/WiFi101/tool/WiFi101.jar. Restart Arduino IDE and redo the tutorial in step e) again.
f) Yay! Your ATWINC1500 firmware is in sync with the WiFi101 library. You should see Check result: PASSED.
g) Your ATWINC1500 is ready to scan for available networks. Open the ScanNetworks sketch in the examples folder. Add the line of code below to the beginning of void setup()
1 2 |
//Configure pins for Adafruit ATWINC1500 Breakout WiFi.setPins(8,7,4); |
Upload the code to your Arduino and open up serial monitor. You should see a list of available networks.
h) Now you can finally connect your Arduino to the Internet! Open up the WiFi101WebClient sketch in the examples folder. Add the line of code below to the beginning of void setup()
1 2 |
//Configure pins for Adafruit ATWINC1500 Breakout WiFi.setPins(8,7,4); |
Replace the ssid and pass variables with your network name and password.
1 2 |
char ssid[] = "goodkarma 1"; // your network SSID (name) char pass[] = "11111111"; // your network password |
Upload code to Arduino and open up serial monitor.
SUCCESS!
Now that your Arduino is connected to the Internet, you can continue on to Part II, where you can start pushing photoresistor sensor data to Adafruit IO.
PART II | PUSHING PHOTORESISTOR SENSOR DATA TO ADAFRUIT IO
2.1 SET UP YOUR PHOTORESISTOR
- Connect one side of the photoresistor to 5 Volts (5V)
- Connect the other side of the photoresistor to ANALOG pin
- Connect a 10K Ohm resistor between ANALOG pin 0 and GND
Your breadboard should look something like this:
To make your own breadboard diagram with adafruit parts (e.g. the ATWINC1500), download this folder and import part to fritzing.
2.2 ADAFRUIT IO LOGISTICS
a) Go to Adafruit IO and sign up for an account.
b) Log in, click on Feeds left column, click on the Actions dropdown to create a new feed called “photoresistorVal”
c) On the left column, click on View AIO Key. Copy the key and keep it handy. We will need this in a bit.
2.3 ADAFRUIT MQTT CODE
a) Download and install the Adafruit MQTT library (place folder into your Arduino/libraries folder)
b) Open up Arduino IDE, copy and paste following code:
***Remember to replace ssid, pass, AIO_USERNAME, and AIO_KEY variables with your own network name, password, Adafruit IO username and Adafruit IO Key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
/********************************************************** CPLN 571 FINAL PROJECT | TUTORIAL ATWINC1500 WIFI BREAKOUT TUTORIAL - PART II +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ |P| |U| |B| |L| |I| |S| |H| |I| |N| |G| |D| |A| |T| |A| +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+ ######## ## ## ######## ###### ###### #### ### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ###### ## ####### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##### ## ###### ## ## #### ## ## ###### This tutorial will show you how to publish your photoresistor sensor data (light level readings) onto the Adafruit IO system. What you need: + Arduino Uno R3 + Adafruit ATWINC1500 WiFi Breakout (see Part I of tutorial for circuitry instructions) https://www.adafruit.com/product/2999 + Photoresistor (light sensor) and 10K Ohm resistor - Connect one side of the photoresistor to 5 Volts (5V) - Connect the other side of the photoresistor to ANALOG pin 0 - Connect a 10K resistor between ANALOG pin 0 and GND + A bundle of jumper wires This code section of the tutorial assumes you have/are: + Downloaded and installed the Adafruit MQTT Library (https://github.com/adafruit/Adafruit_MQTT_Library) + Signed up for an Adafruit IO account (https://io.adafruit.com) // please see tutorial text on finding adafruit.io key and setting up feeds + Using an Arduino (or compatible) + Connecting to Adafruit IO's MQTT server + Connecting via the Internet (see Part I of tutorial on how to do this) Tutorial and code modified based on: https://learn.adafruit.com/mqtt-adafruit-io-and-you?view=all https://github.com/adafruit/Adafruit_MQTT_Library/blob/master/examples/mqtt_winc1500/mqtt_winc1500.ino 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 Libraries ********/ #include <SPI.h> #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include <Adafruit_WINC1500.h> //https://github.com/adafruit/Adafruit_WINC1500 /******** WiFi Breakout Pins Setup *********/ //refer to Part I of tutorial on //connecting your Arduino to the Internet //with the ATWINC1500 WiFi Breakout #define WINC_CS 8 #define WINC_IRQ 7 #define WINC_RST 4 #define WINC_EN 2 // or, tie EN to VCC Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST); char ssid[] = "lovelovestolove"; // your network SSID (name) char pass[] = "ambiguity"; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; /********* Adafruit.io Setup *********/ //sign up and set up feeds on https://io.adafruit.com #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "yourUsername" //replace with your adafruit.io username #define AIO_KEY "yourAIOKey" //replace with your adafruit key (click View AIO Key) /******** Global State ********/ // you don't need to change anything here //Set up the wifi client Adafruit_WINC1500Client 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 on adafruit.io called 'photoresistorVal' for publishing photoresistor sensor values to // Note that MQTT paths for AIO follow the form: <username>/feeds/<feedname> Adafruit_MQTT_Publish photoresistorVal = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photoresistorVal"); // Setup a feed called 'onoff' for subscribing to changes. Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff"); /*************************** Sketch Code ************************************/ #define SENSORPIN A0 // assign analog pin for photoresistor unsigned int lightLevel, high = 0, low = 1023; // set var for reading sensor values void setup() { //Initialize Serial communication while (!Serial); Serial.begin(115200); //set baud rate Serial.println(F("PUSHING PHOTORESISTOR SENSOR VALUES TO ADAFRUIT IO DEMO")); // Initialize 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!"); } void loop() { // Ensure the connection to the MQTT server is alive // (this will make the first connection and // automatically reconnect when disconnected) MQTT_connect(); // See the MQTT_connect function defined further below. // 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); } } // Measure light levels lightLevel = analogRead(SENSORPIN); // Print out to serial monitor to check photoresistor sensor values Serial.print(F("\nSending photoresistor value:")); Serial.print(lightLevel); // Now we can publish stuff! int32_t prDat = lightLevel; if (! photoresistorVal.publish(prDat)) { Serial.println(F("\nFailed")); } else { Serial.println(F("\nSuccess!")); } } // 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, pass); // 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!"); } |
c) Upload to Arduino, open up serial monitor to check photoresistor sensor values. Log on to Adafruit IO, click on Feeds/photoresistorVal to see data logged. (Try shining a light at or covering the photoresistor to check if serial monitor/Adafruit IO is registering variations in light levels)
Tip: Under the Actions dropdown, there are options to download data as JSON or CSV.
aaaaand that’s it folks.