#include #include #include #include int pinsDout[] = {22, 21, 19, 18}; const int numPins = sizeof(pinsDout) / sizeof(pinsDout[0]); int pinSlk = 17; HX711_asukiaaa::Reader reader(pinsDout, numPins, pinSlk); //---------------------------------------------------// // Resistors for HX711 module AE-HX711-SIP // https://akizukidenshi.com/catalog/g/gK-12370/ //---------------------------------------------------// #define HX711_R1 20000.0 #define HX711_R2 8200.0 #define LOAD_CELL_RATED_VOLT 0.002f #define LOAD_CELL_RATED_GRAM 50000.0f const char* ssid = ""; const char* password = ""; HX711_asukiaaa::Parser parser(LOAD_CELL_RATED_VOLT, LOAD_CELL_RATED_GRAM, HX711_R1, HX711_R2); float offsetGrams[numPins]; RTC_DATA_ATTR int lastSendTimeAgo = 0; RTC_DATA_ATTR float lastSendSensors[] = {0, 0, 0, 0}; RTC_DATA_ATTR int sendsFailed = 0; RTC_DATA_ATTR int sendsSucceeded = 0; RTC_DATA_ATTR int metricServerIpCache = 0; int sensorReadDelay = 10; //seconds int significantWeightChange = 50; //grams int erroneousWeightChange = 600; //grams int maxTimeBetweenSamples = 60*15; //seconds int wiFiConnectTimeout = 10; //seconds const char* metricsServerHostname = "pi-grafana"; IPAddress metricsServerIpFallback = IPAddress(192,168,1,84); IPAddress resolveMetricServerIpAddress(bool useCache = true) { if (useCache == true && metricServerIpCache != 0 && sendsFailed % 10 == 0) { Serial.println("Clearing mDNS cache due to too many failures"); metricServerIpCache = 0; } Serial.print("Resolving mDNS..."); if (useCache == true && metricServerIpCache != 0) { IPAddress metricsServerIp = IPAddress(metricServerIpCache); Serial.println(" CACHED! (" + metricsServerIp.toString() + ")"); return metricsServerIp; } if(!MDNS.begin("apiometer-a")) { Serial.println(" ERROR!"); } else { IPAddress metricsServerIp = MDNS.queryHost(metricsServerHostname); if (metricsServerIp.toString() == "0.0.0.0") { Serial.println(" FAILED! Using fallback (" + metricsServerIpFallback.toString() + ")"); } else { Serial.println(" OK! (" + metricsServerIp.toString() + ")"); metricServerIpCache = int(metricsServerIp); return metricsServerIp; } } return metricsServerIpFallback; } bool sendApiRequest(IPAddress metricsServerIp, float lastReadSensors[], float vBat) { Serial.print("Sending API request..."); const String metricsServerUrl = "http://" + metricsServerIp.toString() + ":6634/"; HTTPClient http; http.setTimeout(3000); http.setConnectTimeout(3000); http.begin(metricsServerUrl); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String httpRequestData = "instance=a&sensor[0]=" + String(lastReadSensors[0]) + "&sensor[1]=" + String(lastReadSensors[1]) + "&sensor[2]=" + String(lastReadSensors[2]) + "&sensor[3]=" + String(lastReadSensors[3]) + "&vBat=" + String(vBat) + "&sendsSucceeded=" + String(sendsSucceeded) + "&sendsFailed=" + String(sendsFailed); int httpResponseCode = http.POST(httpRequestData); http.end(); if (httpResponseCode >= 200) { Serial.println(" OK!"); return true; } else { Serial.println(" FAIL! (" + String(httpResponseCode) + ")"); return false; } } bool connectToWifi() { Serial.print("Connecting to WiFi"); WiFi.mode(WIFI_STA); /* Configure ESP32 in STA Mode */ //IPAddress ip(192,168,1,6); //IPAddress gateway(192,168,1,1); //IPAddress subnet(255,255,255,0); //WiFi.config(ip, gateway, subnet); WiFi.begin(ssid, password); /* Connect to Wi-Fi based on the above SSID and Password */ int wiFiConnectAttempts = 0; while(WiFi.status() != WL_CONNECTED && wiFiConnectAttempts++ < wiFiConnectTimeout) { Serial.print("."); delay(1000); } if (WiFi.status() != WL_CONNECTED) { Serial.println(" FAILED!"); return false; } else { Serial.println(" OK!"); return true; } } void setup() { bool send = false; bool sent = false; bool sensorReadOk = true; Serial.begin(115200); Serial.println("start"); Serial.print("Reading vBat... "); float vBat = 0; for (int i = 0; i < 10; i++) { vBat += analogRead(GPIO_NUM_35); } vBat /= 10; Serial.print(vBat); Serial.println(); gpio_hold_dis(GPIO_NUM_16); pinMode(GPIO_NUM_16, OUTPUT); digitalWrite(GPIO_NUM_16, HIGH); delay(1000); reader.begin(); offsetGrams[0] = 98561.95; offsetGrams[1] = 99560.63; offsetGrams[2] = 97561.63; offsetGrams[3] = 98481.84; float lastReadSensors[4]; int suspectSensorReads = 0; auto readState = reader.read(); Serial.print("Reading sensors... "); if (readState == HX711_asukiaaa::ReadState::Success) { for (int i = 0; i < reader.doutLen; ++i) { float sensorWeight = parser.parseToGram(reader.values[i]) - offsetGrams[i]; Serial.print("S" + String(i) + ": " + String (sensorWeight)); lastReadSensors[i] = sensorWeight; float weightChangeSinceLastSend = abs(lastSendSensors[i] - sensorWeight); if (weightChangeSinceLastSend > erroneousWeightChange && lastSendSensors[i] != 0) { suspectSensorReads++; } if (weightChangeSinceLastSend >= significantWeightChange) { send = true; Serial.print("*"); } Serial.print(" "); } } else { sensorReadOk = false; } Serial.println(); digitalWrite(GPIO_NUM_16, LOW); if (suspectSensorReads == 1 || (lastReadSensors[0] + lastReadSensors[1] + lastReadSensors[2] + lastReadSensors[3]) == 0) { sensorReadOk = false; } if (sensorReadOk == false) { Serial.print("Sensor read failed!"); send = false; } else if (lastSendTimeAgo >= (maxTimeBetweenSamples * 1000)) { send = true; Serial.println("Last send too long ago: " + String(lastSendTimeAgo) + "ms"); } if (send == true) { if (connectToWifi() == true) { IPAddress metricServerIp = resolveMetricServerIpAddress(); sent = sendApiRequest(metricServerIp, lastReadSensors, vBat); WiFi.disconnect(true, true); } } if (sent == true) { lastSendSensors[0] = lastReadSensors[0]; lastSendSensors[1] = lastReadSensors[1]; lastSendSensors[2] = lastReadSensors[2]; lastSendSensors[3] = lastReadSensors[3]; lastSendTimeAgo = 0; } else { lastSendTimeAgo += (sensorReadDelay * 1000); lastSendTimeAgo += millis(); } if (send == true) { if (sent == true) { sendsSucceeded += 1; } else { sendsFailed += 1; } } gpio_hold_en(GPIO_NUM_16); Serial.println("Zzzzz"); esp_deep_sleep(sensorReadDelay * 1000000); } void loop() { }