Envoie de données sur un serveur mqtt

AttentionBibliothèque à installer

Le code de départ

Fichier / Exemples / Adafruit MQTT library / mqtt_esp8266

1
/***************************************************
2
 Adafruit MQTT Library ESP8266 Example
3
 Attention code wifi de la maison 
4
 ****************************************************/
5
#include <ESP8266WiFi.h>
6
#include "Adafruit_MQTT.h"
7
#include "Adafruit_MQTT_Client.h"
8
9
/************************* WiFi Access Point *********************************/
10
11
#define WLAN_SSID       " a completer"
12
#define WLAN_PASS       " a completer "
13
14
/************************* Adafruit.io Setup *********************************/
15
16
#define AIO_SERVER      "192.168.1.29"
17
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
18
#define AIO_USERNAME    "...your AIO username (see https://accounts.adafruit.com)..."
19
#define AIO_KEY         "...your AIO key..."
20
21
/************ Global State (you don't need to change this!) ******************/
22
23
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
24
WiFiClient client;
25
// or... use WiFiFlientSecure for SSL
26
//WiFiClientSecure client;
27
28
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
29
//Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
30
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT);
31
/****************************** Feeds ***************************************/
32
33
// Setup a feed called 'photocell' for publishing.
34
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
35
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "temperature/salon");
36
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt,"temperature/salon");
37
38
// Setup a feed called 'onoff' for subscribing to changes.
39
//pas utilisé ici 
40
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
41
42
/*************************** Sketch Code ************************************/
43
44
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
45
// for some reason (only affects ESP8266, likely an arduino-builder bug).
46
void MQTT_connect();
47
48
void setup() {
49
  Serial.begin(115200);
50
  delay(10);
51
52
  Serial.println(F("Adafruit MQTT demo"));
53
54
  // Connect to WiFi access point.
55
  Serial.println(); Serial.println();
56
  Serial.print("Connecting to ");
57
  Serial.println(WLAN_SSID);
58
59
  WiFi.begin(WLAN_SSID, WLAN_PASS);
60
  while (WiFi.status() != WL_CONNECTED) {
61
    delay(500);
62
    Serial.print(".");
63
  }
64
  Serial.println();
65
66
  Serial.println("WiFi connected");
67
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
68
69
  // Setup MQTT subscription for onoff feed.
70
  mqtt.subscribe(&onoffbutton);
71
}
72
73
uint32_t x=0;
74
75
void loop() {
76
  // Ensure the connection to the MQTT server is alive (this will make the first
77
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
78
  // function definition further below.
79
  MQTT_connect();
80
81
  // this is our 'wait for incoming subscription packets' busy subloop
82
  // try to spend your time here
83
84
 // Adafruit_MQTT_Subscribe *subscription;
85
//  while ((subscription = mqtt.readSubscription(5000))) {
86
 //   if (subscription == &onoffbutton) {
87
 //     Serial.print(F("Got: "));
88
  //    Serial.println((char *)onoffbutton.lastread);
89
 //   }
90
 // }
91
92
  // Publication
93
  Serial.print(F("\nSending photocell val "));
94
  Serial.print(x);
95
  Serial.print("...");
96
  if (! temperature.publish(x++)) {
97
    Serial.println(F("Failed"));
98
  } else {
99
    Serial.println(F("OK!"));
100
  }
101
delay(1000);
102
103
}
104
105
// Function to connect and reconnect as necessary to the MQTT server.
106
// Should be called in the loop function and it will take care if connecting.
107
void MQTT_connect() {
108
  int8_t ret;
109
110
  // Stop if already connected.
111
  if (mqtt.connected()) {
112
    return;
113
  }
114
115
  Serial.print("Connecting to MQTT... ");
116
117
  uint8_t retries = 3;
118
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
119
       Serial.println(mqtt.connectErrorString(ret));
120
       Serial.println("Retrying MQTT connection in 5 seconds...");
121
       mqtt.disconnect();
122
       delay(5000);  // wait 5 seconds
123
       retries--;
124
       if (retries == 0) {
125
         // basically die and wait for WDT to reset me
126
         while (1);
127
       }
128
  }
129
  Serial.println("MQTT Connected!");
130
}