From 4525d5e8b243331c840b5a8c7405d1147a444051 Mon Sep 17 00:00:00 2001 From: Maxim Kudlayev Date: Sun, 27 Nov 2016 20:24:39 +0600 Subject: [PATCH] Small refactoring code --- esp_sensor/esp_sensor.ino | 97 ++++++++++++--------------------------- esp_sensor/support.ino | 60 +++++++++--------------- 2 files changed, 51 insertions(+), 106 deletions(-) diff --git a/esp_sensor/esp_sensor.ino b/esp_sensor/esp_sensor.ino index 2d77165..5866391 100644 --- a/esp_sensor/esp_sensor.ino +++ b/esp_sensor/esp_sensor.ino @@ -31,7 +31,7 @@ JsonConf JConf; #if defined(DHT_ON) #include // Uncomment the type of sensor in use: - //#define DHTTYPE DHT11 // DHT 11 + //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT dht(atoi(JConf.dht_pin), DHTTYPE); @@ -42,7 +42,6 @@ JsonConf JConf; OneWire ds(DS18X20_PIN); #endif - #if defined(BH1750_ON) #include "BH1750.h" BH1750 lightSensor; @@ -69,13 +68,12 @@ JsonConf JConf; PZEM_RESET_ENUM pzem_reset_stage = PZEM_STAGE1; #endif -WiFiUDP portUDP; // UDP Syslog - +WiFiUDP portUDP; // UDP Syslog ADC_MODE(ADC_VCC); float voltage_float; -String network_html; // Список доступных Wi-Fi точек +String network_html; // Список доступных Wi-Fi точек ESP8266WebServer WebServer(80); @@ -117,12 +115,9 @@ Adafruit_MQTT_Publish pubTopic_ds5 = Adafruit_MQTT_Publish(&mqtt, JConf.publish_ Adafruit_MQTT_Subscribe subTopicMotionSensorTimer = Adafruit_MQTT_Subscribe(&mqtt, JConf.command_pub_topic); Adafruit_MQTT_Subscribe subTopicMotionSensorTimer2 = Adafruit_MQTT_Subscribe(&mqtt, JConf.command_pub_topic); - Adafruit_MQTT_Subscribe subTopicLightType = Adafruit_MQTT_Subscribe(&mqtt, JConf.command_pub_topic); Adafruit_MQTT_Subscribe subTopicLightType2 = Adafruit_MQTT_Subscribe(&mqtt, JConf.command_pub_topic); - Adafruit_MQTT_Subscribe subTopicUptime = Adafruit_MQTT_Subscribe(&mqtt, JConf.command_pub_topic); - Adafruit_MQTT_Subscribe subTopicPzemReset = Adafruit_MQTT_Subscribe(&mqtt, JConf.command_pub_topic); struct FADING_T @@ -137,8 +132,7 @@ struct FADING_T {atoi(JConf.light2_pin),0,0,0,20} }; - -///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ROOT +//////////////////////////////////////////////////////////////////////////////// /* static char* floatToChar(float charester) @@ -203,7 +197,7 @@ void GetLightSensorData() addLog_P(LOG_LEVEL_DEBUG_MORE, "Func: GetLightSensorData Start"); luxString = String(lightSensor.readLightLevel()); - + snprintf_P(log, sizeof(log), PSTR("GetLightSensorData: Lux: %s"), luxString.c_str()); addLog(LOG_LEVEL_INFO, log); @@ -217,7 +211,7 @@ void GetLightSensorData() #ifdef BME280_ON void GetBmeSensorData() -{ +{ char log[LOGSZ]; unsigned long start_time = millis(); addLog_P(LOG_LEVEL_DEBUG_MORE, "Func: GetBmeSensorData Start"); @@ -230,7 +224,6 @@ void GetBmeSensorData() snprintf_P(log, sizeof(log), PSTR("GetBmeSensorData: Pressure: %s"), pressureString.c_str()); addLog(LOG_LEVEL_INFO, log); - humidityString = String(bmeSensor.readFloatHumidity()); snprintf_P(log, sizeof(log), PSTR("GetBmeSensorData: Humidity: %s %"), humidityString.c_str()); addLog(LOG_LEVEL_INFO, log); @@ -386,7 +379,6 @@ void GetDS18x20SensorData(){ addLog_P(LOG_LEVEL_ERROR, "DS Sensor: Data CRC is not valid!"); return; } - // Convert the data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits @@ -409,11 +401,9 @@ void GetDS18x20SensorData(){ addLog_P(LOG_LEVEL_ERROR, "Device is not a DS18x20 family device!"); } - //float celsius = (float) raw / 16.0; - dsData[currentDsSensor].dsTemp = String((float) raw / 16.0); - dsDataPrint(); + if (findDsSensors == currentDsSensor+1){ currentDsSensor = 0; } else { @@ -431,21 +421,20 @@ void dsDataPrint(){ String dsType = " "; - switch (dsData[currentDsSensor].address[0]) - { - case 0x10: - dsType = "DS18S20"; // or old DS1820 - break; - case 0x28: - dsType = "DS18B20"; - break; - case 0x22: - dsType = "DS1822"; - break; - default: - addLog_P(LOG_LEVEL_ERROR, "Device is not a DS18x20 family device!"); - return; - } + switch (dsData[currentDsSensor].address[0]) { + case 0x10: + dsType = "DS18S20"; // or old DS1820 + break; + case 0x28: + dsType = "DS18B20"; + break; + case 0x22: + dsType = "DS1822"; + break; + default: + addLog_P(LOG_LEVEL_ERROR, "Device is not a DS18x20 family device!"); + return; + } snprintf_P(log, sizeof(log), PSTR("DS type:%s addr:%s temp:%sC"), dsType.c_str(), dsData[currentDsSensor].addressString.c_str(), dsData[currentDsSensor].dsTemp.c_str()); addLog(LOG_LEVEL_INFO, log); @@ -465,7 +454,7 @@ bool GetPzemData(float data, String *val) { unsigned long start_time = millis(); addLog_P(LOG_LEVEL_DEBUG_MORE, "Func: GetPzemData Start"); - if (data < 0.0){ + if (data < 0.0) { addLog_P(LOG_LEVEL_ERROR, "GetPzemData: Error reading data!"); pzem.setAddress(ip_pzem); pzem.setReadTimeout(500); @@ -474,7 +463,7 @@ bool GetPzemData(float data, String *val) { data = data * coil_ratio / 1000; } else if (pzem_current_read == PZEM_CURRENT) { data = data * coil_ratio; - } + } *val = String(data); unsigned long load_time = millis() - start_time; @@ -486,7 +475,7 @@ bool GetPzemData(float data, String *val) { -void GetPzemSerialRead() { +void GetPzemSerialRead() { char log[LOGSZ]; unsigned long start_time = millis(); @@ -515,7 +504,7 @@ void GetPzemSerialRead() { } break; case PZEM_ENERGY: - if (GetPzemData(pzem.energy(ip_pzem), &pzemEnergyString)){ + if (GetPzemData(pzem.energy(ip_pzem), &pzemEnergyString)) { snprintf_P(log, sizeof(log), PSTR("GetPzemSerialRead: Energy: %s Wh"), pzemEnergyString.c_str()); addLog(LOG_LEVEL_INFO, log); } @@ -573,6 +562,7 @@ void PzemResetEnergy() { #endif + void MotionDetect(){ char log[LOGSZ]; @@ -603,8 +593,8 @@ String GetUptimeData(){ unsigned long start_time = millis(); addLog_P(LOG_LEVEL_DEBUG_MORE, "Func: GetUptimeData Start"); - //** Making Note of an expected rollover *****// - if(millis()>=3000000000){ + //** Making Note of an expected rollover *****// + if(millis()>=3000000000){ HighMillis=1; } //** Making note of actual rollover **// @@ -614,7 +604,6 @@ String GetUptimeData(){ } long secsUp = millis()/1000; - Second = secsUp%60; Minute = (secsUp/60)%60; Hour = (secsUp/(60*60))%24; @@ -666,7 +655,6 @@ bool MqttConnect() { } addLog_P(LOG_LEVEL_INFO, "MqttConnect: Connecting to MQTT..."); - if ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected snprintf_P(log, sizeof(log), PSTR("MqttConnect: Error: %s"), mqtt.connectErrorString(ret)); addLog(LOG_LEVEL_ERROR, log); @@ -690,7 +678,6 @@ void MqttInit() { if (atoi(JConf.mqtt_enable) != 1) { return; } - //Publish Topics sprintf(lightType_buff, "%s%s%s", JConf.publish_topic, lightType, JConf.mqtt_name); pubTopicLightType = Adafruit_MQTT_Publish(&mqtt, lightType_buff); @@ -789,7 +776,6 @@ void MqttInitDS() { - bool MqttPubLightState(){ if (atoi(JConf.mqtt_enable) != 1) { @@ -849,7 +835,6 @@ bool MqttPubLightOffDelay() { } pubTopicMotionSensorTimer.publish(JConf.lightoff_delay); - pubTopicMotionSensorTimer2.publish(JConf.light2off_delay); unsigned long load_time = millis() - start_time; @@ -914,7 +899,6 @@ bool MqttPubData() { } #endif //DS18X20_ON - unsigned long load_time = millis() - start_time; snprintf_P(log, sizeof(log), PSTR("Func: MqttPubData load time: %d"), load_time); addLog(LOG_LEVEL_DEBUG_MORE, log); @@ -1110,12 +1094,6 @@ void TestSystemPrint() - - - - - - void getData(){ #ifdef NTP_ON @@ -1176,8 +1154,6 @@ void getData(){ if (millis() - Uart.timerAnalogPin[i] >= 60000){ Uart.valueAnalogPin[i] = 0; Uart.SetAnalogReadCycle(i, 10, "s"); - } else { - } } #endif @@ -1191,8 +1167,6 @@ void setup() { delay(100); Serial.println(); - - if (!SPIFFS.begin()) { addLog_P(LOG_LEVEL_NONE, "setup: Failed to mount file system"); return; @@ -1201,13 +1175,7 @@ void setup() { deleteConfigFile(); #endif } -/* - if (!JConf.saveConfig()) { - Serial.println("Failed to save config"); - } else { - Serial.println("Config saved"); - } -*/ + if (!JConf.loadConfig()) { addLog_P(LOG_LEVEL_NONE, "setup: Failed to load config"); } else { @@ -1252,7 +1220,6 @@ void setup() { dht.begin(); #endif - #ifdef BME280_ON if (atoi(JConf.bme280_enable) == 1) { bmeSensor.settings.commInterface = I2C_MODE; @@ -1281,7 +1248,6 @@ void setup() { myHTU21D.begin(4, 5); //SDA=4, SCL=5 #endif - if (atoi(JConf.mqtt_enable) == 1) { if (atoi(JConf.mqtt_auth_enable) == 1){ mqtt = Adafruit_MQTT_Client(&espClient, JConf.mqtt_server, atoi(JConf.mqtt_port), JConf.mqtt_user, JConf.mqtt_pwd); @@ -1297,7 +1263,6 @@ void setup() { WebServerInit(); #endif // USE_WEBSERVER - #ifdef NTP_ON if (atoi(JConf.ntp_enable) == 1) { NTPSettingsUpdate(); @@ -1324,8 +1289,6 @@ void setup() { #endif GetMacString(); - - CFG_Default(); } @@ -1368,4 +1331,4 @@ void loop() { #endif yield(); -} \ No newline at end of file +} diff --git a/esp_sensor/support.ino b/esp_sensor/support.ino index 5ab0659..c8a72b3 100644 --- a/esp_sensor/support.ino +++ b/esp_sensor/support.ino @@ -1,10 +1,4 @@ -void CFG_Default() { - -} - - - /*********************************************************************************************\ * Wi-Fi \*********************************************************************************************/ @@ -135,14 +129,14 @@ bool wifiSTA() { WiFi.mode(WIFI_STA); //setup station mode WiFi.begin(JConf.sta_ssid, JConf.sta_pwd); delay(500); - + wifi_set_phy_mode((phy_mode_t)PHY_MODE_11N); //setup PHY_MODE if (!wifiTryConnect()) { return false; } WiFi.hostname(JConf.module_id); - return true; + return true; } @@ -221,7 +215,7 @@ void WiFiSafeSetup() void wifiSafeModeReconnect() { if (wifiSafeMode == true && WiFiSetup()) { - wifiSafeMode = false; + wifiSafeMode = false; } } /*********************************************************************************************\ @@ -231,7 +225,7 @@ void wifiSafeModeReconnect() { void GetFreeMemory () { - + char log[LOGSZ]; unsigned long start_time = millis(); addLog_P(LOG_LEVEL_DEBUG_MORE, "Func: GetFreeMemory Start"); @@ -271,7 +265,7 @@ void GetMacString () { uint8_t macData[6]; WiFi.macAddress(macData); sprintf_P(value_buff, (const char *)F("%x:%x:%x:%x:%x:%x"), macData[0], macData[1], macData[2], macData[3], macData[4], macData[5]); - + macString = String(value_buff); unsigned long load_time = millis() - start_time; @@ -293,21 +287,16 @@ IPAddress stringToIp (String strIp) { int count = 0; for(int i=0; i <= strIp.length(); i++) { - if(strIp[i] != '.') - { + if(strIp[i] != '.') { temp += strIp[i]; - } - else - { - if(count < 4) - { + } else { + if(count < 4) { ip[count] = atoi(temp.c_str()); temp = ""; count++; } } - if(i==strIp.length()) - { + if(i==strIp.length()) { ip[count] = atoi(temp.c_str()); } } @@ -361,7 +350,7 @@ bool isIPValid(const char * IP) { return false; } } - + if (dotcount!=3) { //if not 3 dots then it is wrong return false; } @@ -428,7 +417,6 @@ void FadeSwitchLoop(){ } } */ - void FadeSwitchLoop(){ if (fading[0].cycleEnd != fading[0].cycleNow) { FadeSwitchDelay(0); @@ -440,7 +428,6 @@ void FadeSwitchLoop(){ - void restartESP() { addLog_P(LOG_LEVEL_INFO, "restartESP: Restart ESP!"); delay(100); @@ -450,14 +437,14 @@ void restartESP() { void deleteConfigFile() { - pinMode(atoi(JConf.reset_pin), INPUT); + pinMode(atoi(JConf.reset_pin), INPUT); if (digitalRead(atoi(JConf.reset_pin)) == LOW) { delay(3000); if (digitalRead(atoi(JConf.reset_pin)) == LOW) { addLog_P(LOG_LEVEL_INFO, "deleteConfigFile: Reset pin pressed. Delete config file!!!"); JConf.deleteConfig(); } - } + } } @@ -465,8 +452,8 @@ void deleteConfigFile() { * Syslog \*********************************************************************************************/ -void syslog(const char *message) -{ +void syslog(const char *message) { + char mess[MESSZ], str[TOPSZ+MESSZ]; portUDP.beginPacket(JConf.sys_log_host, atoi(JConf.sys_log_port)); @@ -479,17 +466,13 @@ void syslog(const char *message) -void addLog(byte loglevel, const char *line) -{ - //char mxtime[9]; - -// snprintf_P(mxtime, sizeof(mxtime), PSTR("%02d:%02d:%02d"), rtcTime.Hour, rtcTime.Minute, rtcTime.Second); - +void addLog(byte loglevel, const char *line) { + #ifdef DEBUG_ESP_PORT if (atoi(JConf.ntp_enable) == 1) { - DEBUG_ESP_PORT.printf("%s %s\n", ntpTimeString, line); + DEBUG_ESP_PORT.printf("%s %s\n", ntpTimeString, line); } else{ - DEBUG_ESP_PORT.printf("%s\n", line); + DEBUG_ESP_PORT.printf("%s\n", line); } #endif // DEBUG_ESP_PORT @@ -517,10 +500,10 @@ void addLog(byte loglevel, const char *line) -void addLog_P(byte loglevel, const char *formatP) -{ - char mess[MESSZ]; +void addLog_P(byte loglevel, const char *formatP) { + char mess[MESSZ]; + snprintf_P(mess, sizeof(mess), formatP); addLog(loglevel, mess); } @@ -528,4 +511,3 @@ void addLog_P(byte loglevel, const char *formatP) /*********************************************************************************************\ * Syslog * \*********************************************************************************************/ -