-
Notifications
You must be signed in to change notification settings - Fork 1
/
real.py
135 lines (109 loc) · 3.51 KB
/
real.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Micropython Http Server
# Erni Tron ernitron@gmail.com
# Copyright (c) 2016
import time
import sys
import network
import machine
import gc
from ubinascii import hexlify
from config import config
def do_connect(ssid, pwd, TYPE, hard_reset=True):
interface = network.WLAN(TYPE)
# Stage zero if credential are null disconnect
if not pwd or not ssid :
print('Disconnecting ', TYPE)
interface.active(False)
return None
if TYPE == network.AP_IF:
interface.active(True)
time.sleep_ms(200)
interface.config(essid=ssid, password=pwd)
return interface
if hard_reset:
interface.active(True)
interface.connect(ssid, pwd)
# Stage one check for default connection
print('Connecting')
for t in range(120):
time.sleep_ms(250)
if interface.isconnected():
#print('Yes! Connected')
return interface
if t == 60 and not hard_reset:
# if still not connected
interface.active(True)
interface.connect(ssid, pwd)
# No way we are not connected
print('Cant connect', ssid)
return None
#----------------------------------------------------------------
# MAIN PROGRAM STARTS HERE
def main():
# Enable automatic garbage collector
gc.enable()
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('wake from deep sleep')
hard_reset = False
else:
hard_reset = True
print('wake from hard reset')
# Read from file the whole configuration
config.read_config()
# Get WiFi defaults and connect
ssid = config.get_config('ssid')
pwd = config.get_config('pwd')
interface = do_connect(ssid, pwd, network.STA_IF, hard_reset)
if not interface :
# Turn on Access Point only with passw
apssid = 'YoT-%s' % bytes.decode(chipid)
appwd = config.get_config('appwd')
interface = do_connect(apssid, appwd, network.AP_IF)
if not interface :
print('Restart 10"')
time.sleep(10.0)
machine.reset()
# Set Parameters
(address, mask, gateway, dns) = interface.ifconfig()
chipid = hexlify(machine.unique_id())
config.set_config('address', address)
config.set_config('mask', mask)
config.set_config('gateway', gateway)
config.set_config('dns', dns)
config.set_config('mac', hexlify(interface.config('mac'), ':'))
config.set_config('chipid', chipid)
# We can set the time now
# Set Time RTC
from ntptime import settime
try:
settime()
(y, m, d, h, mm, s, c, u) = time.localtime()
starttime = '%d-%d-%d %d:%d:%d UTC' % (y, m, d, h, mm, s)
except:
starttime = '2061-01-01 00:00:00'
print('Cannot set time')
if hard_reset:
config.set_config('starttime', starttime)
# Set hostname
interface.config(dhcp_hostname=chipid)
config.set_config('hostname', interface.config('dhcp_hostname'))
# We will save new configuration only at powerup
if hard_reset:
config.save_config()
# Free some memory
ssid = pwd = None
apssid = appwd = None
address = mask = gateway = dns = None
gc.collect()
# The application hook
from application import application
application()
# Restart
print('Restarting')
time.sleep(5.0)
# If everything was ok we go to sleep for a while
sleep = config.get_config('sleep')
if sleep :
from gotosleep import gotosleep
gotosleep(int(sleep))
machine.reset()