IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Arduino Discussion :

Récupérer des données à partir de Thingspeak pour les afficher avec ESP32


Sujet :

Arduino

  1. #21
    Responsable Arduino et Systèmes Embarqués


    Avatar de f-leb
    Homme Profil pro
    Enseignant
    Inscrit en
    Janvier 2009
    Messages
    12 740
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Janvier 2009
    Messages : 12 740
    Points : 57 540
    Points
    57 540
    Billets dans le blog
    42
    Par défaut
    Bonjour,

    Oui, et c'est aussi la version 3.1.1 de MQTT qui est utilisée par défaut par la bibliothèque PubSubClient.

    J'ai retrouvé mon ESP32 et j'ai réussi à me connecter à Thingspeak au channel public https://thingspeak.com/channels/357142 avec le code suivant à adapter :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    /*
      MqttClient - WiFi Simple Receive
     
      This example connects to a MQTT broker and subscribes to a single topic.
      When a message is received it prints the message to the Serial Monitor.
     
      This example code is in the public domain.
    */
     
    #include <PubSubClient.h>
    #include <WiFi.h>
     
    #include "arduino_secrets.h"
     
    void WIFISetUp(void);
    void mqttCallback(char *topic, byte *payload, unsigned int length);
     
    ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
    char ssid[] = SECRET_SSID;  // your network SSID (name)
    char pass[] = SECRET_PASS;  // your network password (use for WPA, or use as key for WEP)
     
    WiFiClient wifiClient;
    PubSubClient mqttClient(wifiClient);
     
    const char mqttBroker[] = "mqtt3.thingspeak.com";
    int mqttPort = 1883;
    const char mqttTopic[] = "channels/357142/subscribe";
     
    const char mqttClientID[] = "IywXBgxxxxxxxxxxxxxxxxxxx";
    const char mqttUsername[] = "IywXBgxxxxxxxxxxxxxxxxxxx";
    const char mqttPassword[] = "B7V0ujgxxxxxxxxxxxxxxxxxx";
     
    void setup() {
     
      Serial.begin(115200);
      while (!Serial) {
        ;
      }
     
      // attempt to connect to WiFi network:
      Serial.print("Attempting to connect to WPA SSID: ");
      Serial.println(ssid);
     
      WIFISetUp();
      Serial.println("You're connected to the network");
      Serial.println();
     
      mqttClient.setServer(mqttBroker, mqttPort);
      mqttClient.setCallback(mqttCallback);
      mqttClient.setBufferSize(512);
     
      while (!mqttClient.connected()) {
        Serial.print("Attempting to connect to the MQTT broker: ");
        Serial.println(mqttBroker);
        if (mqttClient.connect(mqttClientID, mqttUsername, mqttPassword)) {
          Serial.println("You're connected to the MQTT broker!");
          if (mqttClient.subscribe(mqttTopic)) {
            Serial.print("Subscribing to topic ");
            Serial.print(mqttTopic);
            Serial.println(": success");
            Serial.println("Waiting for messages on topic...");
          } else {
            Serial.println("Subscribing to topic: failed");
          }
        } else {
          Serial.print("Failed, state=");
          Serial.print(mqttClient.state());
          Serial.println(" New try in 5s...");
          delay(5000);
        }
      }
    }
     
    void loop() {
      mqttClient.loop();
    }
     
    void mqttCallback(char *topic, byte *payload, unsigned int length) {
      Serial.print("Message arrived in topic: ");
      Serial.println(topic);
      Serial.print("Message:");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }
      Serial.println();
      Serial.println("-----------------------");
    }
     
    void WIFISetUp(void) {
      // Set WiFi to station mode and disconnect from an AP if it was previously connected
      WiFi.disconnect(true);
      delay(100);
      WiFi.mode(WIFI_STA);
      WiFi.setAutoReconnect(true);
      WiFi.begin(ssid, pass);  //fill in "Your WiFi SSID","Your Password"
      delay(100);
     
      byte count = 0;
      while (WiFi.status() != WL_CONNECTED && count < 15) {
        count++;
        delay(2000);
        Serial.println("Connecting...");
      }
     
      if (WiFi.status() == WL_CONNECTED) {
        Serial.println("Connecting...OK.");
        delay(500);
      } else {
        Serial.println("Connecting...Failed");
        while (1)
          ;
      }
      Serial.println("WIFI Setup done");
      delay(500);
    }
    arduino_secrets.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    #define SECRET_SSID "your SSID"
    #define SECRET_PASS "your password"

    Dans le terminal série, j'ai bien les messages qui arrivent :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Connecting...
    Connecting...OK.
    WIFI Setup done
    You're connected to the network
     
    Attempting to connect to the MQTT broker: mqtt3.thingspeak.com
    You're connected to the MQTT broker!
    Subscribing to topic channels/357142/subscribe: success
    Waiting for messages on topic...
    Message arrived in topic: channels/357142/subscribe
    Message:{"channel_id":357142,"created_at":"2024-08-06T13:52:20+02:00","entry_id":290735,"field1":"3.3","field2":"4.0","field3":"32.360","field4":"978.435","field5":"34.546","field6":null,"field7":"43.715","field8":"5.543","latitude":null,"longitude":null,"elevation":null,"status":"13:52:20: new data"}
    -----------------------
    Message arrived in topic: channels/357142/subscribe
    Message:{"channel_id":357142,"created_at":"2024-08-06T14:04:26+02:00","entry_id":290736,"field1":"2.8","field2":"3.1","field3":"33.383","field4":"978.354","field5":"33.946","field6":null,"field7":"44.298","field8":"5.615","latitude":null,"longitude":null,"elevation":null,"status":"14:04:26: new data"}
    -----------------------
    La ligne mqttClient.setBufferSize(512); est importante, par défaut la taille du buffer est de 256 octets et comme le message est plus long, je ne voyais aucun message arriver

  2. #22
    Membre actif
    Inscrit en
    Juillet 2004
    Messages
    832
    Détails du profil
    Informations forums :
    Inscription : Juillet 2004
    Messages : 832
    Points : 237
    Points
    237
    Par défaut
    Grand merci f-leb

    je reçois les données brutes de Thinkspeack maintenant grâce à ton croquis

    Sans vouloir abuser , un conseil pour "découper" la chaîne de caractères afin d'exploiter ces données ?
    Images attachées Images attachées  

  3. #23
    Responsable Arduino et Systèmes Embarqués


    Avatar de f-leb
    Homme Profil pro
    Enseignant
    Inscrit en
    Janvier 2009
    Messages
    12 740
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Janvier 2009
    Messages : 12 740
    Points : 57 540
    Points
    57 540
    Billets dans le blog
    42
    Par défaut
    Je n'ai pas encore regardé, mais comme la chaîne de caractères est au format JSON, je pense à cette bibliothèque prévue pour :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    #include <ArduinoJson.h>      // https://github.com/bblanchon/ArduinoJson
    Tu la trouveras facilement dans le gestionnaire de bibliothèques de l'EDI Arduino...

  4. #24
    Responsable Arduino et Systèmes Embarqués


    Avatar de f-leb
    Homme Profil pro
    Enseignant
    Inscrit en
    Janvier 2009
    Messages
    12 740
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Janvier 2009
    Messages : 12 740
    Points : 57 540
    Points
    57 540
    Billets dans le blog
    42
    Par défaut
    Par exemple, pour récupérer le champ field7 qui est une température, tu peux compléter la fonction callback :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    // ...
    #include <ArduinoJson.h>      // https://github.com/bblanchon/ArduinoJson
     
    // ...
     
    void mqttCallback(char *topic, byte *payload, unsigned int length) {
      // ...
     
      // voir https://arduinojson.org/v7/tutorial/deserialization/
      JsonDocument doc;
      deserializeJson(doc, (unsigned char*)payload);
      const char* temp = doc["field7"];
      Serial.print("temperature = ");
      Serial.println(temp);
     
      // ...
    }

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Message arrived in topic: channels/357142/subscribe
    Message:{"channel_id":357142,"created_at":"2024-08-07T12:13:15+02:00","entry_id":290846,"field1":"5.6","field2":"7.9","field3":"27.241","field4":"980.199","field5":"57.435","field6":null,"field7":"39.367","field8":"4.685","latitude":null,"longitude":null,"elevation":null,"status":"12:13:15: new data"}
     
    temperature = 39.367
    -----------------------
    Message arrived in topic: channels/357142/subscribe
    Message:{"channel_id":357142,"created_at":"2024-08-07T12:25:20+02:00","entry_id":290847,"field1":"5.2","field2":"5.8","field3":"26.579","field4":"980.320","field5":"62.674","field6":null,"field7":"38.336","field8":"4.496","latitude":null,"longitude":null,"elevation":null,"status":"12:25:20: new data"}
     
    temperature = 38.336
    -----------------------

  5. #25
    Membre actif
    Inscrit en
    Juillet 2004
    Messages
    832
    Détails du profil
    Informations forums :
    Inscription : Juillet 2004
    Messages : 832
    Points : 237
    Points
    237
    Par défaut
    Super ! f-leb

    çà fonctionne très bien , il me reste à traiter l'affichage TFT maintenant

    Merci mille fois pour le coup de main et pour ta patience

    je pense que je vais clôturer le post

    Cordialement
    pascal

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Rappatrier des données d'un site pour les afficher sur le mien
    Par nebil dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 1
    Dernier message: 14/09/2017, 21h10
  2. Récupérer des données d'un fichier pour les afficher dans des pages html
    Par Millie31 dans le forum Général Conception Web
    Réponses: 2
    Dernier message: 15/10/2015, 14h22
  3. Réponses: 2
    Dernier message: 21/05/2006, 14h02
  4. Réponses: 20
    Dernier message: 19/12/2004, 18h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo