Code
Code: Select all
import network
import urequests
import gc
from machine import freq
freq(160000000)
PublicKey= '8BA9FA35FEAF6A356893B577A44408A3'
SecretKey = 'D23D16F1C34F170931894E4C7914121F78C538377BE9CD3F29CC845426B31991'
def setNetworkConfig(ssid, pwd):
# Access Point
ap = network.WLAN(network.AP_IF)
ap.active(False)
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
print('connecting to network...')
wlan.active(True)
wlan.connect(ssid, pwd)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
return
def deviceInfo(protocol,url,keyPublic,keySecret,silent=False):
gc.collect()
# defining the api-endpoint
API_ENDPOINT = protocol+"://" + url + "/api/device/" + keyPublic + "/info"
# Headers
if keySecret is not None:
headers = {"Content-Type": "application/json", "Authorization": "Bearer " + keySecret,'Connection': 'Close'}
else:
headers = {'Connection': 'Close'}
# sending post request and saving response as response object
response = urequests.get(url=API_ENDPOINT, headers=headers)
#statusCode = response.status_code
parsed = response.json()
#parsed = response.text;
if silent is True:
# extracting response text
print(parsed)
response.close()
API_ENDPOINT = None
return parsed
def deviceLog(protocol,url, keyPublic, keySecret, code, message,silent=False):
gc.collect()
# defining the api-endpoint
API_ENDPOINT = protocol+"://" + url + "/api/device/" + keyPublic + "/log"
# Headers
headers = {"Content-Type": "application/json","Authorization": "Bearer " + keySecret, 'Connection': 'Close'}
# Body
body = {"code": code, "message": message}
# sending post
response = urequests.post(url=API_ENDPOINT, headers=headers, json=body)
parsed = response.json()
if silent is True:
# extracting response text
print(parsed)
response.close()
API_ENDPOINT = None
return parsed
def deviceSystemCall(protocol,url,keyPublic,keySecret,silent=False):
gc.collect()
# defining the api-endpoint
API_ENDPOINT = protocol+"://" + url + "/api/device/" + keyPublic +"/system/call"
# Headers
headers = {"Content-Type": "application/json", "Authorization": "Bearer " + keySecret, 'Connection': 'Close'}
# sending post request and saving response as response object
response = urequests.patch(url=API_ENDPOINT, headers=headers)
parsed = response.json()
if silent is True:
# extracting response text
print(parsed)
response.close()
API_ENDPOINT = None
return parsed
def resetCause():
from machine import reset_cause
code = reset_cause()
msg = ("Power reboot","Hardware WDT reset","Fatal exception","Software watchdog reset","Software reset","Deep-sleep","Hardware reset")
response = deviceLog('http','automacao-iot.com.br',PublicKey,SecretKey, code,msg[code],True)
#print(response)
return
setNetworkConfig('xxxxx','xxxxxxx')
resetCause()
while True:
gc.collect()
response = deviceInfo('http','automacao-iot.com.br',PublicKey,SecretKey,True)
#print(response)
response = deviceSystemCall('http','automacao-iot.com.br',PublicKey,SecretKey,True)
#print(response)
print("Free Memory ",gc.mem_free())
Error
Code: Select all
Traceback (most recent call last):
File "main.py", line 115, in <module>
File "main.py", line 43, in deviceInfo
File "urequests.py", line 112, in get
File "urequests.py", line 100, in request
File "urequests.py", line 58, in request
OSError: [Errno 103] ECONNABORTED