-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.py
140 lines (130 loc) · 4.48 KB
/
Helper.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
135
136
137
138
139
140
class Helper():
"""My own custom class for getting an ESP connected and read for use"""
def __init__(self):
import gc
import neopixel
import tinys3
import ntptime
from time import gmtime
from machine import RTC, Pin
self.pixel = neopixel.NeoPixel(Pin(tinys3.RGB_DATA), 1)
self.colors = "None"
self.connection = 0
@micropython.native
def led_off(self):
"""RBG LED is turned 'off'."""
self.pixel[0] = (0, 0, 0, 0)
self.pixel.write()
@micropython.native
def led_on(self,colors):
"""Turn LED ON"""
import tinys3
tinys3.set_pixel_power(True)
if colors == 'red':
self.pixel[0] = ( 255, 0, 0, .5)
self.pixel.write()
elif colors == 'yellow':
self.pixel[0] = ( 255, 128, 0, .5)
self.pixel.write()
elif colors == 'green':
self.pixel[0] = ( 0, 255, 0, .5)
self.pixel.write()
elif colors == 'blue':
self.pixel[0] = ( 0, 0, 255, .5)
self.pixel.write()
@micropython.native
def wifi_connect(self):
"""Connect to local Wi-Fi using secrets.py credentials"""
import network
from time import sleep
from secrets import WIFI_SSID
from secrets import WIFI_PASSWORD
network.MODE_11N
network.WLAN.PM_NONE
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('Connecting to WiFi...')
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected():
pass
print('WiFi Connected! Network Details:')
print(wlan.ifconfig())
self.connection = 1
self.led_on('green')
sleep(.5)
self.led_off()
sleep(.5)
self.led_on('green')
sleep(.5)
self.led_off()
@micropython.native
def wifi_disconnect(self):
"""Connect to local Wi-Fi using secrets.py credentials"""
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(False)
self.connection = 0
@micropython.native
def test_comms(self):
"""Simple connection web test to verify connectivty by getting status code"""
import urequests
from time import sleep
print("Verifying Wi-Fi outbound connection.")
try:
if (urequests.get('https://www.cnn.com').status_code) == 200:
print("Internet connectivity is established and working!")
self.connection = 1
self.led_on('green')
sleep(.5)
self.led_off()
sleep(.5)
self.led_on('green')
sleep(.5)
self.led_off()
sleep(.5)
self.led_on('green')
sleep(.5)
self.led_off()
except:
self.connection = 0
self.led_on('red')
sleep(1)
self.led_off()
sleep(1)
self.led_on('red')
sleep(1)
self.led_off()
sleep(1)
self.led_on()
print("Cannot reach the internet, retry connecting.")
@micropython.native
def battery_voltage(self):
"""Self explanatory"""
import tinys3
voltage = tinys3.get_battery_voltage()
return voltage
@micropython.native
def usb_detect(self):
"""Detect if VBUS (5V) power source is present"""
import tinys3
usb = tinys3.get_vbus_present()
return usb
@micropython.native
def set_rtc_ntp(self):
import network
if self.connection == 0:
print("You must be connected to the internet to set the RTC from NTP. Please connect to Wi-Fi")
pass
else:
from machine import RTC
from time import gmtime
"""Method to set internal RTC using NTP"""
tm = gmtime()
rtc = RTC() #Now holds RTC Time (YYYY,MM,DD,HH,MM,SS,subseconds)TUPLE
rtc.datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
print("RTC Set!")
print("**********************")
print("Date: "f'{tm[1]:02d}',"/",f'{tm[2]:02d}',"/",f'{tm[0]:04d}')
print("Time: "f'{tm[3]:02d}',":",f'{tm[4]:02d}',":",f'{tm[5]:02d}')
print("**********************")