Change function naming to be coherent across files
This commit is contained in:
parent
d729750ed5
commit
a89a8a0727
@ -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)
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
void handleRoot() {
|
||||
void WebHandleRoot() {
|
||||
server.send(200, "text/html",
|
||||
"<head><meta http-equiv=\"refresh\" content=\"" + String(SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
|
||||
"<h1>You are connected</h1><br/>"
|
||||
"Current temperature " + String(temp, 2) + "C<br/>"
|
||||
"Current pressure " + String(pressure, 2) + "mB<br/>"
|
||||
"MQTT Status: " + (MQTT_isConnected() ? "Connected" : "Disconnected") + "<br/>"
|
||||
"BMP 180 (Temp+Pression) Status: " + (isBMPConnected() ? "Connected" : "Disconnected") + "<br/>"
|
||||
"MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "<br/>"
|
||||
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>"
|
||||
"Free space: "+ ESP.getFreeSketchSpace() +"<br/>"
|
||||
"Free heap: "+ ESP.getFreeHeap() +"<br/>"
|
||||
"<a href=\"/setup\">Setup</a><br/>"
|
||||
@ -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", "<form action=\"/save\" method=\"get\">"
|
||||
"<fieldset>"
|
||||
@ -45,17 +45,17 @@ void handleSetup() {
|
||||
"</form>");
|
||||
}
|
||||
|
||||
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", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
|
||||
}
|
||||
|
||||
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() {
|
||||
"<a href=\"/reboot\">Reboot</a><br/>");
|
||||
}
|
||||
|
||||
void handleOTA() {
|
||||
void WebHandleOTA() {
|
||||
Serial.println("Boot mode Set to OTA");
|
||||
saveBootMode(BOOTMODE_OTA);
|
||||
EepromSaveBootMode(BOOTMODE_OTA);
|
||||
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
|
||||
"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", "<h1>Device Reboot</h1><br/>");
|
||||
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");
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user