Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Arduino] ESP8266 POST Json en https ?

Sujet résolu
    6 août 2018 à 8:03:14

    Salut les gars,

    J'essaye de faire "Push notification" de Onesignal depuis NodeMCU mais ça ne marche pas.

    Avec POSTMAN j'ai réussi à envoyer la notification avec ce code:

    POST /api/v1/notifications HTTP/1.1
    Host: onesignal.com
    Authorization: Basic YDFGGFHGHGHGFJTKUKILILILKLKLKKKKLKLKL
    Content-Type: application/json; charset=utf-8
    Cache-Control: no-cache
    Postman-Token: 1cvbhvb42d-7ec8-4aga-9256-e98e54hk4e97
    
    {
      "app_id": "sdgdfgf-5555-5635-1111-111111111",
      "included_segments": ["Active Users"],
      "android_channel_id": "2222222-5555-4444-444-4444444444f",
      "android_accent_color": "f90835",
      "large_icon":"iconApp.png",
      "android_background_layout":{"image": "http://website.ga/copy/uploads/image.jpg", "headings_color": "FFFF0000", "contents_color": "FF00FF00"},
      "data": {"foo": "bar"},
      "contents": {"en": "Hello your alarm est déclenché"}
    }



    Depuis NODEMCU:

    #include <ArduinoJson.h>
    #include <ESP8266WiFi.h>
    #include <WiFiClientSecure.h>
    
    char ssid[] = "TETOS";       // your network SSID (name)
    char password[] = "sfddgp7b";  // your network key
    
    WiFiClientSecure client;
    
    void setup() {
    
      Serial.begin(115200);
    
      // Set WiFi to station mode and disconnect from an AP if it was Previously connected
      WiFi.mode(WIFI_STA);
      WiFi.disconnect();
      delay(100);
    
      // Attempt to connect to Wifi network:
      Serial.print("Connecting Wifi: ");
      Serial.println(ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
      }
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      IPAddress ip = WiFi.localIP();
      Serial.println(ip);
    
    
      String host = "onesignal.com";
      String url = "/api/v1/notifications";
      
      String PostData = "{";
      PostData += "\"app_id\": \"sdgdfgf-5555-5635-1111-111111111\",";
      PostData += "\"included_segments\": [\"Active Users\"],";
      PostData += "\"android_channel_id\": \"2222222-5555-4444-444-4444444444f\",";
      PostData += "\"android_accent_color\": \"f90835\",";
      PostData += "\"large_icon\":\"iconApp.png\",";
      PostData += "\"android_background_layout\":{\"image\": \"http://website.ga/copy/uploads/image.jpg\", \"headings_color\": \"FFFF0000\", \"contents_color\": \"FF00FF00\"},";
      PostData += "\"data\": {\"foo\": \"bar\"},";
      PostData += "\"contents\": {\"en\": \"Hello your alarm est déclenché\"}}";
      
    if (client.connect(host, 443))
      {
      client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                     "Host: " + host + "\r\n" + 
                     "Authorization: Basic YDFGGFHGHGHGFJTKUKILILILKLKLKKKKLKLKL" + "\r\n" + 
                     //"Connection: close\r\n" +        
                     "Content-Type: application/json; charset=utf-8\r\n" +
                     "Content-Length: " + PostData.length() + "\r\n" +
                     "\r\n" + // This is the extra CR+LF pair to signify the start of a body
                     PostData + "\n");
      }
      else Serial.println("Client not connected!");
    }
    
    
    void loop() {
     
    int waitcount = 0;
    while (!client.available() && waitcount++ < 10) {
         delay(10);
     }
    
    // Read all the lines of the reply from server and print them to Serial
    Serial.println("Response: \n");
    while(client.available()){
        String line = client.readStringUntil('\r');
        Serial.print(line);
     }
    
    }




    Qu'es-ce que j'ai bien pu oublié?

    Merci!

    -
    Edité par kamoba 6 août 2018 à 20:32:20

    • Partager sur Facebook
    • Partager sur Twitter
      6 août 2018 à 11:49:40

      Bonjour,

      Tu n'as pas d'appel à client.connect, avant client.print, donc ça n'envoie rien nulle part.

      • Partager sur Facebook
      • Partager sur Twitter
        6 août 2018 à 18:42:40

        Merci @alexisdm j'ai corrigé mais ça ne change rien. :(

        J'ai essayé de le mettre dans une boucle et chaque fois j'ai "Client not connected!" o_O, donc il ne se connecte même pas.

        -
        Edité par kamoba 6 août 2018 à 19:12:20

        • Partager sur Facebook
        • Partager sur Twitter
          6 août 2018 à 19:00:02

          Avec WifiClientSecure et le https, tu devrais te connecter au port 443 pas 80.

          • Partager sur Facebook
          • Partager sur Twitter
            6 août 2018 à 19:26:10

            Je croyais avoir utilisé 443 quelque part! Ok corrigé, merci!

            Maintenant l'envoi est effectué mais j'ai un 400 Bad request en réponse:

            HTTP/1.1 400 Bad Request
            Date: Mon, 06 Aug 2018 17:18:03 GMT
            Content-Type: application/json
            Transfer-Encoding: chunked
            Connection: keep-alive
            Set-Cookie: __cfduid=ph0b6850aj8534kd7e7f8e42ef330dcbc1833575423; expires=Tue, 06-Aug-19 17:18:03 GMT; path=/; domain=.onesignal.com; HttpOnly
            Status: 400 Bad Request
            X-Request-Id: 5fgf1g1-7483-86f0-b6ff-b76534cebd21
            X-Runtime: 0.000947
            X-Powered-By: Phusion Passenger 5.3.2
            Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
            Server: cloudflare
            CF-RAY: 336325254b57bc98-SEA
            
            268
            {"status":400,"error":"There was a problem in the JSON you submitted: expected hash key at line 1, column 440 [parse.c:100] in '{\"app_id\": \"sdgdfgf-5555-5635-1111-111111111\",{\"included_segments\": [\"Active Users\"],{\"android_channel_id\": \"2222222-5555-4444-444-4444444444f\",{\"android_accent_color\": \"f90835\",{\"large_icon\":\"iconApp.png\",{\"android_background_layout\":{\"image\": \"http://webdevtools.ga/copy/uploads/index.jpg\", \"headings_color\": \"FFFF0000\", \"contents_color\": \"FF00FF00\"},{\"data\": {\"foo\": \"bar\"},{\"contents\": {\"en\": \"Hello your alarm est déclenché\"}}"}
            0


            Devrais-je faire un truc :-° avec ArduinoJson sur PostData avant d'envoyer?

            Edit: Merci @alexisdm finalement ça marche! J'avais juste oublié une virgule à la ligne 40. :) Merci!

            -
            Edité par kamoba 6 août 2018 à 20:50:58

            • Partager sur Facebook
            • Partager sur Twitter
              4 novembre 2018 à 11:59:13

              Salut j'ai le même souci, as-tu résolu ton probléme ? Utilises-tu une WEB API en ASP.NET ?
              • Partager sur Facebook
              • Partager sur Twitter
                4 novembre 2018 à 21:40:51

                Salut, oui j'ai résolu mon problème. Je crois que le truc c’était de mettre les noms de fichier en minuscule! Non moi j'utilisais thunkable.

                plus d"infos voir la discussion sur github.

                -
                Edité par kamoba 4 novembre 2018 à 21:42:09

                • Partager sur Facebook
                • Partager sur Twitter
                  9 novembre 2018 à 2:09:16

                  Est-ce tu peut partager ton code une fois les modifications apportées.  Merci

                  -
                  Edité par FrancoisCournoyer 9 novembre 2018 à 2:09:46

                  • Partager sur Facebook
                  • Partager sur Twitter
                    9 novembre 2018 à 20:03:10

                    Je te laisse le code testé depuis Postman:

                    Postman:
                    Authorization  Basic YWFhMDFHGHGLTk3NmYtNJFHJM3NGRFGJGJHJHJFJG
                    Content-Type   application/json; charset=utf-8
                    
                    
                    {
                      "app_id": "xxxxxxx-xxxx-xxx-xxxx-xxxxxxxxxxxxxx",
                      "included_segments": ["Active Users"],
                      "android_sound": "alarmsound",
                      "android_led_color": "FFF000",
                      "large_icon":"iconApp.png",
                      "android_background_layout":{"image": "http://site.com/image.jpg", "headings_color": "FFFF0000", "contents_color": "FF00FF00"},
                      "data": {"foo": "bar"},
                      "contents": {"en": "Hello your alarm est déclenché"}
                    }
                    



                    • Partager sur Facebook
                    • Partager sur Twitter
                      29 mai 2019 à 19:34:53

                      Bonjour,

                      je me permet de répondre à ce sujet également, je ne comprends pas comment votre source peut fonctionner sans avoir passer le fingerprint en paramètre en amont.

                      De mon côté impossible !! Je ne parviens pas à me connecter sans cela.

                      Comment avez vous fait ? Quel est votre tour de magie ?

                      En vous remerciant d'avance énormément, je désespère !!

                      Merci

                      • Partager sur Facebook
                      • Partager sur Twitter

                      [Arduino] ESP8266 POST Json en https ?

                      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                      • Editeur
                      • Markdown