fix indentation
This commit is contained in:
parent
3d7a089e0e
commit
bf61e4c937
200
things.ino
200
things.ino
@ -19,16 +19,16 @@ extern "C" {
|
|||||||
//#define DHTTYPE DHT21 // DHT21 (AM2301)
|
//#define DHTTYPE DHT21 // DHT21 (AM2301)
|
||||||
#define DHTTYPE DHT22 // DHT22 (AM2302)
|
#define DHTTYPE DHT22 // DHT22 (AM2302)
|
||||||
|
|
||||||
//define your default values here, if there are different values in config.json, they are overwritten.
|
// define your default values here, if there are different values in config.json, they are overwritten.
|
||||||
char mqtt_server[40];
|
char mqtt_server[40];
|
||||||
char mqtt_port[6] = "1883";
|
char mqtt_port[6] = "1883";
|
||||||
char mqtt_topic[34] = "OutTopic";
|
char mqtt_topic[34] = "OutTopic";
|
||||||
|
|
||||||
//flag for saving data
|
// flag for saving data
|
||||||
bool shouldSaveConfig = false;
|
bool shouldSaveConfig = false;
|
||||||
|
|
||||||
//callback notifying us of the need to save config
|
// callback notifying us of the need to save config
|
||||||
void saveConfigCallback () {
|
void saveConfigCallback() {
|
||||||
Serial.println("Should save config");
|
Serial.println("Should save config");
|
||||||
shouldSaveConfig = true;
|
shouldSaveConfig = true;
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ void configModeCallback (WiFiManager *myWiFiManager) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isEqual(float a, float b, float epsilon=0.001) {
|
bool isEqual(float a, float b, float epsilon=0.001) {
|
||||||
return fabs(a - b) <= epsilon * fabs(a);
|
return fabs(a - b) <= epsilon * fabs(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_sensor() {
|
void read_sensor() {
|
||||||
@ -96,20 +96,20 @@ void read_sensor() {
|
|||||||
dtostrf(temperature, 1, 2, str_temperature);
|
dtostrf(temperature, 1, 2, str_temperature);
|
||||||
dtostrf(heatindex, 1, 2, str_heatindex);
|
dtostrf(heatindex, 1, 2, str_heatindex);
|
||||||
|
|
||||||
char pub_topic[34];
|
char pub_topic[34];
|
||||||
if (!isEqual(humidity, previousHumidity)) {
|
if (!isEqual(humidity, previousHumidity)) {
|
||||||
strcpy(pub_topic,mqtt_topic);
|
strcpy(pub_topic,mqtt_topic);
|
||||||
strcat(pub_topic,"/humidity");
|
strcat(pub_topic,"/humidity");
|
||||||
client.publish(pub_topic, str_humidity);
|
client.publish(pub_topic, str_humidity);
|
||||||
}
|
}
|
||||||
if (!isEqual(temperature, previousTemperature)) {
|
if (!isEqual(temperature, previousTemperature)) {
|
||||||
strcpy(pub_topic,mqtt_topic);
|
strcpy(pub_topic,mqtt_topic);
|
||||||
strcat(pub_topic,"/temperature");
|
strcat(pub_topic,"/temperature");
|
||||||
client.publish(pub_topic, str_temperature);
|
client.publish(pub_topic, str_temperature);
|
||||||
}
|
}
|
||||||
if (!isEqual(heatindex, previousHeatindex)) {
|
if (!isEqual(heatindex, previousHeatindex)) {
|
||||||
strcpy(pub_topic,mqtt_topic);
|
strcpy(pub_topic,mqtt_topic);
|
||||||
strcat(pub_topic,"/heatindex");
|
strcat(pub_topic,"/heatindex");
|
||||||
client.publish(pub_topic, str_heatindex);
|
client.publish(pub_topic, str_heatindex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,9 +151,9 @@ void reconnect() {
|
|||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
Serial.print("Attempting MQTT connection...");
|
Serial.print("Attempting MQTT connection...");
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
char pub_topic[34];
|
char pub_topic[34];
|
||||||
strcpy(pub_topic,mqtt_topic);
|
strcpy(pub_topic,mqtt_topic);
|
||||||
strcat(pub_topic,"/online");
|
strcat(pub_topic,"/online");
|
||||||
if (client.connect(wifi_station_get_hostname(), pub_topic, MQTTQOS1, true, "0")) {
|
if (client.connect(wifi_station_get_hostname(), pub_topic, MQTTQOS1, true, "0")) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
// Once connected, publish an announcement...
|
// Once connected, publish an announcement...
|
||||||
@ -170,18 +170,18 @@ void reconnect() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int stringToNumber(String thisString) {
|
int stringToNumber(String thisString) {
|
||||||
int i, value, length;
|
int i, value, length;
|
||||||
length = thisString.length();
|
length = thisString.length();
|
||||||
char blah[(length + 1)];
|
char blah[(length + 1)];
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
blah[i] = thisString.charAt(i);
|
blah[i] = thisString.charAt(i);
|
||||||
}
|
}
|
||||||
blah[i] = 0;
|
blah[i] = 0;
|
||||||
value = atoi(blah);
|
value = atoi(blah);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -191,52 +191,48 @@ void setup() {
|
|||||||
// start ticker with 0.5 because we start in AP mode and try to connect
|
// start ticker with 0.5 because we start in AP mode and try to connect
|
||||||
ticker.attach(0.6, toggle_led);
|
ticker.attach(0.6, toggle_led);
|
||||||
|
|
||||||
|
//clean FS, for testing
|
||||||
|
//SPIFFS.format();
|
||||||
|
|
||||||
//clean FS, for testing
|
//read configuration from FS json
|
||||||
//SPIFFS.format();
|
Serial.println("mounting FS...");
|
||||||
|
|
||||||
//read configuration from FS json
|
if (SPIFFS.begin()) {
|
||||||
Serial.println("mounting FS...");
|
Serial.println("mounted file system");
|
||||||
|
if (SPIFFS.exists("/config.json")) {
|
||||||
if (SPIFFS.begin()) {
|
//file exists, reading and loading
|
||||||
Serial.println("mounted file system");
|
Serial.println("reading config file");
|
||||||
if (SPIFFS.exists("/config.json")) {
|
File configFile = SPIFFS.open("/config.json", "r");
|
||||||
//file exists, reading and loading
|
if (configFile) {
|
||||||
Serial.println("reading config file");
|
Serial.println("opened config file");
|
||||||
File configFile = SPIFFS.open("/config.json", "r");
|
size_t size = configFile.size();
|
||||||
if (configFile) {
|
// Allocate a buffer to store contents of the file.
|
||||||
Serial.println("opened config file");
|
std::unique_ptr<char[]> buf(new char[size]);
|
||||||
size_t size = configFile.size();
|
configFile.readBytes(buf.get(), size);
|
||||||
// Allocate a buffer to store contents of the file.
|
DynamicJsonBuffer jsonBuffer;
|
||||||
std::unique_ptr<char[]> buf(new char[size]);
|
JsonObject& json = jsonBuffer.parseObject(buf.get());
|
||||||
|
json.printTo(Serial);
|
||||||
configFile.readBytes(buf.get(), size);
|
if (json.success()) {
|
||||||
DynamicJsonBuffer jsonBuffer;
|
Serial.println("\nparsed json");
|
||||||
JsonObject& json = jsonBuffer.parseObject(buf.get());
|
strcpy(mqtt_server, json["mqtt_server"]);
|
||||||
json.printTo(Serial);
|
strcpy(mqtt_port, json["mqtt_port"]);
|
||||||
if (json.success()) {
|
strcpy(mqtt_topic, json["mqtt_topic"]);
|
||||||
Serial.println("\nparsed json");
|
} else {
|
||||||
|
Serial.println("failed to load json config");
|
||||||
strcpy(mqtt_server, json["mqtt_server"]);
|
|
||||||
strcpy(mqtt_port, json["mqtt_port"]);
|
|
||||||
strcpy(mqtt_topic, json["mqtt_topic"]);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
Serial.println("failed to load json config");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Serial.println("failed to mount FS");
|
|
||||||
}
|
}
|
||||||
//end read
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("failed to mount FS");
|
||||||
|
}
|
||||||
|
//end read
|
||||||
|
|
||||||
// The extra parameters to be configured (can be either global or just in the setup)
|
// 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
|
// After connecting, parameter.getValue() will get you the configured value
|
||||||
// id/name placeholder/prompt default length
|
// id/name placeholder/prompt default length
|
||||||
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
|
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
|
||||||
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
|
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
|
||||||
WiFiManagerParameter custom_mqtt_topic("topic", "mqtt topic", mqtt_topic, 32);
|
WiFiManagerParameter custom_mqtt_topic("topic", "mqtt topic", mqtt_topic, 32);
|
||||||
|
|
||||||
//WiFiManager
|
//WiFiManager
|
||||||
//Local intialization. Once its business is done, there is no need to keep it around
|
//Local intialization. Once its business is done, there is no need to keep it around
|
||||||
@ -247,13 +243,13 @@ void setup() {
|
|||||||
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
|
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
|
||||||
wifiManager.setAPCallback(configModeCallback);
|
wifiManager.setAPCallback(configModeCallback);
|
||||||
|
|
||||||
//set config save notify callback
|
//set config save notify callback
|
||||||
wifiManager.setSaveConfigCallback(saveConfigCallback);
|
wifiManager.setSaveConfigCallback(saveConfigCallback);
|
||||||
|
|
||||||
//add all your parameters here
|
//add all your parameters here
|
||||||
wifiManager.addParameter(&custom_mqtt_server);
|
wifiManager.addParameter(&custom_mqtt_server);
|
||||||
wifiManager.addParameter(&custom_mqtt_port);
|
wifiManager.addParameter(&custom_mqtt_port);
|
||||||
wifiManager.addParameter(&custom_mqtt_topic);
|
wifiManager.addParameter(&custom_mqtt_topic);
|
||||||
|
|
||||||
//fetches ssid and pass and tries to connect
|
//fetches ssid and pass and tries to connect
|
||||||
//if it does not connect it starts an access point with the specified name
|
//if it does not connect it starts an access point with the specified name
|
||||||
@ -272,30 +268,30 @@ void setup() {
|
|||||||
//keep LED on
|
//keep LED on
|
||||||
digitalWrite(BUILTIN_LED, LOW);
|
digitalWrite(BUILTIN_LED, LOW);
|
||||||
|
|
||||||
//read updated parameters
|
//read updated parameters
|
||||||
strcpy(mqtt_server, custom_mqtt_server.getValue());
|
strcpy(mqtt_server, custom_mqtt_server.getValue());
|
||||||
strcpy(mqtt_port, custom_mqtt_port.getValue());
|
strcpy(mqtt_port, custom_mqtt_port.getValue());
|
||||||
strcpy(mqtt_topic, custom_mqtt_topic.getValue());
|
strcpy(mqtt_topic, custom_mqtt_topic.getValue());
|
||||||
|
|
||||||
//save the custom parameters to FS
|
//save the custom parameters to FS
|
||||||
if (shouldSaveConfig) {
|
if (shouldSaveConfig) {
|
||||||
Serial.println("saving config");
|
Serial.println("saving config");
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonBuffer jsonBuffer;
|
||||||
JsonObject& json = jsonBuffer.createObject();
|
JsonObject& json = jsonBuffer.createObject();
|
||||||
json["mqtt_server"] = mqtt_server;
|
json["mqtt_server"] = mqtt_server;
|
||||||
json["mqtt_port"] = mqtt_port;
|
json["mqtt_port"] = mqtt_port;
|
||||||
json["mqtt_topic"] = mqtt_topic;
|
json["mqtt_topic"] = mqtt_topic;
|
||||||
|
|
||||||
File configFile = SPIFFS.open("/config.json", "w");
|
File configFile = SPIFFS.open("/config.json", "w");
|
||||||
if (!configFile) {
|
if (!configFile) {
|
||||||
Serial.println("failed to open config file for writing");
|
Serial.println("failed to open config file for writing");
|
||||||
}
|
}
|
||||||
|
|
||||||
json.printTo(Serial);
|
json.printTo(Serial);
|
||||||
json.printTo(configFile);
|
json.printTo(configFile);
|
||||||
configFile.close();
|
configFile.close();
|
||||||
//end save
|
//end save
|
||||||
}
|
}
|
||||||
|
|
||||||
client.setServer(mqtt_server, stringToNumber(mqtt_port));
|
client.setServer(mqtt_server, stringToNumber(mqtt_port));
|
||||||
client.setCallback(callback);
|
client.setCallback(callback);
|
||||||
@ -304,15 +300,15 @@ void setup() {
|
|||||||
|
|
||||||
// Initial read
|
// Initial read
|
||||||
read_sensor();
|
read_sensor();
|
||||||
char pub_topic[34];
|
char pub_topic[34];
|
||||||
strcpy(pub_topic,mqtt_topic);
|
strcpy(pub_topic,mqtt_topic);
|
||||||
strcat(pub_topic,"/humidity");
|
strcat(pub_topic,"/humidity");
|
||||||
client.publish(pub_topic, str_humidity);
|
client.publish(pub_topic, str_humidity);
|
||||||
strcpy(pub_topic,mqtt_topic);
|
strcpy(pub_topic,mqtt_topic);
|
||||||
strcat(pub_topic,"/temperature");
|
strcat(pub_topic,"/temperature");
|
||||||
client.publish(pub_topic, str_temperature);
|
client.publish(pub_topic, str_temperature);
|
||||||
strcpy(pub_topic,mqtt_topic);
|
strcpy(pub_topic,mqtt_topic);
|
||||||
strcat(pub_topic,"/heatindex");
|
strcat(pub_topic,"/heatindex");
|
||||||
client.publish(pub_topic, str_heatindex);
|
client.publish(pub_topic, str_heatindex);
|
||||||
|
|
||||||
// Handle http requests
|
// Handle http requests
|
||||||
|
Loading…
Reference in New Issue
Block a user