Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a new link to a page #108

Closed
nke69 opened this issue Feb 24, 2018 · 29 comments
Closed

create a new link to a page #108

nke69 opened this issue Feb 24, 2018 · 29 comments

Comments

@nke69
Copy link
Contributor

nke69 commented Feb 24, 2018

Hello,

I just placed my PH probe SKU SEN0161 and it works perfectly.
My python script tells me the good ph and everything is OK.

capture2

this is my script :

#!/usr/bin/python
# -*- coding: utf-8 -*-
#===============================================================
#                          ph.py
#===============================================================
#Import des fichiers
import calendar;
import time
import datetime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
DEBUG = 1

# Initialisation des variables
datebuff = calendar.timegm(time.gmtime())
ph = [0,0,0,0,0]
adcnum = 0
somme = 0
moyenne=0

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
        if ((adcnum > 7) or (adcnum < 0)):
                return -1
        GPIO.output(cspin, True)
 
        GPIO.output(clockpin, False)  # start clock low
        GPIO.output(cspin, False)     # bring CS low
 
        commandout = adcnum
        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3    # we only need to send 5 bits here
        for i in range(5):
                if (commandout & 0x80):
                        GPIO.output(mosipin, True)
                else:
                        GPIO.output(mosipin, False)
                commandout <<= 1
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
 
        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
                adcout <<= 1
                if (GPIO.input(misopin)):
                        adcout |= 0x1
 
        GPIO.output(cspin, True)
        
        adcout >>= 1       # first bit is 'null' so drop it
        return adcout
		
		
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8	

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)	
		
#Debut du programme
print ("--- Acquisition pH ---")

#Acquisition de la distance à 5 reprises
for x in range(5):
    try:
        trim_pot1 = readadc( adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS )
        ph_mesure = ((trim_pot1 * ( 5000.0 / 1024.0))/1000)*3.3+0.1614
        ph_round = round(ph_mesure,2)
        ph[x] = ph_round
        time.sleep(0.2)
    except KeyboardInterrupt:
        print ("You cancelled the program!")
        GPIO.cleanup()
	
#Récupération des valeurs mini et maxi puis suppression de ces valeurs (valeurs abberrantes)
max0 = max(ph)
min0 = min(ph)
ph.remove(max0)
ph.remove(min0)

#Calcul de la valeur moyenne des 3 valeurs restantes
for y in ph:
 somme = somme + y
 moyenne = somme / 3
moyenne=round(moyenne,2)

#Affichage dans le shell
print ("pH moyen = '%s'" % (moyenne))

#Libération des ports GPIO
GPIO.cleanup()

But how to call the python script in a page (tpl) so that it returns the value of the PH.

I created a new link in the menu on the left with the sensors and a new page called ph.tpl tpl but I'm looking for a moment, the code is not interpreted as if it was php.

capture

I do not know this framework at all, would there be a trick, in the immediate future I would need to display the value of the ph on demand no need db storage or other .

in advance thank you for helping me;)

@theyosh
Copy link
Owner

theyosh commented Feb 24, 2018

It is a bit more work. But I suggest that I create a new branch and test this between us.

But did you add an extra ADC converter to it? Because the analog output cannot be read directly. It so, it that device capable of 8 analog devices? If so, I will also add an extra section to configure the pins for the ADC board

@theyosh
Copy link
Owner

theyosh commented Feb 24, 2018

The template engine is described here: http://bottlepy.org/docs/dev/stpl.html#simpletemplate-syntax

You can run python code in it, but that will be cashed with the data first time requested.. :( So that is not really working I am afraid

@nke69
Copy link
Contributor Author

nke69 commented Feb 24, 2018

No worries and thanks in advance;)

yes of course I placed an adc MCP3008 8-channel 10-bit ADC which is the drawing :
phprobe_bb
the probe is connected to the channel 0 on which the ph value is read

@nke69
Copy link
Contributor Author

nke69 commented Feb 24, 2018

i will try anyway

@theyosh
Copy link
Owner

theyosh commented Feb 24, 2018

I have tried this at home: http://rasp.io/wp-content/uploads/2016/06/RasPiO-Analog-Zero.pdf

But it takes a bit of space... And I need a lot of GPIO pins to test everything :P

@theyosh
Copy link
Owner

theyosh commented Feb 24, 2018

Ok, created a new branch. 'feature/ph' . Check it out to test code for supporting the PH device. There are some assumptions done from my side and you have to do some extra steps to get it working.

Make sure the software is not running during this update.

  1. git pull
  2. git checkout feature/ph
  3. sudo ./install.sh (will install new packages)
  4. sudo ./start.sh

Clear browser cache and go to the interface. Now you should be able to add a PH device at the settings page. Use the analog port number as address. Add the sensor and cross your fingers :)

The assumption I have done is that your setup is compatible with gpiozero. Your code does seems to use the same GPIO ports, so I am hoping that it will work directly out the box.

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

Aie,
after restart the server crash with this error :

2018-02-25 00:56:48,000 - INFO - terrariumWrapper - Starting TerrariumPI server at location: http://192.168.1.61:8090 ...
_Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumPI.py", line 11, in <module>
    from terrariumEngine import terrariumEngine
  File "/usr/local/lib/python2.7/dist-packages/gevent/builtins.py", line 93, in __import__
    result = _import(*args, **kwargs)
  File "/home/pi/TerrariumPI/terrariumEngine.py", line 22, in <module>
    from terrariumSensor import terrariumSensor
  File "/usr/local/lib/python2.7/dist-packages/gevent/builtins.py", line 93, in __import__
    result = _import(*args, **kwargs)
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 16, in <module>
    from gpiozero import MCP3008
  File "/usr/local/lib/python2.7/dist-packages/gevent/builtins.py", line 93, in __import__
    result = _import(*args, **kwargs)_
**ImportError: No module named gpiozero**

i run pip install gpiozero

restart

and have this like error :

2018-02-25 01:00:55,000 - INFO - terrariumWrapper - Starting TerrariumPI server at location: http://192.168.1.61:8090 ...
The GPIO channel has not been set up as an OUTPUT
2018-02-25 01:01:14,549 - ERROR   - terrariumSensor    - Error updating hc-sr04 distance sensor 'Hauteur Eau' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 157, in update
    GPIO.output(terrariumUtils.to_BCM_port_number(self.sensor_address['TRIG']), False)
RuntimeError: The GPIO channel has not been set up as an OUTPUT
2018-02-25 01:01:18,000 - INFO - terrariumWebserver - Running webserver at :::8090
The GPIO channel has not been set up as an OUTPUT
2018-02-25 01:01:19,260 - ERROR   - terrariumSensor    - Error updating hc-sr04 distance sensor 'Hauteur Eau' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 157, in update
    GPIO.output(terrariumUtils.to_BCM_port_number(self.sensor_address['TRIG']), False)
RuntimeError: The GPIO channel has not been set up as an OUTPUT

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

i've entered 0 as adress but nothing appear ..

@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Ok, try a new git pull and restart... Not sure if the error above will stop getting data from other sensors, but I will think so

Also, did you run the install script? That should install gpiozero package

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

yes the first time I did the install but the package was missing, I did it manually and the start was able to start at that time ..

I have just restarted but it seems that there are still some errors .. I will check later and tell you what I see ..

Anyway thank you very much for your time and your involvement in this feature :)

theyosh added a commit that referenced this issue Feb 25, 2018
@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Just did another commit... this should make it work :D One more try tonight?

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

here are the last mistakes I have:

I'm going to sleep but thank you in any case :)

2018-02-25 01: 51: 51,000 - INFO - terrariumWrapper - Starting TerrariumPI server at location: http://192.168.1.61:8090 ...
variable local 'pulse_start' referenced before assignment
2018-02-25 01: 52: 09,827 - ERROR - terrariumSensor - Error update hc-sr04 distance sensor 'Water Height' with error:
Traceback (most recent call last):
File "/home/pi/TerrariumPI/terrariumSensor.py", line 167, in update
pulse_duration = pulse_end - pulse_start
UnboundLocalError: local variable 'pulse_start' referenced before assignment
2018-02-25 01: 52: 14,000 - INFO - terrariumWebserver - Running webserver at ::: 8090
variable local 'pulse_start' referenced before assignment
2018-02-25 01: 52: 14,926 - ERROR - terrariumSensor - Error editing hc-sr04 distance sensor 'Water Height' with error:
Traceback (most recent call last):
File "/home/pi/TerrariumPI/terrariumSensor.py", line 167, in update
pulse_duration = pulse_end - pulse_start
UnboundLocalError: local variable 'pulse_start' referenced before assignment
variable local 'pulse_start' referenced before assignment
2018-02-25 01: 53: 16,402 - ERROR - terrariumSensor - Error updating hc-sr04 distance sensor 'Water Height' with error:
Traceback (most recent call last):
File "/home/pi/TerrariumPI/terrariumSensor.py", line 167, in update
pulse_duration = pulse_end - pulse_start
UnboundLocalError: local variable 'pulse_start' referenced before assignment
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 862, in handle
return route.call (** args)
File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 1740, in wrapper
rv = callback (* a, ** ka)
File "/home/pi/TerrariumPI/terrariumWebserver.py", line 43, in webserver_headers
return fn (* args, ** kwargs)
File "/home/pi/TerrariumPI/terrariumWebserver.py", line 81, in wrapper
return func (* a, ** ka)
File "/home/pi/TerrariumPI/terrariumWebserver.py", line 255, in update_api_call
result ['ok'] = self .
terrariumEngine.set_config (path, postdata, request.files)
File "/home/pi/TerrariumPI/terrariumEngine.py", line 915, in set_config
update_ok = self.set_sensors_config (data)
File "/home/pi/TerrariumPI/terrariumEngine.py", line 517, in set_sensors_config
self .
_ load_sensors (data)
File "/home/pi/TerrariumPI/terrariumEngine.py", line 140, in load_sensors
self .
UNIT_TYPE)
File "/home/pi/TerrariumPI/terrariumSensor.py", line 73, in init
self.update ()
File "/home/pi/TerrariumPI/terrariumSensor.py", line 129, in update
old_current = self.get_current ()
File "/home/pi/TerrariumPI/terrariumSensor.py", line 342, in get_current
indicator = self.get_indicator (). lower ()
AttributeError: 'NoneType' object has no attribute 'lower'

theyosh added a commit that referenced this issue Feb 25, 2018
@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Do a git pull and try again? We are getting close to the working code

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

HEllo Yosh,

this is the alert i have in screen :

pi@PIFish:~/TerrariumPI $ screen -r
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 183, in update
    current = round(sum(values[1,-1]) / 3.0,2)
TypeError: list indices must be integers, not tuple
2018-02-25 16:26:33,000 - INFO - terrariumWebserver - Running webserver at :::8090
list indices must be integers, not tuple
2018-02-25 16:26:35,183 - ERROR   - terrariumSensor    - Error updating sku-sen0161 ph sensor 'ph' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 183, in update
    current = round(sum(values[1,-1]) / 3.0,2)
TypeError: list indices must be integers, not tuple
list indices must be integers, not tuple
2018-02-25 16:27:06,286 - ERROR   - terrariumSensor    - Error updating sku-sen0161 ph sensor 'ph' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 183, in update
    current = round(sum(values[1,-1]) / 3.0,2)
TypeError: list indices must be integers, not tuple
The GPIO channel has not been set up as an OUTPUT
2018-02-25 16:27:07,047 - ERROR   - terrariumSensor    - Error updating hc-sr04 distance sensor 'Hauteur Eau' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 157, in update
    GPIO.output(terrariumUtils.to_BCM_port_number(self.sensor_address['TRIG']), False)
RuntimeError: The GPIO channel has not been set up as an OUTPUT
list indices must be integers, not tuple
2018-02-25 16:27:37,887 - ERROR   - terrariumSensor    - Error updating sku-sen0161 ph sensor 'ph' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 183, in update
    current = round(sum(values[1,-1]) / 3.0,2)
TypeError: list indices must be integers, not tuple
The GPIO channel has not been set up as an OUTPUT
2018-02-25 16:27:41,168 - ERROR   - terrariumSensor    - Error updating hc-sr04 distance sensor 'Hauteur Eau' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 157, in update
    GPIO.output(terrariumUtils.to_BCM_port_number(self.sensor_address['TRIG']), False)
RuntimeError: The GPIO channel has not been set up as an OUTPUT

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

New log after restart :

2018-02-25 16:35:12,000 - INFO - terrariumWrapper - Starting TerrariumPI server at location: http://192.168.1.61:8090 ...
/home/pi/.local/lib/python2.7/site-packages/gpiozero/pins/pi.py:132: SPISoftwareFallback: failed to initialize hardware SPI, falling back to software (error was: failed to import spidev)
  'software (error was: %s)' % str(e)))
list indices must be integers, not tuple
2018-02-25 16:35:27,457 - ERROR   - terrariumSensor    - Error updating sku-sen0161 ph sensor 'ph' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 183, in update
    current = round(sum(values[1,-1]) / 3.0,2)
TypeError: list indices must be integers, not tuple
2018-02-25 16:35:35,000 - INFO - terrariumWebserver - Running webserver at :::8090
list indices must be integers, not tuple
2018-02-25 16:35:36,753 - ERROR   - terrariumSensor    - Error updating sku-sen0161 ph sensor 'ph' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 183, in update
    current = round(sum(values[1,-1]) / 3.0,2)
TypeError: list indices must be integers, not tuple

@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Ok, it looks like you have some mixed up code. All errors about hc-sr04 should be fixed, due to the fix in the master branch. So stop the program and remove all the *.pyc files and make sure you have the latest code.

Because I did a small change yesterday late. So please update to latest version on the feature/ph branch. Remove the *.pyc files to make sure new .py files are used.

And see what the output will be

@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Just added some debug. Please do another git pull

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

hum i have this :

   GPIO.output(terrariumUtils.to_BCM_port_number(self.sensor_address['TRIG']), False)
RuntimeError: The GPIO channel has not been set up as an OUTPUT
Start measurement sku-sen0161
[]
Got voltage: 0.420615534929
[0.16806729321873473]
Got voltage: 0.418661455789
[0.16806729321873473, 0.16838215948644356]
Got voltage: 0.409868099658
[0.16806729321873473, 0.16838215948644356, 0.1683191862329018]
Got voltage: 0.42843185149
[0.16806729321873473, 0.16838215948644356, 0.1683191862329018, 0.1683191862329018]
Got voltage: 0.42550073278
[0.16806729321873473, 0.16838215948644356, 0.1683191862329018, 0.1683191862329018, 0.16817749641243282]
Sorted values
[0.16806729321873473, 0.16817749641243282, 0.1683191862329018, 0.1683191862329018, 0.16838215948644356]
list indices must be integers, not tuple
2018-02-25 16:48:40,061 - ERROR   - terrariumSensor    - Error updating sku-sen0161 ph sensor 'valeur P  H' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 190, in update
    current = round((sum(values[1,-1]) / 3.0),2)
TypeError: list indices must be integers, not tuple
The GPIO channel has not been set up as an OUTPUT
2018-02-25 16:48:40,282 - ERROR   - terrariumSensor    - Error updating hc-sr04 distance sensor 'Hauteur Eau' with error:
Traceback (most recent call last):
  File "/home/pi/TerrariumPI/terrariumSensor.py", line 157, in update
    GPIO.output(terrariumUtils.to_BCM_port_number(self.sensor_address['TRIG']), False)
RuntimeError: The GPIO channel has not been set up as an OUTPUT

@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Another try? Updated the code again. Now I hope the errors will be gone

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

git pull and git ph/mesure ./install and reboot . i'm waiting ;)

@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

oh... the install script is not needed now.... that was only once...
we will wait

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

i've got a Current PH to 0.17 ..
with my ither python script i've got 7.12 ?

what's the difference ?

@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

I think the way the voltage is measured... so can you test with your working script what the output is of the variable trim_pot1 = readadc( adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS )

I use a different library to read out the voltage. Could be that we have to invert the value

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

ok I will look a little on my side what it is;) I come back to you for the rest .. thank you for your time for me

theyosh added a commit that referenced this issue Feb 25, 2018
@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

found it... git pull again and it should be a better value :D

your script returned millivolt values. Mine does volts. That is a small difference :D

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

Absolutely PERFECT ! 🥇
Many thanks for your time and patience for this hack
value is effectively correct now ;)

@nke69
Copy link
Contributor Author

nke69 commented Feb 25, 2018

I'm really happy that it works and offers you virtually a good bottle of old Scottish wisky :) health! :)

capture

@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Thanks! And cool that it works. I will update my own installation as well to test if it is still working for me. I guess it will. Then it will be merged to the master branch.

The debug code will then also be removed. And maybe in the future I can do something with this on the environment part. For now it is just measurement.

Also big thanks to you for testing this and giving feedback. Together we fixed it in 24 hours with sleeping in between :) 👍

theyosh added a commit that referenced this issue Feb 25, 2018
@theyosh
Copy link
Owner

theyosh commented Feb 25, 2018

Merged into master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants