-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqtt-double.ino
179 lines (169 loc) · 4.94 KB
/
mqtt-double.ino
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// arnaud@repaircafe-orsay.org
// affichage de 2 topics différents pour 2 capteurs
// les 2 affichages apparaissent suivant leur arrivée sur écran OLED
#include <WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "xxx";
const char* password = "xxx";
const char* mqtt_server = "192.168.1.90";
#define mqtt_port 1883
#define MQTT_USER ""
#define MQTT_PASSWORD ""
#define MQTT_SERIAL_PUBLISH_CH "/ESP32/test"
#define MQTT_SERIAL_RECEIVER_CH1 "/SonOffExp/DHT/temperature"
#define MQTT_SERIAL_RECEIVER_CH2 "/Cave_36/bmp/Temperature"
WiFiClient wifiClient1,wifiClient2;
PubSubClient client1(wifiClient1);
PubSubClient client2(wifiClient2);
//-------------------------------- OLED------------------------------
#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"
#define PIN_GND 32
#define PIN_PLUS 33
#define SCL 25
#define SDA 26
SSD1306Wire display(0x3c, SDA, SCL);//initialisation écran
//initialise le buffer
void initDisplay(void) {
// Initialise the buffer
display.setLogBuffer(5, 30);
display.drawLogBuffer(64, 20);
display.setFont(ArialMT_Plain_16);
display.display();
delay(500);
}
// initialise le buffer avec les parametres choisis
//parametre: String text, int x, int y, int size
//renvoie: rien
void printBuffer(String text, int x, int y, int size){
switch (size) {
case 10:
display.setFont(ArialMT_Plain_10);
break;
case 16:
display.setFont(ArialMT_Plain_16);
break;
case 24:
display.setFont(ArialMT_Plain_24);
break;
}
display.drawString(x, y, text);
}
void init_oled(){
pinMode(PIN_PLUS, OUTPUT); // sets the digital pin as output
pinMode(PIN_GND, OUTPUT);
digitalWrite(PIN_PLUS, HIGH);
digitalWrite(PIN_GND, LOW);
Serial.print("init_oled ");
delay(100);
display.init();
display.flipScreenVertically();
display.setContrast(255);
initDisplay();
initDisplay();
display.clear();
printBuffer("STARTING",0, 10, 16);
display.display();
}
//------------------------
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(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client1.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client1.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) {
Serial.println("connected");
//Once connected, publish an announcement...
client1.publish("/icircuit/presence/ESP32/", "hello world");
// ... and resubscribe
client1.subscribe(MQTT_SERIAL_RECEIVER_CH1);
} else {
Serial.print("failed, rc=");
Serial.print(client1.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
while (!client2.connected()) {
Serial.print("Attempting MQTT 2 connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client2.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) {
Serial.println("connected");
//Once connected, publish an announcement...
client2.publish("/icircuit/presence/ESP32/", "hello world");
// ... and resubscribe
client2.subscribe(MQTT_SERIAL_RECEIVER_CH2);
} else {
Serial.print("failed, rc=");
Serial.print(client1.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte *payload, unsigned int length) {
Serial.println("-------new message from broker-----");
Serial.print("channel:");
Serial.println(topic);
Serial.print("data:");
Serial.write(payload, length);
char tempString[50];
for(int i=0;i< length; i++){
tempString[i]=payload[i];
}
// display.clear();
printBuffer("MQTT",0, 0, 16);
tempString[length]=0;
printBuffer(topic,0, 30, 10);
printBuffer(tempString,0, 46, 16);
display.display();
Serial.println();
}
void setup() {
Serial.begin(115200);
init_oled();
Serial.setTimeout(500);// Set time out for
setup_wifi();
client1.setServer(mqtt_server, mqtt_port);
client1.setCallback(callback);
client2.setServer(mqtt_server, mqtt_port);
client2.setCallback(callback);
reconnect();
}
void publishSerialData(char *serialData){
if (!client1.connected()) {
reconnect();
}
client1.publish(MQTT_SERIAL_PUBLISH_CH, serialData);
}
void loop() {
display.clear();
client1.loop();
client2.loop();
reconnect();
}