Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiFiEsp: Send Post message from Arduino Mega + ESP-01 issue #219

Open
LeopoldoKnett opened this issue Jul 29, 2022 · 8 comments
Open

WiFiEsp: Send Post message from Arduino Mega + ESP-01 issue #219

LeopoldoKnett opened this issue Jul 29, 2022 · 8 comments

Comments

@LeopoldoKnett
Copy link

I'm sending a Json data.
For a short message it works fine.
But it does not for a longer message.
Is there a data size limit?

I'm sending a POST message to a https server

Thanks and best regards,
Leopoldo

@fscheu
Copy link

fscheu commented Jul 29, 2022

Are you sending the "Content-Length" parameter in your POST message with the length of the JSON?

@LeopoldoKnett
Copy link
Author

Yes! This is the code:

  ...
  char server[] = "api.sendgrid.com";
  ...
  Serial.println("Starting connection to server...");
  if (client.connect(server, 443)) {
    Serial.println("Connected to server");
    String data = "{\"personalizations\": [{\"to\": [{\"email\": \"*****.*****@gmail.com\"}]}],\"from\": {\"email\": \"*****@*****.com.br\"},\"subject\": \"Alerta de temperatura\",\"content\": [{\"type\": \"text/plain\", \"value\": \"Temperatura 30,1C\"}]}";
    Serial.println(data);
    Serial.println(String(data.length()));    
    client.println("POST /v3/mail/send HTTP/1.1");
    client.println("Host: api.sendgrid.com:443");
    client.println("Authorization: Bearer SG.******************************************************************");
    client.println("Content-Type: application/json");
    client.println("Connection: keep-alive");
    client.println("Content-Length: " + String(data.length()));
    client.println(data);
    client.println();
  }

There is no error at the Serial Monitor.

This is the debug output (Level 4):

21:58:09.167 -> SEND OK
21:58:09.167 -> > getClientState 3
21:58:09.199 -> ----------------------------------------------
21:58:09.262 -> >> AT+CIPSTATUS21:58:09.262 ->
21:58:09.262 -> +IPD,3,452,3.64.200.27,443:HTTP/1.1 400 Bad Request
21:58:09.326 -> Server: nginx
21:58:09.358 -> Date: Sat, 30 Jul 2022 20:58:09 GMT
21:58:09.390 -> Content-Type: text/html
21:58:09.423 -> Content-Length: 248
21:58:09.423 -> Connection: close
21:58:09.456 -> Strict-Transport-Security: max-age=600; includeSubDomains
21:58:09.519 ->
21:58:09.519 -> <html>
21:58:09.519 -> <head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
21:58:09.616 -> <body>
21:58:09.616 -> <center><h1>400 Bad Request</h1></center>
21:58:09.647 -> <center>The plain HTTP request was sent to HTTPS port</center>
21:58:09.713 -> <hr><center>nginx</center>
21:58:09.744 -> </body>
21:58:09.744 -> </html>
21:58:09.777 -> STATUS:3
21:58:09.777 -> +CIPSTATUS:3,"TCP","3.64.200.27",443,45897,0

I appreciate any help
Thanks

@fscheu
Copy link

fscheu commented Jul 30, 2022

I think the problem is that you are making a post to a 443 port. This port is used for HTTPS connections and you are doing a standard HTTP.
In order to use HTTPS, I believe you need to have a certificate installed on your device. It is something that I never did so I can't help you with that.
You said that for a shorter message it works ¿Are you sure that the only difference is the length of the message?

@LeopoldoKnett
Copy link
Author

The situation is now a little different after I figured out how to see the full log at level 4.
I've noticed that it doesn't work even with short messages either.
I'll change the title of this issue to "WiFiEsp: Send Post message from Arduino Mega + ESP-01 issue."

@LeopoldoKnett LeopoldoKnett changed the title data size limit is too short WiFiEsp: Send Post message from Arduino Mega + ESP-01 issue Jul 31, 2022
@JAndrassy
Copy link

add an empty line between headers and the data. it is required by the http protocol. your error is "bad request" as you can see.

@LeopoldoKnett
Copy link
Author

I made some changes:

  1. Tip from fscheu: client.connectSSL(server, 443);
  2. Tip from JAndrassy's suggestion: Added an empty line between header and data.

Now the code is:

char server[] = "api.sendgrid.com";
...
  if (client.connectSSL(server, 443)) {
    Serial.println("Connected to server");
    String data = "{\"personalizations\": [{\"to\": [{\"email\": \"********.*****@gmail.com\"}]}],\"from\": {\"email\": \"*******@*****.com.br\"},\"subject\": \"Alerta de temperatura\",\"content\": [{\"type\": \"text/plain\", \"value\": \"Temperatura 30,1C\"}]}";
    Serial.println(data);
    Serial.println(String(data.length()));    
    client.println("POST /v3/mail/send HTTP/1.1");
    client.println("Host: api.sendgrid.com:443");
    client.println("Authorization: Bearer SG.******************************************************************");
    client.println("Content-Type: application/json");
    client.println("Connection: keep-alive");
    client.println("Content-Length: " + String(data.length()));
    client.println();
    client.println(data);
  }
...

And this is the Debug output:

10:22:48.221 -> Starting connection to server...
10:22:48.254 -> [WiFiEsp] Connecting to api.sendgrid.com
10:22:48.286 -> > startClient api.sendgrid.com 443
10:22:48.351 -> ----------------------------------------------
10:22:48.383 -> >> AT+CIPSSLSIZE=4096
10:22:48.416 ->
10:22:48.416 -> OK
10:22:48.416 -> ---------------------------------------------- > 0
10:22:48.480 ->
10:22:48.480 -> ----------------------------------------------
10:22:48.513 -> >> AT+CIPSTART=3,"SSL","api.sendgrid.com",443
10:22:48.842 ->
10:22:48.842 -> ERROR
10:22:48.842 -> ---------------------------------------------- > 1
10:22:48.874 ->
10:22:48.906 -> > getClientState 3
10:22:48.906 -> ,CLOSED
10:22:48.906 ->
10:22:48.939 -> Dirty characters in the serial buffer! > 10
10:22:48.972 -> ----------------------------------------------
10:22:49.004 -> >> AT+CIPSTATUS
10:22:49.035 -> STATUS:4
10:22:49.035 ->
10:22:49.035 -> OK
10:22:49.068 -> No start tag found: 0
10:22:49.068 -> ---------------------------------------------- >
10:22:49.132 ->
10:22:49.132 -> Not connected
10:22:49.165 ->
10:22:49.165 -> Disconnecting from server...

@JAndrassy
Copy link

it is not able to connect. the site uses TLS 1.2. the AT firmware doesn't support TLS 1.2.

there is a 'fake' AT firmware which supports TLS 1.2
https://github.com/JiriBilek/ESP_ATMod

and it is better with my WiFiEspAT library

@LeopoldoKnett
Copy link
Author

I just found an article on how to update the ESP-01 firmware...
I'll need to buy an adapter for this...
As soon as I have the adapter and have done the update I will come back here.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants