Dimas
June 17, 2022, 4:46am
1
Hello,
I'm trying to connect an app made in Mit App Inventor to an Amazon server with certificate and key (.pem.crt ; .pem).
With the example code from the Python documentation (HTTPS - AWS IoT Core ) I was able to make a Post work, but I still couldn't with Mit App Inventor, I would like some help, it's for a final course conclusion work.
Basically it uses a certificate (certificate.pem.crt) and a key (key.pem) that I already have and sends a message to the server using a link (with endpoint and topic wherever it is written).
Part of the code in Python (which uses the requests library):
....
-# create and format values for HTTPS request
publish_url = 'https://' + args.endpoint + ':8443/topics/' + args.topic + '?qos=1'
publish_msg = args.message.encode('utf-8')
-# make request
publish = requests.request('POST',
publish_url,
data=publish_msg,
cert=[args.cert, args.key])
.....
I would like some help on how to assemble this python request in Mit App Inventor.
Thanks!
TIMAI2
June 25, 2022, 3:00pm
2
Dimas:
-# create and format values for HTTPS request
publish_url = 'https://' + args.endpoint + ':8443/topics/' + args.topic + '?qos=1'
publish_msg = args.message.encode('utf-8')
-# make request
publish = requests.request('POST',
publish_url,
data=publish_msg,
cert=[args.cert, args.key])
Seems we need to make a web component url and POST request from the above.
Please show your relevant blocks on what you have so far ?
Dimas
June 25, 2022, 6:50pm
3
Thanks for your return
I've tried several ways, this was the last one.
If you can help me I would really appreciate it! To speed up because of time, my email is removed by mod , if we get the solution we will post the solution here.
Peter
June 25, 2022, 7:06pm
4
Please no personal info on the community. I removed you mail address.
TIMAI2
June 25, 2022, 8:10pm
6
You do not need the last block
What do you get as a responseContent in the Web1.GotText event ?
Dimas
June 25, 2022, 8:36pm
7
I removed the last block as you said, the print of the error :
TIMAI2
June 25, 2022, 8:51pm
8
Hmmm, not even getting close to a connection.
You say that with the details you have you can connect using python ?
Can you try with curl
(the next tab on the page you linked to)
That syntax is easier (for me!) to convert to AI2 blocks code
Also just looking at the syntax for curl (and python) both seem to point to files as opposed the content of the files.....
Dimas
June 25, 2022, 9:05pm
9
I did it using Curl and it works too.
The certificate and key are in the same folder and the file name and path is placed in the command.
TIMAI2
June 25, 2022, 9:10pm
11
If you replace the:
--cert certificate.pem.crt
with
-cert "-----BEGIN CERTIFICATE-----MIIDWj....."
and likewise with the private key
does it still work ?
Dimas
June 25, 2022, 9:22pm
12
There is a problem when placing due to the high number of characters in cmd.
TIMAI2
June 25, 2022, 9:36pm
13
Try this:
I also notice that the url you used in curl is slightly different to the one you showed earlier:
https://a3sry5stat6715-ats.iot.us-east-2.amazonaws.com:8443/topics/esp32/sub?qos=1
https://a3sry5stat6715-ats.iot.us-east-2.amazonaws.com:8443/topics/esp32/pub?qos=1
Is it sub or pub ?
Dimas
June 25, 2022, 9:59pm
14
I did as you said
but did not work
To facilitate the files are here:
https://drive.google.com/drive/folders/1BCyCpaRc_Vpmmy8m8fzcljYlplXtDokg?usp=sharing
They must be placed in the same folder where curl is.
Is pub.
TIMAI2
June 25, 2022, 10:11pm
15
Thanks for the files, I will try a few things...
Dimas
June 25, 2022, 10:52pm
16
Thank you very much for your attention!
To make it easier, could upload the files to the app and read the content of the files to put in the request, but I don't really know how to do it that way.
TIMAI2
June 26, 2022, 12:28am
17
Well, I have tried just about every configuration known to man for a POST request, and they all return the same 1103 error.
I get the feeling that the url is just not connecting, and possibly the aws server is expecting some sort of TLS header.
Perhaps @ewpatton can shed some light.
We know your python command works, and the curl command too...
curl --tlsv1.2 \
--cacert AmazonRootCA1.pem \
--cert certificate.pem.crt \
--key private.pem.key \
--request POST \
--data "{ \"message\": \"Hello, world\" }" \
"https://a3sry5stat6715-ats.iot.us-east-2.amazonaws.com:8443/topics/esp32/pub?qos=1"
If all else fails, then you may need to use an intermediate server to run the python/curl command and return the output.
Blocks - draggable
Dimas
June 26, 2022, 12:38am
18
Thanks for the attempts, I hope he (@ewpatton ) has a light.
Do you have any suggestions for an intermediate server?
TIMAI2
June 26, 2022, 10:45pm
19
If you want to run your own VPS then try IONOS @£1.20 per month, monthly contract (it may depend on where you are in the world)
Then setup php and curl and some bash scripts to connect the two.