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