Me and a friend of mine figured out how to do a HTTP post with parameters in micropython. Me used it for sending SMS, but it can be modified to anything else.
https://forum.m5stack.com/topic/1902/http-post-from-arduino-esp8266-esp32-how-to-send-parameters-x-www-form-urlencoded-using-micropython/3
Latest posts made by rbrakus
-
RE: Using a API to send out a SMS
-
RE: HTTP POST from Arduino/ESP8266/ESP32 How to send parameters (x-www-form-urlencoded) using 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 )
-
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); }
-
RE: Initiate an action with Button A or B
Thanks for the reply
Found the tutorial too!
http://forum.m5stack.com/topic/42/lesson-2-buttons-menu -
Initiate an action with Button A or B
I would like to have it so a get request with arguments gets run when an action is made, for example pressing button A or B. I'm new with M5stickC and don't know how to do that.
This is the code for sending a SMS with a delay... but executing a API call with a button is unknown for me...#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("xxxxx", "xxxxxx"); } 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", "xxxxxx"); int httpCode = http.POST("from=+xxxxx&to=+xxxxxx&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); }
-
Using a API to send out a SMS
After running this code, I get no errors only a blank screen. Could anyone tell what I did wrong at first glance?
from m5stack import *
from m5ui import *
from uiflow import *fields = {
'from': 'PythonElk',
'to': '+XXXXXXXX',
'message': 'Test Message To your phone.'
}
try:
req = urequests(
method='POST',
url='https://api.46elks.com/a1/SMS',
headers=fields,
data={'XXXXXXXXXXXXXXXXXXXXX',' YYYYYYYYYYYYYYYYYYYY'}
)
except Exception as e:
print(e)
import urequests
import ujson
setScreenColor(0x111111)
auth = (
'XXXXXXXXXXXXXXXXXXXXXXXX',
'YYYYYYYYYYYYYYYYYYYYYY'
)
fields = {
'from': 'PythonElk',
'to': '+XXXXXXXX',
'message': 'Test Message To your phone.'
}
headers = {'Content-Type': 'application/json'}
try:
#urequests.request(method='POST', url='https://api.46elks.com/a1/SMS', auth, fields,headers
post_data = ujson.dumps('XXXXXXXXXXXXXXXXXXXXXXX','YYYYYYYYYYYYYYYYYYYYYYYYYYY')
request_url = 'https://api.46elks.com/a1/SMS'
res = requests.post(request_url, headers = {'content-type': 'application/json'}, data = post_data,json=fields)
print(res)req = urequests.request(method='POST', url="import urequests setScreenColor(0x222222) fields = { 'from': 'PythonElk', 'to': '+XXXXXXX', 'message': 'Test Message To your phone.' } try: req = urequests.request( method='POST', url='https://api.46elks.com/a1/SMS', headers=fields, data=('XXXXXXXXXXXXXXXX','YYYYYYYYYYYYYYYYYYYYY') ) except: print(req.status_code)",json={'user':'a'}, headers={})
except Exception as e:
print(e)