thingSpeak/ESP8266_convert Str=> Int

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

thingSpeak/ESP8266_convert Str=> Int

Post by samerou » Tue Mar 19, 2019 6:05 am

Hello ,
I'm trying to create a code that allows me to send a specific values from a string and convert it to integer in order to display it in thingspeak charts but the problem that I keep receiving
Traceback (most recent call last)eError: can't convert 'int' object to str implicitly
so mycode is below:

Code: Select all

 i2c = I2C ( scl = Pin(5) , sda = Pin(4))
        accelerometer = mpu6050.accel(i2c)
        r = accelerometer.get_values()  
        print (r)
Result : {'GyZ': 111, 'GyY': 90, 'GyX': -70, 'Tmp': 40.1535, 'AcZ': 10004, 'AcY': 692, 'AcX': -12728}
What I tried to do is to extract a specific value from this result like this :

Code: Select all

   k = (str(r['GyX']))
result : -70
So now Itried to publish this values which is string and convert it into integer before publushing it
my code is below :

Code: Select all

  credentials = "channels/{:s}/publish/{:s}".format(THINGSPEAK_CHANNEL_ID, THINGSPEAK_CHANNEL_WRITE_API_KEY)
        #T2 = [map(int, x) for x in r]
        payload = "field1={:.1f}\n" + int(k)
        client.publish(credentials, payload)
So after executing this code I keep receiving
Traceback (most recent call last)eError: can't convert 'int' object to str implicitly
Anyone can light me on this ?

Christian Walther
Posts: 55
Joined: Fri Aug 19, 2016 11:55 am

Re: thingSpeak/ESP8266_convert Str=> Int

Post by Christian Walther » Tue Mar 19, 2019 12:24 pm

samerou wrote:
Tue Mar 19, 2019 6:05 am

Code: Select all

"field1={:.1f}\n" + int(k)
You are trying to add a string and an integer here, which does not work (it can’t tell whether you mean string concatenation or number addition). Judging from the format placeholder in the string, what you probably meant is

Code: Select all

"field1={:.1f}\n".format(int(k))
To find such problems, execute your program step by step on the REPL, trying to isolate what line and what expression causes the exception. This one is not MicroPython-specific, so you can also do that in Python 3 on your computer (which is what I just did because I’m not near my MicroPython boards).

I’m not sure though what your point is in converting the number r['GyX'] first to a string, then back to a number, then to a string again. Why not just

Code: Select all

k = r['GyX']
"field1={:.1f}\n".format(k)
?

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: thingSpeak/ESP8266_convert Str=> Int

Post by samerou » Tue Mar 19, 2019 1:20 pm

Christian Walther wrote:
Tue Mar 19, 2019 12:24 pm
samerou wrote:
Tue Mar 19, 2019 6:05 am

Code: Select all

"field1={:.1f}\n" + int(k)
You are trying to add a string and an integer here, which does not work (it can’t tell whether you mean string concatenation or number addition). Judging from the format placeholder in the string, what you probably meant is

Code: Select all

"field1={:.1f}\n".format(int(k))
To find such problems, execute your program step by step on the REPL, trying to isolate what line and what expression causes the exception. This one is not MicroPython-specific, so you can also do that in Python 3 on your computer (which is what I just did because I’m not near my MicroPython boards).

I’m not sure though what your point is in converting the number r['GyX'] first to a string, then back to a number, then to a string again. Why not just

Code: Select all

k = r['GyX']
"field1={:.1f}\n".format(k)
?
Thank you for answering me Christian , What I'm trying to do is to send values from this string
Result : {'GyZ': 111, 'GyY': 90, 'GyX': -70, 'Tmp': 40.1535, 'AcZ': 10004, 'AcY': 692, 'AcX': -12728}
like GyX which is -70 to database thingspeak but before that I need to convert it into integer

so I tried the first suggestion and it work :

Code: Select all

"field1={:.1f}\n".format(int(k))

Post Reply