-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEMQX_BMP280_OLED.txt
183 lines (163 loc) · 5.08 KB
/
EMQX_BMP280_OLED.txt
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
//File Name:- EMQX_BMP280_OLED
//Hardware is same to that of BM280 Program
//This Program displays Temp.,Press., and Altitude on OLED
//This Program displays Temp.,Press., on EMQX client
// The client values changes when Temp and Press changes by 0.01
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET LED_BUILTIN
#define Sea_Level_Press_hPa (1013.25)
// WiFi
const char* ssid = ""; // Enter SSID of your Wi-Fi here
const char* password = ""; //Enter Password of your Wi-Fi here
// MQTT Broker
const char *mqtt_broker = "broker.emqx.io";
const char *topic="NodeMCU_BMP_Test";
const char *mqtt_username = "emqx";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
const char *temperature_topic="BMP_Temperature";
const char *pressure_topic="BMP_Pressure";
WiFiClient NodeMCUClient;
PubSubClient client(NodeMCUClient);
Adafruit_BMP280 bmp;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire,OLED_RESET);
void setup() {
Serial.begin(9600);
delay(100);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextColor(WHITE);
delay(2000);
bool status = bmp.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while(1);
}
delay(500);
Serial.println("");//Flushout Garbage
// connecting to a WiFi network
Serial.println("connecting to:");
Serial.println(ssid);
WiFi.begin(ssid,password);//connect to your Local WiFi Network
while(WiFi.status()!=WL_CONNECTED){
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected!");
Serial.print("Got IP:");
Serial.println(WiFi.localIP());
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
}
//---------------
void reconnect() {
// Loop until we're reconnected
//client.setServer(mqtt_broker, mqtt_port);
//client.setCallback(callback);
while (!client.connected()) {
Serial.print("Attempting EMQ X_MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("NodeMCUClient", mqtt_username, mqtt_password)) {
Serial.println("Public emqx MQTT broker connected");
} else {
Serial.print("failed, rc= ");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
client.publish(topic,"hello emqx");
client.subscribe(topic);
}
bool checkBound(float newValue, float prevValue, float maxDiff) {
return !isnan(newValue) &&
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}
long lastMsg = 0;
float temp = 0.0;
float pres = 0.0;
float diff = 0.01;
void loop(){
if (!client.connected()){
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 1000) {
lastMsg = now;
float newTemp = bmp.readTemperature();
float newPres = bmp.readPressure()/100.0F;
if (checkBound(newTemp, temp, diff)) {
temp = newTemp;
Serial.print("New temperature:");
Serial.println(String(temp).c_str());
client.publish(temperature_topic, String(temp).c_str(), true);
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(1);
display.setCursor(0,10);
display.print(String(temp).c_str());
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(1);
display.print(" C");
delay(1000);
}
if (checkBound(newPres, pres, diff)) {
pres = newPres;
Serial.print("New pressure:");
Serial.println(String(pres).c_str());
client.publish(pressure_topic, String(pres).c_str(), true);
display.setTextSize(1);
display.setCursor(0, 20);
display.print("Pressure: ");
display.setTextSize(1);
display.setCursor(0, 30);
display.print(String(pres).c_str());
display.print(" hPa");
display.display();
delay(1000);
}
//display altitude
display.setTextSize(1);
display.setCursor(0, 40);
display.print("Altiitude: ");
display.setTextSize(1);
display.setCursor(0, 50);
display.print(String(bmp.readAltitude(Sea_Level_Press_hPa)));
display.print(" m");
display.display();
delay(1000);
display.clearDisplay();
//client.loop();
}
delay(1000);
}
void callback(char *topic1, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic1);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}