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 12: Line 12:
 
* We use '''[https://www.sketching-with-hardware.org/files/netvars.py netvars.py]'''
 
* We use '''[https://www.sketching-with-hardware.org/files/netvars.py 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)
 
* 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 =
 
= Instructional Videos =
  
== Connecting to WiFI and using Network Variables  ==
+
== 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.  
+
In this video on youtube (22:00) we show how to write text to the OLED Display on the ESP32 module. We then show how to connect a poti to an analog input and display the value in a loop on the display: https://youtu.be/UbxwePvgX-U
  
<youtube>PJ3nQo_Hw3I</youtube>
+
<youtube>UbxwePvgX-U</youtube>
  
 
== Using the uPyCraft IDE to upload files ==
 
== Using the uPyCraft IDE to upload files ==
Line 31: Line 24:
  
 
<youtube>MXfj_gWf0z4</youtube>
 
<youtube>MXfj_gWf0z4</youtube>
 +
  
 
= Code Examples =  
 
= Code Examples =  
Line 56: Line 50:
 
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 ==  
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
Line 225: Line 122:
 
# print(a) # will print valTest222
 
# print(a) # will print valTest222
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== Debugging Network Variables ==
 
For debugging you can see all the variables in our test installation by accessing:
 
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)