Difference between revisions of "Tutorial Network"
Line 56: | Line 56: | ||
print(a) # will print valTest222 | print(a) # will print valTest222 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | == Code Example for use in Jupyter Notebook: updated netvars.py == | ||
+ | This has our server address hard coded - if you run it on your server you have to adapt the URLs. | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | import network, socket | ||
+ | |||
+ | def initNet(ssid, passwd): | ||
+ | wlan = network.WLAN(network.STA_IF) | ||
+ | wlan.active(True) | ||
+ | if not wlan.isconnected(): | ||
+ | print('connecting to network...') | ||
+ | wlan.connect(ssid, passwd) | ||
+ | while not wlan.isconnected(): | ||
+ | |||
+ | pass | ||
+ | print('network config:', wlan.ifconfig()) | ||
+ | |||
+ | def http_get(url): | ||
+ | #print(url) | ||
+ | #print("............") | ||
+ | _, _, host, path = url.split('/', 3) | ||
+ | addr = socket.getaddrinfo(host, 80)[0][-1] | ||
+ | print ("fetch from: ", addr) | ||
+ | s = socket.socket() | ||
+ | s.connect(addr) | ||
+ | s.send(bytes('GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) | ||
+ | #print(s) | ||
+ | retStr ="" | ||
+ | while True: | ||
+ | data = s.recv(1024) | ||
+ | #print(data) | ||
+ | |||
+ | if data: | ||
+ | # this would be all | ||
+ | #retStr = retStr +str(data, 'utf8') | ||
+ | # Only the last line | ||
+ | retStr = str(data, 'utf8') | ||
+ | #print(str(data, 'utf8'), end='') | ||
+ | #print(retStr) | ||
+ | break | ||
+ | else: | ||
+ | print("---data") | ||
+ | #print(retStr) | ||
+ | break | ||
+ | s.close() | ||
+ | return retStr | ||
+ | |||
+ | |||
+ | def setNetVar(varName, varVal): | ||
+ | urlStr = "https://ubicomp.net/sw/db1/var2db.php?varName=" + str(varName) + "&varValue=" + str(varVal) | ||
+ | #print(urlStr) | ||
+ | # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test3&varValue=14") | ||
+ | # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test222&varValue=valTest222") | ||
+ | http_get(urlStr) | ||
+ | |||
+ | |||
+ | def getNetVar(varName): | ||
+ | urlStr = "https://ubicomp.net/sw/db1/var2db.php?varName=" + str(varName) | ||
+ | #print(urlStr) | ||
+ | # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test3&varValue=14") | ||
+ | # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test222&varValue=valTest222") | ||
+ | resStr = http_get(urlStr) | ||
+ | # import re # standrad python | ||
+ | import ure | ||
+ | #_, retVar0 = ure.split('\r\n\r\n|\n\n',resStr) | ||
+ | # retVar = ure.split(' |\n|\r',retVar0) | ||
+ | reg1 = ure.compile('\r\n\r\n|\n\n') | ||
+ | reg2 = ure.compile(' |\n|\r') | ||
+ | #print(reg1) | ||
+ | #print(reg1.split(resStr)) | ||
+ | _, retVar0, _ = reg1.split(resStr) | ||
+ | #print(retVar0) | ||
+ | retVar = reg2.split(retVar0) | ||
+ | #print(retVar) | ||
+ | return retVar[2] | ||
+ | |||
+ | #Example usage | ||
+ | # from netvars import setNetVar, getNetVar, http_get, initNet | ||
+ | # # assuming there is a network with ssid hotspot1 and password 123456789 | ||
+ | # initNet("hotspot1", "123456789") | ||
+ | # setNetVar("test222", "valTest222") | ||
+ | # a=getNetVar("test222") | ||
+ | # print(a) # will print valTest222 | ||
+ | |||
+ | # # assuming there is a network with ssid hotspot1 and password 123456789 | ||
+ | # connect to wifi | ||
+ | initNet("as62", "VivienPetraAlbrecht") | ||
+ | # set the variale with the name test222 to the value valTest222 | ||
+ | setNetVar("1000_albrecht_test222", "XXXalbrecht_valTest222") | ||
+ | # read the variable test222 from the server and print it | ||
+ | a=getNetVar("1000_albrecht_test222") | ||
+ | print(a) # will print valTest222 | ||
+ | </syntaxhighlight> | ||
+ | |||
== Code Example: netvars.py == | == Code Example: netvars.py == | ||
Line 130: | Line 225: | ||
# print(a) # will print valTest222 | # print(a) # will print valTest222 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
== Debugging Network Variables == | == Debugging Network Variables == | ||
For debugging you can see all the variables in our test installation by accessing: | For debugging you can see all the variables in our test installation by accessing: | ||
https://ubicomp.net/sw/db1/var2db.php | https://ubicomp.net/sw/db1/var2db.php |
Revision as of 10:07, 4 April 2024
Write and Read Network Variables
In this part of the tutorial, we explain how to write variables to the server and how to read them again. This is a very simple module/library with very basic functionality to connect objects by using shared variables in the network
Success criteria
- you can write a variable to the network
- you can read a variable from the network
- you understand how the network variables work
- you understand the limitations of the module (only strings, everyone can read and write all variables, naming conditions)
Required Module and Files
- We use netvars.py
- OPTIONAL: you can download the files to set up your own server. This is in php and requires mySQL. https://www.sketching-with-hardware.org/files/netvarsServer.zip (see readme for details)
Related Components
The components are related to the LMUBox. For more components, see the Hardware List. Many of the pages on actuators and sensor include additional examples.
Microcontroller
- ESP32 Web Kit with integrated OLED Display from Heltec
Instructional Videos
Connecting to WiFI and using Network Variables
In this video on youtube (8:52) we show how to Connect the ESP32/ESP8266 to the Internet and how to use the netvars.py module to use variables stored on the server.
Using the uPyCraft IDE to upload files
In this video on youtube (6:48) we show how to upload an run python files with the uPyCraft IDE: https://youtu.be/MXfj_gWf0z4
Code Examples
Code Example: using netvars.py
Variable names can only be strings of letters and numbers without space Variable values can only be strings of letters and numbers without space (numbers will be converted to strings), spaces will give an error You are welcome to improve the module!
1 #Example usage
2
3 #import module
4 from netvars import setNetVar, getNetVar, initNet
5
6 # # assuming there is a network with ssid hotspot1 and password 123456789
7 # connect to wifi
8 initNet("hotspot1", "123456789")
9
10 # set the variale with the name test222 to the value valTest222
11 setNetVar("test222", "valTest222")
12
13 # read the variable test222 from the server and print it
14 a=getNetVar("test222")
15 print(a) # will print valTest222
Code Example for use in Jupyter Notebook: updated netvars.py
This has our server address hard coded - if you run it on your server you have to adapt the URLs.
1 import network, socket
2
3 def initNet(ssid, passwd):
4 wlan = network.WLAN(network.STA_IF)
5 wlan.active(True)
6 if not wlan.isconnected():
7 print('connecting to network...')
8 wlan.connect(ssid, passwd)
9 while not wlan.isconnected():
10
11 pass
12 print('network config:', wlan.ifconfig())
13
14 def http_get(url):
15 #print(url)
16 #print("............")
17 _, _, host, path = url.split('/', 3)
18 addr = socket.getaddrinfo(host, 80)[0][-1]
19 print ("fetch from: ", addr)
20 s = socket.socket()
21 s.connect(addr)
22 s.send(bytes('GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
23 #print(s)
24 retStr =""
25 while True:
26 data = s.recv(1024)
27 #print(data)
28
29 if data:
30 # this would be all
31 #retStr = retStr +str(data, 'utf8')
32 # Only the last line
33 retStr = str(data, 'utf8')
34 #print(str(data, 'utf8'), end='')
35 #print(retStr)
36 break
37 else:
38 print("---data")
39 #print(retStr)
40 break
41 s.close()
42 return retStr
43
44
45 def setNetVar(varName, varVal):
46 urlStr = "https://ubicomp.net/sw/db1/var2db.php?varName=" + str(varName) + "&varValue=" + str(varVal)
47 #print(urlStr)
48 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test3&varValue=14")
49 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test222&varValue=valTest222")
50 http_get(urlStr)
51
52
53 def getNetVar(varName):
54 urlStr = "https://ubicomp.net/sw/db1/var2db.php?varName=" + str(varName)
55 #print(urlStr)
56 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test3&varValue=14")
57 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test222&varValue=valTest222")
58 resStr = http_get(urlStr)
59 # import re # standrad python
60 import ure
61 #_, retVar0 = ure.split('\r\n\r\n|\n\n',resStr)
62 # retVar = ure.split(' |\n|\r',retVar0)
63 reg1 = ure.compile('\r\n\r\n|\n\n')
64 reg2 = ure.compile(' |\n|\r')
65 #print(reg1)
66 #print(reg1.split(resStr))
67 _, retVar0, _ = reg1.split(resStr)
68 #print(retVar0)
69 retVar = reg2.split(retVar0)
70 #print(retVar)
71 return retVar[2]
72
73 #Example usage
74 # from netvars import setNetVar, getNetVar, http_get, initNet
75 # # assuming there is a network with ssid hotspot1 and password 123456789
76 # initNet("hotspot1", "123456789")
77 # setNetVar("test222", "valTest222")
78 # a=getNetVar("test222")
79 # print(a) # will print valTest222
80
81 # # assuming there is a network with ssid hotspot1 and password 123456789
82 # connect to wifi
83 initNet("as62", "VivienPetraAlbrecht")
84 # set the variale with the name test222 to the value valTest222
85 setNetVar("1000_albrecht_test222", "XXXalbrecht_valTest222")
86 # read the variable test222 from the server and print it
87 a=getNetVar("1000_albrecht_test222")
88 print(a) # will print valTest222
Code Example: netvars.py
This has our server address hard coded - if you run it on your server you have to adapt the URLs.
1 import network
2
3 def initNet(ssid, passwd):
4 wlan = network.WLAN(network.STA_IF)
5 wlan.active(True)
6 if not wlan.isconnected():
7 print('connecting to network...')
8 wlan.connect(ssid, passwd)
9 while not wlan.isconnected():
10
11 pass
12 print('network config:', wlan.ifconfig())
13
14 def http_get(url):
15 #print(url)
16 #print("............")
17 import socket
18 _, _, host, path = url.split('/', 3)
19 addr = socket.getaddrinfo(host, 80)[0][-1]
20 s = socket.socket()
21 s.connect(addr)
22 s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
23 retStr =""
24 while True:
25 data = s.recv(500)
26 if data:
27 # this would be all
28 # retStr = retStr +str(data, 'utf8')
29 # Only the last line
30 retStr = str(data, 'utf8')
31 #print(str(data, 'utf8'), end='')
32 else:
33 break
34 s.close()
35 return retStr
36
37
38 def setNetVar(varName, varVal):
39 urlStr = "https://ubicomp.net/sw/db1/var2db.php?varName=" + str(varName) + "&varValue=" + str(varVal)
40 # print(urlStr)
41 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test3&varValue=14")
42 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test222&varValue=valTest222")
43 http_get(urlStr)
44
45
46 def getNetVar(varName):
47 urlStr = "https://ubicomp.net/sw/db1/var2db.php?varName=" + str(varName)
48 # print(urlStr)
49 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test3&varValue=14")
50 # http_get("https://ubicomp.net/sw/db1/var2db.php?varName=test222&varValue=valTest222")
51 resStr = http_get(urlStr)
52 # import re # standrad python
53 import ure
54 #_, retVar0 = ure.split('\r\n\r\n|\n\n',resStr)
55 # retVar = ure.split(' |\n|\r',retVar0)
56 reg1 = ure.compile('\r\n\r\n|\n\n')
57 reg2 = ure.compile(' |\n|\r')
58 _, retVar0 = reg1.split(resStr)
59 retVar = reg2.split(retVar0)
60 return retVar[0]
61
62 #Example usage
63 # from netvars import setNetVar, getNetVar, http_get, initNet
64 # # assuming there is a network with ssid hotspot1 and password 123456789
65 # initNet("hotspot1", "123456789")
66 # setNetVar("test222", "valTest222")
67 # a=getNetVar("test222")
68 # print(a) # will print valTest222
Debugging Network Variables
For debugging you can see all the variables in our test installation by accessing: https://ubicomp.net/sw/db1/var2db.php