gowthamidentity
Posts: 10
Joined: Mon Dec 18, 2017 12:25 pm

Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 3:19 pm

Hello all,

I'm very new to Raspberry pi controller. I'm using Raspberry pi zero w for my project. I need to connect my Raspberry pi to the mobile application(android).

I've programmed the communication part based on python2. But I've added 1 more sensor which was programmed by my colleague in python3. After combining both the programs, commuication part is not working in python3 and sensor is not working in python2. Please hel[p me in finding a solution or idea that I can look for.

Following is the program for the sensor (python3). I don't know which function is not working in python2.

Code: Select all

import smbus
import time
bus = smbus.SMBus(1)
dev_addr = 0x28

def bin_8(num):
 return format(num,'#010b')[2:]

def read_data():
 t = time.time()
 while t - time.time() < 5:
  temp = bus.read_i2c_block_data(dev_addr, 0x00, 4)
  condition = temp[0] & 0b11000000
  temp[0] = temp[0] & 0b00111111
 
  if condition == 0:
   bit = temp[0]*256 + temp[1]
   pressure = (((bit - 1638)*20)/(14745-1638)-10) * 10.197
   t_bit = temp[2]*8 + int((temp[3]&0b11100000)/0b100000)
   temperature = (t_bit/2047*200)-50
   return pressure
  time.sleep(0.1)
 return -1  
  
def read_temp():
 temp = bus.read_i2c_block_data(dev_addr, 0x00, 4)
 condition = temp[0] & 0b11000000
 temp[0] = temp[0] & 0b00111111
 if condition == 0:
  bit = temp[0]*256 + temp[1]
  pressure = (((bit - 1638)*20)/(14745-1638)-10) * 10.197
  t_bit = temp[2]*8 + int((temp[3]&0b11100000)/0b100000)
  temperature = (t_bit/2047*200)-50
  return temperature
  
def burst_read_data(time):
 temp = 0
 for i in range(0,time):
  temp += read_data()
 return temp/time

User avatar
topguy
Posts: 4994
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 3:32 pm

And you get no exceptions or errors when you run the program under Python2 ? It just produces no data ?

Heater
Posts: 10249
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 3:35 pm

The best idea is to update your code to use Python 3. If I understand correctly the required changes are not huge.

See here for info: https://docs.python.org/3/howto/pyporting.html

gowthamidentity
Posts: 10
Joined: Mon Dec 18, 2017 12:25 pm

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 3:51 pm

Following is result of data which I get . In python2 the sensor readings are not correct.

[email protected]:~ $ python2 func_lid_special_withoutdisplay.py
-6.1182
-6.1182
-6.1182
-4.0788
-6.1182
-5.0985
-6.1182
-7.1379
-6.1182
-6.1182
-5.0985
-4.0788
^C
[email protected]:~ $ python3 func_lid_special_withoutdisplay.py
-0.5897097734035244
1.0456067750057216
-0.08557793545433708
-0.0933577477683678
-0.6363886472877086
1.6446523231860888
2.074097962920578
2.141004348821242
-0.9429132524605155

gowthamidentity
Posts: 10
Joined: Mon Dec 18, 2017 12:25 pm

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 3:55 pm

Heater wrote: The best idea is to update your code to use Python 3. If I understand correctly the required changes are not huge.

See here for info: https://docs.python.org/3/howto/pyporting.html
I'm trying to use bluetooth with this. But I'm unable to use socket programming with python3 and android application. I'm not clear whether the python uses BLE or classic bluetooth

User avatar
Jednorozec
Posts: 809
Joined: Sun Nov 24, 2013 2:17 pm
Location: Deposit, NY

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 4:13 pm

The most important leg of a three legged stool is the one that's missing.
It's called thinking. Why don't you try it sometime?

Heater
Posts: 10249
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 4:37 pm

Python is insane.

They broke division operator after all these years. For goodness sake. How nuts is that?

Better to use a language with some proper standardization behind it. C, C++, Javascript etc.

User avatar
Jednorozec
Posts: 809
Joined: Sun Nov 24, 2013 2:17 pm
Location: Deposit, NY

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 4:51 pm

“The wonderful thing about standards is that there are so many of them to choose from.”

― Grace Murray Hopper
The most important leg of a three legged stool is the one that's missing.
It's called thinking. Why don't you try it sometime?

Heater
Posts: 10249
Joined: Tue Jul 17, 2012 3:02 pm

Re: Python2 not compatible with HSC sensor library

Tue Jan 02, 2018 5:00 pm

An oft' quoted phrase.

Totally out of context here as no version of Python is any kind of recognized standard.

It's just a hobbyist hack.

gowthamidentity
Posts: 10
Joined: Mon Dec 18, 2017 12:25 pm

Re: Python2 not compatible with HSC sensor library

Wed Jan 03, 2018 2:21 pm

Jednorozec wrote:
Tue Jan 02, 2018 4:13 pm
This may be your problem http://www.informit.com/articles/article.aspx?p=1439189
Thank you so much. This solved my issue. ;) ;)

Return to “General discussion”