-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature.ino
187 lines (156 loc) · 4.28 KB
/
temperature.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
#include <Wire.h>
#include <SPI.h>
#include <ESP8266WiFi.h> // WiFi
#include <PubSubClient.h> // MQTT
#include <Adafruit_Sensor.h> // bmp280
#include <Adafruit_BMP280.h> // bmp280
#include <Adafruit_GFX.h> // Seven segment
#include "Adafruit_LEDBackpack.h" // Seven segment
#include <WiFiUdp.h> // NTP
#include <NTPClient.h> // NTP
#include "temperature_config.h" // Private configuration file
const char *ssid = WIFI_SSID;
const char *password = WIFI_PASSWORD;
const char *mqtt_server = MQTT_SERVER;
const char *mqtt_user = MQTT_USER;
const char *mqtt_pass = MQTT_PASSWORD;
unsigned long lastMsg = 0;
WiFiClient espClient;
PubSubClient client(espClient);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
// Static IP Configuration
IPAddress staticIP = IP_ADDRESS;
IPAddress gateway = IP_GATEWAY;
IPAddress subnet = IP_SUBNET;
IPAddress dns = IP_DNS;
// Device hostname configuration
const char *deviceName = DEVICE_NAME;
Adafruit_7segment matrix = Adafruit_7segment();
Adafruit_BMP280 bmp; // I2C
void setup_wifi()
{
WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
WiFi.hostname(deviceName);
if (!WiFi.config(staticIP, gateway, subnet, dns)) {
Serial.println("STA Failed to configure");
}
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
// WiFi.printDiag(Serial);
}
void setup_sensor()
{
Serial.println("setup_sensor() bmp280 sensor.");
Serial.println(F("BMP280 test"));
if (!bmp.begin())
{
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1)
;
}
}
void reconnect()
{
// Loop until we're reconnected
Serial.println("In reconnect...");
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("esp8266_01", mqtt_user, mqtt_pass))
{
Serial.println("connected");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void preinit() {
/*
* Global WiFi constructors are not called yet
* (global class instances like WiFi, Serial... are not yet initialized)..
* No global object methods or C++ exceptions can be called in here!
* The below is a static class method, which is similar to a function, so it's ok.
*/
ESP8266WiFiClass::preinitWiFiOff();
}
void setup()
{
/*
* Prevent the device from connecting to any wifi
* before we're able to set the static IP configuration.
*/
WiFi.setAutoConnect(false);
// Enable or disable debug according to your needs.
Serial.setDebugOutput(false);
#ifndef __AVR_ATtiny85__
Serial.begin(115200);
delay(100);
Serial.println("7 Segment Backpack Test");
#endif
Serial.println("setup() matrix.");
matrix.begin(0x70);
Serial.println("setup() wifi.");
setup_wifi();
Serial.println("setup() MQTT.");
client.setServer(mqtt_server, 1883);
Serial.println("setup() bmp280 sensor.");
setup_sensor();
Serial.println("setup() NTP timeClient.begin().");
timeClient.begin();
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
timeClient.update();
char msg[30];
float temperature,pressure;
long epoch;
temperature = bmp.readTemperature();
pressure = bmp.readPressure();
epoch = timeClient.getEpochTime();
Serial.print(F("Epoch = "));
Serial.print(epoch);
Serial.println();
Serial.print(F("Temperature = "));
Serial.print(temperature);
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(pressure/100);
Serial.println(" hPa");
Serial.println();
// print a floating point
matrix.print(temperature);
matrix.writeDisplay();
unsigned long now = millis();
if (now - lastMsg > 60000)
{
lastMsg = now;
//sprintf(msg, "%f", bmp.readTemperature());
sprintf(msg, "%ld;%.2f;%.2f",epoch,temperature,(pressure/100));
client.publish("mq2_mqtt", msg);
Serial.print(F("Message = "));
Serial.print(msg);
Serial.println();
}
delay(5000);
}