Download Project Code
Click "Download Project Bundle" to download a .zip file containing all the code examples for the Adafruit IO Basics series of project guides.
After downloading, unzip the file and save it somewhere safe on your computer. We suggest saving it to the desktop!
"""
'analog_in.py'
==================================
Example of sending analog sensor
values to an Adafruit IO feed.
Author(s): Brent Rubell
Dependencies:
- Adafruit_Blinka
(https://github.com/adafruit/Adafruit_Blinka)
- Adafruit_CircuitPython_MCP3xxx
(https://github.com/adafruit/Adafruit_CircuitPython_MCP3xxx)
"""
# Import standard python modules
import os
import time
# import Adafruit Blinka
import board
import digitalio
import busio
# import Adafruit IO REST client
from Adafruit_IO import Client, Feed, RequestError
# import Adafruit CircuitPython MCP3xxx library
from adafruit_mcp3xxx.mcp3008 import MCP3008
from adafruit_mcp3xxx.analog_in import AnalogIn
# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username)
ADAFRUIT_IO_USERNAME = os.getenv('ADAFRUIT_IO_USERNAME', 'YOUR_AIO_USERNAME')
# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure **not** to publish it when you publish this code!
ADAFRUIT_IO_KEY = os.getenv('ADAFRUIT_IO_KEY', 'YOUR_AIO_KEY')
# Create an instance of the REST client
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
try: # if we have a 'analog' feed
analog = aio.feeds('analog')
except RequestError: # create a analog feed
feed = Feed(name='analog')
analog = aio.create_feed(feed)
# Create an instance of the `busio.spi` class
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D12)
# create a mcp3008 object
mcp = MCP3008(spi, cs)
# create an an adc (single-ended) on pin 0
chan = AnalogIn(mcp, MCP3008.pin_0)
while True:
sensor_data = chan.value
print('Analog Data -> ', sensor_data)
aio.send(analog.key, sensor_data)
# avoid timeout from adafruit io
time.sleep(0.5)
Get Adafruit IO Key
To connect your device to Adafruit IO, you will need your Adafruit IO credentials. To obtain these, navigate to io.adafruit.com and click the key icon on the header:
Modify Project Code
Before we run the example, you will need to change ADAFRUIT_IO_USERNAME and ADAFRUIT_IO_KEY to the username and key for your Adafruit IO account.
# Set to your Adafruit IO key. # Remember, your key is a secret, # so make sure not to publish it when you publish this code! ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' # Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = 'YOUR_AIO_USERNAME'
Run Project Code
In a terminal, navigate to the folder containing the Adafruit IO Basics project examples.
Next, run the script by running the following in your terminal:
python3 analog_in.py
Cover the photocell with your hand and you should see the changing values being sent to Adafruit IO:
sending -> 1024 sending -> 953 sending -> 259 sending -> 476 sending -> 248 sending -> 173 sending -> 224 sending -> 291 sending -> 1024
Page last edited
Text editor powered by tinymce.