Tutorial Network: Difference between revisions
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
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!
#Example usage
#import module
from netvars import setNetVar, getNetVar, initNet
# # assuming there is a network with ssid hotspot1 and password 123456789
# connect to wifi
initNet("hotspot1", "123456789")
# set the variale with the name test222 to the value valTest222
setNetVar("test222", "valTest222")
# read the variable test222 from the server and print it
a=getNetVar("test222")
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.
import network
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("............")
import socket
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
retStr =""
while True:
data = s.recv(500)
if data:
# this would be all
# retStr = retStr +str(data, 'utf8')
# Only the last line
retStr = str(data, 'utf8')
#print(str(data, 'utf8'), end='')
else:
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')
_, retVar0 = reg1.split(resStr)
retVar = reg2.split(retVar0)
return retVar[0]
#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