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

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

+ 16
- 5
net.py View File

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

+ 2
- 1
pico.py View File

@@ -1,5 +1,7 @@
1 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 7
 # "THE BEER-WARE LICENSE" (Revision 42):
@@ -10,7 +12,6 @@
10 12
 
11 13
 import interstate75
12 14
 
13
-
14 15
 class PicoMatrix:
15 16
     def __init__(self, w = 32, h = 32):
16 17
         self.width = w # x-axis

+ 76
- 0
util.py View File

@@ -64,3 +64,79 @@ def getTarget():
64 64
             targetPlatform = "tk"
65 65
 
66 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