This shows you the differences between two versions of the page.
iothings:proiecte:2022sric:voltmeter [2023/06/01 23:05] eduard.olteanu1505 |
iothings:proiecte:2022sric:voltmeter [2023/06/01 23:40] (current) eduard.olteanu1505 |
||
---|---|---|---|
Line 34: | Line 34: | ||
</code> | </code> | ||
- | ==== Demo ==== | + | Communication between the ESP 32 and both the ADS1115 and the LCD screen is done through the I2C protocol. |
- | The main usecase of this project is to measure the voltage of batteries. | + | The button is connected to the GPIO4 pin and is used as a trigger to store in database the value read by the AIN0 pin from the ADS1115. |
- | {{ :iothings:proiecte:2022sric:20230601_225009.jpg ?500 |}} | + | WiFi connection: |
+ | <code> | ||
+ | WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
+ | Serial.print("Connecting to Wi-Fi"); | ||
+ | while (WiFi.status() != WL_CONNECTED){ | ||
+ | Serial.print("."); | ||
+ | delay(300); | ||
+ | } | ||
+ | Serial.println(); | ||
+ | Serial.print("Connected with IP: "); | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | Serial.println(); | ||
+ | </code> | ||
- | {{ :iothings:proiecte:2022sric:20230601_225034.jpg?500 |}} | + | Firebase connection: |
+ | <code> | ||
+ | /* Assign the api key (required) */ | ||
+ | config.api_key = API_KEY; | ||
+ | |||
+ | /* Assign the RTDB URL (required) */ | ||
+ | config.database_url = DATABASE_URL; | ||
+ | |||
+ | /* Sign up */ | ||
+ | if (Firebase.signUp(&config, &auth, "", "")){ | ||
+ | Serial.println("ok"); | ||
+ | signupOK = true; | ||
+ | } | ||
+ | else{ | ||
+ | Serial.printf("%s\n", config.signer.signupError.message.c_str()); | ||
+ | } | ||
+ | |||
+ | /* Assign the callback function for the long running token generation task */ | ||
+ | config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h | ||
+ | |||
+ | Firebase.begin(&config, &auth); | ||
+ | Firebase.reconnectWiFi(true); | ||
+ | </code> | ||
+ | |||
+ | Voltage measurement: | ||
+ | <code> | ||
+ | adc0 = ads.readADC_SingleEnded(0); | ||
+ | delay(10); | ||
+ | volts0 = ads.computeVolts(adc0); | ||
+ | |||
+ | lcd.clear(); | ||
+ | lcd.setCursor(0, 0); | ||
+ | lcd.print("ADC0:"); | ||
+ | lcd.print(adc0); | ||
+ | lcd.setCursor(0, 1); | ||
+ | lcd.print(volts0); | ||
+ | lcd.print("V"); | ||
+ | </code> | ||
+ | |||
+ | Storing the value in the database: | ||
+ | <code> | ||
+ | // read the state of the switch/button: | ||
+ | currentState = digitalRead(BUTTON_PIN); | ||
+ | if ((lastState == LOW && currentState == HIGH) && Firebase.ready() && signupOK) { | ||
+ | sendDataPrevMillis = millis(); | ||
+ | Serial.print ("time: "); | ||
+ | Serial.println (sendDataPrevMillis); | ||
+ | |||
+ | entry_path = path + "/" + String(sendDataPrevMillis); | ||
+ | |||
+ | // Write an Float number on the database path test/float | ||
+ | if (Firebase.RTDB.setFloat(&fbdo, entry_path , volts0)) { | ||
+ | Serial.println("PASSED"); | ||
+ | Serial.println("PATH: " + fbdo.dataPath()); | ||
+ | Serial.println("TYPE: " + fbdo.dataType()); | ||
+ | } | ||
+ | else { | ||
+ | Serial.println("FAILED"); | ||
+ | Serial.println("REASON: " + fbdo.errorReason()); | ||
+ | } | ||
+ | } | ||
+ | lastState = currentState; | ||
+ | </code> | ||
+ | We use millis to store the value in order not to overwrite already known values. | ||
+ | |||
+ | ==== Problems and Improvements ==== | ||
+ | * One problem that I could not fix is the noise the ADS1115 is getting when reading, even if there is nothing measured the pin reads 0.9V, but that doesn't impact measurements. | ||
+ | * An improvement could be to add some other form of identification for the batteries when stored in the database. | ||
+ | * Another improvement could be to have a phone application that manages the database for the batteries and also that communicates with the ESP32. | ||
+ | |||
+ | ===== Results ===== | ||
+ | The main use-case of this project is to measure the voltage of batteries. | ||
+ | |||
+ | {{ :iothings:proiecte:2022sric:20230601_225009.jpg ?700 |}} | ||
+ | |||
+ | {{ :iothings:proiecte:2022sric:20230601_225034.jpg?700 |}} | ||
We can see that the value is pretty accurate, only 0.01 error delta. | We can see that the value is pretty accurate, only 0.01 error delta. | ||
Also, if the button is pressed the value is stored in the database. | Also, if the button is pressed the value is stored in the database. | ||
- | {{ :iothings:proiecte:2022sric:database_batteries.png?500 |}} | + | {{ :iothings:proiecte:2022sric:database_batteries.png?700 |}} |
- | + | ||
- | ===== Conclusion ===== | + | |
===== Bibliography===== | ===== Bibliography===== | ||
* [[https://ocw.cs.pub.ro/courses/iothings/laboratoare/2022/lab4]] | * [[https://ocw.cs.pub.ro/courses/iothings/laboratoare/2022/lab4]] | ||
* [[https://how2electronics.com/how-to-use-ads1115-16-bit-adc-module-with-esp32/]] | * [[https://how2electronics.com/how-to-use-ads1115-16-bit-adc-module-with-esp32/]] | ||
+ | * [[https://github.com/mobizt/Firebase-ESP-Client]] | ||
+ | * [[https://github.com/adafruit/Adafruit_ADS1X15]] | ||