-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathanalog_in.py
66 lines (51 loc) · 1.7 KB
/
analog_in.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
'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 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 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'
# 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)