|
| 1 | +""" |
| 2 | +'analog_in.py' |
| 3 | +================================== |
| 4 | +Example of sending analog sensor |
| 5 | +values to an Adafruit IO feed. |
| 6 | +
|
| 7 | +Author(s): Brent Rubell |
| 8 | +
|
| 9 | +Dependencies: |
| 10 | + - Adafruit_Blinka |
| 11 | + (https://github.com/adafruit/Adafruit_Blinka) |
| 12 | + - Adafruit_CircuitPython_MCP3xxx |
| 13 | + (https://github.com/adafruit/Adafruit_CircuitPython_MCP3xxx) |
| 14 | +""" |
| 15 | +# Import standard python modules |
| 16 | +import time |
| 17 | + |
| 18 | +# import Adafruit Blinka |
| 19 | +import board |
| 20 | +import digitalio |
| 21 | +import busio |
| 22 | + |
| 23 | +# import Adafruit IO REST client |
| 24 | +from Adafruit_IO import Client, Feed, RequestError |
| 25 | + |
| 26 | +# import Adafruit CircuitPython MCP3xxx library |
| 27 | +from adafruit_mcp3xxx import adafruit_mcp3xxx |
| 28 | + |
| 29 | +# Set to your Adafruit IO key. |
| 30 | +# Remember, your key is a secret, |
| 31 | +# so make sure not to publish it when you publish this code! |
| 32 | +ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' |
| 33 | + |
| 34 | +# Set to your Adafruit IO username. |
| 35 | +# (go to https://accounts.adafruit.com to find your username) |
| 36 | +ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME' |
| 37 | +# Create an instance of the REST client |
| 38 | +aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) |
| 39 | + |
| 40 | +try: # if we have a 'analog' feed |
| 41 | + analog = aio.feeds('analog') |
| 42 | +except RequestError: # create a analog feed |
| 43 | + feed = Feed(name='analog') |
| 44 | + analog = aio.create_feed(feed) |
| 45 | + |
| 46 | +# Create an instance of the `busio.spi` class |
| 47 | +spi = busio.SPI(board.SCLK, board.MOSI, board.MISO) |
| 48 | + |
| 49 | +# create the cs (chip select) |
| 50 | +cs = digitalio.DigitalInOut(board.D12) |
| 51 | +# create a mcp3008 object |
| 52 | +mcp = adafruit_mcp3xxx.MCP3008(spi,cs) |
| 53 | + |
| 54 | +# create an an adc (single-ended) on pin 0 |
| 55 | +adc_single_ended = adafruit_mcp3xxx.AnalogIn(mcp,0) |
| 56 | + |
| 57 | +while True: |
| 58 | + sensor_data = adc_single_ended.value |
| 59 | + |
| 60 | + print('Analog Data -> ', sensor_data) |
| 61 | + aio.send(analog.key, sensor_data) |
| 62 | + |
| 63 | + # avoid timeout from adafruit io |
| 64 | + time.sleep(0.5) |
0 commit comments