HTTP POST from Arduino/ESP8266/ESP32 How to send parameters (x-www-form-urlencoded) using micropython!
-
I don't have ANY experience with micropython..but I would like to use blockly features of UI flow for development thus I would appreciate if anyone could help me out how to translate this simple http post with parameters into micropython?
http.begin("https://api.46elks.com/a1/sms"); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); http.setAuthorization("XXXXX", "YYYYYY"); int httpCode = http.POST("'from:+XXXXX','to:+XXXXX','message:Hej'");Here is the rest of the code! Thank you!
#include <M5StickC.h> #include <Arduino.h> #include <WiFi.h> #include <WiFiMulti.h> #include <HTTPClient.h> #define USE_SERIAL Serial WiFiMulti wifiMulti; void setup() { USE_SERIAL.begin(115200); USE_SERIAL.println(); USE_SERIAL.println(); USE_SERIAL.println(); for(uint8_t t = 4; t > 0; t--) { USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); USE_SERIAL.flush(); delay(1000); } wifiMulti.addAP("YYYY", "ZZZZZ"); } void loop() { // wait for WiFi connection if((wifiMulti.run() == WL_CONNECTED)) { HTTPClient http; USE_SERIAL.print("[HTTP] begin...\n"); // configure traged server and url http.begin("https://api.46elks.com/a1/sms"); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); http.setAuthorization("XXXXX", "YYYYYY"); int httpCode = http.POST("'from:+XXXXX','to:+XXXXX','message:Hej'"); USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header httpCode = http.GET(); // httpCode will be negative on error if(httpCode > 0) { // HTTP header has been send and Server response header has been handled USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); USE_SERIAL.println(payload); } } else { USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } delay(10000); } -
I've been trying something similar and it appears to be impossible with uiflow. Recently saw someone had success with raw micropython
-
My friend and colleague programmed this..
https://github.com/46elks/46elks-getting-started/blob/master/code-examples/MicroPython/https_post_with_micropython.py
If you would like to use 46elks Telco API mentioned in the code, register here and contact us at help@46elks.com for help.# Rudimentary HTTPS POST request using MicroPython without any dependencies. # Supports Basic Auth and encodes data as x-www-form-urlencoded. # # Written by Johannes Ridderstedt <johannesl@46elks.com> # This code is public domain. Use freely. from ubinascii import b2a_base64 import usocket import ussl # API credidentials username = 'u2c11ef65b429a8e16ccb1f960d02c734' password = 'C0ACCEEC0FAFE879189DD5D57F6EC348' def quote( value ): l = [] for ch in value.encode( 'utf-8' ): if ch == b' ': l.append( b'+' ) elif ch > 32 and ch < 128 and ch not in b'?=': l.append( b'%c' % ch ) else: l.append( b'%%%02X' % ch ) return b''.join( l ) def api_post( path, data ): info = usocket.getaddrinfo( 'api.46elks.com', 443 ) ip = info[0][-1] args = [] for key in data: args.append( quote( key ) + '=' + quote( data[key] ) ) content = b'&'.join( args ) lines = [ b'POST /a1/%s HTTP/1.0' % path, b'Authorization: Basic %s' % b2a_base64( username + ':' + password )[:-1], b'Content-type: application/x-www-form-urlencoded', b'Content-Length: %d' % len( content ), b'', content ] conn = usocket.socket() conn.connect( ip ) conn = ussl.wrap_socket( conn ) conn.write( b'\r\n'.join( lines ) ) print( conn.read(4096).decode('utf-8') ) conn.close() sms = { 'to': '+46704508449', 'from': 'MicroPython', 'message': 'Hello from MicroPython!' } api_post( '/sms', sms )
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login