TypeError: can't convert NoneType to int

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

TypeError: can't convert NoneType to int

Post by nikhiledutech » Mon Feb 26, 2018 12:17 pm

hello ,
I am interfacing color sensor with stm32f4 disc board.
i am facing error of "" TypeError: can't convert NoneType to int "" . can anyone help me in this.
# main.py -- put your code here!
# /**I2C1 GPIO Configuration
# PB8 ------> I2C1_SCL
# PB9 ------> I2C1_SDA

import pyb
from pyb import I2C


#/* Control Register */
COLOR_SENSOR_REG_CTL = const(0x80)
REG_CTL_POWER = const(0x01)
REG_CTL_ADC_EN = const(0x02)

#/* Color read Register */
COLOR_SENSOR_REG_CMD = const(0x90) #The REG_BLOCK_READ and REG_GREEN_LOW direction are the same

REG_GREEN_LOW = const(0x90)
REG_GREEN_HIGH = const(0x91)
REG_RED_LOW = const(0x92)
REG_RED_HIGH = const(0x93)
REG_BLUE_LOW = const(0x94)
REG_BLUE_HIGH = const(0x95)
REG_CLEAR_LOW = const(0x96)
REG_CLEAR_HIGH = const(0x97)

GREEN = 0
RED = 1
BLUE = 2
CLEAR = 3
NUM_COLORS = 4


i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate=200000)
sensor = i2c.scan()


"""
* @brief To transmit data on I2C
* @param Wr_Addr Register address on which Wr_Data will be written
* @param Wr_Data data
* @return value if data written successful, it will return 0 otherwise 1
"""

def I2C_WriteRegister(Wr_Addr, Wr_Data):
global i2c, sensor
i2c.mem_write(Wr_Data,sensor[1],Wr_Addr,timeout=1000)



def I2C_ReadRegister(Rd_Addr):

global i2c, sensor
i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)




# @brief This Function configures and Initializes I2C1

def BSP_COLOR_SENSOR_I2C1_Init():
global i2c, sensor

i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate = 100000)



# @brief This function is for De-Initializing I2C1

def BSP_COLOR_SENSOR_I2C1_MspDeInit():

i2c.deinit()



def ColorSensor_Init():
global i2c, sensor

BSP_COLOR_SENSOR_I2C1_Init()
sensor = i2c.scan() #Only sensor will be detected in list
if i2c.mem_write(1,sensor[0],1,timeout=500) == False:
# if((i2c.is_ready(sensor[0])):

while(1):
print("Unable to initialize color sensor \n")




def ColorSensor_ReadColor(colour):

ColorRegistersCommand = 0
colorvalue = bytearray(2)
color = 0
ColorRegisters0Command = REG_GREEN_LOW + (2 * (colour))
colorvalue[0] = I2C_ReadRegister(ColorRegistersCommand)
colorvalue[1] = I2C_ReadRegister((ColorRegistersCommand + 1))
color = ((colorvalue[1]<<8)*256)|colorvalue[0]
return color



def main():
colour = green_ = red_ = blue_ = clear_ = None
displayvalue = bytearray(5)
tmp = maxColor = None

while True:

# for (colour = GREEN; colour <= NUM_COLORS; colour++):
#
colour = GREEN
if (colour <= NUM_COLORS):
displayvalue[colour] = ColorSensor_ReadColor(colour)
colour+=1

green_ = displayvalue[0]
red_ = displayvalue[1]
blue_ = displayvalue[2]
clear_ = displayvalue[3]

red_ = red_ * 1.70
blue_ = blue_ * 1.35

maxColor = max(red_, green_)
maxColor = max(maxColor, blue_)

if(maxColor > 255):

tmp = 250.0/maxColor
green_ *= tmp
red_ *= tmp
blue_ *= tmp


print(red_)
print(green_)
print(blue_)

pyb.delay(500)


while True:
main()
*In the bolded line in the code , i am facing this issue. can anyone rectify this out .
Last edited by nikhiledutech on Fri Apr 13, 2018 6:36 am, edited 1 time in total.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: TypeError: can't convert NoneType to int

Post by cefn » Mon Feb 26, 2018 1:21 pm

You do not return the result of mem_read() in your function so it returns None, which can't be assigned as integer entry in a bytearray...

Code: Select all

def I2C_ReadRegister(Rd_Addr):
    global i2c, sensor
    i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)	
Try...

Code: Select all

def I2C_ReadRegister(Rd_Addr):
    global i2c, sensor
    return i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)	
Though there may be other issues. Crazy variable names using British and American spellings to mean different things! I don't think there's an obfuscated python contest, though :) https://www.ioccc.org/

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: TypeError: can't convert NoneType to int

Post by nikhiledutech » Tue Feb 27, 2018 5:04 am

When i add return in read register function. it gives me the error of """ TypeError: can't convert bytes to int
""" in the following line
colorvalue[0] = I2C_ReadRegister(ColorRegistersCommand)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: TypeError: can't convert NoneType to int

Post by pythoncoder » Tue Feb 27, 2018 9:08 am

Your I2C_ReadRegister function returns a bytes object which is an immutable byte array. You're trying to put a bytes object into a single element of a bytearray; such an element can only hold a single byte. One option is to change I2C_ReadRegister to return a single byte:

Code: Select all

def I2C_ReadRegister(Rd_Addr):
    global i2c, sensor
    return i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)[0]  # Return one byte
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: TypeError: can't convert NoneType to int

Post by nikhiledutech » Tue Feb 27, 2018 9:52 am

I tried the above code and it throws up the new error .of "" IndexError: list index out of range"" in the blod line in the code.



def I2C_ReadRegister(Rd_Addr):

global i2c, sensor
return i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)[0]

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: TypeError: can't convert NoneType to int

Post by pythoncoder » Tue Feb 27, 2018 11:13 am

It sounds to me as if in some circumstances the device is returning no data. You could instrument the code:

Code: Select all

def I2C_ReadRegister(Rd_Addr):
    global i2c, sensor
    result = i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)[0]
    if result is None:
        print('No data returned from', Rd_Addr)
    else:  # result is a bytes object
        return result[0]  # Return a single byte
This won't fix the bug, but it might help you determine which address is causing the problem.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: TypeError: can't convert NoneType to int

Post by nikhiledutech » Wed Feb 28, 2018 6:51 am

Hello,
I altered my code as per code given by you. If result is none, it should print No data returned. But it's not printed, means I am getting some value. And the final result printed for R, G, B color was 0, which is not True.

So i thought to check the value of the return byte in the read register function. So i changed that else condition little bit. Instead of using return, I printed out the value. And it gives me error of """ TypeError: can't convert None Type to int """.

So i cant understand, if its a None type why my if condition didn't get executed ?


# main.py -- put your code here!
# /**I2C1 GPIO Configuration
# PB8 ------> I2C1_SCL
# PB9 ------> I2C1_SDA

import pyb
from pyb import I2C


#/* Control Register */
COLOR_SENSOR_REG_CTL = const(0x80)
REG_CTL_POWER = const(0x01)
REG_CTL_ADC_EN = const(0x02)

#/* Color read Register */
COLOR_SENSOR_REG_CMD = const(0x90) #The REG_BLOCK_READ and REG_GREEN_LOW direction are the same

REG_GREEN_LOW = const(0x90)
REG_GREEN_HIGH = const(0x91)
REG_RED_LOW = const(0x92)
REG_RED_HIGH = const(0x93)
REG_BLUE_LOW = const(0x94)
REG_BLUE_HIGH = const(0x95)
REG_CLEAR_LOW = const(0x96)
REG_CLEAR_HIGH = const(0x97)

GREEN = 0
RED = 1
BLUE = 2
CLEAR = 3
NUM_COLORS = 4


i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate=200000)
sensor = i2c.scan()


"""
* @brief To transmit data on I2C
* @param Wr_Addr Register address on which Wr_Data will be written
* @param Wr_Data data
* @return value if data written successful, it will return 0 otherwise 1
"""

def I2C_WriteRegister(Wr_Addr, Wr_Data):
global i2c, sensor
i2c.mem_write(Wr_Data,sensor[1],Wr_Addr,timeout=1000)


#def I2C_ReadRegister(Rd_Addr):
# global i2c, sensor
# result = i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)[0]
# if result is None:
# print('No data returned from', Rd_Addr)
# else: # result is a bytes object
# return result[0] # Return a single byte
def I2C_ReadRegister(Rd_Addr):

global i2c, sensor
result = i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)
if result is None:
print("No data returned from",Rd_Addr)
else:
return result[0]
#print(result[0]) This gave me above mentioned Type Error.


# @brief This Function configures and Initializes I2C1

def BSP_COLOR_SENSOR_I2C1_Init():
global i2c, sensor

i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate = 100000)



# @brief This function is for De-Initializing I2C1

def BSP_COLOR_SENSOR_I2C1_MspDeInit():

i2c.deinit()



def ColorSensor_Init():
global i2c, sensor

BSP_COLOR_SENSOR_I2C1_Init()
sensor = i2c.scan() #Only sensor will be detected in list
# if i2c.mem_write(1,sensor[0],1,timeout=500) == False:
# if((i2c.is_ready(sensor[0])):
if i2c.send(1,sensor[0]):
while(1):
print("Unable to initialize color sensor \n")




def ColorSensor_ReadColor(colour):

ColorRegistersCommand = 0
colorvalue = bytearray(2)
color = 0
ColorRegisters0Command = REG_GREEN_LOW + (2 * (colour))
colorvalue[0] = int(I2C_ReadRegister(ColorRegistersCommand))
colorvalue[1] = I2C_ReadRegister((ColorRegistersCommand + 1))
color = ((colorvalue[1]<<8)*256)|colorvalue[0]
return color



def main():
colour = green_ = red_ = blue_ = clear_ = None
displayvalue = bytearray(5)
tmp = maxColor = None

while True:

# for (colour = GREEN; colour <= NUM_COLORS; colour++):
#
colour = GREEN
if (colour <= NUM_COLORS):
displayvalue[colour] = ColorSensor_ReadColor(colour)
colour+=1

green_ = displayvalue[0]
red_ = displayvalue[1]
blue_ = displayvalue[2]
clear_ = displayvalue[3]

red_ = red_ * 1.70
blue_ = blue_ * 1.35

maxColor = max(red_, green_)
maxColor = max(maxColor, blue_)

if(maxColor > 255):

tmp = 250.0/maxColor
green_ *= tmp
red_ *= tmp
blue_ *= tmp


print("R=",red_)
print("G=",green_)
print("B=",blue_)

pyb.delay(500)


while True:
main()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: TypeError: can't convert NoneType to int

Post by pythoncoder » Wed Feb 28, 2018 9:36 am

OK, we need to figure out what is being returned. Try this:

Code: Select all

def I2C_ReadRegister(Rd_Addr):
    global i2c, sensor
    result = i2c.mem_read(1, sensor[0], Rd_Addr, timeout=1000)
    if isinstance(result, bytes):
    	if len(result) > 0:
            return result[0]  # Return a single byte
        else:
            print('Zero length bytes object returned')
    else:  # result is not a bytes object
        print(type(result))
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: TypeError: can't convert NoneType to int

Post by nikhiledutech » Wed Feb 28, 2018 9:49 am

Hey Sir,
As you know i am currently using async method to print data on LCD. I wanted to know how to print data using position on LCD,.


For ex:
On terminal we can directly print data on terminal and string together using simple print command.

ex:
x = 2
y = 3
print("data:",x,"as" ,y )
result: data 2 as 3

So is it possible to print string after the data is printed on lcd.
lcd[0] = "value:{}".format(concentration) "Here i want another string to be displayed"

So how to print that on LCD?

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: TypeError: can't convert NoneType to int

Post by nikhiledutech » Wed Feb 28, 2018 10:50 am

Regarding the I2C_ReadRegister function issue.

I have altered code and tested it.

def I2C_ReadRegister(Rd_Addr):
global i2c, sensor
result = i2c.mem_read(1, sensor[0], Rd_Addr, timeout=1000)
if isinstance(result, bytes):
if len(result) > 0:
# print(result[0])
return result[0] # Return a single byte

else:
print('Zero length bytes object returned')
else: # result is not a bytes object
print(type(result))
Above code didn't print out any data. It only printd values of R_G_B = 0 which isn't true. But this also means that the return value from the read Register function is byte. So i just used print command to print out result[0] and it gives me 1,0,1,0 goes on alternatively.

Post Reply