|
| 1 | +// Adafruit IO Analog Out Example |
| 2 | +// |
| 3 | +// Adafruit invests time and resources providing this open source code. |
| 4 | +// Please support Adafruit and open source hardware by purchasing |
| 5 | +// products from Adafruit! |
| 6 | +// |
| 7 | +// Written by Todd Treece for Adafruit Industries |
| 8 | +// Copyright (c) 2016 Adafruit Industries |
| 9 | +// Licensed under the MIT license. |
| 10 | +// |
| 11 | +// All text above must be included in any redistribution. |
| 12 | + |
| 13 | +/************************** Configuration ***********************************/ |
| 14 | + |
| 15 | +// edit the config.h tab and enter your Adafruit IO credentials |
| 16 | +// and any additional configuration needed for WiFi, cellular, |
| 17 | +// or ethernet clients. |
| 18 | +#include "config.h" |
| 19 | + |
| 20 | +/************************ Example Starts Here *******************************/ |
| 21 | + |
| 22 | +// digital pin 5 |
| 23 | +// this should correspond to a pin with PWM capability |
| 24 | +#define LED_PIN 5 |
| 25 | + |
| 26 | +// set up the 'analog' feed |
| 27 | +AdafruitIO_Feed *analog = io.feed("analog"); |
| 28 | + |
| 29 | +void setup() { |
| 30 | + |
| 31 | + // start the serial connection |
| 32 | + Serial.begin(115200); |
| 33 | + |
| 34 | + // wait for serial monitor to open |
| 35 | + while(! Serial); |
| 36 | + |
| 37 | + // connect to io.adafruit.com |
| 38 | + Serial.print("Connecting to Adafruit IO"); |
| 39 | + io.connect(); |
| 40 | + |
| 41 | + // set up a message handler for the 'analog' feed. |
| 42 | + // the handleMessage function (defined below) |
| 43 | + // will be called whenever a message is |
| 44 | + // received from adafruit io. |
| 45 | + analog->onMessage(handleMessage); |
| 46 | + |
| 47 | + // wait for a connection |
| 48 | + while(io.status() < AIO_CONNECTED) { |
| 49 | + Serial.print("."); |
| 50 | + delay(500); |
| 51 | + } |
| 52 | + |
| 53 | + // we are connected |
| 54 | + Serial.println(); |
| 55 | + Serial.println(io.statusText()); |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +void loop() { |
| 60 | + |
| 61 | + // io.run(); is required for all sketches. |
| 62 | + // it should always be present at the top of your loop |
| 63 | + // function. it keeps the client connected to |
| 64 | + // io.adafruit.com, and processes any incoming data. |
| 65 | + io.run(); |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +// this function is called whenever an 'analog' message |
| 70 | +// is received from Adafruit IO. it was attached to |
| 71 | +// the analog feed in the setup() function above. |
| 72 | +void handleMessage(AdafruitIO_Data *data) { |
| 73 | + |
| 74 | + // LED gets brighter the darker it is at the sensor |
| 75 | + // that means we have to invert the reading |
| 76 | + // from 0-1023 back to 1023-0 |
| 77 | + int reading = 1023 - data->toInt(); |
| 78 | + |
| 79 | + Serial.print("received <- "); |
| 80 | + Serial.println(reading); |
| 81 | + analogWrite(LED_PIN, reading); |
| 82 | + |
| 83 | +} |
0 commit comments