Browse Source

add wifi support for Pico

Thomas Buck 1 year ago
parent
commit
97233a2e28
5 changed files with 117 additions and 6 deletions
  1. 14
    0
      config.py
  2. 9
    0
      copy.sh
  3. 16
    5
      net.py
  4. 2
    1
      pico.py
  5. 76
    0
      util.py

+ 14
- 0
config.py View File

1
+#!/usr/bin/env python3
2
+
3
+# ----------------------------------------------------------------------------
4
+# "THE BEER-WARE LICENSE" (Revision 42):
5
+# <xythobuz@xythobuz.de> wrote this file.  As long as you retain this notice
6
+# you can do whatever you want with this stuff. If we meet some day, and you
7
+# think this stuff is worth it, you can buy me a beer in return.   Thomas Buck
8
+# ----------------------------------------------------------------------------
9
+
10
+class Config:
11
+    networks = [
12
+        ("SSID_1", "PASS_1"),
13
+        ("SSID_2", "PASS_2"),
14
+    ]

+ 9
- 0
copy.sh View File

2
 
2
 
3
 if [ $# -ne 0 ] ; then
3
 if [ $# -ne 0 ] ; then
4
 cat << EOF | rshell
4
 cat << EOF | rshell
5
+cp config.py /pyboard
5
 cp pico.py /pyboard
6
 cp pico.py /pyboard
6
 cp util.py /pyboard
7
 cp util.py /pyboard
8
+cp manager.py /pyboard
9
+cp net.py /pyboard
10
+cp solid.py /pyboard
7
 cp $1 /pyboard/main.py
11
 cp $1 /pyboard/main.py
8
 EOF
12
 EOF
9
 else
13
 else
10
 cat << EOF | rshell
14
 cat << EOF | rshell
15
+rm /pyboard/main.py
16
+cp config.py /pyboard
11
 cp pico.py /pyboard
17
 cp pico.py /pyboard
12
 cp util.py /pyboard
18
 cp util.py /pyboard
19
+cp manager.py /pyboard
20
+cp net.py /pyboard
21
+cp solid.py /pyboard
13
 cp life.py /pyboard
22
 cp life.py /pyboard
14
 EOF
23
 EOF
15
 fi
24
 fi

+ 16
- 5
net.py View File

8
 # ----------------------------------------------------------------------------
8
 # ----------------------------------------------------------------------------
9
 
9
 
10
 import time
10
 import time
11
-import requests
11
+import util
12
 
12
 
13
 class CheckHTTP:
13
 class CheckHTTP:
14
     def __init__(self, u, r = 600.0):
14
     def __init__(self, u, r = 600.0):
16
         self.refresh = r
16
         self.refresh = r
17
         self.successScreen = None
17
         self.successScreen = None
18
         self.failScreen = None
18
         self.failScreen = None
19
+        self.get = util.getRequests()
20
+
19
         self.restart()
21
         self.restart()
20
 
22
 
21
     def success(self, s):
23
     def success(self, s):
35
             self.failScreen.restart()
37
             self.failScreen.restart()
36
 
38
 
37
     def request(self):
39
     def request(self):
40
+        if self.get == None:
41
+            return
42
+
38
         if (self.response == None) or ((time.time() - self.start) >= self.refresh):
43
         if (self.response == None) or ((time.time() - self.start) >= self.refresh):
39
             self.start = time.time()
44
             self.start = time.time()
40
             try:
45
             try:
41
-                r = requests.get(self.url)
42
-                self.response = r.ok
46
+                r = self.get(self.url)
47
+                self.response = (r.status_code < 400)
43
             except:
48
             except:
44
                 self.response = False
49
                 self.response = False
45
 
50
 
46
     def finished(self):
51
     def finished(self):
52
+        if self.get == None:
53
+            return True
54
+
47
         self.request()
55
         self.request()
48
         if self.response:
56
         if self.response:
49
             return self.successScreen.finished()
57
             return self.successScreen.finished()
51
             return self.failScreen.finished()
59
             return self.failScreen.finished()
52
 
60
 
53
     def draw(self):
61
     def draw(self):
62
+        if self.get == None:
63
+            return
64
+
54
         self.request()
65
         self.request()
55
         if self.response:
66
         if self.response:
56
-            return self.successScreen.draw()
67
+            self.successScreen.draw()
57
         else:
68
         else:
58
-            return self.failScreen.draw()
69
+            self.failScreen.draw()
59
 
70
 
60
 if __name__ == "__main__":
71
 if __name__ == "__main__":
61
     from draw import ScrollText
72
     from draw import ScrollText

+ 2
- 1
pico.py View File

1
 #!/usr/bin/env python3
1
 #!/usr/bin/env python3
2
 
2
 
3
+# For the Pimoroni Interstate75 Raspberry Pi Pico RGB LED Matrix interface:
4
+# https://github.com/pimoroni/pimoroni-pico
3
 #
5
 #
4
 # ----------------------------------------------------------------------------
6
 # ----------------------------------------------------------------------------
5
 # "THE BEER-WARE LICENSE" (Revision 42):
7
 # "THE BEER-WARE LICENSE" (Revision 42):
10
 
12
 
11
 import interstate75
13
 import interstate75
12
 
14
 
13
-
14
 class PicoMatrix:
15
 class PicoMatrix:
15
     def __init__(self, w = 32, h = 32):
16
     def __init__(self, w = 32, h = 32):
16
         self.width = w # x-axis
17
         self.width = w # x-axis

+ 76
- 0
util.py View File

64
             targetPlatform = "tk"
64
             targetPlatform = "tk"
65
 
65
 
66
     return target
66
     return target
67
+
68
+# https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/python_test_tcp/micropython_test_tcp_client.py
69
+def connectToWiFi():
70
+    import network
71
+    import time
72
+    from config import Config
73
+
74
+    # Check if wifi details have been set
75
+    if len(Config.networks) == 0:
76
+        print('Please set wifi ssid and password in config.py')
77
+        return False
78
+
79
+    # Start WiFi hardware
80
+    wlan = network.WLAN(network.STA_IF)
81
+    wlan.active(True)
82
+
83
+    # Look for known networks
84
+    visible = wlan.scan()
85
+    ssid = None
86
+    password = None
87
+    for name, a, b, c, d, e in visible:
88
+        for t_ssid, t_password in Config.networks:
89
+            if name.decode("utf-8") == t_ssid:
90
+                ssid = t_ssid
91
+                password = t_password
92
+                break
93
+    if (ssid == None) or (password == None):
94
+        print("No known network found")
95
+        return False
96
+
97
+    # Start connection
98
+    wlan.connect(ssid, password)
99
+
100
+    # Wait for connect success or failure
101
+    max_wait = 20
102
+    error_count = 20
103
+    while max_wait > 0:
104
+        if wlan.status() >= 3:
105
+            break
106
+        elif wlan.status() < 0:
107
+            wlan.connect(ssid, password)
108
+            error_count -= 1
109
+            if error_count <= 0:
110
+                break
111
+        else:
112
+            max_wait -= 1
113
+        print('waiting for connection...')
114
+        time.sleep(0.5)
115
+
116
+    # Handle connection error
117
+    if wlan.status() != 3:
118
+        print('wifi connection failed %d' % wlan.status())
119
+        return False
120
+    else:
121
+        print('connected')
122
+        status = wlan.ifconfig()
123
+        print('ip = ' + status[0])
124
+
125
+    return True
126
+
127
+def getRequests():
128
+    try:
129
+        # try to get normal python lib
130
+        import requests
131
+        return requests.get
132
+    except:
133
+        # if it fails try the Pi Pico MicroPython implementation
134
+        import urequests as requests
135
+
136
+        # in this case we also need to connect to WiFi first
137
+        if not connectToWiFi():
138
+            return None
139
+
140
+        return requests.get
141
+
142
+    return None

Loading…
Cancel
Save