error : object with buffer protocol required

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

error : object with buffer protocol required

Post by bellad » Sat Nov 13, 2021 2:58 pm

hello,
with my little prog i have error : object with buffer protocol required

can you help , thank

Code: Select all

from machine import Pin, ADC
import time
amp = ADC(Pin(34))# amperage du moteur
amp.atten(ADC.ATTN_11DB)
BTN = Pin(13, Pin.IN, Pin.PULL_UP)
a=0
b=0
while True:
  time.sleep(0.25)
  a = amp.read() * 0.000805
  if a > b :
    b=a
    print(b)#amperage le plus fort
  if BTN.value() == 0 :
    f = open('a.txt', 'w')  # Fichier ampere
    f.write(b)  # line 19
    f.close()
    break
with webrepl :

Code: Select all

>>> import amp                                                                                                                                        
1.46671                                                                                                                                               
1.480395                                                                                                                                              
1.491665                                                                                                                                              
1.50374                                                                                                                                               
1.520645                                                                                                                                              
1.53111                                                                                                                                               
Traceback (most recent call last):                                                                                                                    
  File "<stdin>", line 1, in <module>                                                                                                                 
  File "amp.py", line 19, in <module>                                                                                                                 
TypeError: object with buffer protocol required  

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: error : object with buffer protocol required

Post by Roberthh » Sat Nov 13, 2021 5:02 pm

f.write() requires a string or byte array or bytes object as argument, not a number. You have to convert b to a suitable data type, like with str( b).

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: error : object with buffer protocol required

Post by bellad » Sat Nov 13, 2021 6:36 pm

oups !! , thank you
sorry

Post Reply