Advertisement
snaggedagge

Tutorial adafruit

May 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. // Adafruit IO REST API access with ESP8266
  2. //
  3. // For use with ESP8266 Arduino from:
  4. //   https://github.com/esp8266/Arduino
  5. //
  6. // Works great with ESP8266 modules like the Adafruit Huzzah ESP:
  7. //  ----> https://www.adafruit.com/product/2471
  8. //
  9. // Written by Tony DiCola for Adafruit Industries.  
  10. // MIT license, all text above must be included in any redistribution.
  11. #include <ESP8266WiFi.h>
  12. #include "Adafruit_IO_Client.h"
  13.  
  14.  
  15. // Configure WiFi access point details.
  16. #define WLAN_SSID  "...your SSID..."
  17. #define WLAN_PASS  "...your password..."
  18.  
  19. // Configure Adafruit IO access.
  20. #define AIO_KEY    "...your Adafruit IO key value ..."
  21.  
  22.  
  23. // Create an ESP8266 WiFiClient class to connect to the AIO server.
  24. WiFiClient client;
  25.  
  26. // Create an Adafruit IO Client instance.  Notice that this needs to take a
  27. // WiFiClient object as the first parameter, and as the second parameter a
  28. // default Adafruit IO key to use when accessing feeds (however each feed can
  29. // override this default key value if required, see further below).
  30. Adafruit_IO_Client aio = Adafruit_IO_Client(client, AIO_KEY);
  31.  
  32. // Finally create instances of Adafruit_IO_Feed objects, one per feed.  Do this
  33. // by calling the getFeed function on the Adafruit_IO_FONA object and passing
  34. // it at least the name of the feed, and optionally a specific AIO key to use
  35. // when accessing the feed (the default is to use the key set on the
  36. // Adafruit_IO_Client class).
  37. Adafruit_IO_Feed testFeed = aio.getFeed("esptestfeed");
  38.  
  39. // Alternatively to access a feed with a specific key:
  40. //Adafruit_IO_Feed testFeed = aio.getFeed("esptestfeed", "...esptestfeed key...");
  41.  
  42. // Global state to increment a number and send it to the feed.
  43. unsigned int count = 0;
  44.  
  45. void setup() {
  46.   // Setup serial port access.
  47.   Serial.begin(115200);
  48.   delay(10);
  49.   Serial.println(); Serial.println();
  50.   Serial.println(F("Adafruit IO ESP8266 test!"));
  51.  
  52.   // Connect to WiFi access point.
  53.   Serial.print("Connecting to ");
  54.   Serial.println(WLAN_SSID);
  55.  
  56.   WiFi.begin(WLAN_SSID, WLAN_PASS);
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.     delay(500);
  59.     Serial.print(".");
  60.   }
  61.   Serial.println();
  62.  
  63.   Serial.println("WiFi connected");  
  64.   Serial.println("IP address: "); Serial.println(WiFi.localIP());
  65.  
  66.   // Initialize the Adafruit IO client class (not strictly necessary with the
  67.   // client class, but good practice).
  68.   aio.begin();
  69.  
  70.   Serial.println(F("Ready!"));
  71. }
  72.  
  73. void loop() {
  74.   // Increment the count value and write it to the feed.
  75.   count += 1;
  76.   // To write a value just call the feed's send function and pass it the value.
  77.   // Send will create the feed on Adafruit IO if it doesn't already exist and
  78.   // then add the value to it.  Send returns a boolean that's true if it works
  79.   // and false if it fails for some reason.
  80.   if (testFeed.send(count)) {
  81.     Serial.print(F("Wrote value to feed: ")); Serial.println(count, DEC);
  82.   }
  83.   else {
  84.     Serial.println(F("Error writing value to feed!"));
  85.   }
  86.  
  87.   // Now wait 10 seconds and read the current feed value.
  88.   Serial.println(F("Waiting 10 seconds and then reading the feed value."));
  89.   delay(10000);
  90.  
  91.   // To read the latest feed value call the receive function on the feed.
  92.   // The returned object will be a FeedData instance and you can check if it's
  93.   // valid (i.e. was successfully read) by calling isValid(), and then get the
  94.   // value either as a text value, or converted to an int, float, etc.
  95.   FeedData latest = testFeed.receive();
  96.   if (latest.isValid()) {
  97.     Serial.print(F("Received value from feed: ")); Serial.println(latest);
  98.     // By default the received feed data item has a string value, however you
  99.     // can use the following functions to attempt to convert it to a numeric
  100.     // value like an int or float.  Each function returns a boolean that indicates
  101.     // if the conversion succeeded, and takes as a parameter by reference the
  102.     // output value.
  103.     int i;
  104.     if (latest.intValue(&i)) {
  105.       Serial.print(F("Value as an int: ")); Serial.println(i, DEC);
  106.     }
  107.     // Other functions that you can use include:
  108.     //  latest.uintValue() (unsigned int)
  109.     //  latest.longValue() (long)
  110.     //  latest.ulongValue() (unsigned long)
  111.     //  latest.floatValue() (float)
  112.     //  latest.doubleValue() (double)
  113.   }
  114.   else {
  115.     Serial.print(F("Failed to receive the latest feed value!"));
  116.   }
  117.  
  118.   // Now wait 10 more seconds and repeat.
  119.   Serial.println(F("Waiting 10 seconds and then writing a new feed value."));
  120.   delay(10000);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement