GadgetData
I am setting up a Raspberry Pi (call it "the gadget") that will read Property:GadgetTestProperty encoded on this page. If the value is between 0 and 10 it will drive a servo in a specific way.
Setting the value
Setting Property:GadgetTestProperty to 8
- SMW could encode more, but for now let's have one so my gadget doesn't have to error check.
API code to read the value
The following will query what the value of Property:GadgetTestProperty for this page:
- https://sandbox.semantic-mediawiki.org/w/api.php?action=ask&query=[[GadgetData]]|?GadgetTestProperty&format=json
The following will return all properties for this page:
- https://sandbox.semantic-mediawiki.org/w/api.php?action=browsebysubject&subject=GadgetData&format=json
Python code that reads the value and moves servo
import pigpio
import time
import json
import requests #used to grab the data at the URL
import time #for sleeping
respURL = "https://sandbox.semantic-mediawiki.org/w/api.php?action=ask&query=GadgetData|?GadgetTestProperty&format=json"
pollDelay = 10 # the delay in s between polls of respURL
timeOneUnit = 1 # time it takes to travel DOWN one unit of time - empirical
upRatio = 1.28 # the ratio of time it takes to go up rather than down - empirical
tapeMax = 6.5 # 6.5s goes to not quite the max of red tape
dataMax = 10 # show value between 0 and this, ie divide the tapeMax by this to get the distance/time per unit
def tapeHold():
print("tapeHold")
pi.hardware_PWM(18,47,71000) # parameters are a mix between spec and empirical
def tapeUp(howMuch):
print("tapeUp by " + str(howMuch))
pi.hardware_PWM(18,47,61000) # parameters are a mix between spec and empirical
time.sleep(howMuch * tapeMax/dataMax * upRatio)
tapeHold()
def tapeDown(howMuch):
print("tapeDown by " + str(howMuch))
pi.hardware_PWM(18,46,78300) # parameters are a mix between spec and empirical
time.sleep(howMuch * tapeMax/dataMax)
tapeHold()
- initialize
pi = pigpio.pi()
curr = 0
newVal = 0
while True:
response = requests.get(respURL)
AllData = (response.text)
JSONData = json.loads(AllData)
KeyValue = (JSONData["query"]["results"]["GadgetData"]["printouts"]["GadgetTestProperty"])
if str(KeyValue) == "[]": # that's what we get when there is no value or an out of range value
print ("Not getting good data from page, assuming current value stays")
newVal = curr
else:
newVal = (KeyValue[0])
if newVal == curr:
print("same")
elif newVal > curr:
tapeUp(newVal-curr)
curr = newVal
elif newVal < curr:
tapeDown(curr-newVal)
curr = newVal
else:
print("this should never happen")
time.sleep(pollDelay)