astro-pi / python-sense-hat

Source code for Sense HAT Python library
https://sense-hat.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
508 stars 255 forks source link

Calling SenseHat() raises UnicodeDecodeError #78

Closed quassbutreally closed 6 years ago

quassbutreally commented 7 years ago

Hi all,

I'm practising CGI scripts with an Apache 2.4 localhost running on my Raspberry Pi 3B. Essentially I have an HTML form that sends a string and an RGB value to a Python CGI file, which is then supposed to make the LED matrix on the sense hat display the message with that RGB value.

The issue is that when I call sense = SenseHat(), I get the following error:

Traceback (most recent call last): File "/var/www/html/cgi-bin/test.py", line 23, in SenseHat().show_message(text, text_colour=[red, green, blue]) File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 92, in __init__ self._stick = SenseStick() File "/usr/lib/python3/dist-packages/sense_hat/stick.py", line 57, in __init__ self._stick_file = io.open(self._stick_device(), 'rb', buffering=0) File "/usr/lib/python3/dist-packages/sense_hat/stick.py", line 83, in _stick_device if f.read().strip() == self.SENSE_HAT_EVDEV_NAME: File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 19: ordinal not in range(128)

I can successfully send commands to the sense hat using python from that directory and all other directories. I tried building a module that would handle pushing the information to the sense_hat separately but that threw the same error.

For completeness, I'll post the HTML and Python in its completeness below.

HTML:

<html>
<head>
  <title>Homepage</title>
</head>
<body>
  <h1>Tell my Raspberry Pi's Sense Hat To Say Stuff!</h1>
  <br></br>
  <form action="/cgi-bin/test.py" method="post">
    <label>Text: </label>
    <input type="text" name="text"></input>
    <br>
    <label>RGB values: </label>
    <input type="number" min="0" max="255" name="red" id="red"></input>
    <input type="number" min="0" max="255" name="green" id="green"></input>
    <input type="number" min="0" max="255" name="blue" id="blue"></input>
    <input type="submit" value="submit"></input>
  </form>
</body>
</html>

and the Python:

#!/usr/bin/python3.4
# -*- coding: UTF-8 -*-# enable debugging

from sense_hat import SenseHat

import cgi
import traceback

form = cgi.FieldStorage()

print("Content-type:text/html\r\n\r\n")

try:

    text = form.getvalue("text")
    red = int(form.getvalue("red"))
    green = int(form.getvalue("green"))
    blue = int(form.getvalue("blue"))

    if len(text) in range(0, 51) and red in range(0,256) and green in range(0,256) and blue in range(0,256):

        #this if statement checks that the values for R, G and B are all valid, and that the text
        #length is <= 50 characters

        print("Now running your request<br>")
        SenseHat().show_message(text, text_colour=[red, green, blue])

    else:

        raise SyntaxError

except:

    print('<h1 style="color:red;">ERROR!</h1>')
    print(traceback.format_exc())
bennuttall commented 6 years ago

Closing in favour of #81