Giter VIP home page Giter VIP logo

Comments (8)

fscheu avatar fscheu commented on August 10, 2024

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

from wifiesp.

LeopoldoKnett avatar LeopoldoKnett commented on August 10, 2024

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

from wifiesp.

fscheu avatar fscheu commented on August 10, 2024

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?

from wifiesp.

LeopoldoKnett avatar LeopoldoKnett commented on August 10, 2024

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."

from wifiesp.

JAndrassy avatar JAndrassy commented on August 10, 2024

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.

from wifiesp.

LeopoldoKnett avatar LeopoldoKnett commented on August 10, 2024

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...

from wifiesp.

JAndrassy avatar JAndrassy commented on August 10, 2024

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

from wifiesp.

LeopoldoKnett avatar LeopoldoKnett commented on August 10, 2024

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

from wifiesp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.