Page 1 of 1

Help writing a Python Code for Non Coder

Posted: Wed Jun 27, 2018 10:05 pm
by ghostrider513
Short version. I got the RPI to build a simple research data logger that will upload data to my dbase on my website. I have many scientists that will review my data and make conclusions. Problem is, I suck at coding in any language - lol Well it's true.

I have after several months, finally got my RPI 3B+ running and have a sensor (SHT31-D) working on it. That's it after several months. Frustrated doesn't come close. But I got this far. I need help.

Here's my code:

Code: Select all

import smbus
import time
 
# Get I2C bus
bus = smbus.SMBus(1)
 
# SHT31 address, 0x44(68)
bus.write_i2c_block_data(0x44, 0x2C, [0x06])
 
time.sleep(0.5)
 
# SHT31 address, 0x44(68)
# Read data back from 0x00(00), 6 bytes
# Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC
data = bus.read_i2c_block_data(0x44, 0x00, 6)
 
# Convert the data
temp = data[0] * 256 + data[1]
cTemp = -45 + (175 * temp / 65535.0)
fTemp = -49 + (315 * temp / 65535.0)
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
 
# Output data to screen
print "Temperature in Celsius is : %.2f C" %cTemp
print "Temperature in Fahrenheit is : %.2f F" %fTemp
print "Relative Humidity is : %.2f %%RH" %humidity


These are the problems I'm trying to overcome:
1. Have the sensor continue providing data more than one time when I type: sudo python sht31.py
2. have the data saved (data logging)
3. Have that data sent to my MySQL website

I have created the database on my website and have the username, password and info for the database. I built my website so that's the easy part for me. This has take months to get this little amount of info. All help would be greatly appreciated.

Re: Help writing a Python Code for Non Coder

Posted: Wed Jun 27, 2018 10:22 pm
by DougieLawson
You DO NOT need sudo for I2C with Jessie or Stretch.

You need a main loop

Code: Select all

while True:
  # do stuff continually
  bus.write_i2c_block_data(0x44,0x??,0x??)
  result = bus.read_i2c_block_data(0x44,0x??,4) # ",4" is the length
  print ('stuff you've read:', str(result))
After bus.write_i2c_block_data() call you need a bus.read_byte_data(0x44,0x??) to read the results from one of the chip's registers.

Re: Help writing a Python Code for Non Coder

Posted: Wed Jun 27, 2018 10:26 pm
by ghostrider513
DougieLawson wrote:
Wed Jun 27, 2018 10:22 pm
You DO NOT need sudo for I2C with Jessie or Stretch.

You need a main loop

Code: Select all

while True:
  # do stuff continually
  bus.write_i2c_block_data(0x44,0x??,0x??)
  result = bus.read_i2c_block_data(0x44,0x??,4) # ",4" is the length
  print ('stuff you've read:', str(result))
After bus.write_i2c_block_data() call you need a bus.read_byte_data(0x44,0x??) to read the results from one of the chip's registers.
Forgive me for saying this but, I haven't a clue what you mean. This doesn't make any sense. I'm sorry. I know how to post it on my nano but beyond that I'm lost. Thanks for the fast reply.

Re: Help writing a Python Code for Non Coder

Posted: Wed Jun 27, 2018 10:27 pm
by ghostrider513
ghostrider513 wrote:
Wed Jun 27, 2018 10:26 pm
DougieLawson wrote:
Wed Jun 27, 2018 10:22 pm
You DO NOT need sudo for I2C with Jessie or Stretch.

You need a main loop

Code: Select all

while True:
  # do stuff continually
  bus.write_i2c_block_data(0x44,0x??,0x??)
  result = bus.read_i2c_block_data(0x44,0x??,4) # ",4" is the length
  print ('stuff you've read:', str(result))
After bus.write_i2c_block_data() call you need a bus.read_byte_data(0x44,0x??) to read the results from one of the chip's registers.
Forgive me for saying this but, I haven't a clue what you mean. This doesn't make any sense. I'm sorry. I know how to post it on my nano but beyond that I'm lost. Thanks for the fast reply.
I can make the code work without sudo. My mistake. Thanks

Re: Help writing a Python Code for Non Coder

Posted: Wed Jun 27, 2018 10:39 pm
by DougieLawson
Try this program

Code: Select all

import smbus
import time
bus = smbus.SMBus(1)
while True:
  bus.write_i2c_block_data(0x44, 0x2C, [0x06])
  time.sleep(0.5)
  shtdata = bus.read_i2c_block_data(0x44, 0x00, 6)
  temp = shtdata[0] * 256 + shtdata[1]
  cTemp = -45 + (175 * temp / 65535.0)
  humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
  print "Temperature in Celsius is : %.2f C" %cTemp
  print "Relative Humidity is : %.2f %%RH" %humidity
  time.sleep(9.5)
That will print some stuff every ten seconds.

Re: Help writing a Python Code for Non Coder

Posted: Thu Jun 28, 2018 3:07 am
by ghostrider513
DougieLawson wrote:
Wed Jun 27, 2018 10:39 pm
Try this program

Code: Select all

import smbus
import time
bus = smbus.SMBus(1)
while True:
  bus.write_i2c_block_data(0x44, 0x2C, [0x06])
  time.sleep(0.5)
  shtdata = bus.read_i2c_block_data(0x44, 0x00, 6)
  temp = shtdata[0] * 256 + shtdata[1]
  cTemp = -45 + (175 * temp / 65535.0)
  humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
  print "Temperature in Celsius is : %.2f C" %cTemp
  print "Relative Humidity is : %.2f %%RH" %humidity
  time.sleep(9.5)
That will print some stuff every ten seconds.
Thanks. Just saw this. It works. Thank you so much! One problem down and two to go. Made my month. I have tried for over a week to find the code to do that. Every tutorial I found didn't work. It has been incredibly difficult. Best wishes to you for the assist.
Mike

Re: Help writing a Python Code for Non Coder

Posted: Thu Jun 28, 2018 8:02 am
by DougieLawson
It's got a bug in it - change " data[" to " shtdata[" everywhere and the humidity stuff should start working.

I don't have an SHT31 sensor so I can't test it.

Re: Help writing a Python Code for Non Coder

Posted: Thu Jun 28, 2018 10:02 pm
by ghostrider513
DougieLawson wrote:
Thu Jun 28, 2018 8:02 am
It's got a bug in it - change " data[" to " shtdata[" everywhere and the humidity stuff should start working.

I don't have an SHT31 sensor so I can't test it.
Humidity is working now. Got it. Thanks for that follow up.