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[-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()
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!
Below are the printouts:
i = 0 water sens = 0.00 pwr9v = 2.77 send data to ThingSpeak ai = [(2, 1, 0, 'api.thingspeak.com', ('188.8.131.52', 80))] addr = ('184.108.40.206', 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 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).
Does it work? If not, please post what’s printed in the Terminal window.
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.
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).
- 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!