fix comments

This commit is contained in:
Ronald Schaten 2016-02-24 09:00:11 +01:00
parent 8e71160516
commit 0bc473da09

View File

@ -60,16 +60,16 @@ void toggle_led() {
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
//entered config mode, make led toggle faster
ticker.attach(0.1, toggle_led);
ticker.attach(0.1, toggle_led); // toggle led faster
}
// compare float values
bool isEqual(float a, float b, float epsilon=0.001) {
return fabs(a - b) <= epsilon * fabs(a);
}
// concatenate MQTT topic prefix to individual topic
char* topic(const char* this_topic) {
static char topic[34];
strcpy(topic, mqtt_topic);
@ -78,6 +78,7 @@ char* topic(const char* this_topic) {
return topic;
}
// get values from DHT sensor
void read_sensor() {
// wait at least 2 seconds seconds between measurements
unsigned long currentMillis = millis();
@ -89,8 +90,6 @@ void read_sensor() {
float previousHeatindex = heatindex;
humidity = dht.readHumidity(); // read humidity as a percent
temperature = dht.readTemperature(); // read temperature as Celsius
// compute heat index
heatindex = dht.computeHeatIndex(temperature, humidity, false);
// check if any reads failed and exit early (to try again)
@ -126,7 +125,8 @@ void read_sensor() {
}
}
void callback(char* topic, byte* payload, unsigned int length) {
// callback for MQTT, gets called if we receive a message
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
@ -135,26 +135,25 @@ void callback(char* topic, byte* payload, unsigned int length) {
}
Serial.println();
// Switch on the LED if an 1 was received as first character
// switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
Serial.println("setting LED to low");
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
digitalWrite(BUILTIN_LED, LOW); // turn the LED on
} else {
Serial.println("setting LED to high");
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
digitalWrite(BUILTIN_LED, HIGH); // turn the LED off
}
}
void reconnect() {
// Loop until we're reconnected
// make sure we're connected to MQTT broker
void mqtt_reconnect() {
// loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// attempt to connect
if (client.connect(wifi_station_get_hostname(), topic("online"), MQTTQOS1, true, "0")) {
Serial.println("connected");
// Once connected, publish an announcement...
// once connected, publish an announcement...
client.publish(topic("online"), "1");
// ... and resubscribe:
client.subscribe("inTopic");
@ -162,12 +161,12 @@ void reconnect() {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// convert string to integer
int stringToNumber(String thisString) {
int i, value, length;
length = thisString.length();
@ -204,7 +203,7 @@ void setup() {
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
// allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
@ -223,17 +222,14 @@ void setup() {
} else {
Serial.println("failed to mount FS");
}
//end read
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
// WiFiManager
// extra parameters to be configured
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
WiFiManagerParameter custom_mqtt_topic("topic", "mqtt topic", mqtt_topic, 32);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
// local intialization
WiFiManager wifiManager;
// reset settings - for testing
//wifiManager.resetSettings();
@ -250,20 +246,16 @@ void setup() {
wifiManager.addParameter(&custom_mqtt_topic);
// fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
// if it does not connect it starts an access point
if (!wifiManager.autoConnect()) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
// reset and try again
ESP.reset();
delay(1000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
Serial.println("connected to WiFi");
ticker.detach();
//keep LED on
digitalWrite(BUILTIN_LED, LOW);
// read updated parameters
@ -288,21 +280,20 @@ void setup() {
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
client.setServer(mqtt_server, stringToNumber(mqtt_port));
client.setCallback(callback);
client.setCallback(mqtt_callback);
dht.begin();
// Initial read
// initial read
read_sensor();
client.publish(topic("humidity"), str_humidity);
client.publish(topic("temperature"), str_temperature);
client.publish(topic("heatindex"), str_heatindex);
// Handle http requests
// handle http requests
server.on("/", [](){
read_sensor();
String response = "<!DOCTYPE HTML>\r\n";
@ -375,17 +366,17 @@ void setup() {
server.send(200, "text/plain", response);
});
// Start the web server
// start the web server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// Listen for http requests
// listen for http requests
server.handleClient();
if (!client.connected()) {
reconnect();
mqtt_reconnect();
}
client.loop();