macgyver3636
Posts: 5
Joined: Fri Jul 24, 2020 12:29 am

Weather Station

Fri Jul 24, 2020 12:42 am

Hi there,

I am trying to setup a weather station using a BME280 and 18B20 sensor.
I have a Pi3 new rasbian loaded and updated.
I am following steps from this build https://projects.raspberrypi.org/en/pro ... -station/2
this is the step I am on and the code I have written but I get an error.

import bme280
import smbus2
from time import sleep

port = 1
address = 0x77 # Adafruit BME280 address. Other BME280s may be different
bus = smbus2.SMBus(port)

bme280.load_calibration_params(bus,address)

while True:
bme280_data = bme280.sample(bus,address)
humidity = bme280_data.humidity
pressure = bme280_data.pressure
ambient_temperature = bme280_data.temperature
print(humidity, pressure, ambient_temperature)
sleep(1)

Error
Python 3.7.3 (/usr/bin/python3)
>>> %Run test.py
Traceback (most recent call last):
File "/home/pi/test.py", line 17
sleep(1)
^
SyntaxError: invalid syntax


I am a noob at this stuff but trying to figure it out.

Thanks in advance

pidd
Posts: 575
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: Weather Station

Fri Jul 24, 2020 4:32 am

You need to use the code tags to show tabulation, anyway, this should resolve your problem

Code: Select all

import time
.
.
.
.

time.sleep(1)
Edit: Soz, talking trash.
Last edited by pidd on Sun Jul 26, 2020 6:11 pm, edited 1 time in total.

vffgaston
Posts: 6
Joined: Wed May 13, 2020 3:33 pm

Re: Weather Station

Fri Jul 24, 2020 7:07 am

Hi,
Just to calibrate the help you need:
Are you proficient in python?
Have you got linux and/or Raspberry background?
Best regards.

macgyver3636
Posts: 5
Joined: Fri Jul 24, 2020 12:29 am

Re: Weather Station

Fri Jul 24, 2020 12:34 pm

I grew up using commodore 64 and dos.
Very new to Python.
Basically a newbie

vffgaston
Posts: 6
Joined: Wed May 13, 2020 3:33 pm

Re: Weather Station

Fri Jul 24, 2020 3:57 pm

Hi,
Looks like a (time.)sleep is missing ... (the line has to be "time.sleep(...)")
Guess it won't help much, as, probably, new problems will come up. I'd try starting with simpler programs, then trying to read the DS18B20 through the 1-wire bus ... You'll need some weeks to learn the basics.
Best regards

macgyver3636
Posts: 5
Joined: Fri Jul 24, 2020 12:29 am

Re: Weather Station

Sat Jul 25, 2020 12:49 am

So i was able to get the ds18b20 to read sensor fine.
I added time.sleep but still get invalid syntax error at line 17 time.sleep.

Thanks

pidd
Posts: 575
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: Weather Station

Sat Jul 25, 2020 2:37 am

macgyver3636 wrote:
Sat Jul 25, 2020 12:49 am
So i was able to get the ds18b20 to read sensor fine.
I added time.sleep but still get invalid syntax error at line 17 time.sleep.

Thanks
As I indicated above you must import time in its entirety as well, not just the sleep module.

Code: Select all

import time

User avatar
rpiMike
Posts: 1386
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Weather Station

Sat Jul 25, 2020 7:39 am

Please repost the full code in code tags. Formatting ie spacing is crucial in Python and is lost without code tags.

hippy
Posts: 7737
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Weather Station

Sat Jul 25, 2020 11:00 am

I sometimes wonder how much experience of Python those offering advice on Python actually have. This is perfectly fine for Python 2 and Python 3, can be easily tested ...

Code: Select all

from time import sleep
sleep(10)
There's nothing wrong with the sleep() line in the OP's post. Python is notorious for detecting an error in the line(s) before the line it reports the error on. Viz -

Code: Select all

pi@Pi3B:~/tmp $ cat xyzzy.py 
from time import sleep
print( ( 1 , 2
sleep(10)

Code: Select all

pi@Pi3B:~/tmp $ python3 xyzzy.py 
  File "xyzzy.py", line 3
    sleep(10)
        ^
SyntaxError: invalid syntax
So, whatever the syntax error is, it is before that sleep() line. Where; I can't tell.

hippy
Posts: 7737
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Weather Station

Sat Jul 25, 2020 11:31 am

rpiMike wrote:
Sat Jul 25, 2020 7:39 am
Please repost the full code in code tags. Formatting ie spacing is crucial in Python and is lost without code tags.
And please make sure you post the actual code you have, not the code of the original project you are copying.

I ran your code with a small kludge to make it work because I don't have smbus2 or bme280 modules available and it ran fine without any syntax errors, gave the output I would have expected.

If you have forgotten the closing ')' on your print() line ( line 16 ) that would produce the Syntax Error on the sleep() line which you are seeing.

Maybe forget what you have and simply copy the original code into Thonny again, or whatever IDE you are using.

pidd
Posts: 575
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: Weather Station

Sat Jul 25, 2020 1:34 pm

hippy wrote:
Sat Jul 25, 2020 11:00 am
I sometimes wonder how much experience of Python those offering advice on Python actually have. This is perfectly fine for Python 2 and Python 3, can be easily tested ...

Code: Select all

from time import sleep
sleep(10)

You got me there. I tried it in thonny and got exactly the same error as the OP, I changed it to what I suggested and the error went. However, tried it again just now and it won't reproduce and unfortunately I overwrote the file so I can't see what I had yesterday. I must have double typo'd

I went through the tabbing possibilities and I can't see a way that makes that fault but admit I haven't tried it out in practice.

It would be difficult to believe the OP typed that into the forum, surely its a cut & paste which I thought makes the bracket suggestion unlikely?

But when you've tried everything else, the unlikeliest ......

nliviu
Posts: 27
Joined: Tue Jun 23, 2020 1:24 pm
Location: Romania

Re: Weather Station

Sat Jul 25, 2020 2:38 pm

Copy/paste the code from here and modified the I2C address for my sensor, works with either python2 or python3

Code: Select all

import bme280
import smbus2
from time import sleep

port = 1
address = 0x76 # Adafruit BME280 address. Other BME280s may be different
bus = smbus2.SMBus(port)

bme280.load_calibration_params(bus,address)

while True:
    bme280_data = bme280.sample(bus,address)
    humidity  = bme280_data.humidity
    pressure  = bme280_data.pressure
    ambient_temperature = bme280_data.temperature
    print(humidity, pressure, ambient_temperature)
    sleep(1)

Results python2

Code: Select all

python bme280-test.py 
(42.791012684849456, 999.243281267399, 28.602841960458317)
(42.89506071974343, 999.2696537307783, 28.602841960458317)
(43.06031373206641, 999.3142375735582, 28.59781691962853)
Results python3

Code: Select all

python3 bme280-test.py 
43.360204199574675 999.2370110709059 28.5827417988854
43.323480274767306 999.1497364204783 28.577716759219765
43.2378002082407 999.1842679102365 28.5827417988854

macgyver3636
Posts: 5
Joined: Fri Jul 24, 2020 12:29 am

Re: Weather Station

Sat Jul 25, 2020 5:34 pm

Python 3.7.3 (/usr/bin/python3)
>>> %Run test2.py
Traceback (most recent call last):
File "/home/pi/test2.py", line 7, in <module>
bus = smbus2.SMBus(port)
File "/usr/local/lib/python3.7/dist-packages/smbus2/smbus2.py", line 279, in __init__
self.open(bus)
File "/usr/local/lib/python3.7/dist-packages/smbus2/smbus2.py", line 308, in open
self.fd = os.open(filepath, os.O_RDWR)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1'
>>>
Hi all thanks for the replies.
The above is what I get when I copied and pasted code from memberlist.php?mode=viewprofile&u=339855
I changed address to 0x77 but same error.

Thanks

hippy
Posts: 7737
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Weather Station

Sun Jul 26, 2020 8:06 am

macgyver3636 wrote:
Sat Jul 25, 2020 5:34 pm
The above is what I get when I copied and pasted code from memberlist.php?mode=viewprofile&u=339855
I think you have the wrong link there.

Did you fix your earlier program ? Have you got that working ?

Have you enabled I2C through 'sudo raspi-config' ?

vffgaston
Posts: 6
Joined: Wed May 13, 2020 3:33 pm

Re: Weather Station

Sun Jul 26, 2020 8:27 am

hippy wrote:
Sat Jul 25, 2020 11:00 am
I sometimes wonder how much experience of Python those offering advice on Python actually have. This is perfectly fine for Python 2 and Python 3, can be easily tested ...

Code: Select all

from time import sleep
sleep(10)
There's nothing wrong with the sleep() line in the OP's post. Python is notorious for detecting an error in the line(s) before the line it reports the error on. Viz -

Code: Select all

pi@Pi3B:~/tmp $ cat xyzzy.py 
from time import sleep
print( ( 1 , 2
sleep(10)

Code: Select all

pi@Pi3B:~/tmp $ python3 xyzzy.py 
  File "xyzzy.py", line 3
    sleep(10)
        ^
SyntaxError: invalid syntax
So, whatever the syntax error is, it is before that sleep() line. Where; I can't tell.
Hi all,
hippy is absolutely right: importing just a function from a library/module ("from time import sleep") does imply to use the function alone ("sleep") instead to referring to the module ("time.sleep").
(Bad habit this of mine of importing complete modules always: "import time". Fortunately we have real experts to teach us).
Best regards.

macgyver3636
Posts: 5
Joined: Fri Jul 24, 2020 12:29 am

Re: Weather Station

Sun Jul 26, 2020 6:09 pm

Thank you all for your help...
I enabled IC2, which I was sure I did before, but anyway now getting data.
No to figure out how to get this on the WWW using WUG.

Thanks again

hippy
Posts: 7737
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Weather Station

Tue Jul 28, 2020 11:19 am

macgyver3636 wrote:
Sun Jul 26, 2020 6:09 pm
I enabled IC2, which I was sure I did before, but anyway now getting data.
Enabling that wouldn't have magically solved a syntax error but glad to hear you have resolved your problem. And thanks for letting us know.

Return to “General discussion”