mathz.nu Asterisk Blacklist Hobby webbhotell

2016/11/01

Micropyhton esp8266 JSON client

Filed under: esp8266 — Mathz @ 13:55

# Sample code to request the state fron JSON service.


API_PASSWORD = 'YOUR_PASSWORD'
URL = 'http://x.x.x.x:xxxx/api/states/'
ENTITY = 'sensor'
TIMEOUT = 30
PIN = 5

def get_data():
import urequests
url = '{}{}'.format(URL, ENTITY)
headers = {'x-ha-access': API_PASSWORD,
'content-type': 'application/json'}
resp = urequests.get(URL, headers=headers)
return resp.json()['state']

def main():
import machine
import time

pin = machine.Pin(PIN, machine.Pin.OUT)
while True:
try:
if int(get_data()) >= 20:
pin.high()
else:
pin.low()
except TypeError:
pass
time.sleep(TIMEOUT)

if __name__ == '__main__':
print('Get the state of {}'.format(ENTITY))
main()

2016/10/30

Start network on esp8266

Filed under: esp8266 — Mathz @ 08:46

Configure network as client
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'password')

Show IP-adress
wlan.ifconfig()

Configure network as access Point

import network
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid="network-name", authmode=network.AUTH_WPA_WPA2_PSK, password="abcdabcdabcd")

Powered by WordPress