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 :

Esp8266 Deep Sleep MQTT


Sujet :

Arduino

  1. #1
    Inactif  
    Homme Profil pro
    Responsable marketing opérationnel
    Inscrit en
    Septembre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Algérie

    Informations professionnelles :
    Activité : Responsable marketing opérationnel
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Septembre 2016
    Messages : 4
    Points : 10
    Points
    10
    Par défaut Esp8266 Deep Sleep MQTT
    Bonjour tout le monde j'ai un petit projet c'est utiliser une sonde de température pour envoyer les données mqtt a travers une esp8266
    comme le montage et sous batterie j'aimerais bien avoir un Deep Sleep pour être bref et franc j'arrive pas à le faire
    voilà mon code actuel
    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
    116
    117
    118
    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    // Data wire is plugged into port 2 on the Arduino
    #define ONE_WIRE_BUS 4 // pin 4
     
    // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);
     
    // Pass our oneWire reference to Dallas Temperature. 
    DallasTemperature sensors(&oneWire);
     
     
    // Update these with values suitable for your network.
     
    const char* ssid = "*******************";
    const char* password = "****************";
    const char* mqtt_server = "***************"; /// MQTT Broker
    int mqtt_port = 1883;
     
    WiFiClient espClient;
    PubSubClient client(espClient);
    long lastMsg = 0;
    char msg[50];
    int value = 0;
     
    void setup() { 
     Serial.begin(115200);
     // Start up the library
     sensors.begin();
     
     setup_wifi();
     client.setServer(mqtt_server, mqtt_port);
     client.setCallback(callback);
     
     Serial.println("Connected ");
     Serial.print("MQTT Server ");
     Serial.print(mqtt_server);
     Serial.print(":");
     Serial.println(String(mqtt_port)); 
     Serial.print("ESP8266 IP ");
     Serial.println(WiFi.localIP()); 
     Serial.println("Modbus RTU Master Online");
     
     
     
    }
     
    void setup_wifi() {
     
     delay(10);
     // We start by connecting to a WiFi network
     Serial.println();
     Serial.print("Connecting to ");
     Serial.println(ssid);
     
     WiFi.begin(ssid, password);
     
     while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
     }
     
     Serial.println("");
     Serial.println("WiFi connected");
     Serial.println("IP address: ");
     Serial.println(WiFi.localIP());
    }
     
    void callback(char* topic, byte* payload, unsigned int length) {
     Serial.print("Message arrived [");
     Serial.print(topic);
     Serial.print("] ");
     for (int i = 0; i < length; i++) {
     Serial.print((char)payload[i]);
     }
     Serial.println();
     
    }
    void reconnect() {
     // Loop until we're reconnected
     while (!client.connected()) {
     Serial.print("Attempting MQTT connection...");
     // Attempt to connect
     if (client.connect("ESP8266Client")) {
     
     Serial.println("connected");
     // client.subscribe("event");
     } else {
     Serial.print("failed, rc=");
     Serial.print(client.state());
     Serial.println(" try again in 5 seconds");
     // Wait 5 seconds before retrying
     delay(5000);
     }
     }
    }
    void loop() {
     
     sensors.requestTemperatures();
     float celsius = sensors.getTempCByIndex(0);
     Serial.println(sensors.getTempCByIndex(0));
     
     char temperaturenow [15];
     dtostrf(celsius,7, 3, temperaturenow); //// convert float to char 
     client.publish("temperature/belka/sensor", temperaturenow); /// send char 
     
     if (!client.connected()) {
     reconnect();
     
     }
     {
       client.loop();  //Ensure we've sent & received everything
       delay(10000);
      }
     
      }

  2. #2
    Expert confirmé

    Homme Profil pro
    mad scientist :)
    Inscrit en
    Septembre 2019
    Messages
    2 711
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : mad scientist :)

    Informations forums :
    Inscription : Septembre 2019
    Messages : 2 711
    Points : 5 390
    Points
    5 390
    Par défaut
    qu'avez vous essayé ? il y a des tutos en ligne, en voici un par exemple assez riche

Discussions similaires

  1. Esp8266 mqtt capteur d'humidité sol
    Par kaiezer198000 dans le forum Embarqué
    Réponses: 1
    Dernier message: 10/07/2020, 10h54
  2. ESP VROOM deep sleep fonctionne à moitié
    Par J4e8a16n dans le forum Arduino
    Réponses: 6
    Dernier message: 31/12/2019, 23h36
  3. [Thread]sleep()
    Par jokoss dans le forum Concurrence et multi-thread
    Réponses: 15
    Dernier message: 03/07/2004, 14h33
  4. [langage] fonction sleep + probleme fenetre dos
    Par yokito dans le forum Langage
    Réponses: 2
    Dernier message: 30/08/2003, 19h46

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