Datalogging RH from Si7021, can't get my code to work

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Ammonia
Posts: 1
Joined: Sun Jan 15, 2017 5:39 pm

Datalogging RH from Si7021, can't get my code to work

Post by Ammonia » Sun Jan 15, 2017 5:48 pm

I want to log a few hours of RH data from an Si7021 sensor using my PyBoard v1.1, I can't get my microSD card to work with it for the moment - which is why I'm writing to flash (shouldn't be a problem for the time being?).

I wrote some dirty python to do the job, as below:
.........

import pyb
from pyb import I2C
i2c = I2C(1)
i2c = I2C(1, I2C.MASTER)
i2c.init(I2C.MASTER, baudrate=100000)
log = open('/flash/RHlog.csv', 'w')

while True:
i2c.send(0xF5, 0x40)
[x, y] = i2c.recv(2, 0x40)
RH_val = (x<<8) | y
RH = ((125*RH_val)/65536)-6
t = pyb.millis()
log.write('{},{}\n'.format(RH,t))
pyb.delay(2000)

.......

It works fine when I send it line by line over the REPL, it makes RHlog.csv on the flash and writes a new line each time with the RH value and time (millis). But when I save it as a main.py on the board all I get is a boot error (red and green LED flash alternately). Can't seem to fix it by any means.

Anyone know what I'm doing wrong? Why the code won't work? Maybe I should just write the sensor output to file directly and do the math (conversion) later?

Thanks.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Datalogging RH from Si7021, can't get my code to work

Post by dhylands » Sun Jan 15, 2017 6:59 pm

What I normally do is to put my code in a file, say rshlog.py and make sure I can do:

Code: Select all

import rshlog
from the REPL and not get any errors.

Then I just add the import rshlog line to main.py. You're much less likely to make an error adding an import statement than adding a bunch of code.

Post Reply