nodesign / weio

weio
http://we-io.net
128 stars 35 forks source link

JS : Sensors use #277

Closed qleroux closed 7 years ago

qleroux commented 7 years ago

Hi ! Could you add a way to call for sensors values from JS (HC-SR04, DHT11, ...) ? Like you can do in PY, I don't know if that's possible. HCSR04(0,1).distCentimeters()

Cheers !

manuio commented 7 years ago

Hi @qlroux, You can use serverPush() as here: https://github.com/nodesign/weio/blob/master/examples/pythonAndJS/serverPush/main.py#L22 to send data from python to JS. You need to previously define your JS callback as here: https://github.com/nodesign/weio/blob/master/examples/pythonAndJS/serverPush/index.html#L20

Ciao!

qleroux commented 7 years ago

Hi @manuIO ! Thanks a lot, I've tried and It works perfectly ! It doesn't seams to be as speed as printing the results in PY, I guess it works just fine if you don't send it too often.

Though I just can't manage to send two messages. Do you happend to know why ?

from weioLib.weio import *
from things.input.distance.HCSR04 import HCSR04

def setup():
    attach.process(myProcess)

def myProcess():
    while True:
        print HCSR04(0,1).distCentimeters()
        serverPush("HC-SR04", sensor.distCentimeters())
        delay(100)

    while True:
        data = dhtRead(8)
        serverPush("DHT11", data)
        delay(5000)
ukicar commented 7 years ago

Hello, That's because you have 2 while loops! Your program is prisoner of the first loop so it will never reach the second one.

However you can launch two separate processes.

from weioLib.weio import *
from things.input.distance.HCSR04 import HCSR04

def setup():
    attach.process(firstProc)
    attach.process(secondProc)

def firstProc():
    while True:
        print HCSR04(0,1).distCentimeters()
        serverPush("HC-SR04", sensor.distCentimeters())
        delay(100)

def secondProc():
    while True:
        data = dhtRead(8)
        serverPush("DHT11", data)
        delay(5000)
qleroux commented 7 years ago

Thanks a lot Uros, that's really healpfull ! I'm having a lot a fun with WeIO. Cheers