Prueba
  1. Lectura de datos desde un Arduino

#

  1. Basado en el ejemplo AnalogInOutSerial de Arduino (CC-BY-SA 3.0)
  2. http://arduino.cc/en/Tutorial/AnalogInOutSerial

#

  1. 2014 Juan Luis Cano <moc.liamg|100ulnauj#moc.liamg|100ulnauj>

import time
import requests
try:
import serial
arduino = serial.Serial('/dev/ttyACM1', baudrate=9600, timeout=1.0)

# Nota: provocamos un reseteo manual de la placa para leer desde
# el principio, ver http://stackoverflow.com/a/21082531/554319
arduino.setDTR(False)
time.sleep(1)
arduino.flushInput()
arduino.setDTR(True)

except (ImportError, serial.SerialException):
# No hay módulo serial o placa Arduino disponibles
import io

class FakeArduino(io.RawIOBase):
"""Clase para representar un "falso Arduino"
"""
def readline(self):
time.sleep(0.1)
return b'sensor = 0\toutput = 0\r\n'

arduino = FakeArduino()

  1. Con la sentencia with el arduino se cierra automáticamente, ver
  2. http://docs.python.org/3/reference/datamodel.html#context-managers

with arduino:
while True:
try:
# En Python 3 esta función devuelve un objeto bytes, ver
# http://docs.python.org/3/library/stdtypes.html#typebytes
line = arduino.readline()
# Con errors='replace' se evitan problemas con bytes erróneos, ver
# http://docs.python.org/3/library/stdtypes.html#bytes.decode
# Con end='' se evita un doble salto de línea
var=0
a= str(line.decode('ascii', errors='replace'))
# print(line.decode('ascii', errors='replace'), end='')
print(a)
if a in ['Salida']:
headers = {
'Content-Type': 'application/json',
}
params = (
('userToken', '4oceb92f1sc6p66cf0svrgsoro'),
)

data = '{"sensorId":"58ff93867be12b532d5053f9", "captureTypeName": "SalidaPersonas", "value":"1", "captureDate":"2017-03-05 5:05:30"}'

requests.post('https://bucket.usantotomas.edu.co:8443/api/sensors/58ff93867be12b532d5053f9/captures', headers=headers, params=params, data=data)

#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# requests.post('https://bucket.usantotomas.edu.co:8443/api/sensors/58ff93867be12b532d5053f9/captures?userToken=4oceb92f1sc6p66cf0svrgsoro', headers=headers, data=data)
elif a in ['Ingres']:
var=var-1
headers = {
'Content-Type': 'application/json',
}

params = (
('userToken', '4oceb92f1sc6p66cf0svrgsoro'),
)

data = '{"sensorId":"58ff93867be12b532d5053f9", "captureTypeName": "IngresoPersonas", "value": "value":"1", "captureDate":"2017-03-05 5:05:30"}'

requests.post('https://bucket.usantotomas.edu.co:8443/api/sensors/58ff93867be12b532d5053f9/captures', headers=headers, params=params, data=data)
else:
print("nada"),
#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# requests.post('https://bucket.usantotomas.edu.co:8443/api/sensors/58ff93867be12b532d5053f9/captures?userToken=4oceb92f1sc6p66cf0svrgsoro', headers=headers, data=data)

except KeyboardInterrupt:
print("Exiting")
break

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License