├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── arduino_cc3000 ├── analog_in │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── .leonardo.test.skip │ ├── analog_in.ino │ └── cc3000helper.cpp ├── analog_out │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── .leonardo.test.skip │ ├── analog_out.ino │ └── cc3000helper.cpp ├── digital_in │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── .leonardo.test.skip │ ├── cc3000helper.cpp │ └── digital_in.ino ├── digital_out │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── .leonardo.test.skip │ ├── cc3000helper.cpp │ └── digital_out.ino └── group_sensor │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── .leonardo.test.skip │ ├── cc3000helper.cpp │ └── group_sensor.ino ├── arduino_fona ├── analog_in │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── analog_in.ino │ └── fonahelper.cpp ├── analog_out │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── analog_out.ino │ └── fonahelper.cpp ├── digital_in │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── digital_in.ino │ └── fonahelper.cpp ├── digital_out │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── digital_out.ino │ └── fonahelper.cpp ├── gps │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── fonahelper.cpp │ └── gps.ino └── group_sensor │ ├── .due.test.skip │ ├── .esp8266.test.skip │ ├── fonahelper.cpp │ └── group_sensor.ino ├── esp8266 ├── analog_in │ ├── .due.test.skip │ ├── .leonardo.test.skip │ ├── .uno.test.skip │ └── analog_in.ino ├── analog_out │ ├── .due.test.skip │ ├── .leonardo.test.skip │ ├── .uno.test.skip │ └── analog_out.ino ├── digital_in │ ├── .due.test.skip │ ├── .leonardo.test.skip │ ├── .uno.test.skip │ └── digital_in.ino ├── digital_out │ ├── .due.test.skip │ ├── .leonardo.test.skip │ ├── .uno.test.skip │ └── digital_out.ino ├── group_sensor │ ├── .due.test.skip │ ├── .leonardo.test.skip │ ├── .uno.test.skip │ └── group_sensor.ino └── trigger │ ├── .due.test.skip │ ├── .leonardo.test.skip │ ├── .uno.test.skip │ └── trigger.ino └── raspberry_pi ├── digital_in.js ├── digital_out.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.swp 3 | *.swo 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | sudo: false 3 | before_install: 4 | - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh) 5 | install: 6 | - arduino --install-library "Adafruit SleepyDog Library,Adafruit FONA Library,Adafruit CC3000 Library,Adafruit AM2315,Adafruit MQTT Library,Adafruit IO Arduino" 7 | script: 8 | - build_main_platforms 9 | notifications: 10 | email: 11 | on_success: change 12 | on_failure: change 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Adafruit Industries 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## We've moved! Check out the Adafruit IO Arduino Library over at https://github.com/adafruit/Adafruit_IO_Arduino 2 | 3 | #### Note: IO-Basics Guide Examples have been moved into the /examples/ folder: https://github.com/adafruit/Adafruit_IO_Arduino/tree/master/examples 4 | -------------------------------------------------------------------------------- /arduino_cc3000/analog_in/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/analog_in/.due.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/analog_in/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/analog_in/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/analog_in/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/analog_in/.leonardo.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/analog_in/analog_in.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library CC3000 Example 3 | 4 | Designed specifically to work with the Adafruit WiFi products: 5 | ----> https://www.adafruit.com/products/1469 6 | 7 | Adafruit invests time and resources providing this open source code, 8 | please support Adafruit and open-source hardware by purchasing 9 | products from Adafruit! 10 | 11 | Written by Limor Fried/Ladyada for Adafruit Industries. 12 | Adafruit IO example additions by Todd Treece. 13 | MIT license, all text above must be included in any redistribution 14 | ****************************************************/ 15 | #include 16 | #include 17 | #include 18 | #include "utility/debug.h" 19 | #include "Adafruit_MQTT.h" 20 | #include "Adafruit_MQTT_CC3000.h" 21 | 22 | /****************************** Pins ******************************************/ 23 | 24 | #define PHOTOCELL A0 // analog 0 25 | #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! 26 | #define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins. 27 | #define ADAFRUIT_CC3000_CS 10 28 | // Use hardware SPI for the remaining pins 29 | // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 30 | 31 | /************************* WiFi Access Point *********************************/ 32 | 33 | #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters! 34 | #define WLAN_PASS "...your password..." 35 | #define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP, 36 | // WLAN_SEC_WPA or WLAN_SEC_WPA2 37 | 38 | /************************* Adafruit.io Setup *********************************/ 39 | 40 | #define AIO_SERVER "io.adafruit.com" 41 | #define AIO_SERVERPORT 1883 42 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 43 | #define AIO_KEY "...your AIO key..." 44 | 45 | /************ Global State (you don't need to change this!) ******************/ 46 | 47 | // Setup the main CC3000 class, just like a normal CC3000 sketch. 48 | Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT); 49 | 50 | // Store the MQTT server, client ID, username, and password in flash memory. 51 | // This is required for using the Adafruit MQTT library. 52 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 53 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 54 | // was compiled (so this should be unique across multiple devices for a user, 55 | // alternatively you can manually set this to a GUID or other random value). 56 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 57 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 58 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 59 | 60 | // Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details. 61 | Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 62 | 63 | // You don't need to change anything below this line! 64 | #define halt(s) { Serial.println(F( s )); while(1); } 65 | 66 | // CC3000connect is a helper function that sets up the CC3000 and connects to 67 | // the WiFi network. See the cc3000helper.cpp tab above for the source! 68 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security); 69 | 70 | /****************************** Feeds ***************************************/ 71 | 72 | // Setup a feed called 'photocell' for publishing changes. 73 | // Notice MQTT paths for AIO follow the form: /feeds/ 74 | const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell"; 75 | Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED); 76 | 77 | /*************************** Sketch Code ************************************/ 78 | 79 | // photocell state 80 | int current = 0; 81 | int last = -1; 82 | 83 | void setup() { 84 | 85 | Serial.begin(115200); 86 | 87 | Serial.println(F("Adafruit IO Example:")); 88 | Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC); 89 | 90 | // Initialise the CC3000 module 91 | Serial.print(F("\nInit the CC3000...")); 92 | if (! cc3000.begin()) 93 | halt("Failed"); 94 | 95 | // attempt wifi connection 96 | while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { 97 | Serial.println(F("Retrying WiFi")); 98 | while(1); 99 | } 100 | Serial.println(F("Connected to WiFi!")); 101 | 102 | // connect to adafruit io 103 | connect(); 104 | 105 | } 106 | 107 | void loop() { 108 | 109 | // Make sure to reset watchdog every loop iteration! 110 | Watchdog.reset(); 111 | 112 | // ping adafruit io a few times to make sure we remain connected 113 | if(! mqtt.ping(3)) { 114 | // reconnect to adafruit io 115 | if(! mqtt.connected()) 116 | connect(); 117 | } 118 | 119 | // grab the current state of the photocell 120 | current = analogRead(PHOTOCELL); 121 | 122 | // return if the value hasn't changed 123 | if(current == last) 124 | return; 125 | 126 | // Now we can publish stuff! 127 | Serial.print(F("\nSending photocell value: ")); 128 | Serial.print(current); 129 | Serial.print("... "); 130 | 131 | if (! photocell.publish((int32_t)current)) 132 | Serial.println(F("Failed.")); 133 | else 134 | Serial.println(F("Success!")); 135 | 136 | // save the photocell state 137 | last = current; 138 | 139 | } 140 | 141 | // connect to adafruit io via MQTT 142 | void connect() { 143 | 144 | Serial.print(F("Connecting to Adafruit IO... ")); 145 | 146 | int8_t ret; 147 | 148 | while ((ret = mqtt.connect()) != 0) { 149 | 150 | switch (ret) { 151 | case 1: Serial.println(F("Wrong protocol")); break; 152 | case 2: Serial.println(F("ID rejected")); break; 153 | case 3: Serial.println(F("Server unavail")); break; 154 | case 4: Serial.println(F("Bad user/pass")); break; 155 | case 5: Serial.println(F("Not authed")); break; 156 | case 6: Serial.println(F("Failed to subscribe")); break; 157 | default: 158 | Serial.println(F("Connection failed")); 159 | CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); 160 | break; 161 | } 162 | 163 | if(ret >= 0) 164 | mqtt.disconnect(); 165 | 166 | Serial.println(F("Retrying connection...")); 167 | delay(5000); 168 | 169 | } 170 | 171 | Serial.println(F("Adafruit IO Connected!")); 172 | 173 | } 174 | -------------------------------------------------------------------------------- /arduino_cc3000/analog_in/cc3000helper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //#define STATICIP 7 | 8 | #define halt(s) { Serial.println(F( s )); while(1); } 9 | 10 | uint16_t checkFirmwareVersion(void); 11 | bool displayConnectionDetails(void); 12 | 13 | extern Adafruit_CC3000 cc3000; 14 | 15 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security) { 16 | Watchdog.reset(); 17 | 18 | // Check for compatible firmware 19 | if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!"); 20 | 21 | // Delete any old connection data on the module 22 | Serial.println(F("\nDeleting old connection profiles")); 23 | if (!cc3000.deleteProfiles()) halt("Failed!"); 24 | 25 | #ifdef STATICIP 26 | Serial.println(F("Setting static IP")); 27 | uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19); 28 | uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0); 29 | uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1); 30 | uint32_t dns = cc3000.IP2U32(8, 8, 4, 4); 31 | 32 | if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) { 33 | Serial.println(F("Failed to set static IP!")); 34 | while(1); 35 | } 36 | #endif 37 | 38 | // Attempt to connect to an access point 39 | Serial.print(F("\nAttempting to connect to ")); 40 | Serial.print(wlan_ssid); Serial.print(F("...")); 41 | 42 | Watchdog.disable(); 43 | // try 3 times 44 | if (!cc3000.connectToAP(wlan_ssid, wlan_pass, wlan_security, 3)) { 45 | return false; 46 | } 47 | 48 | Watchdog.enable(8000); 49 | Serial.println(F("Connected!")); 50 | 51 | uint8_t retries; 52 | #ifndef STATICIP 53 | /* Wait for DHCP to complete */ 54 | Serial.println(F("Requesting DHCP")); 55 | retries = 10; 56 | while (!cc3000.checkDHCP()) 57 | { 58 | Watchdog.reset(); 59 | delay(1000); 60 | retries--; 61 | if (!retries) return false; 62 | } 63 | #endif 64 | /* Display the IP address DNS, Gateway, etc. */ 65 | retries = 10; 66 | while (! displayConnectionDetails()) { 67 | Watchdog.reset(); 68 | delay(1000); 69 | retries--; 70 | if (!retries) return false; 71 | } 72 | 73 | Watchdog.reset(); 74 | 75 | return true; 76 | } 77 | 78 | 79 | /**************************************************************************/ 80 | /*! 81 | @brief Tries to read the CC3000's internal firmware patch ID 82 | */ 83 | /**************************************************************************/ 84 | uint16_t checkFirmwareVersion(void) 85 | { 86 | uint8_t major, minor; 87 | uint16_t version; 88 | 89 | if(!cc3000.getFirmwareVersion(&major, &minor)) 90 | { 91 | Serial.println(F("Unable to retrieve the firmware version!\r\n")); 92 | version = 0; 93 | } 94 | else 95 | { 96 | Serial.print(F("Firmware V. : ")); 97 | Serial.print(major); Serial.print(F(".")); Serial.println(minor); 98 | version = major; version <<= 8; version |= minor; 99 | } 100 | return version; 101 | } 102 | 103 | 104 | /**************************************************************************/ 105 | /*! 106 | @brief Tries to read the IP address and other connection details 107 | */ 108 | /**************************************************************************/ 109 | bool displayConnectionDetails(void) 110 | { 111 | uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; 112 | 113 | if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) 114 | { 115 | Serial.println(F("Unable to retrieve the IP Address!\r\n")); 116 | return false; 117 | } 118 | else 119 | { 120 | Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); 121 | Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); 122 | Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); 123 | Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); 124 | Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); 125 | Serial.println(); 126 | return true; 127 | } 128 | } 129 | 130 | -------------------------------------------------------------------------------- /arduino_cc3000/analog_out/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/analog_out/.due.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/analog_out/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/analog_out/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/analog_out/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/analog_out/.leonardo.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/analog_out/analog_out.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library CC3000 Example 3 | 4 | Designed specifically to work with the Adafruit WiFi products: 5 | ----> https://www.adafruit.com/products/1469 6 | 7 | Adafruit invests time and resources providing this open source code, 8 | please support Adafruit and open-source hardware by purchasing 9 | products from Adafruit! 10 | 11 | Written by Limor Fried/Ladyada for Adafruit Industries. 12 | Adafruit IO example additions by Todd Treece. 13 | MIT license, all text above must be included in any redistribution 14 | ****************************************************/ 15 | #include 16 | #include 17 | #include 18 | #include "utility/debug.h" 19 | #include "Adafruit_MQTT.h" 20 | #include "Adafruit_MQTT_CC3000.h" 21 | 22 | /****************************** Pins ******************************************/ 23 | 24 | #define LED 9 // PWM pin 25 | #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! 26 | #define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins. 27 | #define ADAFRUIT_CC3000_CS 10 28 | // Use hardware SPI for the remaining pins 29 | // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 30 | 31 | /************************* WiFi Access Point *********************************/ 32 | 33 | #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters! 34 | #define WLAN_PASS "...your password..." 35 | #define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP, 36 | // WLAN_SEC_WPA or WLAN_SEC_WPA2 37 | 38 | /************************* Adafruit.io Setup *********************************/ 39 | 40 | #define AIO_SERVER "io.adafruit.com" 41 | #define AIO_SERVERPORT 1883 42 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 43 | #define AIO_KEY "...your AIO key..." 44 | 45 | /************ Global State (you don't need to change this!) ******************/ 46 | 47 | // Setup the main CC3000 class, just like a normal CC3000 sketch. 48 | Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT); 49 | 50 | // Store the MQTT server, client ID, username, and password in flash memory. 51 | // This is required for using the Adafruit MQTT library. 52 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 53 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 54 | // was compiled (so this should be unique across multiple devices for a user, 55 | // alternatively you can manually set this to a GUID or other random value). 56 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 57 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 58 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 59 | 60 | // Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details. 61 | Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 62 | 63 | // You don't need to change anything below this line! 64 | #define halt(s) { Serial.println(F( s )); while(1); } 65 | 66 | // CC3000connect is a helper function that sets up the CC3000 and connects to 67 | // the WiFi network. See the cc3000helper.cpp tab above for the source! 68 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security); 69 | 70 | /****************************** Feeds ***************************************/ 71 | 72 | // Setup a feed called 'photocell' for subscribing to changes. 73 | // Notice MQTT paths for AIO follow the form: /feeds/ 74 | const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell"; 75 | Adafruit_MQTT_Subscribe photocell = Adafruit_MQTT_Subscribe(&mqtt, PHOTOCELL_FEED); 76 | 77 | /*************************** Sketch Code ************************************/ 78 | 79 | void setup() { 80 | 81 | Serial.begin(115200); 82 | 83 | Serial.println(F("Adafruit IO Example:")); 84 | Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC); 85 | 86 | // Initialise the CC3000 module 87 | Serial.print(F("\nInit the CC3000...")); 88 | if (! cc3000.begin()) 89 | halt("Failed"); 90 | 91 | // attempt wifi connection 92 | while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { 93 | Serial.println(F("Retrying WiFi")); 94 | while(1); 95 | } 96 | Serial.println(F("Connected to WiFi!")); 97 | 98 | // listen for events on the photocell feed 99 | mqtt.subscribe(&photocell); 100 | 101 | // connect to adafruit io 102 | connect(); 103 | 104 | } 105 | 106 | void loop() { 107 | 108 | Adafruit_MQTT_Subscribe *subscription; 109 | 110 | // Make sure to reset watchdog every loop iteration! 111 | Watchdog.reset(); 112 | 113 | // ping adafruit io a few times to make sure we remain connected 114 | if(! mqtt.ping(3)) { 115 | // reconnect to adafruit io 116 | if(! mqtt.connected()) 117 | connect(); 118 | } 119 | 120 | // this is our 'wait for incoming subscription packets' busy subloop 121 | while (subscription = mqtt.readSubscription(1000)) { 122 | 123 | // we only care about the photocell events 124 | if (subscription == &photocell) { 125 | 126 | // convert mqtt ascii payload to int 127 | char *value = (char*)photocell.lastread; 128 | Serial.print(F("Received: ")); 129 | Serial.println(value); 130 | int reading = atoi(value); 131 | 132 | // LED gets brighter the darker it is at the sensor 133 | // that means we have to -invert- the reading from 0-1023 back to 1023-0 134 | reading = 1023 - reading; 135 | 136 | //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses 137 | int brightness = map(reading, 0, 1023, 0, 255); 138 | analogWrite(LED, brightness); 139 | 140 | } 141 | 142 | } 143 | 144 | } 145 | 146 | // connect to adafruit io via MQTT 147 | void connect() { 148 | 149 | Serial.print(F("Connecting to Adafruit IO... ")); 150 | 151 | int8_t ret; 152 | 153 | while ((ret = mqtt.connect()) != 0) { 154 | 155 | switch (ret) { 156 | case 1: Serial.println(F("Wrong protocol")); break; 157 | case 2: Serial.println(F("ID rejected")); break; 158 | case 3: Serial.println(F("Server unavail")); break; 159 | case 4: Serial.println(F("Bad user/pass")); break; 160 | case 5: Serial.println(F("Not authed")); break; 161 | case 6: Serial.println(F("Failed to subscribe")); break; 162 | default: 163 | Serial.println(F("Connection failed")); 164 | CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); 165 | break; 166 | } 167 | 168 | if(ret >= 0) 169 | mqtt.disconnect(); 170 | 171 | Serial.println(F("Retrying connection...")); 172 | delay(5000); 173 | 174 | } 175 | 176 | Serial.println(F("Adafruit IO Connected!")); 177 | 178 | } 179 | -------------------------------------------------------------------------------- /arduino_cc3000/analog_out/cc3000helper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //#define STATICIP 7 | 8 | #define halt(s) { Serial.println(F( s )); while(1); } 9 | 10 | uint16_t checkFirmwareVersion(void); 11 | bool displayConnectionDetails(void); 12 | 13 | extern Adafruit_CC3000 cc3000; 14 | 15 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security) { 16 | Watchdog.reset(); 17 | 18 | // Check for compatible firmware 19 | if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!"); 20 | 21 | // Delete any old connection data on the module 22 | Serial.println(F("\nDeleting old connection profiles")); 23 | if (!cc3000.deleteProfiles()) halt("Failed!"); 24 | 25 | #ifdef STATICIP 26 | Serial.println(F("Setting static IP")); 27 | uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19); 28 | uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0); 29 | uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1); 30 | uint32_t dns = cc3000.IP2U32(8, 8, 4, 4); 31 | 32 | if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) { 33 | Serial.println(F("Failed to set static IP!")); 34 | while(1); 35 | } 36 | #endif 37 | 38 | // Attempt to connect to an access point 39 | Serial.print(F("\nAttempting to connect to ")); 40 | Serial.print(wlan_ssid); Serial.print(F("...")); 41 | 42 | Watchdog.disable(); 43 | // try 3 times 44 | if (!cc3000.connectToAP(wlan_ssid, wlan_pass, wlan_security, 3)) { 45 | return false; 46 | } 47 | 48 | Watchdog.enable(8000); 49 | Serial.println(F("Connected!")); 50 | 51 | uint8_t retries; 52 | #ifndef STATICIP 53 | /* Wait for DHCP to complete */ 54 | Serial.println(F("Requesting DHCP")); 55 | retries = 10; 56 | while (!cc3000.checkDHCP()) 57 | { 58 | Watchdog.reset(); 59 | delay(1000); 60 | retries--; 61 | if (!retries) return false; 62 | } 63 | #endif 64 | /* Display the IP address DNS, Gateway, etc. */ 65 | retries = 10; 66 | while (! displayConnectionDetails()) { 67 | Watchdog.reset(); 68 | delay(1000); 69 | retries--; 70 | if (!retries) return false; 71 | } 72 | 73 | Watchdog.reset(); 74 | 75 | return true; 76 | } 77 | 78 | 79 | /**************************************************************************/ 80 | /*! 81 | @brief Tries to read the CC3000's internal firmware patch ID 82 | */ 83 | /**************************************************************************/ 84 | uint16_t checkFirmwareVersion(void) 85 | { 86 | uint8_t major, minor; 87 | uint16_t version; 88 | 89 | if(!cc3000.getFirmwareVersion(&major, &minor)) 90 | { 91 | Serial.println(F("Unable to retrieve the firmware version!\r\n")); 92 | version = 0; 93 | } 94 | else 95 | { 96 | Serial.print(F("Firmware V. : ")); 97 | Serial.print(major); Serial.print(F(".")); Serial.println(minor); 98 | version = major; version <<= 8; version |= minor; 99 | } 100 | return version; 101 | } 102 | 103 | 104 | /**************************************************************************/ 105 | /*! 106 | @brief Tries to read the IP address and other connection details 107 | */ 108 | /**************************************************************************/ 109 | bool displayConnectionDetails(void) 110 | { 111 | uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; 112 | 113 | if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) 114 | { 115 | Serial.println(F("Unable to retrieve the IP Address!\r\n")); 116 | return false; 117 | } 118 | else 119 | { 120 | Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); 121 | Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); 122 | Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); 123 | Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); 124 | Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); 125 | Serial.println(); 126 | return true; 127 | } 128 | } 129 | 130 | -------------------------------------------------------------------------------- /arduino_cc3000/digital_in/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/digital_in/.due.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/digital_in/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/digital_in/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/digital_in/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/digital_in/.leonardo.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/digital_in/cc3000helper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //#define STATICIP 7 | 8 | #define halt(s) { Serial.println(F( s )); while(1); } 9 | 10 | uint16_t checkFirmwareVersion(void); 11 | bool displayConnectionDetails(void); 12 | 13 | extern Adafruit_CC3000 cc3000; 14 | 15 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security) { 16 | Watchdog.reset(); 17 | 18 | // Check for compatible firmware 19 | if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!"); 20 | 21 | // Delete any old connection data on the module 22 | Serial.println(F("\nDeleting old connection profiles")); 23 | if (!cc3000.deleteProfiles()) halt("Failed!"); 24 | 25 | #ifdef STATICIP 26 | Serial.println(F("Setting static IP")); 27 | uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19); 28 | uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0); 29 | uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1); 30 | uint32_t dns = cc3000.IP2U32(8, 8, 4, 4); 31 | 32 | if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) { 33 | Serial.println(F("Failed to set static IP!")); 34 | while(1); 35 | } 36 | #endif 37 | 38 | // Attempt to connect to an access point 39 | Serial.print(F("\nAttempting to connect to ")); 40 | Serial.print(wlan_ssid); Serial.print(F("...")); 41 | 42 | Watchdog.disable(); 43 | // try 3 times 44 | if (!cc3000.connectToAP(wlan_ssid, wlan_pass, wlan_security, 3)) { 45 | return false; 46 | } 47 | 48 | Watchdog.enable(8000); 49 | Serial.println(F("Connected!")); 50 | 51 | uint8_t retries; 52 | #ifndef STATICIP 53 | /* Wait for DHCP to complete */ 54 | Serial.println(F("Requesting DHCP")); 55 | retries = 10; 56 | while (!cc3000.checkDHCP()) 57 | { 58 | Watchdog.reset(); 59 | delay(1000); 60 | retries--; 61 | if (!retries) return false; 62 | } 63 | #endif 64 | /* Display the IP address DNS, Gateway, etc. */ 65 | retries = 10; 66 | while (! displayConnectionDetails()) { 67 | Watchdog.reset(); 68 | delay(1000); 69 | retries--; 70 | if (!retries) return false; 71 | } 72 | 73 | Watchdog.reset(); 74 | 75 | return true; 76 | } 77 | 78 | 79 | /**************************************************************************/ 80 | /*! 81 | @brief Tries to read the CC3000's internal firmware patch ID 82 | */ 83 | /**************************************************************************/ 84 | uint16_t checkFirmwareVersion(void) 85 | { 86 | uint8_t major, minor; 87 | uint16_t version; 88 | 89 | if(!cc3000.getFirmwareVersion(&major, &minor)) 90 | { 91 | Serial.println(F("Unable to retrieve the firmware version!\r\n")); 92 | version = 0; 93 | } 94 | else 95 | { 96 | Serial.print(F("Firmware V. : ")); 97 | Serial.print(major); Serial.print(F(".")); Serial.println(minor); 98 | version = major; version <<= 8; version |= minor; 99 | } 100 | return version; 101 | } 102 | 103 | 104 | /**************************************************************************/ 105 | /*! 106 | @brief Tries to read the IP address and other connection details 107 | */ 108 | /**************************************************************************/ 109 | bool displayConnectionDetails(void) 110 | { 111 | uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; 112 | 113 | if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) 114 | { 115 | Serial.println(F("Unable to retrieve the IP Address!\r\n")); 116 | return false; 117 | } 118 | else 119 | { 120 | Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); 121 | Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); 122 | Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); 123 | Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); 124 | Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); 125 | Serial.println(); 126 | return true; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /arduino_cc3000/digital_in/digital_in.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library CC3000 Example 3 | 4 | Designed specifically to work with the Adafruit WiFi products: 5 | ----> https://www.adafruit.com/products/1469 6 | 7 | Adafruit invests time and resources providing this open source code, 8 | please support Adafruit and open-source hardware by purchasing 9 | products from Adafruit! 10 | 11 | Written by Limor Fried/Ladyada for Adafruit Industries. 12 | Adafruit IO example additions by Todd Treece. 13 | MIT license, all text above must be included in any redistribution 14 | ****************************************************/ 15 | #include 16 | #include 17 | #include 18 | #include "utility/debug.h" 19 | #include "Adafruit_MQTT.h" 20 | #include "Adafruit_MQTT_CC3000.h" 21 | 22 | /****************************** Pins ******************************************/ 23 | 24 | #define BUTTON 2 25 | #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! 26 | #define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins. 27 | #define ADAFRUIT_CC3000_CS 10 28 | // Use hardware SPI for the remaining pins 29 | // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 30 | 31 | /************************* WiFi Access Point *********************************/ 32 | 33 | #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters! 34 | #define WLAN_PASS "...your password..." 35 | #define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP, 36 | // WLAN_SEC_WPA or WLAN_SEC_WPA2 37 | 38 | /************************* Adafruit.io Setup *********************************/ 39 | 40 | #define AIO_SERVER "io.adafruit.com" 41 | #define AIO_SERVERPORT 1883 42 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 43 | #define AIO_KEY "...your AIO key..." 44 | 45 | /************ Global State (you don't need to change this!) ******************/ 46 | 47 | // Setup the main CC3000 class, just like a normal CC3000 sketch. 48 | Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT); 49 | 50 | // Store the MQTT server, client ID, username, and password in flash memory. 51 | // This is required for using the Adafruit MQTT library. 52 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 53 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 54 | // was compiled (so this should be unique across multiple devices for a user, 55 | // alternatively you can manually set this to a GUID or other random value). 56 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 57 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 58 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 59 | 60 | // Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details. 61 | Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 62 | 63 | // You don't need to change anything below this line! 64 | #define halt(s) { Serial.println(F( s )); while(1); } 65 | 66 | // CC3000connect is a helper function that sets up the CC3000 and connects to 67 | // the WiFi network. See the cc3000helper.cpp tab above for the source! 68 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security); 69 | 70 | /****************************** Feeds ***************************************/ 71 | 72 | // Setup a feed called 'button' for publishing changes. 73 | // Notice MQTT paths for AIO follow the form: /feeds/ 74 | const char BUTTON_FEED[] PROGMEM = AIO_USERNAME "/feeds/button"; 75 | Adafruit_MQTT_Publish button = Adafruit_MQTT_Publish(&mqtt, BUTTON_FEED); 76 | 77 | /*************************** Sketch Code ************************************/ 78 | 79 | // button state 80 | int current = 0; 81 | int last = -1; 82 | 83 | void setup() { 84 | 85 | // set button pin as an input 86 | pinMode(BUTTON, INPUT_PULLUP); 87 | 88 | Serial.begin(115200); 89 | 90 | Serial.println(F("Adafruit IO Example:")); 91 | Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC); 92 | 93 | // Initialise the CC3000 module 94 | Serial.print(F("\nInit the CC3000...")); 95 | if (! cc3000.begin()) 96 | halt("Failed"); 97 | 98 | // attempt wifi connection 99 | while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { 100 | Serial.println(F("Retrying WiFi")); 101 | while(1); 102 | } 103 | Serial.println(F("Connected to WiFi!")); 104 | 105 | // connect to adafruit io 106 | connect(); 107 | 108 | } 109 | 110 | void loop() { 111 | 112 | // Make sure to reset watchdog every loop iteration! 113 | Watchdog.reset(); 114 | 115 | // ping adafruit io a few times to make sure we remain connected 116 | if(! mqtt.ping(3)) { 117 | // reconnect to adafruit io 118 | if(! mqtt.connected()) 119 | connect(); 120 | } 121 | 122 | // grab the current state of the button 123 | current = digitalRead(BUTTON); 124 | 125 | // return if the value hasn't changed 126 | if(current == last) 127 | return; 128 | 129 | int32_t value = (current == LOW ? 1 : 0); 130 | 131 | // Now we can publish stuff! 132 | Serial.print(F("\nSending button value: ")); 133 | Serial.print(value); 134 | Serial.print("... "); 135 | 136 | if (! button.publish(value)) 137 | Serial.println(F("Failed.")); 138 | else 139 | Serial.println(F("Success!")); 140 | 141 | // save the button state 142 | last = current; 143 | 144 | } 145 | 146 | // connect to adafruit io via MQTT 147 | void connect() { 148 | 149 | Serial.print(F("Connecting to Adafruit IO... ")); 150 | 151 | int8_t ret; 152 | 153 | while ((ret = mqtt.connect()) != 0) { 154 | 155 | switch (ret) { 156 | case 1: Serial.println(F("Wrong protocol")); break; 157 | case 2: Serial.println(F("ID rejected")); break; 158 | case 3: Serial.println(F("Server unavail")); break; 159 | case 4: Serial.println(F("Bad user/pass")); break; 160 | case 5: Serial.println(F("Not authed")); break; 161 | case 6: Serial.println(F("Failed to subscribe")); break; 162 | default: 163 | Serial.println(F("Connection failed")); 164 | CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); 165 | break; 166 | } 167 | 168 | if(ret >= 0) 169 | mqtt.disconnect(); 170 | 171 | Serial.println(F("Retrying connection...")); 172 | delay(5000); 173 | 174 | } 175 | 176 | Serial.println(F("Adafruit IO Connected!")); 177 | 178 | } 179 | -------------------------------------------------------------------------------- /arduino_cc3000/digital_out/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/digital_out/.due.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/digital_out/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/digital_out/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/digital_out/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/digital_out/.leonardo.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/digital_out/cc3000helper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //#define STATICIP 7 | 8 | #define halt(s) { Serial.println(F( s )); while(1); } 9 | 10 | uint16_t checkFirmwareVersion(void); 11 | bool displayConnectionDetails(void); 12 | 13 | extern Adafruit_CC3000 cc3000; 14 | 15 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security) { 16 | Watchdog.reset(); 17 | 18 | // Check for compatible firmware 19 | if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!"); 20 | 21 | // Delete any old connection data on the module 22 | Serial.println(F("\nDeleting old connection profiles")); 23 | if (!cc3000.deleteProfiles()) halt("Failed!"); 24 | 25 | #ifdef STATICIP 26 | Serial.println(F("Setting static IP")); 27 | uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19); 28 | uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0); 29 | uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1); 30 | uint32_t dns = cc3000.IP2U32(8, 8, 4, 4); 31 | 32 | if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) { 33 | Serial.println(F("Failed to set static IP!")); 34 | while(1); 35 | } 36 | #endif 37 | 38 | // Attempt to connect to an access point 39 | Serial.print(F("\nAttempting to connect to ")); 40 | Serial.print(wlan_ssid); Serial.print(F("...")); 41 | 42 | Watchdog.disable(); 43 | // try 3 times 44 | if (!cc3000.connectToAP(wlan_ssid, wlan_pass, wlan_security, 3)) { 45 | return false; 46 | } 47 | 48 | Watchdog.enable(8000); 49 | Serial.println(F("Connected!")); 50 | 51 | uint8_t retries; 52 | #ifndef STATICIP 53 | /* Wait for DHCP to complete */ 54 | Serial.println(F("Requesting DHCP")); 55 | retries = 10; 56 | while (!cc3000.checkDHCP()) 57 | { 58 | Watchdog.reset(); 59 | delay(1000); 60 | retries--; 61 | if (!retries) return false; 62 | } 63 | #endif 64 | /* Display the IP address DNS, Gateway, etc. */ 65 | retries = 10; 66 | while (! displayConnectionDetails()) { 67 | Watchdog.reset(); 68 | delay(1000); 69 | retries--; 70 | if (!retries) return false; 71 | } 72 | 73 | Watchdog.reset(); 74 | 75 | return true; 76 | } 77 | 78 | 79 | /**************************************************************************/ 80 | /*! 81 | @brief Tries to read the CC3000's internal firmware patch ID 82 | */ 83 | /**************************************************************************/ 84 | uint16_t checkFirmwareVersion(void) 85 | { 86 | uint8_t major, minor; 87 | uint16_t version; 88 | 89 | if(!cc3000.getFirmwareVersion(&major, &minor)) 90 | { 91 | Serial.println(F("Unable to retrieve the firmware version!\r\n")); 92 | version = 0; 93 | } 94 | else 95 | { 96 | Serial.print(F("Firmware V. : ")); 97 | Serial.print(major); Serial.print(F(".")); Serial.println(minor); 98 | version = major; version <<= 8; version |= minor; 99 | } 100 | return version; 101 | } 102 | 103 | 104 | /**************************************************************************/ 105 | /*! 106 | @brief Tries to read the IP address and other connection details 107 | */ 108 | /**************************************************************************/ 109 | bool displayConnectionDetails(void) 110 | { 111 | uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; 112 | 113 | if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) 114 | { 115 | Serial.println(F("Unable to retrieve the IP Address!\r\n")); 116 | return false; 117 | } 118 | else 119 | { 120 | Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); 121 | Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); 122 | Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); 123 | Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); 124 | Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); 125 | Serial.println(); 126 | return true; 127 | } 128 | } 129 | 130 | -------------------------------------------------------------------------------- /arduino_cc3000/digital_out/digital_out.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library CC3000 Example 3 | 4 | Designed specifically to work with the Adafruit WiFi products: 5 | ----> https://www.adafruit.com/products/1469 6 | 7 | Adafruit invests time and resources providing this open source code, 8 | please support Adafruit and open-source hardware by purchasing 9 | products from Adafruit! 10 | 11 | Written by Limor Fried/Ladyada for Adafruit Industries. 12 | Adafruit IO example additions by Todd Treece. 13 | MIT license, all text above must be included in any redistribution 14 | ****************************************************/ 15 | #include 16 | #include 17 | #include 18 | #include "utility/debug.h" 19 | #include "Adafruit_MQTT.h" 20 | #include "Adafruit_MQTT_CC3000.h" 21 | 22 | /****************************** Pins ******************************************/ 23 | 24 | #define LAMP 8 // power switch tail pin 25 | #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! 26 | #define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins. 27 | #define ADAFRUIT_CC3000_CS 10 28 | // Use hardware SPI for the remaining pins 29 | // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 30 | 31 | /************************* WiFi Access Point *********************************/ 32 | 33 | #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters! 34 | #define WLAN_PASS "...your password..." 35 | #define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP, 36 | // WLAN_SEC_WPA or WLAN_SEC_WPA2 37 | 38 | /************************* Adafruit.io Setup *********************************/ 39 | 40 | #define AIO_SERVER "io.adafruit.com" 41 | #define AIO_SERVERPORT 1883 42 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 43 | #define AIO_KEY "...your AIO key..." 44 | 45 | /************ Global State (you don't need to change this!) ******************/ 46 | 47 | // Setup the main CC3000 class, just like a normal CC3000 sketch. 48 | Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT); 49 | 50 | // Store the MQTT server, client ID, username, and password in flash memory. 51 | // This is required for using the Adafruit MQTT library. 52 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 53 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 54 | // was compiled (so this should be unique across multiple devices for a user, 55 | // alternatively you can manually set this to a GUID or other random value). 56 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 57 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 58 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 59 | 60 | // Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details. 61 | Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 62 | 63 | // You don't need to change anything below this line! 64 | #define halt(s) { Serial.println(F( s )); while(1); } 65 | 66 | // CC3000connect is a helper function that sets up the CC3000 and connects to 67 | // the WiFi network. See the cc3000helper.cpp tab above for the source! 68 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security); 69 | 70 | /****************************** Feeds ***************************************/ 71 | 72 | // Setup a feed called 'lamp' for subscribing to changes. 73 | // Notice MQTT paths for AIO follow the form: /feeds/ 74 | const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp"; 75 | Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, LAMP_FEED); 76 | 77 | /*************************** Sketch Code ************************************/ 78 | 79 | void setup() { 80 | 81 | // set power switch tail pin as an output 82 | pinMode(LAMP, OUTPUT); 83 | 84 | Serial.begin(115200); 85 | 86 | Serial.println(F("Adafruit IO Example:")); 87 | Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC); 88 | 89 | // Initialise the CC3000 module 90 | Serial.print(F("\nInit the CC3000...")); 91 | if (! cc3000.begin()) 92 | halt("Failed"); 93 | 94 | // attempt wifi connection 95 | while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { 96 | Serial.println(F("Retrying WiFi")); 97 | while(1); 98 | } 99 | Serial.println(F("Connected to WiFi!")); 100 | 101 | // listen for events on the lamp feed 102 | mqtt.subscribe(&lamp); 103 | 104 | // connect to adafruit io 105 | connect(); 106 | 107 | } 108 | 109 | void loop() { 110 | 111 | Adafruit_MQTT_Subscribe *subscription; 112 | 113 | // Make sure to reset watchdog every loop iteration! 114 | Watchdog.reset(); 115 | 116 | // ping adafruit io a few times to make sure we remain connected 117 | if(! mqtt.ping(3)) { 118 | // reconnect to adafruit io 119 | if(! mqtt.connected()) 120 | connect(); 121 | } 122 | 123 | // this is our 'wait for incoming subscription packets' busy subloop 124 | while (subscription = mqtt.readSubscription(1000)) { 125 | 126 | // we only care about the lamp events 127 | if (subscription == &lamp) { 128 | 129 | // convert mqtt ascii payload to int 130 | char *value = (char *)lamp.lastread; 131 | Serial.print(F("Received: ")); 132 | Serial.println(value); 133 | int current = atoi(value); 134 | 135 | // write the current state to the power switch tail 136 | digitalWrite(LAMP, current == 1 ? HIGH : LOW); 137 | 138 | } 139 | 140 | } 141 | 142 | } 143 | 144 | // connect to adafruit io via MQTT 145 | void connect() { 146 | 147 | Serial.print(F("Connecting to Adafruit IO... ")); 148 | 149 | int8_t ret; 150 | 151 | while ((ret = mqtt.connect()) != 0) { 152 | 153 | switch (ret) { 154 | case 1: Serial.println(F("Wrong protocol")); break; 155 | case 2: Serial.println(F("ID rejected")); break; 156 | case 3: Serial.println(F("Server unavail")); break; 157 | case 4: Serial.println(F("Bad user/pass")); break; 158 | case 5: Serial.println(F("Not authed")); break; 159 | case 6: Serial.println(F("Failed to subscribe")); break; 160 | default: 161 | Serial.println(F("Connection failed")); 162 | CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); 163 | break; 164 | } 165 | 166 | if(ret >= 0) 167 | mqtt.disconnect(); 168 | 169 | Serial.println(F("Retrying connection...")); 170 | delay(5000); 171 | 172 | } 173 | 174 | Serial.println(F("Adafruit IO Connected!")); 175 | 176 | } 177 | 178 | -------------------------------------------------------------------------------- /arduino_cc3000/group_sensor/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/group_sensor/.due.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/group_sensor/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/group_sensor/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/group_sensor/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_cc3000/group_sensor/.leonardo.test.skip -------------------------------------------------------------------------------- /arduino_cc3000/group_sensor/cc3000helper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | //#define STATICIP 7 | 8 | #define halt(s) { Serial.println(F( s )); while(1); } 9 | 10 | uint16_t checkFirmwareVersion(void); 11 | bool displayConnectionDetails(void); 12 | 13 | extern Adafruit_CC3000 cc3000; 14 | 15 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security) { 16 | Watchdog.reset(); 17 | 18 | // Check for compatible firmware 19 | if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!"); 20 | 21 | // Delete any old connection data on the module 22 | Serial.println(F("\nDeleting old connection profiles")); 23 | if (!cc3000.deleteProfiles()) halt("Failed!"); 24 | 25 | #ifdef STATICIP 26 | Serial.println(F("Setting static IP")); 27 | uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19); 28 | uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0); 29 | uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1); 30 | uint32_t dns = cc3000.IP2U32(8, 8, 4, 4); 31 | 32 | if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) { 33 | Serial.println(F("Failed to set static IP!")); 34 | while(1); 35 | } 36 | #endif 37 | 38 | // Attempt to connect to an access point 39 | Serial.print(F("\nAttempting to connect to ")); 40 | Serial.print(wlan_ssid); Serial.print(F("...")); 41 | 42 | Watchdog.disable(); 43 | // try 3 times 44 | if (!cc3000.connectToAP(wlan_ssid, wlan_pass, wlan_security, 3)) { 45 | return false; 46 | } 47 | 48 | Watchdog.enable(8000); 49 | Serial.println(F("Connected!")); 50 | 51 | uint8_t retries; 52 | #ifndef STATICIP 53 | /* Wait for DHCP to complete */ 54 | Serial.println(F("Requesting DHCP")); 55 | retries = 10; 56 | while (!cc3000.checkDHCP()) 57 | { 58 | Watchdog.reset(); 59 | delay(1000); 60 | retries--; 61 | if (!retries) return false; 62 | } 63 | #endif 64 | /* Display the IP address DNS, Gateway, etc. */ 65 | retries = 10; 66 | while (! displayConnectionDetails()) { 67 | Watchdog.reset(); 68 | delay(1000); 69 | retries--; 70 | if (!retries) return false; 71 | } 72 | 73 | Watchdog.reset(); 74 | 75 | return true; 76 | } 77 | 78 | 79 | /**************************************************************************/ 80 | /*! 81 | @brief Tries to read the CC3000's internal firmware patch ID 82 | */ 83 | /**************************************************************************/ 84 | uint16_t checkFirmwareVersion(void) 85 | { 86 | uint8_t major, minor; 87 | uint16_t version; 88 | 89 | if(!cc3000.getFirmwareVersion(&major, &minor)) 90 | { 91 | Serial.println(F("Unable to retrieve the firmware version!\r\n")); 92 | version = 0; 93 | } 94 | else 95 | { 96 | Serial.print(F("Firmware V. : ")); 97 | Serial.print(major); Serial.print(F(".")); Serial.println(minor); 98 | version = major; version <<= 8; version |= minor; 99 | } 100 | return version; 101 | } 102 | 103 | 104 | /**************************************************************************/ 105 | /*! 106 | @brief Tries to read the IP address and other connection details 107 | */ 108 | /**************************************************************************/ 109 | bool displayConnectionDetails(void) 110 | { 111 | uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; 112 | 113 | if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) 114 | { 115 | Serial.println(F("Unable to retrieve the IP Address!\r\n")); 116 | return false; 117 | } 118 | else 119 | { 120 | Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); 121 | Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); 122 | Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); 123 | Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); 124 | Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); 125 | Serial.println(); 126 | return true; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /arduino_cc3000/group_sensor/group_sensor.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library CC3000 Example 3 | 4 | Designed specifically to work with the Adafruit WiFi products: 5 | ----> https://www.adafruit.com/products/1469 6 | 7 | Adafruit invests time and resources providing this open source code, 8 | please support Adafruit and open-source hardware by purchasing 9 | products from Adafruit! 10 | 11 | Written by Limor Fried/Ladyada for Adafruit Industries. 12 | Adafruit IO example additions by Todd Treece. 13 | MIT license, all text above must be included in any redistribution 14 | ****************************************************/ 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include "utility/debug.h" 20 | #include "Adafruit_MQTT.h" 21 | #include "Adafruit_MQTT_CC3000.h" 22 | #include 23 | 24 | /****************************** Pins ******************************************/ 25 | 26 | #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! 27 | #define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins. 28 | #define ADAFRUIT_CC3000_CS 10 29 | // Use hardware SPI for the remaining pins 30 | // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 31 | 32 | /************************* WiFi Access Point *********************************/ 33 | 34 | #define WLAN_SSID "...your SSID..." // can't be longer than 32 characters! 35 | #define WLAN_PASS "...your password..." 36 | #define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP, 37 | // WLAN_SEC_WPA or WLAN_SEC_WPA2 38 | 39 | /************************* Adafruit.io Setup *********************************/ 40 | 41 | #define AIO_SERVER "io.adafruit.com" 42 | #define AIO_SERVERPORT 1883 43 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 44 | #define AIO_KEY "...your AIO key..." 45 | 46 | /************ Global State (you don't need to change this!) ******************/ 47 | 48 | // Setup the main CC3000 class, just like a normal CC3000 sketch. 49 | Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT); 50 | 51 | // Store the MQTT server, client ID, username, and password in flash memory. 52 | // This is required for using the Adafruit MQTT library. 53 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 54 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 55 | // was compiled (so this should be unique across multiple devices for a user, 56 | // alternatively you can manually set this to a GUID or other random value). 57 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 58 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 59 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 60 | 61 | // Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details. 62 | Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 63 | 64 | // You don't need to change anything below this line! 65 | #define halt(s) { Serial.println(F( s )); while(1); } 66 | 67 | // CC3000connect is a helper function that sets up the CC3000 and connects to 68 | // the WiFi network. See the cc3000helper.cpp tab above for the source! 69 | boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security); 70 | 71 | /****************************** Feeds ***************************************/ 72 | 73 | // Setup a group called 'weather' for publishing changes. 74 | // Notice MQTT group CSV paths for AIO follow the form: /groups//csv 75 | const char WEATHER_FEED[] PROGMEM = AIO_USERNAME "/groups/weather/csv"; 76 | Adafruit_MQTT_Publish weather = Adafruit_MQTT_Publish(&mqtt, WEATHER_FEED); 77 | 78 | /*************************** Sketch Code ************************************/ 79 | 80 | Adafruit_AM2315 am2315; 81 | 82 | void setup() { 83 | 84 | Serial.begin(115200); 85 | 86 | Serial.println(F("Adafruit IO Example:")); 87 | Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC); 88 | 89 | // Initialise the CC3000 module 90 | Serial.print(F("\nInit the CC3000...")); 91 | if (! cc3000.begin()) 92 | halt("Failed"); 93 | 94 | // attempt wifi connection 95 | while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { 96 | Serial.println(F("Retrying WiFi")); 97 | while(1); 98 | } 99 | Serial.println(F("Connected to WiFi!")); 100 | 101 | if (! am2315.begin()) { 102 | Serial.println(F("AM2315 not found, check wiring & pullups!")); 103 | while (1); 104 | } 105 | 106 | // connect to adafruit io 107 | connect(); 108 | 109 | } 110 | 111 | void loop() { 112 | 113 | // Make sure to reset watchdog every loop iteration! 114 | Watchdog.reset(); 115 | 116 | // ping adafruit io a few times to make sure we remain connected 117 | if(! mqtt.ping(3)) { 118 | // reconnect to adafruit io 119 | if(! mqtt.connected()) 120 | connect(); 121 | } 122 | 123 | char sendbuffer[80]; 124 | char numberbuffer[20]; 125 | float temp = (am2315.readTemperature() * 1.8) + 32; 126 | float humidity = am2315.readHumidity(); 127 | 128 | // add temp feed name 129 | strcpy(sendbuffer, "temp,"); 130 | 131 | // add temp value 132 | dtostrf(temp, 2, 6, numberbuffer); 133 | strcat(sendbuffer, numberbuffer); 134 | 135 | // add new line and humidity feed name 136 | strcat(sendbuffer, "\nhumidity,"); 137 | 138 | // Now we can publish stuff! 139 | Serial.println(F("\nSending: ")); 140 | Serial.println(sendbuffer); 141 | 142 | if (! weather.publish(sendbuffer)) 143 | Serial.println(F("Failed.")); 144 | else 145 | Serial.println(F("Success!")); 146 | 147 | } 148 | 149 | // connect to adafruit io via MQTT 150 | void connect() { 151 | 152 | Serial.print(F("Connecting to Adafruit IO... ")); 153 | 154 | int8_t ret; 155 | 156 | while ((ret = mqtt.connect()) != 0) { 157 | 158 | switch (ret) { 159 | case 1: Serial.println(F("Wrong protocol")); break; 160 | case 2: Serial.println(F("ID rejected")); break; 161 | case 3: Serial.println(F("Server unavail")); break; 162 | case 4: Serial.println(F("Bad user/pass")); break; 163 | case 5: Serial.println(F("Not authed")); break; 164 | case 6: Serial.println(F("Failed to subscribe")); break; 165 | default: 166 | Serial.println(F("Connection failed")); 167 | CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); 168 | break; 169 | } 170 | 171 | if(ret >= 0) 172 | mqtt.disconnect(); 173 | 174 | Serial.println(F("Retrying connection...")); 175 | delay(5000); 176 | 177 | } 178 | 179 | Serial.println(F("Adafruit IO Connected!")); 180 | 181 | } 182 | -------------------------------------------------------------------------------- /arduino_fona/analog_in/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/analog_in/.due.test.skip -------------------------------------------------------------------------------- /arduino_fona/analog_in/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/analog_in/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_fona/analog_in/analog_in.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library FONA Example 3 | Designed specifically to work with the Adafruit FONA 4 | ----> http://www.adafruit.com/products/1946 5 | ----> http://www.adafruit.com/products/1963 6 | ----> http://www.adafruit.com/products/2468 7 | ----> http://www.adafruit.com/products/2542 8 | 9 | These cellular modules use TTL Serial to communicate, 2 pins are 10 | required to interface. 11 | 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit and open-source hardware by purchasing 14 | products from Adafruit! 15 | 16 | Written by Limor Fried/Ladyada for Adafruit Industries. 17 | Adafruit IO example additions by Todd Treece. 18 | MIT license, all text above must be included in any redistribution 19 | ****************************************************/ 20 | #include 21 | #include 22 | #include "Adafruit_FONA.h" 23 | #include "Adafruit_MQTT.h" 24 | #include "Adafruit_MQTT_FONA.h" 25 | 26 | /****************************** Pins ****************************************/ 27 | #define PHOTOCELL A0 28 | #define FONA_RX 2 29 | #define FONA_TX 3 30 | #define FONA_RST 4 31 | SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); 32 | 33 | Adafruit_FONA fona = Adafruit_FONA(FONA_RST); 34 | 35 | /*************************** Cellular APN *************************************/ 36 | 37 | // Optionally configure a GPRS APN, username, and password. 38 | // You might need to do this to access your network's GPRS/data 39 | // network. Contact your provider for the exact APN, username, 40 | // and password values. Username and password are optional and 41 | // can be removed, but APN is required. 42 | #define FONA_APN "" 43 | #define FONA_USERNAME "" 44 | #define FONA_PASSWORD "" 45 | 46 | /************************* Adafruit.io Setup *********************************/ 47 | 48 | #define AIO_SERVER "io.adafruit.com" 49 | #define AIO_SERVERPORT 1883 50 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 51 | #define AIO_KEY "...your AIO key..." 52 | 53 | /************ Global State (you don't need to change this!) ******************/ 54 | 55 | // Store the MQTT server, client ID, username, and password in flash memory. 56 | // This is required for using the Adafruit MQTT library. 57 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 58 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 59 | // was compiled (so this should be unique across multiple devices for a user, 60 | // alternatively you can manually set this to a GUID or other random value). 61 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 62 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 63 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 64 | 65 | // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. 66 | Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 67 | 68 | // You don't need to change anything below this line! 69 | #define halt(s) { Serial.println(F( s )); while(1); } 70 | 71 | // FONAconnect is a helper function that sets up the FONA and connects to 72 | // the GPRS network. See the fonahelper.cpp tab above for the source! 73 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); 74 | 75 | /****************************** Feeds ***************************************/ 76 | 77 | // Setup a feed called 'photocell' for publishing changes. 78 | // Notice MQTT paths for AIO follow the form: /feeds/ 79 | const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell"; 80 | Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED); 81 | 82 | /*************************** Sketch Code ************************************/ 83 | 84 | // photocell state 85 | int current = 0; 86 | int last = -1; 87 | 88 | void setup() { 89 | 90 | Serial.begin(115200); 91 | 92 | Serial.println(F("Adafruit IO Example")); 93 | 94 | // Initialise the FONA module 95 | while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) 96 | halt("Retrying FONA"); 97 | 98 | Serial.println(F("Connected to Cellular!")); 99 | 100 | Watchdog.reset(); 101 | delay(3000); // wait a few seconds to stabilize connection 102 | Watchdog.reset(); 103 | 104 | // connect to adafruit io 105 | connect(); 106 | 107 | } 108 | 109 | void loop() { 110 | 111 | // Make sure to reset watchdog every loop iteration! 112 | Watchdog.reset(); 113 | 114 | // ping adafruit io a few times to make sure we remain connected 115 | if(! mqtt.ping(3)) { 116 | // reconnect to adafruit io 117 | if(! mqtt.connected()) 118 | connect(); 119 | } 120 | 121 | // grab the current state of the photocell 122 | current = analogRead(PHOTOCELL); 123 | 124 | // return if the value hasn't changed 125 | if(current == last) 126 | return; 127 | 128 | // Now we can publish stuff! 129 | Serial.print(F("\nSending photocell value: ")); 130 | Serial.print(current); 131 | Serial.print("... "); 132 | 133 | if (! photocell.publish((int32_t)current)) 134 | Serial.println(F("Failed.")); 135 | else 136 | Serial.println(F("Success!")); 137 | 138 | // save the photocell state 139 | last = current; 140 | 141 | } 142 | 143 | // connect to adafruit io via MQTT 144 | void connect() { 145 | 146 | Serial.print(F("Connecting to Adafruit IO... ")); 147 | 148 | int8_t ret, retries = 5; 149 | 150 | while ((ret = mqtt.connect()) != 0) { 151 | 152 | switch (ret) { 153 | case 1: Serial.println(F("Wrong protocol")); break; 154 | case 2: Serial.println(F("ID rejected")); break; 155 | case 3: Serial.println(F("Server unavail")); break; 156 | case 4: Serial.println(F("Bad user/pass")); break; 157 | case 5: Serial.println(F("Not authed")); break; 158 | case 6: Serial.println(F("Failed to subscribe")); break; 159 | default: Serial.println(F("Connection failed")); break; 160 | } 161 | 162 | if(ret >= 0) 163 | mqtt.disconnect(); 164 | 165 | retries--; 166 | 167 | if (retries == 0) 168 | halt("Resetting system"); 169 | 170 | Serial.println(F("Retrying connection...")); 171 | delay(5000); 172 | 173 | } 174 | 175 | Serial.println(F("Adafruit IO Connected!")); 176 | 177 | } 178 | -------------------------------------------------------------------------------- /arduino_fona/analog_in/fonahelper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Adafruit_FONA.h" 3 | 4 | #define halt(s) { Serial.println(F( s )); while(1); } 5 | 6 | extern Adafruit_FONA fona; 7 | extern SoftwareSerial fonaSS; 8 | 9 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { 10 | 11 | Watchdog.enable(8000); 12 | Watchdog.reset(); 13 | 14 | Serial.println(F("Initializing FONA....(May take a few seconds)")); 15 | fonaSS.begin(4800); 16 | 17 | if (! fona.begin(fonaSS)) { 18 | Serial.println(F("Couldn't find FONA")); 19 | return false; 20 | } 21 | 22 | fonaSS.println("AT+CMEE=2"); 23 | Serial.println(F("FONA is OK")); 24 | 25 | Watchdog.reset(); 26 | Serial.println(F("Checking for network...")); 27 | 28 | while (fona.getNetworkStatus() != 1) 29 | delay(500); 30 | 31 | Watchdog.reset(); 32 | 33 | fona.setGPRSNetworkSettings(apn, username, password); 34 | 35 | Serial.println(F("Disabling GPRS")); 36 | fona.enableGPRS(false); 37 | 38 | Watchdog.reset(); 39 | Serial.println(F("Enabling GPRS")); 40 | if (! fona.enableGPRS(true)) { 41 | Serial.println(F("Failed to turn GPRS on")); 42 | return false; 43 | } 44 | Watchdog.reset(); 45 | 46 | return true; 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /arduino_fona/analog_out/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/analog_out/.due.test.skip -------------------------------------------------------------------------------- /arduino_fona/analog_out/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/analog_out/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_fona/analog_out/analog_out.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library FONA Example 3 | Designed specifically to work with the Adafruit FONA 4 | ----> http://www.adafruit.com/products/1946 5 | ----> http://www.adafruit.com/products/1963 6 | ----> http://www.adafruit.com/products/2468 7 | ----> http://www.adafruit.com/products/2542 8 | 9 | These cellular modules use TTL Serial to communicate, 2 pins are 10 | required to interface. 11 | 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit and open-source hardware by purchasing 14 | products from Adafruit! 15 | 16 | Written by Limor Fried/Ladyada for Adafruit Industries. 17 | Adafruit IO example additions by Todd Treece. 18 | MIT license, all text above must be included in any redistribution 19 | ****************************************************/ 20 | #include 21 | #include 22 | #include "Adafruit_FONA.h" 23 | #include "Adafruit_MQTT.h" 24 | #include "Adafruit_MQTT_FONA.h" 25 | 26 | /****************************** Pins ****************************************/ 27 | #define LED 9 // PWM pin 28 | #define FONA_RX 2 29 | #define FONA_TX 3 30 | #define FONA_RST 4 31 | SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); 32 | 33 | Adafruit_FONA fona = Adafruit_FONA(FONA_RST); 34 | 35 | /*************************** Cellular APN *************************************/ 36 | 37 | // Optionally configure a GPRS APN, username, and password. 38 | // You might need to do this to access your network's GPRS/data 39 | // network. Contact your provider for the exact APN, username, 40 | // and password values. Username and password are optional and 41 | // can be removed, but APN is required. 42 | #define FONA_APN "" 43 | #define FONA_USERNAME "" 44 | #define FONA_PASSWORD "" 45 | 46 | /************************* Adafruit.io Setup *********************************/ 47 | 48 | #define AIO_SERVER "io.adafruit.com" 49 | #define AIO_SERVERPORT 1883 50 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 51 | #define AIO_KEY "...your AIO key..." 52 | 53 | /************ Global State (you don't need to change this!) ******************/ 54 | 55 | // Store the MQTT server, client ID, username, and password in flash memory. 56 | // This is required for using the Adafruit MQTT library. 57 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 58 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 59 | // was compiled (so this should be unique across multiple devices for a user, 60 | // alternatively you can manually set this to a GUID or other random value). 61 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 62 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 63 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 64 | 65 | // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. 66 | Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 67 | 68 | // You don't need to change anything below this line! 69 | #define halt(s) { Serial.println(F( s )); while(1); } 70 | 71 | // FONAconnect is a helper function that sets up the FONA and connects to 72 | // the GPRS network. See the fonahelper.cpp tab above for the source! 73 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); 74 | 75 | /****************************** Feeds ***************************************/ 76 | 77 | // Setup a feed called 'photocell' for subscribing to changes. 78 | // Notice MQTT paths for AIO follow the form: /feeds/ 79 | const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell"; 80 | Adafruit_MQTT_Subscribe photocell = Adafruit_MQTT_Subscribe(&mqtt, PHOTOCELL_FEED); 81 | 82 | /*************************** Sketch Code ************************************/ 83 | 84 | void setup() { 85 | 86 | Serial.begin(115200); 87 | 88 | Serial.println(F("Adafruit IO Example:")); 89 | 90 | // Initialise the FONA module 91 | while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) 92 | halt("Retrying FONA"); 93 | 94 | Serial.println(F("Connected to Cellular!")); 95 | 96 | Watchdog.reset(); 97 | delay(3000); // wait a few seconds to stabilize connection 98 | Watchdog.reset(); 99 | 100 | // listen for events on the photocell feed 101 | mqtt.subscribe(&photocell); 102 | 103 | // connect to adafruit io 104 | connect(); 105 | 106 | } 107 | 108 | void loop() { 109 | 110 | Adafruit_MQTT_Subscribe *subscription; 111 | 112 | // Make sure to reset watchdog every loop iteration! 113 | Watchdog.reset(); 114 | 115 | // ping adafruit io a few times to make sure we remain connected 116 | if(! mqtt.ping(3)) { 117 | // reconnect to adafruit io 118 | if(! mqtt.connected()) 119 | connect(); 120 | } 121 | 122 | // this is our 'wait for incoming subscription packets' busy subloop 123 | while (subscription = mqtt.readSubscription(1000)) { 124 | 125 | // we only care about the photocell events 126 | if (subscription == &photocell) { 127 | 128 | // convert mqtt ascii payload to int 129 | char *value = (char *)photocell.lastread; 130 | Serial.print(F("Received: ")); 131 | Serial.println(value); 132 | int reading = atoi(value); 133 | 134 | // LED gets brighter the darker it is at the sensor 135 | // that means we have to -invert- the reading from 0-1023 back to 1023-0 136 | reading = 1023 - reading; 137 | 138 | //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses 139 | int brightness = map(reading, 0, 1023, 0, 255); 140 | analogWrite(LED, brightness); 141 | 142 | } 143 | 144 | } 145 | 146 | } 147 | 148 | // connect to adafruit io via MQTT 149 | void connect() { 150 | 151 | Serial.print(F("Connecting to Adafruit IO... ")); 152 | 153 | int8_t ret, retries = 5; 154 | 155 | while ((ret = mqtt.connect()) != 0) { 156 | 157 | switch (ret) { 158 | case 1: Serial.println(F("Wrong protocol")); break; 159 | case 2: Serial.println(F("ID rejected")); break; 160 | case 3: Serial.println(F("Server unavail")); break; 161 | case 4: Serial.println(F("Bad user/pass")); break; 162 | case 5: Serial.println(F("Not authed")); break; 163 | case 6: Serial.println(F("Failed to subscribe")); break; 164 | default: Serial.println(F("Connection failed")); break; 165 | } 166 | 167 | if(ret >= 0) 168 | mqtt.disconnect(); 169 | 170 | retries--; 171 | 172 | if (retries == 0) 173 | halt("Resetting system"); 174 | 175 | Serial.println(F("Retrying connection...")); 176 | delay(5000); 177 | 178 | } 179 | 180 | Serial.println(F("Adafruit IO Connected!")); 181 | 182 | } 183 | -------------------------------------------------------------------------------- /arduino_fona/analog_out/fonahelper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Adafruit_FONA.h" 3 | 4 | #define halt(s) { Serial.println(F( s )); while(1); } 5 | 6 | extern Adafruit_FONA fona; 7 | extern SoftwareSerial fonaSS; 8 | 9 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { 10 | 11 | Watchdog.enable(8000); 12 | Watchdog.reset(); 13 | 14 | Serial.println(F("Initializing FONA....(May take a few seconds)")); 15 | fonaSS.begin(4800); 16 | 17 | if (! fona.begin(fonaSS)) { 18 | Serial.println(F("Couldn't find FONA")); 19 | return false; 20 | } 21 | 22 | fonaSS.println("AT+CMEE=2"); 23 | Serial.println(F("FONA is OK")); 24 | 25 | Watchdog.reset(); 26 | Serial.println(F("Checking for network...")); 27 | 28 | while (fona.getNetworkStatus() != 1) 29 | delay(500); 30 | 31 | Watchdog.reset(); 32 | 33 | fona.setGPRSNetworkSettings(apn, username, password); 34 | 35 | Serial.println(F("Disabling GPRS")); 36 | fona.enableGPRS(false); 37 | 38 | Watchdog.reset(); 39 | Serial.println(F("Enabling GPRS")); 40 | if (! fona.enableGPRS(true)) { 41 | Serial.println(F("Failed to turn GPRS on")); 42 | return false; 43 | } 44 | Watchdog.reset(); 45 | 46 | return true; 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /arduino_fona/digital_in/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/digital_in/.due.test.skip -------------------------------------------------------------------------------- /arduino_fona/digital_in/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/digital_in/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_fona/digital_in/digital_in.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library FONA Example 3 | Designed specifically to work with the Adafruit FONA 4 | ----> http://www.adafruit.com/products/1946 5 | ----> http://www.adafruit.com/products/1963 6 | ----> http://www.adafruit.com/products/2468 7 | ----> http://www.adafruit.com/products/2542 8 | 9 | These cellular modules use TTL Serial to communicate, 2 pins are 10 | required to interface. 11 | 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit and open-source hardware by purchasing 14 | products from Adafruit! 15 | 16 | Written by Limor Fried/Ladyada for Adafruit Industries. 17 | Adafruit IO example additions by Todd Treece. 18 | MIT license, all text above must be included in any redistribution 19 | ****************************************************/ 20 | #include 21 | #include 22 | #include "Adafruit_FONA.h" 23 | #include "Adafruit_MQTT.h" 24 | #include "Adafruit_MQTT_FONA.h" 25 | 26 | /****************************** Pins ****************************************/ 27 | #define BUTTON 8 28 | #define FONA_RX 2 29 | #define FONA_TX 3 30 | #define FONA_RST 4 31 | SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); 32 | 33 | Adafruit_FONA fona = Adafruit_FONA(FONA_RST); 34 | 35 | /*************************** Cellular APN *************************************/ 36 | 37 | // Optionally configure a GPRS APN, username, and password. 38 | // You might need to do this to access your network's GPRS/data 39 | // network. Contact your provider for the exact APN, username, 40 | // and password values. Username and password are optional and 41 | // can be removed, but APN is required. 42 | #define FONA_APN "" 43 | #define FONA_USERNAME "" 44 | #define FONA_PASSWORD "" 45 | 46 | /************************* Adafruit.io Setup *********************************/ 47 | 48 | #define AIO_SERVER "io.adafruit.com" 49 | #define AIO_SERVERPORT 1883 50 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 51 | #define AIO_KEY "...your AIO key..." 52 | 53 | /************ Global State (you don't need to change this!) ******************/ 54 | 55 | // Store the MQTT server, client ID, username, and password in flash memory. 56 | // This is required for using the Adafruit MQTT library. 57 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 58 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 59 | // was compiled (so this should be unique across multiple devices for a user, 60 | // alternatively you can manually set this to a GUID or other random value). 61 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 62 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 63 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 64 | 65 | // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. 66 | Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 67 | 68 | // You don't need to change anything below this line! 69 | #define halt(s) { Serial.println(F( s )); while(1); } 70 | 71 | // FONAconnect is a helper function that sets up the FONA and connects to 72 | // the GPRS network. See the fonahelper.cpp tab above for the source! 73 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); 74 | 75 | /****************************** Feeds ***************************************/ 76 | 77 | // Setup a feed called 'button' for publishing changes. 78 | // Notice MQTT paths for AIO follow the form: /feeds/ 79 | const char BUTTON_FEED[] PROGMEM = AIO_USERNAME "/feeds/button"; 80 | Adafruit_MQTT_Publish button = Adafruit_MQTT_Publish(&mqtt, BUTTON_FEED); 81 | 82 | /*************************** Sketch Code ************************************/ 83 | 84 | // button state 85 | int current = 0; 86 | int last = -1; 87 | 88 | void setup() { 89 | 90 | // set button pin as an input 91 | pinMode(BUTTON, INPUT_PULLUP); 92 | 93 | Serial.begin(115200); 94 | 95 | Serial.println(F("Adafruit IO Example")); 96 | 97 | // Initialise the FONA module 98 | while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) 99 | halt("Retrying FONA"); 100 | 101 | Serial.println(F("Connected to Cellular!")); 102 | 103 | Watchdog.reset(); 104 | delay(3000); // wait a few seconds to stabilize connection 105 | Watchdog.reset(); 106 | 107 | // connect to adafruit io 108 | connect(); 109 | 110 | } 111 | 112 | void loop() { 113 | 114 | // Make sure to reset watchdog every loop iteration! 115 | Watchdog.reset(); 116 | 117 | // ping adafruit io a few times to make sure we remain connected 118 | if(! mqtt.ping(3)) { 119 | // reconnect to adafruit io 120 | if(! mqtt.connected()) 121 | connect(); 122 | } 123 | 124 | // grab the current state of the button 125 | current = digitalRead(BUTTON); 126 | 127 | // return if the value hasn't changed 128 | if(current == last) 129 | return; 130 | 131 | int32_t value = (current == LOW ? 1 : 0); 132 | 133 | // Now we can publish stuff! 134 | Serial.print(F("\nSending button value: ")); 135 | Serial.print(value); 136 | Serial.print("... "); 137 | 138 | if (! button.publish(value)) 139 | Serial.println(F("Failed.")); 140 | else 141 | Serial.println(F("Success!")); 142 | 143 | // save the button state 144 | last = current; 145 | 146 | } 147 | 148 | // connect to adafruit io via MQTT 149 | void connect() { 150 | 151 | Serial.print(F("Connecting to Adafruit IO... ")); 152 | 153 | int8_t ret, retries = 5; 154 | 155 | while ((ret = mqtt.connect()) != 0) { 156 | 157 | switch (ret) { 158 | case 1: Serial.println(F("Wrong protocol")); break; 159 | case 2: Serial.println(F("ID rejected")); break; 160 | case 3: Serial.println(F("Server unavail")); break; 161 | case 4: Serial.println(F("Bad user/pass")); break; 162 | case 5: Serial.println(F("Not authed")); break; 163 | case 6: Serial.println(F("Failed to subscribe")); break; 164 | default: Serial.println(F("Connection failed")); break; 165 | } 166 | 167 | if(ret >= 0) 168 | mqtt.disconnect(); 169 | 170 | retries--; 171 | 172 | if (retries == 0) 173 | halt("Resetting system"); 174 | 175 | Serial.println(F("Retrying connection...")); 176 | delay(5000); 177 | 178 | } 179 | 180 | Serial.println(F("Adafruit IO Connected!")); 181 | 182 | } 183 | -------------------------------------------------------------------------------- /arduino_fona/digital_in/fonahelper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Adafruit_FONA.h" 3 | 4 | #define halt(s) { Serial.println(F( s )); while(1); } 5 | 6 | extern Adafruit_FONA fona; 7 | extern SoftwareSerial fonaSS; 8 | 9 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { 10 | 11 | Watchdog.enable(8000); 12 | Watchdog.reset(); 13 | 14 | Serial.println(F("Initializing FONA....(May take a few seconds)")); 15 | fonaSS.begin(4800); 16 | 17 | if (! fona.begin(fonaSS)) { 18 | Serial.println(F("Couldn't find FONA")); 19 | return false; 20 | } 21 | 22 | fonaSS.println("AT+CMEE=2"); 23 | Serial.println(F("FONA is OK")); 24 | 25 | Watchdog.reset(); 26 | Serial.println(F("Checking for network...")); 27 | 28 | while (fona.getNetworkStatus() != 1) 29 | delay(500); 30 | 31 | Watchdog.reset(); 32 | 33 | fona.setGPRSNetworkSettings(apn, username, password); 34 | 35 | Serial.println(F("Disabling GPRS")); 36 | fona.enableGPRS(false); 37 | 38 | Watchdog.reset(); 39 | Serial.println(F("Enabling GPRS")); 40 | if (! fona.enableGPRS(true)) { 41 | Serial.println(F("Failed to turn GPRS on")); 42 | return false; 43 | } 44 | Watchdog.reset(); 45 | 46 | return true; 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /arduino_fona/digital_out/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/digital_out/.due.test.skip -------------------------------------------------------------------------------- /arduino_fona/digital_out/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/digital_out/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_fona/digital_out/digital_out.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library FONA Example 3 | Designed specifically to work with the Adafruit FONA 4 | ----> http://www.adafruit.com/products/1946 5 | ----> http://www.adafruit.com/products/1963 6 | ----> http://www.adafruit.com/products/2468 7 | ----> http://www.adafruit.com/products/2542 8 | 9 | These cellular modules use TTL Serial to communicate, 2 pins are 10 | required to interface. 11 | 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit and open-source hardware by purchasing 14 | products from Adafruit! 15 | 16 | Written by Limor Fried/Ladyada for Adafruit Industries. 17 | Adafruit IO example additions by Todd Treece. 18 | MIT license, all text above must be included in any redistribution 19 | ****************************************************/ 20 | #include 21 | #include 22 | #include "Adafruit_FONA.h" 23 | #include "Adafruit_MQTT.h" 24 | #include "Adafruit_MQTT_FONA.h" 25 | 26 | /****************************** Pins ****************************************/ 27 | #define LAMP 5 28 | #define FONA_RX 2 29 | #define FONA_TX 3 30 | #define FONA_RST 4 31 | SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); 32 | 33 | Adafruit_FONA fona = Adafruit_FONA(FONA_RST); 34 | 35 | /*************************** Cellular APN *************************************/ 36 | 37 | // Optionally configure a GPRS APN, username, and password. 38 | // You might need to do this to access your network's GPRS/data 39 | // network. Contact your provider for the exact APN, username, 40 | // and password values. Username and password are optional and 41 | // can be removed, but APN is required. 42 | #define FONA_APN "" 43 | #define FONA_USERNAME "" 44 | #define FONA_PASSWORD "" 45 | 46 | /************************* Adafruit.io Setup *********************************/ 47 | 48 | #define AIO_SERVER "io.adafruit.com" 49 | #define AIO_SERVERPORT 1883 50 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 51 | #define AIO_KEY "...your AIO key..." 52 | 53 | /************ Global State (you don't need to change this!) ******************/ 54 | 55 | // Store the MQTT server, client ID, username, and password in flash memory. 56 | // This is required for using the Adafruit MQTT library. 57 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 58 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 59 | // was compiled (so this should be unique across multiple devices for a user, 60 | // alternatively you can manually set this to a GUID or other random value). 61 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 62 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 63 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 64 | 65 | // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. 66 | Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 67 | 68 | // You don't need to change anything below this line! 69 | #define halt(s) { Serial.println(F( s )); while(1); } 70 | 71 | // FONAconnect is a helper function that sets up the FONA and connects to 72 | // the GPRS network. See the fonahelper.cpp tab above for the source! 73 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); 74 | 75 | /****************************** Feeds ***************************************/ 76 | 77 | // Setup a feed called 'lamp' for subscribing to changes. 78 | // Notice MQTT paths for AIO follow the form: /feeds/ 79 | const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp"; 80 | Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, LAMP_FEED); 81 | 82 | /*************************** Sketch Code ************************************/ 83 | 84 | void setup() { 85 | 86 | // set power switch tail pin as an output 87 | pinMode(LAMP, OUTPUT); 88 | 89 | Serial.begin(115200); 90 | 91 | Serial.println(F("Adafruit IO Example:")); 92 | 93 | // Initialise the FONA module 94 | while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) 95 | halt("Retrying FONA"); 96 | 97 | Serial.println(F("Connected to Cellular!")); 98 | 99 | Watchdog.reset(); 100 | delay(3000); // wait a few seconds to stabilize connection 101 | Watchdog.reset(); 102 | 103 | // listen for events on the lamp feed 104 | mqtt.subscribe(&lamp); 105 | 106 | // connect to adafruit io 107 | connect(); 108 | 109 | } 110 | 111 | void loop() { 112 | 113 | Adafruit_MQTT_Subscribe *subscription; 114 | 115 | // Make sure to reset watchdog every loop iteration! 116 | Watchdog.reset(); 117 | 118 | // ping adafruit io a few times to make sure we remain connected 119 | if(! mqtt.ping(3)) { 120 | // reconnect to adafruit io 121 | if(! mqtt.connected()) 122 | connect(); 123 | } 124 | 125 | // this is our 'wait for incoming subscription packets' busy subloop 126 | while (subscription = mqtt.readSubscription(1000)) { 127 | 128 | // we only care about the lamp events 129 | if (subscription == &lamp) { 130 | 131 | // convert mqtt ascii payload to int 132 | char *value = (char *)lamp.lastread; 133 | Serial.print(F("Received: ")); 134 | Serial.println(value); 135 | int current = atoi(value); 136 | 137 | // write the current state to the power switch tail 138 | digitalWrite(LAMP, current == 1 ? HIGH : LOW); 139 | 140 | } 141 | 142 | } 143 | 144 | } 145 | 146 | // connect to adafruit io via MQTT 147 | void connect() { 148 | 149 | Serial.print(F("Connecting to Adafruit IO... ")); 150 | 151 | int8_t ret, retries = 5; 152 | 153 | while ((ret = mqtt.connect()) != 0) { 154 | 155 | switch (ret) { 156 | case 1: Serial.println(F("Wrong protocol")); break; 157 | case 2: Serial.println(F("ID rejected")); break; 158 | case 3: Serial.println(F("Server unavail")); break; 159 | case 4: Serial.println(F("Bad user/pass")); break; 160 | case 5: Serial.println(F("Not authed")); break; 161 | case 6: Serial.println(F("Failed to subscribe")); break; 162 | default: Serial.println(F("Connection failed")); break; 163 | } 164 | 165 | if(ret >= 0) 166 | mqtt.disconnect(); 167 | 168 | retries--; 169 | 170 | if (retries == 0) 171 | halt("Resetting system"); 172 | 173 | Serial.println(F("Retrying connection...")); 174 | delay(5000); 175 | 176 | } 177 | 178 | Serial.println(F("Adafruit IO Connected!")); 179 | 180 | } 181 | -------------------------------------------------------------------------------- /arduino_fona/digital_out/fonahelper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Adafruit_FONA.h" 4 | 5 | #define halt(s) { Serial.println(F( s )); while(1); } 6 | 7 | extern Adafruit_FONA fona; 8 | extern SoftwareSerial fonaSS; 9 | 10 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { 11 | 12 | Watchdog.enable(8000); 13 | Watchdog.reset(); 14 | 15 | Serial.println(F("Initializing FONA....(May take a few seconds)")); 16 | fonaSS.begin(4800); 17 | 18 | if (! fona.begin(fonaSS)) { 19 | Serial.println(F("Couldn't find FONA")); 20 | return false; 21 | } 22 | 23 | fonaSS.println("AT+CMEE=2"); 24 | Serial.println(F("FONA is OK")); 25 | 26 | Watchdog.reset(); 27 | Serial.println(F("Checking for network...")); 28 | 29 | while (fona.getNetworkStatus() != 1) 30 | delay(500); 31 | 32 | Watchdog.reset(); 33 | 34 | fona.setGPRSNetworkSettings(apn, username, password); 35 | 36 | Serial.println(F("Disabling GPRS")); 37 | fona.enableGPRS(false); 38 | 39 | Watchdog.reset(); 40 | Serial.println(F("Enabling GPRS")); 41 | if (! fona.enableGPRS(true)) { 42 | Serial.println(F("Failed to turn GPRS on")); 43 | return false; 44 | } 45 | Watchdog.reset(); 46 | 47 | return true; 48 | 49 | } 50 | -------------------------------------------------------------------------------- /arduino_fona/gps/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/gps/.due.test.skip -------------------------------------------------------------------------------- /arduino_fona/gps/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/gps/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_fona/gps/fonahelper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Adafruit_FONA.h" 3 | 4 | #define halt(s) { Serial.println(F( s )); while(1); } 5 | 6 | extern Adafruit_FONA fona; 7 | extern SoftwareSerial fonaSS; 8 | 9 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { 10 | 11 | Watchdog.enable(8000); 12 | Watchdog.reset(); 13 | 14 | Serial.println(F("Initializing FONA....(May take a few seconds)")); 15 | fonaSS.begin(4800); 16 | 17 | if (! fona.begin(fonaSS)) { 18 | Serial.println(F("Couldn't find FONA")); 19 | return false; 20 | } 21 | 22 | fonaSS.println("AT+CMEE=2"); 23 | Serial.println(F("FONA is OK")); 24 | 25 | Watchdog.reset(); 26 | Serial.println(F("Checking for network...")); 27 | 28 | while (fona.getNetworkStatus() != 1) 29 | delay(500); 30 | 31 | Watchdog.reset(); 32 | 33 | fona.setGPRSNetworkSettings(apn, username, password); 34 | 35 | Serial.println(F("Disabling GPRS")); 36 | fona.enableGPRS(false); 37 | 38 | Watchdog.reset(); 39 | Serial.println(F("Enabling GPRS")); 40 | if (! fona.enableGPRS(true)) { 41 | Serial.println(F("Failed to turn GPRS on")); 42 | return false; 43 | } 44 | Watchdog.reset(); 45 | 46 | return true; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /arduino_fona/gps/gps.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit IO FONA 808 GPS DEMO 3 | Designed specifically to work with the Adafruit FONA 808 4 | ----> https://www.adafruit.com/products/2542 5 | 6 | These cellular modules use TTL Serial to communicate, 2 pins are 7 | required to interface. 8 | 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Todd Treece for Adafruit Industries. 14 | MIT license, all text above must be included in any redistribution 15 | ****************************************************/ 16 | #include 17 | #include 18 | #include "Adafruit_FONA.h" 19 | #include "Adafruit_MQTT.h" 20 | #include "Adafruit_MQTT_FONA.h" 21 | 22 | /*************************** FONA Pins ***********************************/ 23 | 24 | #define FONA_RX 2 25 | #define FONA_TX 3 26 | #define FONA_RST 4 27 | SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); 28 | 29 | Adafruit_FONA fona = Adafruit_FONA(FONA_RST); 30 | 31 | /*************************** Cellular APN *************************************/ 32 | 33 | // Optionally configure a GPRS APN, username, and password. 34 | // You might need to do this to access your network's GPRS/data 35 | // network. Contact your provider for the exact APN, username, 36 | // and password values. Username and password are optional and 37 | // can be removed, but APN is required. 38 | #define FONA_APN "" 39 | #define FONA_USERNAME "" 40 | #define FONA_PASSWORD "" 41 | 42 | /************************* Adafruit.io Setup *********************************/ 43 | 44 | #define AIO_SERVER "io.adafruit.com" 45 | #define AIO_SERVERPORT 1883 46 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 47 | #define AIO_KEY "...your AIO key..." 48 | 49 | /************ Global State (you don't need to change this!) ******************/ 50 | 51 | // Store the MQTT server, client ID, username, and password in flash memory. 52 | // This is required for using the Adafruit MQTT library. 53 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 54 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 55 | // was compiled (so this should be unique across multiple devices for a user, 56 | // alternatively you can manually set this to a GUID or other random value). 57 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 58 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 59 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 60 | 61 | // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. 62 | Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 63 | 64 | // You don't need to change anything below this line! 65 | #define halt(s) { Serial.println(F( s )); while(1); } 66 | 67 | // FONAconnect is a helper function that sets up the FONA and connects to 68 | // the GPRS network. See the fonahelper.cpp tab above for the source! 69 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); 70 | 71 | /****************************** Feeds ***************************************/ 72 | 73 | // Notice MQTT paths for AIO follow the form: /feeds//csv 74 | const char GSMLOC_FEED[] PROGMEM = AIO_USERNAME "/feeds/gsmloc/csv"; 75 | Adafruit_MQTT_Publish gsmloc = Adafruit_MQTT_Publish(&mqtt, GSMLOC_FEED); 76 | const char GPSLOC_FEED[] PROGMEM = AIO_USERNAME "/feeds/gps/csv"; 77 | Adafruit_MQTT_Publish gpsloc = Adafruit_MQTT_Publish(&mqtt, GPSLOC_FEED); 78 | 79 | char gpsbuffer[120]; 80 | 81 | /*************************** Sketch Code ************************************/ 82 | 83 | // How many transmission failures in a row we're willing to be ok with before reset 84 | uint8_t txfailures = 0; 85 | #define MAXTXFAILURES 3 86 | 87 | void setup() { 88 | 89 | while (!Serial); 90 | 91 | Serial.begin(115200); 92 | 93 | Serial.println(F("Adafruit FONA GPS MQTT demo")); 94 | 95 | if (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) 96 | halt("Retrying FONA"); 97 | 98 | Serial.println(F("Connected to Cellular!")); 99 | 100 | fona.enableGPS(true); 101 | 102 | Watchdog.reset(); 103 | delay(3000); // wait a few seconds to stabilize connection 104 | Watchdog.reset(); 105 | 106 | // connect to adafruit io 107 | connect(); 108 | 109 | } 110 | 111 | uint32_t x=0; 112 | 113 | void loop() { 114 | 115 | char sendbuffer[120]; 116 | 117 | // Make sure to reset watchdog every loop iteration! 118 | Watchdog.reset(); 119 | 120 | // ping adafruit io a few times to make sure we remain connected 121 | if(! mqtt.ping(3)) { 122 | // reconnect to adafruit io 123 | if(! mqtt.connected()) 124 | connect(); 125 | } 126 | 127 | float latitude, longitude, speed_kph, heading, speed_mph, altitude; 128 | 129 | // if you ask for an altitude reading, getGPS will return false if there isn't a 3D fix 130 | boolean gps_success = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude); 131 | 132 | if (gps_success) { 133 | 134 | Serial.print("GPS lat:"); 135 | Serial.println(latitude); 136 | Serial.print("GPS long:"); 137 | Serial.println(longitude); 138 | Serial.print("GPS speed KPH:"); 139 | Serial.println(speed_kph); 140 | Serial.print("GPS speed MPH:"); 141 | speed_mph = speed_kph * 0.621371192; 142 | Serial.println(speed_mph); 143 | Serial.print("GPS heading:"); 144 | Serial.println(heading); 145 | Serial.print("GPS altitude:"); 146 | Serial.println(altitude); 147 | 148 | // snprintf(sendbuffer, 120, "%d,%f,%f,0", x, latitude, longitude); 149 | // but that doesnt work in arduino 150 | char *p = sendbuffer; 151 | // add speed value 152 | dtostrf(speed_mph, 2, 6, p); 153 | p += strlen(p); 154 | p[0] = ','; p++; 155 | 156 | // concat latitude 157 | dtostrf(latitude, 2, 6, p); 158 | p += strlen(p); 159 | p[0] = ','; p++; 160 | 161 | // concat longitude 162 | dtostrf(longitude, 3, 6, p); 163 | p += strlen(p); 164 | p[0] = ','; p++; 165 | 166 | // concat altitude 167 | dtostrf(altitude, 2, 6, p); 168 | p += strlen(p); 169 | 170 | // null terminate 171 | p[0] = 0; 172 | 173 | Serial.print("Sending: "); Serial.println(sendbuffer); 174 | 175 | if (! gpsloc.publish(sendbuffer)) { 176 | Serial.println(F("Failed")); 177 | txfailures++; 178 | } else { 179 | Serial.println(F("OK!")); 180 | txfailures = 0; 181 | } 182 | 183 | Watchdog.reset(); 184 | 185 | } 186 | 187 | boolean gsmloc_success = fona.getGSMLoc(&latitude, &longitude); 188 | 189 | if (gsmloc_success) { 190 | 191 | Serial.print("GSMLoc lat:"); 192 | Serial.println(latitude); 193 | Serial.print("GSMLoc long:"); 194 | Serial.println(longitude); 195 | 196 | // snprintf(sendbuffer, 120, "%d,%f,%f,0", x, latitude, longitude); 197 | // but that doesnt work in arduino 198 | char *p; 199 | 200 | // paste in 'value' first, just an incrementer for GSMLoc 201 | itoa(x, sendbuffer, 10); 202 | p = sendbuffer+strlen(sendbuffer); 203 | p[0] = ','; p++; 204 | 205 | // concat latitude 206 | dtostrf(latitude, 2, 6, p); 207 | p += strlen(p); 208 | p[0] = ','; p++; 209 | 210 | // concat longitude 211 | dtostrf(longitude, 3, 6, p); 212 | p += strlen(p); 213 | p[0] = ','; p++; 214 | p[0] = '0'; p++; // 0 altitude 215 | // null terminate 216 | p[0] = 0; 217 | 218 | Serial.print("Sending: "); Serial.println(sendbuffer); 219 | if (! gsmloc.publish(sendbuffer)) { 220 | Serial.println(F("Failed")); 221 | txfailures++; 222 | } else { 223 | Serial.println(F("OK!")); 224 | txfailures = 0; 225 | } 226 | 227 | } 228 | 229 | x++; 230 | 231 | // wait a couple seconds before starting over 232 | Watchdog.reset(); 233 | delay(2000); 234 | Watchdog.reset(); 235 | 236 | } 237 | 238 | // connect to adafruit io via MQTT 239 | void connect() { 240 | 241 | // check if we're still connected 242 | // and make sure there aren't a bunch 243 | // of send failures 244 | if(fona.TCPconnected() && txfailures < MAXTXFAILURES) 245 | return; 246 | 247 | Serial.println(F("Connecting to MQTT...")); 248 | 249 | int8_t ret, retries = 5; 250 | 251 | while (retries && (ret = mqtt.connect()) != 0) { 252 | switch (ret) { 253 | case 1: Serial.println(F("Wrong protocol")); break; 254 | case 2: Serial.println(F("ID rejected")); break; 255 | case 3: Serial.println(F("Server unavail")); break; 256 | case 4: Serial.println(F("Bad user/pass")); break; 257 | case 5: Serial.println(F("Not authed")); break; 258 | case 6: Serial.println(F("Failed to subscribe")); break; 259 | default: Serial.println(F("Connection failed")); break; 260 | } 261 | 262 | Serial.println(F("Retrying MQTT connection")); 263 | retries--; 264 | if (retries == 0) halt("Resetting system"); 265 | delay(5000); 266 | 267 | } 268 | 269 | Serial.println(F("MQTT Connected!")); 270 | txfailures = 0; 271 | 272 | } 273 | 274 | -------------------------------------------------------------------------------- /arduino_fona/group_sensor/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/group_sensor/.due.test.skip -------------------------------------------------------------------------------- /arduino_fona/group_sensor/.esp8266.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/arduino_fona/group_sensor/.esp8266.test.skip -------------------------------------------------------------------------------- /arduino_fona/group_sensor/fonahelper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Adafruit_FONA.h" 3 | 4 | #define halt(s) { Serial.println(F( s )); while(1); } 5 | 6 | extern Adafruit_FONA fona; 7 | extern SoftwareSerial fonaSS; 8 | 9 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { 10 | 11 | Watchdog.enable(8000); 12 | Watchdog.reset(); 13 | 14 | Serial.println(F("Initializing FONA....(May take a few seconds)")); 15 | fonaSS.begin(4800); 16 | 17 | if (! fona.begin(fonaSS)) { 18 | Serial.println(F("Couldn't find FONA")); 19 | return false; 20 | } 21 | 22 | fonaSS.println("AT+CMEE=2"); 23 | Serial.println(F("FONA is OK")); 24 | 25 | Watchdog.reset(); 26 | Serial.println(F("Checking for network...")); 27 | 28 | while (fona.getNetworkStatus() != 1) 29 | delay(500); 30 | 31 | Watchdog.reset(); 32 | 33 | fona.setGPRSNetworkSettings(apn, username, password); 34 | 35 | Serial.println(F("Disabling GPRS")); 36 | fona.enableGPRS(false); 37 | 38 | Watchdog.reset(); 39 | Serial.println(F("Enabling GPRS")); 40 | if (! fona.enableGPRS(true)) { 41 | Serial.println(F("Failed to turn GPRS on")); 42 | return false; 43 | } 44 | Watchdog.reset(); 45 | 46 | return true; 47 | 48 | } 49 | 50 | -------------------------------------------------------------------------------- /arduino_fona/group_sensor/group_sensor.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library FONA Example 3 | Designed specifically to work with the Adafruit FONA 4 | ----> http://www.adafruit.com/products/1946 5 | ----> http://www.adafruit.com/products/1963 6 | ----> http://www.adafruit.com/products/2468 7 | ----> http://www.adafruit.com/products/2542 8 | 9 | These cellular modules use TTL Serial to communicate, 2 pins are 10 | required to interface. 11 | 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit and open-source hardware by purchasing 14 | products from Adafruit! 15 | 16 | Written by Limor Fried/Ladyada for Adafruit Industries. 17 | Adafruit IO example additions by Todd Treece. 18 | MIT license, all text above must be included in any redistribution 19 | ****************************************************/ 20 | #include 21 | #include 22 | #include "Adafruit_FONA.h" 23 | #include "Adafruit_MQTT.h" 24 | #include "Adafruit_MQTT_FONA.h" 25 | #include 26 | #include 27 | 28 | /****************************** Pins ****************************************/ 29 | #define FONA_RX 2 30 | #define FONA_TX 3 31 | #define FONA_RST 4 32 | SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); 33 | 34 | Adafruit_FONA fona = Adafruit_FONA(FONA_RST); 35 | 36 | /*************************** Cellular APN *************************************/ 37 | 38 | // Optionally configure a GPRS APN, username, and password. 39 | // You might need to do this to access your network's GPRS/data 40 | // network. Contact your provider for the exact APN, username, 41 | // and password values. Username and password are optional and 42 | // can be removed, but APN is required. 43 | #define FONA_APN "" 44 | #define FONA_USERNAME "" 45 | #define FONA_PASSWORD "" 46 | 47 | /************************* Adafruit.io Setup *********************************/ 48 | 49 | #define AIO_SERVER "io.adafruit.com" 50 | #define AIO_SERVERPORT 1883 51 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 52 | #define AIO_KEY "...your AIO key..." 53 | 54 | /************ Global State (you don't need to change this!) ******************/ 55 | 56 | // Store the MQTT server, client ID, username, and password in flash memory. 57 | // This is required for using the Adafruit MQTT library. 58 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 59 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 60 | // was compiled (so this should be unique across multiple devices for a user, 61 | // alternatively you can manually set this to a GUID or other random value). 62 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 63 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 64 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 65 | 66 | // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. 67 | Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 68 | 69 | // You don't need to change anything below this line! 70 | #define halt(s) { Serial.println(F( s )); while(1); } 71 | 72 | // FONAconnect is a helper function that sets up the FONA and connects to 73 | // the GPRS network. See the fonahelper.cpp tab above for the source! 74 | boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); 75 | 76 | /****************************** Feeds ***************************************/ 77 | 78 | // Setup a group called 'weather' for publishing changes. 79 | // Notice MQTT group CSV paths for AIO follow the form: /groups//csv 80 | const char WEATHER_FEED[] PROGMEM = AIO_USERNAME "/groups/weather/csv"; 81 | Adafruit_MQTT_Publish weather = Adafruit_MQTT_Publish(&mqtt, WEATHER_FEED); 82 | 83 | /*************************** Sketch Code ************************************/ 84 | 85 | Adafruit_AM2315 am2315; 86 | 87 | void setup() { 88 | 89 | Serial.begin(115200); 90 | 91 | Serial.println(F("Adafruit IO Example")); 92 | 93 | // Initialise the FONA module 94 | if (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) 95 | halt("Retrying FONA"); 96 | 97 | Serial.println(F("Connected to Cellular!")); 98 | 99 | Watchdog.reset(); 100 | delay(3000); // wait a few seconds to stabilize connection 101 | Watchdog.reset(); 102 | 103 | if (! am2315.begin()) { 104 | Serial.println(F("AM2315 not found, check wiring & pullups!")); 105 | while (1); 106 | } 107 | // connect to adafruit io 108 | connect(); 109 | 110 | } 111 | 112 | void loop() { 113 | 114 | // Make sure to reset watchdog every loop iteration! 115 | Watchdog.reset(); 116 | 117 | // ping adafruit io a few times to make sure we remain connected 118 | if(! mqtt.ping(3)) { 119 | // reconnect to adafruit io 120 | if(! mqtt.connected()) 121 | connect(); 122 | } 123 | 124 | char sendbuffer[80]; 125 | char numberbuffer[20]; 126 | float temp = (am2315.readTemperature() * 1.8) + 32; 127 | float humidity = am2315.readHumidity(); 128 | 129 | // add temp feed name 130 | strcpy(sendbuffer, "temp,"); 131 | 132 | // add temp value 133 | dtostrf(temp, 2, 6, numberbuffer); 134 | strcat(sendbuffer, numberbuffer); 135 | 136 | // add new line and humidity feed name 137 | strcat(sendbuffer, "\nhumidity,"); 138 | 139 | // add humidity value 140 | dtostrf(humidity, 2, 6, numberbuffer); 141 | strcat(sendbuffer, numberbuffer); 142 | 143 | // Now we can publish stuff! 144 | Serial.println(F("\nSending: ")); 145 | Serial.println(sendbuffer); 146 | 147 | if (! weather.publish(sendbuffer)) 148 | Serial.println(F("Failed.")); 149 | else 150 | Serial.println(F("Success!")); 151 | 152 | delay(1000); 153 | 154 | } 155 | 156 | // connect to adafruit io via MQTT 157 | void connect() { 158 | 159 | Serial.print(F("Connecting to Adafruit IO... ")); 160 | 161 | int8_t ret, retries = 5; 162 | 163 | while ((ret = mqtt.connect()) != 0) { 164 | 165 | switch (ret) { 166 | case 1: Serial.println(F("Wrong protocol")); break; 167 | case 2: Serial.println(F("ID rejected")); break; 168 | case 3: Serial.println(F("Server unavail")); break; 169 | case 4: Serial.println(F("Bad user/pass")); break; 170 | case 5: Serial.println(F("Not authed")); break; 171 | case 6: Serial.println(F("Failed to subscribe")); break; 172 | default: Serial.println(F("Connection failed")); break; 173 | } 174 | 175 | if(ret >= 0) 176 | mqtt.disconnect(); 177 | 178 | retries--; 179 | 180 | if (retries == 0) 181 | halt("Resetting system"); 182 | 183 | Serial.println(F("Retrying connection...")); 184 | delay(5000); 185 | 186 | } 187 | 188 | Serial.println(F("Adafruit IO Connected!")); 189 | 190 | } 191 | -------------------------------------------------------------------------------- /esp8266/analog_in/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/analog_in/.due.test.skip -------------------------------------------------------------------------------- /esp8266/analog_in/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/analog_in/.leonardo.test.skip -------------------------------------------------------------------------------- /esp8266/analog_in/.uno.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/analog_in/.uno.test.skip -------------------------------------------------------------------------------- /esp8266/analog_in/analog_in.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library ESP8266 Example 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | 7 | Works great with Adafruit's Huzzah ESP board: 8 | ----> https://www.adafruit.com/product/2471 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Tony DiCola for Adafruit Industries. 14 | Adafruit IO example additions by Todd Treece. 15 | MIT license, all text above must be included in any redistribution 16 | ****************************************************/ 17 | #include 18 | #include "Adafruit_MQTT.h" 19 | #include "Adafruit_MQTT_Client.h" 20 | 21 | // function prototypes 22 | void connect(void); 23 | 24 | /****************************** Pins ******************************************/ 25 | 26 | #define PHOTOCELL A0 // analog 0 27 | 28 | /************************* WiFi Access Point *********************************/ 29 | 30 | #define WLAN_SSID "...your SSID..." 31 | #define WLAN_PASS "...your password..." 32 | 33 | /************************* Adafruit.io Setup *********************************/ 34 | 35 | #define AIO_SERVER "io.adafruit.com" 36 | #define AIO_SERVERPORT 1883 37 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 38 | #define AIO_KEY "...your AIO key..." 39 | 40 | /************ Global State (you don't need to change this!) ******************/ 41 | 42 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 43 | WiFiClient client; 44 | 45 | // Store the MQTT server, client ID, username, and password in flash memory. 46 | // This is required for using the Adafruit MQTT library. 47 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 48 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 49 | // was compiled (so this should be unique across multiple devices for a user, 50 | // alternatively you can manually set this to a GUID or other random value). 51 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 52 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 53 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 54 | 55 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 56 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 57 | 58 | /****************************** Feeds ***************************************/ 59 | 60 | // Setup a feed called 'photocell' for publishing changes. 61 | // Notice MQTT paths for AIO follow the form: /feeds/ 62 | const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell"; 63 | Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED); 64 | 65 | /*************************** Sketch Code ************************************/ 66 | 67 | // photocell state 68 | int current = 0; 69 | int last = -1; 70 | 71 | void setup() { 72 | 73 | Serial.begin(115200); 74 | 75 | Serial.println(F("Adafruit IO Example")); 76 | 77 | // Connect to WiFi access point. 78 | Serial.println(); Serial.println(); 79 | delay(10); 80 | Serial.print(F("Connecting to ")); 81 | Serial.println(WLAN_SSID); 82 | 83 | WiFi.begin(WLAN_SSID, WLAN_PASS); 84 | while (WiFi.status() != WL_CONNECTED) { 85 | delay(500); 86 | Serial.print(F(".")); 87 | } 88 | Serial.println(); 89 | 90 | Serial.println(F("WiFi connected")); 91 | Serial.println(F("IP address: ")); 92 | Serial.println(WiFi.localIP()); 93 | 94 | // connect to adafruit io 95 | connect(); 96 | 97 | } 98 | 99 | void loop() { 100 | 101 | // ping adafruit io a few times to make sure we remain connected 102 | if(! mqtt.ping(3)) { 103 | // reconnect to adafruit io 104 | if(! mqtt.connected()) 105 | connect(); 106 | } 107 | 108 | // grab the current state of the photocell 109 | current = analogRead(PHOTOCELL); 110 | 111 | // return if the value hasn't changed 112 | if(current == last) 113 | return; 114 | 115 | // Now we can publish stuff! 116 | Serial.print(F("\nSending photocell value: ")); 117 | Serial.print(current); 118 | Serial.print("... "); 119 | 120 | if (! photocell.publish((int32_t)current)) 121 | Serial.println(F("Failed.")); 122 | else 123 | Serial.println(F("Success!")); 124 | 125 | // save the photocell state 126 | last = current; 127 | 128 | } 129 | 130 | // connect to adafruit io via MQTT 131 | void connect() { 132 | 133 | Serial.print(F("Connecting to Adafruit IO... ")); 134 | 135 | int8_t ret; 136 | 137 | while ((ret = mqtt.connect()) != 0) { 138 | 139 | switch (ret) { 140 | case 1: Serial.println(F("Wrong protocol")); break; 141 | case 2: Serial.println(F("ID rejected")); break; 142 | case 3: Serial.println(F("Server unavail")); break; 143 | case 4: Serial.println(F("Bad user/pass")); break; 144 | case 5: Serial.println(F("Not authed")); break; 145 | case 6: Serial.println(F("Failed to subscribe")); break; 146 | default: Serial.println(F("Connection failed")); break; 147 | } 148 | 149 | if(ret >= 0) 150 | mqtt.disconnect(); 151 | 152 | Serial.println(F("Retrying connection...")); 153 | delay(5000); 154 | 155 | } 156 | 157 | Serial.println(F("Adafruit IO Connected!")); 158 | 159 | } 160 | -------------------------------------------------------------------------------- /esp8266/analog_out/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/analog_out/.due.test.skip -------------------------------------------------------------------------------- /esp8266/analog_out/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/analog_out/.leonardo.test.skip -------------------------------------------------------------------------------- /esp8266/analog_out/.uno.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/analog_out/.uno.test.skip -------------------------------------------------------------------------------- /esp8266/analog_out/analog_out.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library ESP8266 Example 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | 7 | Works great with Adafruit's Huzzah ESP board: 8 | ----> https://www.adafruit.com/product/2471 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Tony DiCola for Adafruit Industries. 14 | Adafruit IO example additions by Todd Treece. 15 | MIT license, all text above must be included in any redistribution 16 | ****************************************************/ 17 | #include 18 | #include "Adafruit_MQTT.h" 19 | #include "Adafruit_MQTT_Client.h" 20 | 21 | // function prototypes 22 | void connect(void); 23 | 24 | /****************************** Pins ******************************************/ 25 | 26 | #define LED 2 // any PWM pin 27 | 28 | /************************* WiFi Access Point *********************************/ 29 | 30 | #define WLAN_SSID "...your SSID..." 31 | #define WLAN_PASS "...your password..." 32 | 33 | /************************* Adafruit.io Setup *********************************/ 34 | 35 | #define AIO_SERVER "io.adafruit.com" 36 | #define AIO_SERVERPORT 1883 37 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 38 | #define AIO_KEY "...your AIO key..." 39 | 40 | /************ Global State (you don't need to change this!) ******************/ 41 | 42 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 43 | WiFiClient client; 44 | 45 | // Store the MQTT server, client ID, username, and password in flash memory. 46 | // This is required for using the Adafruit MQTT library. 47 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 48 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 49 | // was compiled (so this should be unique across multiple devices for a user, 50 | // alternatively you can manually set this to a GUID or other random value). 51 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 52 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 53 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 54 | 55 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 56 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 57 | 58 | /****************************** Feeds ***************************************/ 59 | 60 | // Setup a feed called 'photocell' for subscribing to changes. 61 | // Notice MQTT paths for AIO follow the form: /feeds/ 62 | const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell"; 63 | Adafruit_MQTT_Subscribe photocell = Adafruit_MQTT_Subscribe(&mqtt, PHOTOCELL_FEED); 64 | 65 | /*************************** Sketch Code ************************************/ 66 | 67 | void setup() { 68 | 69 | Serial.begin(115200); 70 | 71 | Serial.println(F("Adafruit IO Example")); 72 | 73 | // Connect to WiFi access point. 74 | Serial.println(); Serial.println(); 75 | delay(10); 76 | Serial.print(F("Connecting to ")); 77 | Serial.println(WLAN_SSID); 78 | 79 | WiFi.begin(WLAN_SSID, WLAN_PASS); 80 | while (WiFi.status() != WL_CONNECTED) { 81 | delay(500); 82 | Serial.print(F(".")); 83 | } 84 | Serial.println(); 85 | 86 | Serial.println(F("WiFi connected")); 87 | Serial.println(F("IP address: ")); 88 | Serial.println(WiFi.localIP()); 89 | 90 | // listen for events on the photocell feed 91 | mqtt.subscribe(&photocell); 92 | 93 | // connect to adafruit io 94 | connect(); 95 | 96 | } 97 | 98 | void loop() { 99 | 100 | Adafruit_MQTT_Subscribe *subscription; 101 | 102 | // ping adafruit io a few times to make sure we remain connected 103 | if(! mqtt.ping(3)) { 104 | // reconnect to adafruit io 105 | if(! mqtt.connected()) 106 | connect(); 107 | } 108 | 109 | // this is our 'wait for incoming subscription packets' busy subloop 110 | while (subscription = mqtt.readSubscription(1000)) { 111 | 112 | // we only care about the photocell events 113 | if (subscription == &photocell) { 114 | 115 | // convert mqtt ascii payload to int 116 | char *value = (char *)photocell.lastread; 117 | Serial.print(F("Received: ")); 118 | Serial.println(value); 119 | int reading = atoi(value); 120 | 121 | // LED gets brighter the darker it is at the sensor 122 | // that means we have to -invert- the reading from 0-1023 back to 1023-0 123 | reading = 1023 - reading; 124 | analogWrite(LED, reading); 125 | 126 | } 127 | 128 | } 129 | 130 | } 131 | 132 | // connect to adafruit io via MQTT 133 | void connect() { 134 | 135 | Serial.print(F("Connecting to Adafruit IO... ")); 136 | 137 | int8_t ret; 138 | 139 | while ((ret = mqtt.connect()) != 0) { 140 | 141 | switch (ret) { 142 | case 1: Serial.println(F("Wrong protocol")); break; 143 | case 2: Serial.println(F("ID rejected")); break; 144 | case 3: Serial.println(F("Server unavail")); break; 145 | case 4: Serial.println(F("Bad user/pass")); break; 146 | case 5: Serial.println(F("Not authed")); break; 147 | case 6: Serial.println(F("Failed to subscribe")); break; 148 | default: Serial.println(F("Connection failed")); break; 149 | } 150 | 151 | if(ret >= 0) 152 | mqtt.disconnect(); 153 | 154 | Serial.println(F("Retrying connection...")); 155 | delay(5000); 156 | 157 | } 158 | 159 | Serial.println(F("Adafruit IO Connected!")); 160 | 161 | } 162 | -------------------------------------------------------------------------------- /esp8266/digital_in/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/digital_in/.due.test.skip -------------------------------------------------------------------------------- /esp8266/digital_in/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/digital_in/.leonardo.test.skip -------------------------------------------------------------------------------- /esp8266/digital_in/.uno.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/digital_in/.uno.test.skip -------------------------------------------------------------------------------- /esp8266/digital_in/digital_in.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library ESP8266 Example 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | 7 | Works great with Adafruit's Huzzah ESP board: 8 | ----> https://www.adafruit.com/product/2471 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Tony DiCola for Adafruit Industries. 14 | Adafruit IO example additions by Todd Treece. 15 | MIT license, all text above must be included in any redistribution 16 | ****************************************************/ 17 | #include 18 | #include "Adafruit_MQTT.h" 19 | #include "Adafruit_MQTT_Client.h" 20 | 21 | // function prototypes 22 | void connect(void); 23 | 24 | /****************************** Pins ******************************************/ 25 | 26 | #define BUTTON 2 27 | 28 | /************************* WiFi Access Point *********************************/ 29 | 30 | #define WLAN_SSID "...your SSID..." 31 | #define WLAN_PASS "...your password..." 32 | 33 | /************************* Adafruit.io Setup *********************************/ 34 | 35 | #define AIO_SERVER "io.adafruit.com" 36 | #define AIO_SERVERPORT 1883 37 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 38 | #define AIO_KEY "...your AIO key..." 39 | 40 | /************ Global State (you don't need to change this!) ******************/ 41 | 42 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 43 | WiFiClient client; 44 | 45 | // Store the MQTT server, client ID, username, and password in flash memory. 46 | // This is required for using the Adafruit MQTT library. 47 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 48 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 49 | // was compiled (so this should be unique across multiple devices for a user, 50 | // alternatively you can manually set this to a GUID or other random value). 51 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 52 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 53 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 54 | 55 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 56 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);/****************************** Feeds ***************************************/ 57 | 58 | // Setup a feed called 'button' for publishing changes. 59 | // Notice MQTT paths for AIO follow the form: /feeds/ 60 | const char BUTTON_FEED[] PROGMEM = AIO_USERNAME "/feeds/button"; 61 | Adafruit_MQTT_Publish button = Adafruit_MQTT_Publish(&mqtt, BUTTON_FEED); 62 | 63 | /*************************** Sketch Code ************************************/ 64 | 65 | // button state 66 | int current = 0; 67 | int last = -1; 68 | 69 | void setup() { 70 | 71 | // set button pin as an input 72 | pinMode(BUTTON, INPUT_PULLUP); 73 | 74 | Serial.begin(115200); 75 | 76 | Serial.println(F("Adafruit IO Example")); 77 | 78 | // Connect to WiFi access point. 79 | Serial.println(); Serial.println(); 80 | delay(10); 81 | Serial.print(F("Connecting to ")); 82 | Serial.println(WLAN_SSID); 83 | 84 | WiFi.begin(WLAN_SSID, WLAN_PASS); 85 | while (WiFi.status() != WL_CONNECTED) { 86 | delay(500); 87 | Serial.print(F(".")); 88 | } 89 | Serial.println(); 90 | 91 | Serial.println(F("WiFi connected")); 92 | Serial.println(F("IP address: ")); 93 | Serial.println(WiFi.localIP()); 94 | 95 | // connect to adafruit io 96 | connect(); 97 | 98 | } 99 | 100 | void loop() { 101 | 102 | // ping adafruit io a few times to make sure we remain connected 103 | if(! mqtt.ping(3)) { 104 | // reconnect to adafruit io 105 | if(! mqtt.connected()) 106 | connect(); 107 | } 108 | 109 | // grab the current state of the button 110 | current = digitalRead(BUTTON); 111 | 112 | // return if the value hasn't changed 113 | if(current == last) 114 | return; 115 | 116 | int32_t value = (current == LOW ? 1 : 0); 117 | 118 | // Now we can publish stuff! 119 | Serial.print(F("\nSending button value: ")); 120 | Serial.print(value); 121 | Serial.print("... "); 122 | 123 | if (! button.publish(value)) 124 | Serial.println(F("Failed.")); 125 | else 126 | Serial.println(F("Success!")); 127 | 128 | // save the button state 129 | last = current; 130 | 131 | } 132 | 133 | // connect to adafruit io via MQTT 134 | void connect() { 135 | 136 | Serial.print(F("Connecting to Adafruit IO... ")); 137 | 138 | int8_t ret; 139 | 140 | while ((ret = mqtt.connect()) != 0) { 141 | 142 | switch (ret) { 143 | case 1: Serial.println(F("Wrong protocol")); break; 144 | case 2: Serial.println(F("ID rejected")); break; 145 | case 3: Serial.println(F("Server unavail")); break; 146 | case 4: Serial.println(F("Bad user/pass")); break; 147 | case 5: Serial.println(F("Not authed")); break; 148 | case 6: Serial.println(F("Failed to subscribe")); break; 149 | default: Serial.println(F("Connection failed")); break; 150 | } 151 | 152 | if(ret >= 0) 153 | mqtt.disconnect(); 154 | 155 | Serial.println(F("Retrying connection...")); 156 | delay(5000); 157 | 158 | } 159 | 160 | Serial.println(F("Adafruit IO Connected!")); 161 | 162 | } 163 | -------------------------------------------------------------------------------- /esp8266/digital_out/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/digital_out/.due.test.skip -------------------------------------------------------------------------------- /esp8266/digital_out/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/digital_out/.leonardo.test.skip -------------------------------------------------------------------------------- /esp8266/digital_out/.uno.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/digital_out/.uno.test.skip -------------------------------------------------------------------------------- /esp8266/digital_out/digital_out.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library ESP8266 Example 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | 7 | Works great with Adafruit's Huzzah ESP board: 8 | ----> https://www.adafruit.com/product/2471 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Tony DiCola for Adafruit Industries. 14 | Adafruit IO example additions by Todd Treece. 15 | MIT license, all text above must be included in any redistribution 16 | ****************************************************/ 17 | #include 18 | #include "Adafruit_MQTT.h" 19 | #include "Adafruit_MQTT_Client.h" 20 | 21 | // function prototypes 22 | void connect(void); 23 | 24 | /****************************** Pins ******************************************/ 25 | 26 | #define LAMP 2 // power switch tail 27 | 28 | /************************* WiFi Access Point *********************************/ 29 | 30 | #define WLAN_SSID "...your SSID..." 31 | #define WLAN_PASS "...your password..." 32 | 33 | /************************* Adafruit.io Setup *********************************/ 34 | 35 | #define AIO_SERVER "io.adafruit.com" 36 | #define AIO_SERVERPORT 1883 37 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 38 | #define AIO_KEY "...your AIO key..." 39 | 40 | /************ Global State (you don't need to change this!) ******************/ 41 | 42 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 43 | WiFiClient client; 44 | 45 | // Store the MQTT server, client ID, username, and password in flash memory. 46 | // This is required for using the Adafruit MQTT library. 47 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 48 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 49 | // was compiled (so this should be unique across multiple devices for a user, 50 | // alternatively you can manually set this to a GUID or other random value). 51 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 52 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 53 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 54 | 55 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 56 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 57 | 58 | /****************************** Feeds ***************************************/ 59 | 60 | // Setup a feed called 'lamp' for subscribing to changes. 61 | // Notice MQTT paths for AIO follow the form: /feeds/ 62 | const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp"; 63 | Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, LAMP_FEED); 64 | 65 | /*************************** Sketch Code ************************************/ 66 | 67 | void setup() { 68 | 69 | // set power switch tail pin as an output 70 | pinMode(LAMP, OUTPUT); 71 | 72 | Serial.begin(115200); 73 | 74 | Serial.println(F("Adafruit IO Example")); 75 | 76 | // Connect to WiFi access point. 77 | Serial.println(); Serial.println(); 78 | delay(10); 79 | Serial.print(F("Connecting to ")); 80 | Serial.println(WLAN_SSID); 81 | 82 | WiFi.begin(WLAN_SSID, WLAN_PASS); 83 | while (WiFi.status() != WL_CONNECTED) { 84 | delay(500); 85 | Serial.print(F(".")); 86 | } 87 | Serial.println(); 88 | 89 | Serial.println(F("WiFi connected")); 90 | Serial.println(F("IP address: ")); 91 | Serial.println(WiFi.localIP()); 92 | 93 | // listen for events on the lamp feed 94 | mqtt.subscribe(&lamp); 95 | 96 | // connect to adafruit io 97 | connect(); 98 | 99 | } 100 | 101 | void loop() { 102 | 103 | Adafruit_MQTT_Subscribe *subscription; 104 | 105 | // ping adafruit io a few times to make sure we remain connected 106 | if(! mqtt.ping(3)) { 107 | // reconnect to adafruit io 108 | if(! mqtt.connected()) 109 | connect(); 110 | } 111 | 112 | // this is our 'wait for incoming subscription packets' busy subloop 113 | while (subscription = mqtt.readSubscription(1000)) { 114 | 115 | // we only care about the lamp events 116 | if (subscription == &lamp) { 117 | 118 | // convert mqtt ascii payload to int 119 | char *value = (char *)lamp.lastread; 120 | Serial.print(F("Received: ")); 121 | Serial.println(value); 122 | int current = atoi(value); 123 | 124 | // write the current state to the power switch tail 125 | digitalWrite(LAMP, current == 1 ? HIGH : LOW); 126 | 127 | } 128 | 129 | } 130 | 131 | } 132 | 133 | // connect to adafruit io via MQTT 134 | void connect() { 135 | 136 | Serial.print(F("Connecting to Adafruit IO... ")); 137 | 138 | int8_t ret; 139 | 140 | while ((ret = mqtt.connect()) != 0) { 141 | 142 | switch (ret) { 143 | case 1: Serial.println(F("Wrong protocol")); break; 144 | case 2: Serial.println(F("ID rejected")); break; 145 | case 3: Serial.println(F("Server unavail")); break; 146 | case 4: Serial.println(F("Bad user/pass")); break; 147 | case 5: Serial.println(F("Not authed")); break; 148 | case 6: Serial.println(F("Failed to subscribe")); break; 149 | default: Serial.println(F("Connection failed")); break; 150 | } 151 | 152 | if(ret >= 0) 153 | mqtt.disconnect(); 154 | 155 | Serial.println(F("Retrying connection...")); 156 | delay(5000); 157 | 158 | } 159 | 160 | Serial.println(F("Adafruit IO Connected!")); 161 | 162 | } 163 | -------------------------------------------------------------------------------- /esp8266/group_sensor/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/group_sensor/.due.test.skip -------------------------------------------------------------------------------- /esp8266/group_sensor/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/group_sensor/.leonardo.test.skip -------------------------------------------------------------------------------- /esp8266/group_sensor/.uno.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/group_sensor/.uno.test.skip -------------------------------------------------------------------------------- /esp8266/group_sensor/group_sensor.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit MQTT Library ESP8266 Example 3 | 4 | Must use ESP8266 Arduino from: 5 | https://github.com/esp8266/Arduino 6 | 7 | Works great with Adafruit's Huzzah ESP board: 8 | ----> https://www.adafruit.com/product/2471 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Tony DiCola for Adafruit Industries. 14 | Adafruit IO example additions by Todd Treece. 15 | MIT license, all text above must be included in any redistribution 16 | ****************************************************/ 17 | #include 18 | #include "Adafruit_MQTT.h" 19 | #include "Adafruit_MQTT_Client.h" 20 | #include 21 | #include 22 | 23 | // function prototypes 24 | void connect(void); 25 | 26 | /************************* WiFi Access Point *********************************/ 27 | 28 | #define WLAN_SSID "...your SSID..." 29 | #define WLAN_PASS "...your password..." 30 | 31 | /************************* Adafruit.io Setup *********************************/ 32 | 33 | #define AIO_SERVER "io.adafruit.com" 34 | #define AIO_SERVERPORT 1883 35 | #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." 36 | #define AIO_KEY "...your AIO key..." 37 | 38 | /************ Global State (you don't need to change this!) ******************/ 39 | 40 | // Create an ESP8266 WiFiClient class to connect to the MQTT server. 41 | WiFiClient client; 42 | 43 | // Store the MQTT server, client ID, username, and password in flash memory. 44 | // This is required for using the Adafruit MQTT library. 45 | const char MQTT_SERVER[] PROGMEM = AIO_SERVER; 46 | // Set a unique MQTT client ID using the AIO key + the date and time the sketch 47 | // was compiled (so this should be unique across multiple devices for a user, 48 | // alternatively you can manually set this to a GUID or other random value). 49 | const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME; 50 | const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; 51 | const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; 52 | 53 | // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. 54 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 55 | 56 | /****************************** Feeds ***************************************/ 57 | 58 | // Setup a group called 'weather' for publishing changes. 59 | // Notice MQTT group CSV paths for AIO follow the form: /groups//csv 60 | const char WEATHER_FEED[] PROGMEM = AIO_USERNAME "/groups/weather/csv"; 61 | Adafruit_MQTT_Publish weather = Adafruit_MQTT_Publish(&mqtt, WEATHER_FEED); 62 | 63 | /*************************** Sketch Code ************************************/ 64 | 65 | Adafruit_AM2315 am2315; 66 | 67 | void setup() { 68 | 69 | Serial.begin(115200); 70 | 71 | Serial.println(F("Adafruit IO Example")); 72 | 73 | // Connect to WiFi access point. 74 | Serial.println(); Serial.println(); 75 | delay(10); 76 | Serial.print(F("Connecting to ")); 77 | Serial.println(WLAN_SSID); 78 | 79 | WiFi.begin(WLAN_SSID, WLAN_PASS); 80 | while (WiFi.status() != WL_CONNECTED) { 81 | delay(500); 82 | Serial.print(F(".")); 83 | } 84 | Serial.println(); 85 | 86 | Serial.println(F("WiFi connected")); 87 | Serial.println(F("IP address: ")); 88 | Serial.println(WiFi.localIP()); 89 | 90 | if (! am2315.begin()) { 91 | Serial.println(F("AM2315 not found, check wiring & pullups!")); 92 | while (1); 93 | } 94 | 95 | // connect to adafruit io 96 | connect(); 97 | 98 | } 99 | 100 | void loop() { 101 | 102 | // ping adafruit io a few times to make sure we remain connected 103 | if(! mqtt.ping(3)) { 104 | // reconnect to adafruit io 105 | if(! mqtt.connected()) 106 | connect(); 107 | } 108 | 109 | char sendbuffer[80]; 110 | char numberbuffer[20]; 111 | float temp = (am2315.readTemperature() * 1.8) + 32; 112 | float humidity = am2315.readHumidity(); 113 | 114 | // add temp feed name 115 | strcpy(sendbuffer, "temp,"); 116 | 117 | // add temp value 118 | dtostrf(temp, 2, 6, numberbuffer); 119 | strcat(sendbuffer, numberbuffer); 120 | 121 | // add new line and humidity feed name 122 | strcat(sendbuffer, "\nhumidity,"); 123 | 124 | // add humidity value 125 | dtostrf(humidity, 2, 6, numberbuffer); 126 | strcat(sendbuffer, numberbuffer); 127 | 128 | // Now we can publish stuff! 129 | Serial.println(F("\nSending: ")); 130 | Serial.println(sendbuffer); 131 | 132 | if (! weather.publish(sendbuffer)) 133 | Serial.println(F("Failed.")); 134 | else 135 | Serial.println(F("Success!")); 136 | 137 | } 138 | 139 | // connect to adafruit io via MQTT 140 | void connect() { 141 | 142 | Serial.print(F("Connecting to Adafruit IO... ")); 143 | 144 | int8_t ret; 145 | 146 | while ((ret = mqtt.connect()) != 0) { 147 | 148 | switch (ret) { 149 | case 1: Serial.println(F("Wrong protocol")); break; 150 | case 2: Serial.println(F("ID rejected")); break; 151 | case 3: Serial.println(F("Server unavail")); break; 152 | case 4: Serial.println(F("Bad user/pass")); break; 153 | case 5: Serial.println(F("Not authed")); break; 154 | case 6: Serial.println(F("Failed to subscribe")); break; 155 | default: Serial.println(F("Connection failed")); break; 156 | } 157 | 158 | if(ret >= 0) 159 | mqtt.disconnect(); 160 | 161 | Serial.println(F("Retrying connection...")); 162 | delay(5000); 163 | 164 | } 165 | 166 | Serial.println(F("Adafruit IO Connected!")); 167 | 168 | } 169 | -------------------------------------------------------------------------------- /esp8266/trigger/.due.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/trigger/.due.test.skip -------------------------------------------------------------------------------- /esp8266/trigger/.leonardo.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/trigger/.leonardo.test.skip -------------------------------------------------------------------------------- /esp8266/trigger/.uno.test.skip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/adafruit-io-basics/a00668a326f1e83d40a2281af3574021fa79eddd/esp8266/trigger/.uno.test.skip -------------------------------------------------------------------------------- /esp8266/trigger/trigger.ino: -------------------------------------------------------------------------------- 1 | /*************************************************** 2 | Adafruit IO Trigger Example 3 | Must use ESP8266 Arduino from: 4 | https://github.com/esp8266/Arduino 5 | 6 | Works great with Adafruit's Huzzah ESP board: 7 | ----> https://www.adafruit.com/product/2471 8 | 9 | Adafruit invests time and resources providing this open source code, 10 | please support Adafruit and open-source hardware by purchasing 11 | products from Adafruit! 12 | 13 | Written by Todd Treece for Adafruit Industries. 14 | MIT license, all text above must be included in any redistribution 15 | ****************************************************/ 16 | #include 17 | #include 18 | #include "Adafruit_IO_Client.h" 19 | 20 | // function prototypes 21 | void battery_level(); 22 | void door_open(); 23 | 24 | // door gpio pin 25 | #define DOOR 13 26 | 27 | // how often to report battery level to adafruit IO (in minutes) 28 | #define BATTERY_INTERVAL 5 29 | 30 | // how long to sleep between checking the door state (in seconds) 31 | #define SLEEP_LENGTH 3 32 | 33 | // wifi credentials 34 | #define WLAN_SSID "...your SSID..." 35 | #define WLAN_PASS "...your password..." 36 | 37 | // configure Adafruit IO access 38 | #define AIO_KEY "...your AIO key..." 39 | 40 | // create an Adafruit IO client instance 41 | WiFiClient client; 42 | Adafruit_IO_Client aio = Adafruit_IO_Client(client, AIO_KEY); 43 | 44 | void setup() { 45 | Serial.begin(115200); 46 | Serial.println("HUZZAH Trigger Basic"); 47 | 48 | EEPROM.begin(512); 49 | pinMode(DOOR, INPUT_PULLUP); 50 | 51 | // get the current count position from eeprom 52 | byte battery_count = EEPROM.read(0); 53 | 54 | // we only need this to happen once every X minutes, 55 | // so we use eeprom to track the count between resets. 56 | if(battery_count >= ((BATTERY_INTERVAL * 60) / SLEEP_LENGTH)) { 57 | // reset counter 58 | battery_count = 0; 59 | // report battery level to Adafruit IO 60 | battery_level(); 61 | } else { 62 | // increment counter 63 | battery_count++; 64 | } 65 | 66 | // save the current count 67 | EEPROM.write(0, battery_count); 68 | EEPROM.commit(); 69 | 70 | // if door isn't open, we don't need to send anything 71 | if(digitalRead(DOOR) == LOW) { 72 | Serial.println("Door closed"); 73 | // we don't do anything 74 | } else { 75 | // the door is open if we have reached here, 76 | // so we should send a value to Adafruit IO. 77 | Serial.println("Door is open!"); 78 | door_open(); 79 | } 80 | 81 | // we are done here. go back to sleep. 82 | Serial.println("zzzz"); 83 | ESP.deepSleep(SLEEP_LENGTH * 1000000, WAKE_RF_DISABLED); 84 | } 85 | 86 | // noop 87 | void loop() {} 88 | 89 | // connect to wifi network. used by 90 | // door and battery functions. 91 | void wifi_init() { 92 | 93 | // wifi init 94 | Serial.println("Starting WiFi"); 95 | WiFi.begin(WLAN_SSID, WLAN_PASS); 96 | 97 | // wait for connection 98 | while (WiFi.status() != WL_CONNECTED) { 99 | Serial.write('.'); 100 | delay(500); 101 | } 102 | 103 | // AIO init 104 | Serial.println("Connecting to Adafruit.io"); 105 | aio.begin(); 106 | 107 | } 108 | 109 | void door_open() { 110 | 111 | // turn on wifi if we aren't connected 112 | if(WiFi.status() != WL_CONNECTED) { 113 | wifi_init(); 114 | } 115 | 116 | // grab the door feed 117 | Adafruit_IO_Feed door = aio.getFeed("door"); 118 | 119 | Serial.println("Sending to Adafruit.io"); 120 | // send door open value to AIO 121 | door.send("1"); 122 | 123 | } 124 | 125 | void battery_level() { 126 | 127 | // read the battery level from the ESP8266 analog in pin. 128 | // analog read level is 10 bit 0-1023 (0V-1V). 129 | // our 1M & 220K voltage divider takes the max 130 | // lipo value of 4.2V and drops it to 0.758V max. 131 | // this means our min analog read value should be 580 (3.14V) 132 | // and the max analog read value should be 774 (4.2V). 133 | int level = analogRead(A0); 134 | 135 | // convert battery level to percent 136 | level = map(level, 580, 774, 0, 100); 137 | Serial.print("Battery level: "); Serial.print(level); Serial.println("%"); 138 | // turn on wifi if we aren't connected 139 | if(WiFi.status() != WL_CONNECTED) 140 | wifi_init(); 141 | 142 | // grab the battery feed 143 | Adafruit_IO_Feed battery = aio.getFeed("battery"); 144 | 145 | // send battery level to AIO 146 | battery.send(level); 147 | } 148 | -------------------------------------------------------------------------------- /raspberry_pi/digital_in.js: -------------------------------------------------------------------------------- 1 | var GpioStream = require('gpio-stream'), 2 | button = GpioStream.readable(17), 3 | AIO = require('adafruit-io'); 4 | 5 | // replace xxxxxxxxxxx with your Adafruit IO key 6 | var AIO_KEY = 'xxxxxxxxxxx', 7 | AIO_USERNAME = 'your_username'; 8 | 9 | // aio init 10 | var aio = AIO(AIO_USERNAME, AIO_KEY); 11 | 12 | // pipe button presses to the button feed 13 | button.pipe(aio.feeds('Button')); 14 | 15 | console.log('listening for button presses...'); 16 | -------------------------------------------------------------------------------- /raspberry_pi/digital_out.js: -------------------------------------------------------------------------------- 1 | var GpioStream = require('gpio-stream'), 2 | light = GpioStream.writable(17), 3 | AIO = require('adafruit-io'); 4 | 5 | 6 | // replace xxxxxxxxxxx with your Adafruit IO key 7 | var AIO_KEY = 'xxxxxxxxxxx', 8 | AIO_USERNAME = 'your_username'; 9 | 10 | // aio init 11 | var aio = AIO(AIO_USERNAME, AIO_KEY); 12 | 13 | // pipe light data to the powerswitch tail 14 | aio.feeds('Light').pipe(light); 15 | 16 | console.log('listening for light data...'); 17 | -------------------------------------------------------------------------------- /raspberry_pi/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "adafruit-io-basics", 3 | "version": "1.0.3", 4 | "description": "adafruit io example scripts", 5 | "main": "button.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/adafruit/adafruit-io-basics" 12 | }, 13 | "keywords": [ 14 | "adafruit", 15 | "io" 16 | ], 17 | "author": "Todd Treece ", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/adafruit/adafruit-io-basics/issues" 21 | }, 22 | "homepage": "https://github.com/adafruit/adafruit-io-basics", 23 | "dependencies": { 24 | "adafruit-io": "^2.0.0", 25 | "gpio-stream": "^1.0.16" 26 | } 27 | } 28 | --------------------------------------------------------------------------------