diff options
Diffstat (limited to 'examples/clients')
| -rw-r--r-- | examples/clients/coap-client.py | 15 | ||||
| -rw-r--r-- | examples/clients/webiopi-client.py | 46 |
2 files changed, 61 insertions, 0 deletions
diff --git a/examples/clients/coap-client.py b/examples/clients/coap-client.py new file mode 100644 index 0000000..eba80e9 --- /dev/null +++ b/examples/clients/coap-client.py | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | from webiopi.protocols.coap import * | ||
| 2 | from time import sleep | ||
| 3 | |||
| 4 | client = COAPClient() | ||
| 5 | client.sendRequest(COAPPost("coap://224.0.1.123/GPIO/25/function/out")) | ||
| 6 | state = True | ||
| 7 | |||
| 8 | while True: | ||
| 9 | response = client.sendRequest(COAPPost("coap://224.0.1.123/GPIO/25/value/%d" % state)) | ||
| 10 | if response: | ||
| 11 | print("Received response:\n%s" % response) | ||
| 12 | state = not state | ||
| 13 | else: | ||
| 14 | print("No response received") | ||
| 15 | sleep(0.5) | ||
diff --git a/examples/clients/webiopi-client.py b/examples/clients/webiopi-client.py new file mode 100644 index 0000000..c277682 --- /dev/null +++ b/examples/clients/webiopi-client.py | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | from webiopi.clients import * | ||
| 2 | from time import sleep | ||
| 3 | |||
| 4 | # Create a WebIOPi client | ||
| 5 | client = PiHttpClient("192.168.1.234") | ||
| 6 | #client = PiMixedClient("192.168.1.234") | ||
| 7 | #client = PiCoapClient("192.168.1.234") | ||
| 8 | #client = PiMulticastClient() | ||
| 9 | |||
| 10 | client.setCredentials("webiopi", "raspberry") | ||
| 11 | |||
| 12 | # RPi native GPIO | ||
| 13 | gpio = NativeGPIO(client) | ||
| 14 | gpio.setFunction(25, "out") | ||
| 15 | state = True | ||
| 16 | |||
| 17 | # DAC named "dac1" | ||
| 18 | dac = DAC(client, "dac1") | ||
| 19 | |||
| 20 | # ADC named "adc1" | ||
| 21 | adc = ADC(client, "adc1") | ||
| 22 | value = 0.0 | ||
| 23 | |||
| 24 | # Temperature sensor named "temp0" | ||
| 25 | temp = Temperature(client, "temp0") | ||
| 26 | |||
| 27 | while True: | ||
| 28 | # toggle digital state | ||
| 29 | state = not state | ||
| 30 | gpio.digitalWrite(25, state) | ||
| 31 | |||
| 32 | # increase analog value | ||
| 33 | value += 0.01 | ||
| 34 | if value > 1.0: | ||
| 35 | value = 0.0 | ||
| 36 | dac.writeFloat(0, value) | ||
| 37 | |||
| 38 | # DAC output 0 is wired to ADC input 1 | ||
| 39 | val = adc.readFloat(1) | ||
| 40 | print("Analog = %.2f" % val) | ||
| 41 | |||
| 42 | # Retrieve temperature | ||
| 43 | t = temp.getCelsius() | ||
| 44 | print("Temperature = %.2f Celsius" % t) | ||
| 45 | |||
| 46 | sleep(1) | ||
