diff --git a/WifiControlSensor/BMP180.ino b/WifiControlSensor/BMP180.ino index 811b6b8..03634e3 100644 --- a/WifiControlSensor/BMP180.ino +++ b/WifiControlSensor/BMP180.ino @@ -1,16 +1,16 @@ SFE_BMP180 bmp180; int bmp180Connected = 0; -int setupBMP180() { +int BMP180Setup() { bmp180Connected = bmp180.begin(SDA, SCL); return bmp180Connected; } -bool isBMPConnected() { +bool BMP180IsConnected() { return bmp180Connected != 0; } -int getTemperature(double &t) { +int BMP180GetTemperature(double &t) { char status; status = bmp180.startTemperature(); if (status != 0) @@ -24,8 +24,8 @@ int getTemperature(double &t) { return -1; } -int getTempAndPressure(double &t, double &p) { - if (getTemperature(t) == 0) { +int BMP180GetTempAndPressure(double &t, double &p) { + if (BMP180GetTemperature(t) == 0) { char status; status = bmp180.startPressure(3); if (status != 0) diff --git a/WifiControlSensor/EEPROM.ino b/WifiControlSensor/EEPROM.ino index 32b0951..bc27126 100644 --- a/WifiControlSensor/EEPROM.ino +++ b/WifiControlSensor/EEPROM.ino @@ -5,7 +5,7 @@ */ -int saveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttPasswd, int mqttPort) { +int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttPasswd, int mqttPort) { String eeprom; eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttPasswd + ";" + String(mqttPort) + ";"; @@ -25,7 +25,7 @@ int saveConfig(uint8_t bootMode, String ssid, String password, String host, Stri return 0; } -int saveBootMode(uint8_t bootMode) { +int EepromSaveBootMode(uint8_t bootMode) { EEPROM.write(0, String(bootMode).charAt(0)); EEPROM.commit(); delay(100); @@ -45,7 +45,7 @@ void readConfElement(char** element, int &i) { } -void readEEPROM(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort) { +void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort) { int i = 2; diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index ea316d8..9839c1b 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -19,7 +19,7 @@ const char *GPIO_SET_FEED[] = { GPIO_BASE"12/set", GPIO_BASE"13/set"}; Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {}; -int setupMQTT(char *server, char *user, char *passwd, int port) { +int MqttSetup(char *server, char *user, char *passwd, int port) { // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd); mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED); @@ -34,13 +34,13 @@ int setupMQTT(char *server, char *user, char *passwd, int port) { return 0; } -int MQTT_isConnected() { +int MqttIsConnected() { return mqtt->connected(); } // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. -int MQTT_connect() { +int MqttConnect() { int8_t ret; // Stop if already connected. @@ -62,8 +62,8 @@ int MQTT_connect() { return 0; } -int publishMQTT(double temp, double pressure) { - if (MQTT_connect() == 0) { +int MqttPublish(double temp, double pressure) { + if (MqttConnect() == 0) { Serial.println("publishing !"); mqtt_temp->publish(temp); mqtt_pressure->publish(pressure); @@ -92,7 +92,7 @@ int getGpioWatchedIndex(int gpio) { return -1; } -void changeGpioValue(int gpio, int value) { +void MqttChangeGpioValue(int gpio, int value) { pinMode(gpio, OUTPUT); digitalWrite(gpio, value); int watchIdx = getGpioWatchedIndex(gpio); @@ -101,8 +101,8 @@ void changeGpioValue(int gpio, int value) { } } -void checkMqttSubscription() { - if (MQTT_connect() == 0) { +void MqttCheckSubscription() { + if (MqttConnect() == 0) { Adafruit_MQTT_Subscribe *subscription; while (subscription = mqtt->readSubscription(0)) { int gpio = getGpioFromSubscription(subscription); @@ -112,7 +112,7 @@ void checkMqttSubscription() { char *value = (char *) subscription->lastread; Serial.print("Receive data: "); Serial.println(value); - changeGpioValue(gpio, atoi(value)); + MqttChangeGpioValue(gpio, atoi(value)); } } } diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index 87c8bef..8b9301b 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -1,11 +1,11 @@ -void handleRoot() { +void WebHandleRoot() { server.send(200, "text/html", "" "

You are connected


" "Current temperature " + String(temp, 2) + "C
" "Current pressure " + String(pressure, 2) + "mB
" - "MQTT Status: " + (MQTT_isConnected() ? "Connected" : "Disconnected") + "
" - "BMP 180 (Temp+Pression) Status: " + (isBMPConnected() ? "Connected" : "Disconnected") + "
" + "MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "
" + "BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "
" "Free space: "+ ESP.getFreeSketchSpace() +"
" "Free heap: "+ ESP.getFreeHeap() +"
" "Setup
" @@ -15,7 +15,7 @@ void handleRoot() { ); } -void handleSetup() { +void WebHandleSetup() { uint8_t mode; char *confSsid = ""; char *confPassword = ""; @@ -25,7 +25,7 @@ void handleSetup() { char *mqttPasswd = ""; int mqttPort = 1883; - readEEPROM(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort); + EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort); server.send(200, "text/html", "
" "
" @@ -45,17 +45,17 @@ void handleSetup() { ""); } -void handleGpio() { +void WebHandleGpio() { if (!server.hasArg("gpio") || !server.hasArg("value")) { server.send(500, "text/plain", "Bad arguments\r\n"); return; } - changeGpioValue(server.arg("gpio").toInt(), server.arg("value").toInt()); + MqttChangeGpioValue(server.arg("gpio").toInt(), server.arg("value").toInt()); server.send(200, "text/html", "

GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "

"); } -void handleSave() { +void WebHandleSave() { String password; String ssid; String hostName; @@ -68,7 +68,7 @@ void handleSave() { return; } - if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd"), server.arg("mqttPort").toInt()) < 0) { + if (EepromSaveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd"), server.arg("mqttPort").toInt()) < 0) { server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; } @@ -77,14 +77,14 @@ void handleSave() { "Reboot
"); } -void handleOTA() { +void WebHandleOTA() { Serial.println("Boot mode Set to OTA"); - saveBootMode(BOOTMODE_OTA); + EepromSaveBootMode(BOOTMODE_OTA); server.send(200, "text/html", "

OTA Mode set


" "You can reboot now"); } -void handleNotFound() { +void WebHandleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); @@ -99,21 +99,21 @@ void handleNotFound() { server.send(404, "text/plain", message); } -void handleReboot() { +void WebHandleReboot() { Serial.println("HTTP request to reboot"); server.send(200, "text/html", "

Device Reboot


"); ESP.restart(); } -void setupWebServer(int bootmode) { - server.on("/", handleRoot); - server.on("/setup", handleSetup); - server.on("/save", handleSave); - server.on("/gpio", handleGpio); - server.on("/otamode", handleOTA); - server.on("/reboot", handleReboot); +void WebSetupServer(int bootmode) { + server.on("/", WebHandleRoot); + server.on("/setup", WebHandleSetup); + server.on("/save", WebHandleSave); + server.on("/gpio", WebHandleGpio); + server.on("/otamode", WebHandleOTA); + server.on("/reboot", WebHandleReboot); - server.onNotFound(handleNotFound); + server.onNotFound(WebHandleNotFound); server.begin(); Serial.println("HTTP server started"); } diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index a98c560..9714731 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -51,35 +51,35 @@ const char *ssid = "ESPConfigurator"; ESP8266WebServer server(80); /* WebServer decl*/ -void handleRoot(); -void handleSetup(); -void handleGpio(); -void handleSave(); -void handleOTA(); -void handleNotFound(); -void setupWebServer(int bootmode); +void WebHandleRoot(); +void WebHandleSetup(); +void WebHandleGpio(); +void WebHandleSave(); +void WebHandleOTA(); +void WebHandleNotFound(); +void WebSetupServer(int bootmode); /* EEPROM decl */ -int saveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttpasswd, int mqttPort); -int saveBootMode(uint8_t bootMode); -void readEEPROM(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort); +int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttpasswd, int mqttPort); +int EepromSaveBootMode(uint8_t bootMode); +void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort); /* BMP180 decl */ -int getTemperature(double &t); -int getTempAndPressure(double &t, double &p); -int setupBMP180(); -bool isBMPConnected(); +int BMP180GetTemperature(double &t); +int BMP180GetTempAndPressure(double &t, double &p); +int BMP180Setup(); +bool BMP180IsConnected(); /* MQTT decl */ -int MQTT_connect(); -int MQTT_isConnected(); -int setupMQTT(char *server, char *user, char *passwd, int port); -int publishMQTT(double temp, double pressure); -void checkMqttSubscription(); -void changeGpioValue(int gpio, int value); +int MqttConnect(); +int MqttIsConnected(); +int MqttSetup(char *server, char *user, char *passwd, int port); +int MqttPublish(double temp, double pressure); +void MqttCheckSubscription(); +void MqttChangeGpioValue(int gpio, int value); -void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { +void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { IPAddress myIP; if (bootmode == BOOTMODE_SETUP || forceSetup) { Serial.println("Configuring access point..."); @@ -110,7 +110,7 @@ void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, Serial.println(myIP); } -void setupOTA() { +void OTASetup() { // Port defaults to 8266 // ArduinoOTA.setPort(8266); @@ -122,7 +122,7 @@ void setupOTA() { //Disable OTA mode to avoid forever loop //Force BOOTMODE_SETUP in case eeprom layout have changed - saveBootMode(BOOTMODE_SETUP); + EepromSaveBootMode(BOOTMODE_SETUP); ArduinoOTA.onStart([]() { Serial.println("Start"); @@ -173,7 +173,7 @@ void setup() { #endif EEPROM.begin(EEPROM_SIZE); - readEEPROM(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort); + EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort); if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) { Serial.println("Configuration Found !:"); Serial.println(mode); @@ -193,15 +193,15 @@ void setup() { Serial.print(txStatus ? "No" : "Yes"); Serial.println(); - setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost); - setupMQTT(mqttServer, mqttUser, mqttPasswd, mqttPort); + WifiSetup(mode, txStatus == 0, confSsid, confPassword, confHost); + MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort); if (mode == BOOTMODE_OTA) { - setupOTA(); + OTASetup(); } else { - if (setupBMP180()) + if (BMP180Setup()) Serial.println("BMP180 init success"); - setupWebServer(mode); + WebSetupServer(mode); } } @@ -211,17 +211,17 @@ void loop() { ArduinoOTA.handle(); } else { server.handleClient(); - checkMqttSubscription(); + MqttCheckSubscription(); delay(WEB_DELAY_MS); nbCycle++; if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) { - if (isBMPConnected() && getTempAndPressure(temp, pressure) == 0) { + if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) { Serial.print("Current T°C "); Serial.print(temp); Serial.print( " Pressure mB "); Serial.println(pressure); - publishMQTT(temp, pressure); + MqttPublish(temp, pressure); } else { Serial.println("Cannot get T°C"); }