Page 1 of 1

thingSpeak/ESP8266_convert Str=> Int

Posted: Tue Mar 19, 2019 6:05 am
by samerou
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 ?

Re: thingSpeak/ESP8266_convert Str=> Int

Posted: Tue Mar 19, 2019 12:24 pm
by Christian Walther
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)
?

Re: thingSpeak/ESP8266_convert Str=> Int

Posted: Tue Mar 19, 2019 1:20 pm
by samerou
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))