SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Mon Oct 16, 2017 4:33 pm

bensimmo wrote:
Mon Oct 16, 2017 4:14 pm
Surfertim,
If you have the raspbian desktop you can install, if not already there, the SenseHat emulator and use that, just import sense_emu instead.
It is designed around Python3 so no need to switch to python2
Thanks for the info, but I was concerned only with getting the python script to work.

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Tue Oct 17, 2017 10:14 am

Hi,
I can get Hello World on the web page.
However if I add the sense hat code , Hello World is not displayed or the Hello World from the sense hat.
So I am wodering if the proposed action in the program for the sense hat is not possible.
To be clear, this code does not work

Code: Select all

#!/usr/bin/python3

from sense_hat import SenseHat

sense = SenseHat()
sense.show_message("Hello World")

#print("Hello World")
This code displays Hello World in the web page.

Code: Select all

#!/usr/bin/python3

# sense_hat import SenseHat

# = SenseHat()
#sense.show_message("Hello World")

print("Hello World")

SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Tue Oct 17, 2017 10:19 am

Change this

Code: Select all

#!/usr/bin/python3
To this

Code: Select all

#!/usr/bin/python

bensimmo
Posts: 1814
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Communicating between sense hats

Tue Oct 17, 2017 11:01 am

SurferTim wrote:
Tue Oct 17, 2017 10:19 am
Change this

Code: Select all

#!/usr/bin/python3
To this

Code: Select all

#!/usr/bin/python
I have to ask, why (if it works)? Given SenseHat (python) is designed around Python3 (and then to be backwards compatible with Python2)
Nothing in that code is Python2 code only.

SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Tue Oct 17, 2017 11:06 am

bensimmo wrote: I have to ask, why (if it works)? Given SenseHat (python) is designed around Python3 (and then to be backwards compatible with Python2)
Nothing in that code is Python2 code only.
Here is why:
With python it runs but reports a "no device" error because I don't have a sense hat.
With python3, it doesn't run. It fails. It aborts when it reaches the SenseHat() function call.

Edit: Are you testing this code with an Apache server using the php code previously posted?

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Tue Oct 17, 2017 12:20 pm

Hi
Yes I am using the php code I previously posted on my Apache server.

Code: Select all

$output=shell_exec("/var/www/html/wp-content/themes/twentysixteen/sense-test.py");
echo $output;

SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Tue Oct 17, 2017 12:21 pm

hawkesley wrote: Hi
Yes I am using the php code I previously posted on my Apache server.

Code: Select all

$output=shell_exec("/var/www/html/wp-content/themes/twentysixteen/sense-test.py");
echo $output;
...and you changed the first line in your python script to python instead of python3?

Edit: We may have a language problem here. How about this?
python = success
python3 = fail

python = good
python3 = bad

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Tue Oct 17, 2017 12:45 pm

Hi,
With the sense hat code commented out, either python or python3 work.
With the sense hat code not commented but Hello World commented,
with either python or python 3 there is no output.

SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Tue Oct 17, 2017 12:52 pm

Here is my /home/pi/python/test.py script

Code: Select all

#!/usr/bin/python

from sense_hat import SenseHat

try:
   sense = SenseHat()
except Exception as e:
        print "ERROR!"
        print(e)
#       exit()

print "<br>end of my python script"
Here is my index.php code

Code: Select all

<html>
<body>
PYTHON TEST<br>
<?php
$output = shell_exec('/home/pi/python/test.py');
echo $output;
?>
</body>
</html>
Here is the output using Chrome:

Code: Select all

PYTHON TEST
ERROR! Cannot detect RPi-Sense FB device
end of my python script 
Edit: If I change the first line to python3 in the python code, I get this

Code: Select all

PYTHON TEST

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Tue Oct 17, 2017 1:41 pm

Hi
I have replicated your code and tried the shebang with python and python3 and I get PYTHON TEST.
So a bit stuck.
For the record my test.py is in a folder SH-test which is in /var/www/html/
I made test.py chmod +x
Python test.py

Code: Select all

#!/usr/bin/python

from sense_hat import SenseHat

try:
   sense = SenseHat()
except Exception as e:
        print "ERROR!"
        print(e)
#       exit()

print "<br>end of my python script"
PHP sh.php (its php because Apache is set to serve php)

Code: Select all

html>
<body>
PYTHON TEST<br>
<?php
$output = shell_exec('/var/www/html/SH-test/test.py');
echo $output;
?>
</body>
</html>

SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Tue Oct 17, 2017 1:55 pm

I entered your directories and files exactly as you posted.. My server is at 192.168.1.3, and When I entered this in the address bar
http://192.168.1.3/sh.php
I got this

Code: Select all

PYTHON TEST
ERROR! Cannot detect RPi-Sense FB device
end of my python script 

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Tue Oct 17, 2017 3:18 pm

I still get the same result.
Maybe its because I do have a sense hat attached.
Maybe I will detach it and try.
Is your result not the result you would expect if you did not have a Hat or do you think my py script is not running yet it seems to be.

bensimmo
Posts: 1814
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Communicating between sense hats

Tue Oct 17, 2017 3:53 pm

If you had a SenseHAT you wouldn't get the error.

test.py wouldn't work in Python3 anyway, probably why it doesn't work.
It would fail on the print statements.


-- I'm following but cannot test as I have no wordpress setup.

But all I can see is it's working, just the SenseHat display is not triggering.
So you get no error either way.

Is there a permission problem with running the SenseHat from there. Perhaps grab the AstroPi/Sense-hat section of the forum?

You are getting Hello World with your code. It could just be the SenseHat not working as wanted.

I assume it works if you run it from IDLE's/Thonny on the Pi itself from a normal folder.
Last edited by bensimmo on Tue Oct 17, 2017 4:44 pm, edited 1 time in total.

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Tue Oct 17, 2017 4:28 pm

will do
thanks

bensimmo
Posts: 1814
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Communicating between sense hats

Tue Oct 17, 2017 5:00 pm

hawkesley wrote:
Tue Oct 17, 2017 4:28 pm
will do
thanks
As a test for me
https://trinket.io/python/484a93f4e8
The code works fine and scroll text and shows just <br>end of my python script
as expected

If I force an error, say change to sense = Sense6Hat()
I get
ERROR!
NameError: name 'Sense6Hat' is not defined on line 6
<br>end of my python script


force the error in your code and see what it returns

So the "Python code" is fine, in my opinion.
Just the way it's being triggered and how that works or allows the sesehat to work.

Does the 'user group' have access to all that the SenseHat needs (probably I2C mainly,)

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Wed Oct 18, 2017 9:51 am

Hi,
Ok I replicated your script and I get the same result, so as you say I think the code is fine.
I have now run my SH-test with test.py and get this error
PYTHON TEST
ERROR! [Errno 13] Permission denied: '/var/www/.config'
end of my python script

So it is permissions but how to give Apache (www-data) the right permissions?

Code fro completeness
sh.php

Code: Select all

<html>
<body>
PYTHON TEST<br>
<?php
$output = shell_exec('/var/www/html/SH-test/test.py');
echo $output;
?>
</body>
</html
test.py

Code: Select all

#!/usr/bin/python3

from sense_hat import SenseHat

try:
   sense = SenseHat()
except Exception as e:
        print ("ERROR!")
        print(e)
#       exit()

print ("<br>end of my python script")

bensimmo
Posts: 1814
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Communicating between sense hats

Wed Oct 18, 2017 10:52 am

I don't know own the correct answer to that, surfertim might though?

SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Wed Oct 18, 2017 12:05 pm

I have no file 'var/www/.config' in my install. Do you?

Code: Select all

cd /var/www
ls -a
Edit: I installed apache2 and php5 from the repository. I didn't need to install sense hat. It was installed by default. Same with you?

hawkesley
Posts: 83
Joined: Tue Jan 12, 2016 10:16 am

Re: Communicating between sense hats

Wed Oct 18, 2017 12:31 pm

Yes I didnt need to install sense hat.
I will investigate /var/www/config.
Its odd because the path is /var/www/html/SH-test/
In html there is a config file for wordpress of course.

SurferTim
Posts: 1002
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Communicating between sense hats

Wed Oct 18, 2017 12:36 pm

Because of where in the python script you are getting that error, it is an error generated by the SenseHat() call. Maybe it expects a .config file there. ??

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 24 guests