roscoe81 / enviro-monitor

An environment monitor based on a Raspberry Pi Zero W, a Pimoroni Enviro+ and a Plantower air quality sensor to monitor, display and report on air particles, gases, temperature, humidity, noise and air pressure.
MIT License
80 stars 26 forks source link

SyntaxError: Non-ASCII character '\xc2' in file Northcliff_AQI_Monitor_Gen.py on line 1103 #5

Closed ChrisKoh83 closed 4 years ago

ChrisKoh83 commented 4 years ago

I´m getting this error if I try to run Northcliff_AQI_Monitor_Gen.py on my Raspi Zero.

File "Northcliff_AQI_Monitor_Gen.py", line 1103
SyntaxError: Non-ASCII character '\xc2' in file Northcliff_AQI_Monitor_Gen.py on line 1103, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

If I google the error message: SyntaxError: Non-ASCII character '\xc2' in file I´m getting the hint to: https://stackoverflow.com/questions/18078851/syntaxerror-of-non-ascii-character Set # -- coding: utf-8 -- on top of the script.

But if I do that and run the py I`m gettin this:

File "Northcliff_AQI_Monitor_Gen.py", line 1105
    temp_string = f"{data['Temp'][1]:.0f}°C"
                                            ^
SyntaxError: invalid syntax

maybe this has something to do with my location settings?

pi@raspberrypi:~/enviro-monitor $ sudo raspi-config
Generating locales (this might take a while)...
  de_DE.UTF-8...
roscoe81 commented 4 years ago

What happens when you run?

import sys print sys.getdefaultencoding()

roscoe81 commented 4 years ago

Another thought ...

Have you tried this at the top of your script? # -*- coding: de_DE.UTF-8 -*-

ChrisKoh83 commented 4 years ago

What happens when you run?

import sys print sys.getdefaultencoding()

pi@raspberrypi:~/enviro-monitor $ tail testLocation.py
#!/usr/bin/env python3

import sys
print sys.getdefaultencoding()
pi@raspberrypi:~/enviro-monitor $ python testLocation.py
ascii
pi@raspberrypi:~/enviro-monitor $
ChrisKoh83 commented 4 years ago

Another thought ...

Have you tried this at the top of your script? # -*- coding: de_DE.UTF-8 -*-

python Northcliff_AQI_Monitor_Gen.py
  File "Northcliff_AQI_Monitor_Gen.py", line 1
SyntaxError: encoding problem: de_DE.UTF-8
pi@raspberrypi:~/enviro-monitor $ head -5 Northcliff_AQI_Monitor_Gen.py
# -*- coding: de_DE.UTF-8 -*-
#!/usr/bin/env python3
#Northcliff Environment Monitor
# Requires Home Manager >=8.54 with Enviro Monitor timeout
roscoe81 commented 4 years ago

Thanks for that @ChrisKoh83

I'm far from an expert in this area but it looks like your default encoding is ASCII and that's causing the issue. Due to formatting issues, I'm not sure what you initially tried.

Did you try this at the top of your file?

# -*- coding: utf-8 -*-

Other than checking that, it might be a difference with the degree symbol entry via a German keyboard. You could try replacing the degree symbol in temp_string = f"{data['Temp'][1]:.0f}°C" with the degree symbol from your own keyboard and if that fails, delete the degree symbol altogether.

ChrisKoh83 commented 4 years ago

I have to thank you for your support!

I tryed the following things:

added: # -*- coding: utf-8 -*-

pi@raspberrypi:~/enviro-monitor $ nano Northcliff_AQI_Monitor_Gen.py
pi@raspberrypi:~/enviro-monitor $ python Northcliff_AQI_Monitor_Gen.py
  File "Northcliff_AQI_Monitor_Gen.py", line 1111
    temp_string = f"{data['Temp'][1]:.0f}°C"
                                            ^
SyntaxError: invalid syntax

changed the "°" degreed symbol

pi@raspberrypi:~/enviro-monitor $ nano Northcliff_AQI_Monitor_Gen.py
pi@raspberrypi:~/enviro-monitor $ python Northcliff_AQI_Monitor_Gen.py
  File "Northcliff_AQI_Monitor_Gen.py", line 1111
    temp_string = f"{data['Temp'][1]:.0f}°C"
                                            ^
SyntaxError: invalid syntax

removed the "°" degreed symbol

pi@raspberrypi:~/enviro-monitor $ nano Northcliff_AQI_Monitor_Gen.py
pi@raspberrypi:~/enviro-monitor $ python Northcliff_AQI_Monitor_Gen.py
  File "Northcliff_AQI_Monitor_Gen.py", line 1111
    temp_string = f"{data['Temp'][1]:.0f} C"
                                           ^
SyntaxError: invalid syntax

removed the degree and the "C"

pi@raspberrypi:~/enviro-monitor $ nano Northcliff_AQI_Monitor_Gen.py
pi@raspberrypi:~/enviro-monitor $ python Northcliff_AQI_Monitor_Gen.py
  File "Northcliff_AQI_Monitor_Gen.py", line 1111
    temp_string = f"{data['Temp'][1]:.0f}"
                                         ^
SyntaxError: invalid syntax

I have no idea whats going on here :(

ChrisKoh83 commented 4 years ago

python is complaining about the double qutes: https://stackoverflow.com/questions/25445439/what-does-syntaxerror-missing-parentheses-in-call-to-print-mean-in-python

so I added round bracket on every "f" I found. Like temp_string = f("{data['Temp'][1]:.0f} °C")

That pushed me right to the next error...

pi@raspberrypi:~/enviro-monitor $ python Northcliff_AQI_Monitor_Gen.py
Traceback (most recent call last):
  File "Northcliff_AQI_Monitor_Gen.py", line 18, in <module>
    import requests
ImportError: No module named requests
pi@raspberrypi:~/enviro-monitor $ pip install requests
...
...
Installing collected packages: idna, urllib3, certifi, chardet, requests
  The script chardetect is installed in '/home/pi/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0 urllib3-1.25.10
pi@raspberrypi:~/enviro-monitor $ python Northcliff_AQI_Monitor_Gen.py
Traceback (most recent call last):
  File "Northcliff_AQI_Monitor_Gen.py", line 27, in <module>
    from astral.geocoder import database, lookup, add_locations
  File "/usr/local/lib/python2.7/dist-packages/astral-2.2-py2.7.egg/astral/__init__.py", line 77
    def now(tzinfo: datetime.tzinfo = pytz.utc) -> datetime.datetime:
                  ^
SyntaxError: invalid syntax

:(

roscoe81 commented 4 years ago

Wow! Lots of issues at each step - even for those parts that are identical to the Pimoroni Examples. I'm running Python 3 and it looks like you're running Python 2.7. Have you tried running it on Python 3?

ChrisKoh83 commented 4 years ago

python3 is a good hint!

but then:

pi@raspberrypi:~/enviro-monitor $ python3 Northcliff_AQI_Monitor_Gen.py
2020-08-24 11:14:50.690 INFO     Northcliff_Environment_Monitor.py - Pimoroni Enviro+ (and optional SGP30) sensor capture and display, plus external sensor capture and Luftdaten, mqtt and Adafruit IO Updates
#Note: you'll need to register with Luftdaten at:
#https://meine.luftdaten.info/ and enter your Raspberry Pi
#serial number that's displayed on the Enviro plus LCD along
#with the other details before the data appears on the
#Luftdaten map.

#
5.22 - Gen
Config Retrieval Failed
Traceback (most recent call last):
  File "Northcliff_AQI_Monitor_Gen.py", line 145, in <module>
    city_name, time_zone, custom_locations) = retrieve_config()
  File "Northcliff_AQI_Monitor_Gen.py", line 90, in retrieve_config
    temp_offset = parsed_config_parameters['temp_offset']
UnboundLocalError: local variable 'parsed_config_parameters' referenced before assignment

after that I "reordered" some lines in your script to get rid of the "referenced before assignment" error:

# Initialize display
disp.begin()

def retrieve_config():
    try:
        with open('<Your config.json file location>', 'r') as f:
            parsed_config_parameters = json.loads(f.read())
            print('Retrieved Config', parsed_config_parameters)
    except IOError:
        print('Config Retrieval Failed')

# Config Setup
  (temp_offset, altitude, enable_display, enable_adafruit_io, aio_user_name, aio_key, aio_feed_window, aio_feed_sequence,
  aio_household_prefix, aio_location_prefix, aio_package, enable_send_data_to_homemanager,
  enable_receive_data_from_homemanager, enable_indoor_outdoor_functionality, mqtt_broker_name,
  enable_luftdaten, enable_climate_and_gas_logging,  enable_particle_sensor, enable_eco2_tvoc, gas_daily_r0_calibration_hour, reset_gas_sensor_calibration,
  incoming_temp_hum_mqtt_topic, incoming_temp_hum_mqtt_sensor_name, incoming_barometer_mqtt_topic, incoming_barometer_sensor_id,
  indoor_outdoor_function, mqtt_client_name, outdoor_mqtt_topic, indoor_mqtt_topic,
  city_name, time_zone) = retrieve_config()

    temp_offset = parsed_config_parameters['temp_offset']
    altitude = parsed_config_parameters['altitude']
...
...
...

but now I´m stuck there:

pi@raspberrypi:~/enviro-monitor $ python3 Northcliff_AQI_Monitor_Gen.py
  File "Northcliff_AQI_Monitor_Gen.py", line 105
    temp_offset = parsed_config_parameters['temp_offset']
    ^
IndentationError: unexpected indent
roscoe81 commented 4 years ago

It appears that you haven’t correctly set up your config file as per these instructions and you haven’t updated Line 77 to reference the location of your config file.

That caused the referenced before assignment error and you didn’t need to reorder any lines of the script.

Do not re-order the script but instead, read all of the readme instructions and use the config file here as a starting point, store it in your chosen location and reference it in Line 77.

Also, you will need to modify all other file references enclosed with <> to suit your setup. The script is generic and needs to be customised for a particular user’s setup.

ChrisKoh83 commented 4 years ago

I´m sorry! I didn't read this instructions carefully enough. I had already edited the the config.json. but I didn´t realise the instructions in Line 77.

After doing this and restarting the script I got a couple of errors but this could I solve by undo the round brackets around the temp_string = f("{data['Temp'][1]:.0f} °C")

and et voila:

image

Thank you so much!!