Editing Tutorial Network

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
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 225: Line 130:
 
# 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

Please note that all contributions to Sketching with Hardware at LMU Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see My wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)