Displaying data from a python script

Hi everybody, I just started with OpenHybrid and for a proof of concept I wanted to display data readings from a python script on a webpage. I’m a beginner with all this, with very limited coding knowledge.

OpenHybrid is running on a raspberry pi B+ (Jessie) and working fine so far.
I created an object, which is recognized and displayed correctly.
On the other hand I installed a DHT22 humidity sensor. Temperature and humidity can be read via python script (based on the adafruit test file):

#!/usr/bin/python
#simpletest.py
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# edit...
import sys
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
   print 'Temp:{0:0.1f}*C  Humidity:{1:0.1f}%'.format(temperature, humidity)
else:
   print 'Failed to obtain a reading. Try again!'

My question is, how can I display this data, as I cannot include python call in the index.html? I tried to create an php-file (index.php) which called the script, but this does not seem to work either, as the php-script is not recognized. Code here:

<?php passthru("sudo /home/pi/DHT22read.py"); ?>

Any ideas, on how I could display the information within the index.html?

Hi @elaiel , welcome to the forum :slightly_smiling:

You need to pass the values obtained from the sensor through python script to HybridObject ,
you can do this by using python-shell .
which calls a python script within a nodejs code.
Once you send the values for I/O point to the HybridObject server like this , you can then get the required I/O data in the AR webpage by calling the .read() js function ,
you can use this for reference http://openhybrid.org/reference.html
also check out this PI interface in the hardware interfaces folder https://github.com/openhybrid/object.git
If you are not familiar with nodejs do ask, we might be able to help you

Hi @V_Mohammed_Ibrahim
thanks for the warm welcome and the quick reply. I’ll try as you suggested, but I have to read up on nodeJS a bit first (not coding background).
Thanks :slightly_smiling:

@elaiel it will be better if you try to learn nodejs as you try to understand the interface i mentioned in above post

Hi @elaiel,

i’m not familiar with the DHT22 sensor but it seems there exists a nodejs library to access it https://github.com/momenso/node-dht-sensor

You should check out the beta_hardwareInterfaces branch of OpenHybrid and implement your own hardware interface. There already is some documentation available on how to do that: https://github.com/openhybrid/documentation/blob/master/10_For_Developers/01_Hardware_Interfaces.md
@valentin http://documentation.openhybrid.org/ still seems to be down…

As @V_Mohammed_Ibrahim said you could also take a look at the raspberry_pi hardware interface to get an idea on how it works. But it think you will have to implement your own hardware interface because the raspberry pi hardware interface already present only enables you to read/write values directly from the pins. It uses the onOff library but in your case it is probably better to use the dht library mentioned earlier.

Edit: If you only want to display data and don’t want to expose the humidity as IOPoint you could also do something like creating a WebSocket server or a REST interface within your python code which exposes the humidity and connect to that WebSocket server or REST interface from inside your index.html. For example see https://github.com/dpallot/simple-websocket-server

1 Like

Yea , using full nodejs code will be better than having ,python-nodejs mixed code , unless it is unavoidale