Difference between revisions of "Tutorial Network"
Line 51: | Line 51: | ||
== Code Example: netvars.py == | == Code Example: 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'> | <syntaxhighlight lang="python" line='line'> | ||
import network | import network |
Revision as of 00:22, 15 August 2020
Contents
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)
Instructional Videos
Writing to the OLED Display over I2C
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: 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