• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Accessing ThingSpeak in micropython

Q&A Forum › Category: ESP32 › Accessing ThingSpeak in micropython
0 Vote Up Vote Down
vgershman asked 4 years ago

Hello,

Is there a simple example of how to publish a value to ThingSpeak site in MicroPython?

I am trying to send out IFTTT email and to send data to ThingSpeak.

IFTTT works, but ThingSpeak plot is not updating. I  use the following to send data to ThingSpeak:

i copied part of code with changes to port #)  from blog.gypsyengineer.com/en/diy-electronics/micropython-esp8266-sending-data-to-thingspeak.html

#Replace with your unique Thing Speak WRITE API KEY
api_key_W_ThingSpeak = 'xxx'
resource = "/update?api_key="
#Thing Speak API server
server = "api.thingspeak.com"

API_THINGSPEAK_HOST = 'api.thingspeak.com'
#API_THINGSPEAK_PORT = 443
API_THINGSPEAK_PORT = 80
THINGSPEAK_WRITE_KEY = api_key_W_ThingSpeak # put your key here

THINGSPEAK_POST_TEMPLATE = """
POST /update HTTP/1.1
Host: api.thingspeak.com
Connection: close
X-THINGSPEAKAPIKEY: %s
Content-Type: application/x-www-form-urlencoded
Content-Length: %d
%s
"""

print('send data to ThingSpeak')
s = socket.socket()
ai = socket.getaddrinfo(API_THINGSPEAK_HOST, API_THINGSPEAK_PORT)
print ('ai =', ai)

addr = ai[0][-1]
print ('addr = ', addr)

s.connect(addr)
# was before - s = ssl.wrap_socket(s)
print ('s = ', s)

t = i
h = 66.0
data = 'field1=%.2f&field2=%.2f' % (t, h)
http_data = THINGSPEAK_POST_TEMPLATE % (THINGSPEAK_WRITE_KEY, len(data), data)
# was before - resp = s.write(http_data.encode())
resp = s.write(http_data)
print ('s.write resp = ', resp)
s.close()

thanks

Question Tags: ESP32, MicroPython, thingSpeak
7 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Can you post the response that is printed in the Terminal window? Did you double-check that you’ve entered all the correct API keys? Thanks! 

0 Vote Up Vote Down
vgershman answered 4 years ago

Hi Rui,

Below are the printouts:

i = 0 water sens = 0.00 pwr9v = 2.77
send data to ThingSpeak
ai = [(2, 1, 0, 'api.thingspeak.com', ('34.226.171.107', 80))]
addr = ('34.226.171.107', 80)
s = <socket>
s.write resp = 195

The comments with “was before” are what it used to be in the original code when SSL was used.

The key should be ok, because  I re-ran your example from ESP8266 course with ESp8266 in Arduino and i got the data to update in ThingSpeak plots.

Today i ran just the example from the link above and it did not work either. He used ESP8266 and i used ESp32 , but this should not matter in this case – correct?

So, i do not want to debug his code and just would like to know the how to send HTTP request in MicroPython like you did in your C code for Arduino in the ESP8266 course.

Thanks

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Thanks for posting the debugging information, but I’m not exactly sure what’s missing from that code.

Can you try the following script and let me know if it works for you? (Just enter your SSID, password, and Thing Speak API key).

https://gist.github.com/RuiSantosdotme/914ea499fad94da98f22ca5cf2885f37

Does it work? If not, please post what’s printed in the Terminal window.

Thanks!

0 Vote Up Vote Down
vgershman answered 4 years ago

Hi Rui,
That is exactly what i needed:) The syntax for ThingSpeak HTTP call when using MicroPython . This is one thing i do not like about Web programming that calls could be inconsistent – or i have to learn it better :). I noticed that ThingSpeak was missing from ESP32 with MicroPython course.
Thanks a lot, man!
P.S. In my frustration i rebuilt the program in Arduino C. I used “IFTTTWebhook.h” , that found via Google, for IFTT and it was very handy. Now i see one advantage of MicroPython over :  Conversions and string manipulations are easier in MicroPython. However, Arduino IDE is a lot more stable than uPyCraft. So, i am kind of at crossroads.

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Just to double-check.

  • Did my exact code posted earlier worked for you? Can you post readings to Thing Speak?

Did you try the other IDE that I recommend? Thonny IDE, in my opinion it’s more stable than uPyCraft IDE (but it has less features).

0 Vote Up Vote Down
vgershman answered 4 years ago
  • yep, your exact code worked great. I am able to post the values to ThingSpeak.

I had found Thonny IDE to be more unstable than uPyCraft. I think, i will stick with Micropython and uPyCraft.The loading time is faster that Arduino and again, less hassle with coding than C.
Thanks again, Rui!

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Awesome! I’m glad the code worked. I can point anyone trying to do the same, to that script.

I’m always looking for new IDEs, so when I find a better MicroPython IDE, I’ll write a guide about it.

Regards,
Rui

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.