rename stuff, for disambiguation
This commit is contained in:
parent
0bc473da09
commit
e38d330d77
58
things.ino
58
things.ino
@ -34,12 +34,12 @@ void saveConfigCallback() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initialize modules
|
// initialize modules
|
||||||
ESP8266WebServer server(80); // webserver
|
ESP8266WebServer http_server(80); // webserver
|
||||||
DHT dht(DHTPIN, DHTTYPE); // DHT sensor
|
DHT dht(DHTPIN, DHTTYPE); // DHT sensor
|
||||||
Ticker ticker; // LED status
|
Ticker ticker; // LED status
|
||||||
|
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
PubSubClient client(wifiClient);
|
PubSubClient mqtt_client(wifiClient);
|
||||||
|
|
||||||
float humidity, temperature; // raw values from the sensor
|
float humidity, temperature; // raw values from the sensor
|
||||||
float heatindex; // computed value from the sensor
|
float heatindex; // computed value from the sensor
|
||||||
@ -104,13 +104,13 @@ void read_sensor() {
|
|||||||
dtostrf(heatindex, 1, 2, str_heatindex);
|
dtostrf(heatindex, 1, 2, str_heatindex);
|
||||||
|
|
||||||
if (!isEqual(humidity, previousHumidity)) {
|
if (!isEqual(humidity, previousHumidity)) {
|
||||||
client.publish(topic("humidity"), str_humidity);
|
mqtt_client.publish(topic("humidity"), str_humidity);
|
||||||
}
|
}
|
||||||
if (!isEqual(temperature, previousTemperature)) {
|
if (!isEqual(temperature, previousTemperature)) {
|
||||||
client.publish(topic("temperature"), str_temperature);
|
mqtt_client.publish(topic("temperature"), str_temperature);
|
||||||
}
|
}
|
||||||
if (!isEqual(heatindex, previousHeatindex)) {
|
if (!isEqual(heatindex, previousHeatindex)) {
|
||||||
client.publish(topic("heatindex"), str_heatindex);
|
mqtt_client.publish(topic("heatindex"), str_heatindex);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.print("Humidity: ");
|
Serial.print("Humidity: ");
|
||||||
@ -148,18 +148,18 @@ void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
// make sure we're connected to MQTT broker
|
// make sure we're connected to MQTT broker
|
||||||
void mqtt_reconnect() {
|
void mqtt_reconnect() {
|
||||||
// loop until we're reconnected
|
// loop until we're reconnected
|
||||||
while (!client.connected()) {
|
while (!mqtt_client.connected()) {
|
||||||
Serial.print("Attempting MQTT connection...");
|
Serial.print("Attempting MQTT connection...");
|
||||||
// attempt to connect
|
// attempt to connect
|
||||||
if (client.connect(wifi_station_get_hostname(), topic("online"), MQTTQOS1, true, "0")) {
|
if (mqtt_client.connect(wifi_station_get_hostname(), topic("online"), MQTTQOS1, true, "0")) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
// once connected, publish an announcement...
|
// once connected, publish an announcement...
|
||||||
client.publish(topic("online"), "1");
|
mqtt_client.publish(topic("online"), "1");
|
||||||
// ... and resubscribe:
|
// ... and resubscribe:
|
||||||
client.subscribe("inTopic");
|
mqtt_client.subscribe("inTopic");
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
Serial.print(client.state());
|
Serial.print(mqtt_client.state());
|
||||||
Serial.println(" try again in 5 seconds");
|
Serial.println(" try again in 5 seconds");
|
||||||
delay(5000);
|
delay(5000);
|
||||||
}
|
}
|
||||||
@ -282,19 +282,19 @@ void setup() {
|
|||||||
configFile.close();
|
configFile.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
client.setServer(mqtt_server, stringToNumber(mqtt_port));
|
mqtt_client.setServer(mqtt_server, stringToNumber(mqtt_port));
|
||||||
client.setCallback(mqtt_callback);
|
mqtt_client.setCallback(mqtt_callback);
|
||||||
|
|
||||||
dht.begin();
|
dht.begin();
|
||||||
|
|
||||||
// initial read
|
// initial read
|
||||||
read_sensor();
|
read_sensor();
|
||||||
client.publish(topic("humidity"), str_humidity);
|
mqtt_client.publish(topic("humidity"), str_humidity);
|
||||||
client.publish(topic("temperature"), str_temperature);
|
mqtt_client.publish(topic("temperature"), str_temperature);
|
||||||
client.publish(topic("heatindex"), str_heatindex);
|
mqtt_client.publish(topic("heatindex"), str_heatindex);
|
||||||
|
|
||||||
// handle http requests
|
// handle http requests
|
||||||
server.on("/", [](){
|
http_server.on("/", [](){
|
||||||
read_sensor();
|
read_sensor();
|
||||||
String response = "<!DOCTYPE HTML>\r\n";
|
String response = "<!DOCTYPE HTML>\r\n";
|
||||||
response += "<html lang=\"en\">\r\n";
|
response += "<html lang=\"en\">\r\n";
|
||||||
@ -330,11 +330,11 @@ void setup() {
|
|||||||
response += "</div>\r\n";
|
response += "</div>\r\n";
|
||||||
response += "</body>\r\n";
|
response += "</body>\r\n";
|
||||||
response += "</html>\n";
|
response += "</html>\n";
|
||||||
server.send(200, "text/html", response);
|
http_server.send(200, "text/html", response);
|
||||||
delay(100);
|
delay(100);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/values", [](){
|
http_server.on("/values", [](){
|
||||||
read_sensor();
|
read_sensor();
|
||||||
String response = "updatetime\t";
|
String response = "updatetime\t";
|
||||||
response += previousMillis;
|
response += previousMillis;
|
||||||
@ -348,37 +348,37 @@ void setup() {
|
|||||||
response += "heatindex\t";
|
response += "heatindex\t";
|
||||||
response += str_heatindex;
|
response += str_heatindex;
|
||||||
response += "\n";
|
response += "\n";
|
||||||
server.send(200, "text/plain", response);
|
http_server.send(200, "text/plain", response);
|
||||||
delay(100);
|
delay(100);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/temp", [](){
|
http_server.on("/temp", [](){
|
||||||
read_sensor();
|
read_sensor();
|
||||||
char response[50];
|
char response[50];
|
||||||
snprintf(response, 50, "Temperature: %s °C", str_temperature);
|
snprintf(response, 50, "Temperature: %s °C", str_temperature);
|
||||||
server.send(200, "text/plain", response);
|
http_server.send(200, "text/plain", response);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/humidity", [](){
|
http_server.on("/humidity", [](){
|
||||||
read_sensor();
|
read_sensor();
|
||||||
char response[50];
|
char response[50];
|
||||||
snprintf(response, 50, "Humidity: %s %", str_humidity);
|
snprintf(response, 50, "Humidity: %s %", str_humidity);
|
||||||
server.send(200, "text/plain", response);
|
http_server.send(200, "text/plain", response);
|
||||||
});
|
});
|
||||||
|
|
||||||
// start the web server
|
// start the web server
|
||||||
server.begin();
|
http_server.begin();
|
||||||
Serial.println("HTTP server started");
|
Serial.println("HTTP server started");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// listen for http requests
|
// listen for http requests
|
||||||
server.handleClient();
|
http_server.handleClient();
|
||||||
|
|
||||||
if (!client.connected()) {
|
if (!mqtt_client.connected()) {
|
||||||
mqtt_reconnect();
|
mqtt_reconnect();
|
||||||
}
|
}
|
||||||
client.loop();
|
mqtt_client.loop();
|
||||||
|
|
||||||
long now = millis();
|
long now = millis();
|
||||||
if (now - lastMsg > 10000) {
|
if (now - lastMsg > 10000) {
|
||||||
|
Loading…
Reference in New Issue
Block a user