-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_mqtt.ino
200 lines (161 loc) · 4.96 KB
/
client_mqtt.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*********
Based on Rui Santos work :
https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/
Modified by GM
Modified by Benjamin Dekeyser & Gabriel Curinga
*********/
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include "OneWire.h"
#include "DallasTemperature.h";
WiFiClient espClient; // Wifi
PubSubClient client(espClient); // MQTT client
/*===== MQTT broker/server and TOPICS ========*/
const char* mqtt_server = "broker.hivemq.com"; /* "broker.shiftr.io"; */
#define TOPIC_TEMP "m1/miage/dc/temperature"
/*============= GPIO ======================*/
float temperature = 21.0;
float light = 0;
const int ledPin = 23; // LED Pin
const int temperaturePin = 21;
const int serial = 9600;
/** TEMPERATURE **/
OneWire oneWire(temperaturePin);
DallasTemperature tempSensor(&oneWire);
/*================ WIFI =======================*/
void print_connection_status() {
Serial.print("WiFi status : \n");
Serial.print("\tIP address : ");
Serial.println(WiFi.localIP());
Serial.print("\tMAC address : ");
Serial.println(WiFi.macAddress());
}
void connect_wifi() {
/** CHOOSE YOUR OWN WIFI**/
//const char* ssid = "HUAWEI-6EC2";
//const char *password= "FGY9MLBL";
const char* ssid = "Villalucie";
const char *password= "villaluciejulie";
Serial.println("Connecting Wifi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Attempting to connect Wifi ..");
delay(1000);
}
Serial.print("Connected to local Wifi\n");
print_connection_status();
}
/*=============== SETUP =====================*/
void setup() {
Serial.begin(serial);
tempSensor.begin(); // Init temperature sensor
//pinMode(ledPin, OUTPUT);
connect_wifi();
client.setServer(mqtt_server, 1883);
// set callback when publishes arrive for the subscribed topic
//client.setCallback(mqtt_pubcallback);
}
/*============== CALLBACK ===================*/
void mqtt_pubcallback(char* topic, byte* message,
unsigned int length) {
// Callback if a message is published on this topic.
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
// Byte list to String and print to Serial
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic,
// you check if the message is either "on" or "off".
// Changes the output state according to the message
/*if (String(topic) == TOPIC_LED) {
Serial.print("Changing output to ");
if (messageTemp == "on") {
Serial.println("on");
set_LED(HIGH);
}
else if (messageTemp == "off") {
Serial.println("off");
set_LED(LOW);
}
}*/
}
void mqtt_publish(char *topic, char *message){
if(client.connect("esp32", "try", "try")){
client.publish(topic, message);
// Serial info
Serial.print("Published datas : ");
Serial.println(message);
}else{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
void set_LED(int v){
}
/*============= SUBSCRIBE =====================*/
void mqtt_mysubscribe(char *topic) {
while (!client.connected()) { // Loop until we're reconnected
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("esp32", "try", "try")) {
Serial.println("connected");
// Subscribe
client.subscribe(topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
float get_Temperature(){
float t;
tempSensor.requestTemperaturesByIndex(0);
t = tempSensor.getTempCByIndex(0);
return t;
}
char* generate_json(char* sensor, float value){
String message = "{\"sensor\"";
//sensor type
message += ":\"";
message += sensor;
message += "\",";
//sensor value
message += "\"value\"";
message += ":\"";
message += temperature;
message += "\"}";
char* copy = new char[message.length()+1];
message.toCharArray(copy, message.length()+1);
return copy;
}
/*================= LOOP ======================*/
void loop() {
int32_t period = 5000; // 5 sec
/*--- subscribe to TOPIC_TEMPERATURE if not yet ! */
/*if (!client.connected()) {
mqtt_mysubscribe((char *)(TOPIC_TEMP));
}*/
/*--- Publish Temperature periodically */
delay(period);
//temperature = get_Temperature();
// Convert the value to a char array
temperature = temperature + 1;
/*char tempString[8];
dtostrf(temperature, 1, 2, tempString);*/
// MQTT Publish
mqtt_publish(TOPIC_TEMP, generate_json("temperature", temperature));
client.loop(); // Process MQTT ... une fois par loop()
}