-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhai.py
66 lines (56 loc) · 1.94 KB
/
hai.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
import urequests
import board
import digitalio
import machine
import time
from analogio import AnalogIn
ESSID = 'nasa'
PASSWORD = 'mars-adventure'
USER_NAME = "donblair" #PLEASE CHANGE TO YOUR AIO USERNAME
FEED_NAME = 'analog-feed-test-number-1' #PLEASE CHANGE TO YOUR AIO FEED NAME
FEED_KEY = '3515b3ecee734780927d7f4ab1654917' #PLEASE CHANGE TO YOUR AIO KEY
PARAMETER_PATH = 'params.txt'
headers={'Content-Type': 'application/json','X-AIO-Key':FEED_KEY}
url='https://io.adafruit.com/api/v2/{username}/feeds/{feedname}/data.json'.format(username=USER_NAME,feedname=FEED_NAME)
def get_params():
f=open(PARAMETER_PATH)
p=f.read().strip().split(',')
return p
def get_adc():
with AnalogIn(board.ADC) as ai:
return ai.value/65535.0
#import machine
#adc=machine.ADC(0)
#return adc.read()
def blink(sleeptime):
import machine
led = machine.Pin(0,machine.Pin.OUT)
led.value(0)
time.sleep(sleeptime)
led.value(1)
def post(value):
json=dict(value=value)
post_json(json)
def post_json(json):
r=urequests.post(url,json=json,headers=headers)
blink(1)
return r
def post_to_feed(value,USER_NAME,FEED_KEY,FEED_NAME):
json=dict(value=value)
headers={'Content-Type': 'application/json','X-AIO-Key':FEED_KEY} url='https://io.adafruit.com/api/v2/{username}/feeds/{feedname}/data.json'.format(username=USER_NAME,feedname=FEED_NAME)
r=urequests.post(url,json=json,headers=headers)
blink(1)
return r
def do_connect(essid=ESSID,password=PASSWORD):
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(essid, password)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
#json=dict(foo='0.2')
#do_connect(ESSID,PASSWORD)
#post(json)