Temp captor:fixed, Battery measurement:work, solder on esp32, not final version

This commit is contained in:
auel 2022-05-20 17:01:50 +02:00
parent a8f2c7ebf3
commit 6105de9f97
11 changed files with 365 additions and 15 deletions

View file

@ -15,14 +15,16 @@
#include <Pangodream_18650_CL.h> #include <Pangodream_18650_CL.h>
#include <secret.h> #include <secret.h>
#define TOPIC "test1" #define TOPIC "test-alex"
#define MQTT_ADDRESS "192.168.0.242" #define MQTT_ADDRESS "185.233.103.24"
#define MQTT_PORT 1883 #define MQTT_PORT 1883
#define ESPNAME "esp32-bastien" #define ESPNAME "esp32-bastien"
#define CLUSTER "grappe1" #define CLUSTER "grappe1"
#define SENSORS_NUMBER 5 #define SENSORS_NUMBER 5
#define DHT22 22 #define DHT22 22
#define TIME_TO_SLEEP 598 //comment for the test part this is the good frequency
//#define TIME_TO_SLEEP 598
#define TIME_TO_SLEEP 8
#define US_TO_S_FACTOR 1000000 #define US_TO_S_FACTOR 1000000
#define R2 100 #define R2 100
#define R3 10 #define R3 10
@ -33,8 +35,9 @@
#define VOLTAGE_TO_ADC(in) ((ADC_REFERENCE * (in)) / 4096) #define VOLTAGE_TO_ADC(in) ((ADC_REFERENCE * (in)) / 4096)
#define BATTERY_MAX_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MAX)) #define BATTERY_MAX_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MAX))
#define BATTERY_MIN_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MIN)) #define BATTERY_MIN_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MIN))
#define ADC_PIN 34 // Solder apply on all riders behind the ESP32 for the battery voltage captor
#define CONV_FACTOR 2.92 #define ADC_PIN 35
#define CONV_FACTOR 1.7
#define READS 20 #define READS 20
void setupMQTT(const char *address, int port); void setupMQTT(const char *address, int port);
@ -48,4 +51,9 @@ void writeMsg(char *txt, float *temp, float *hum, int number);
//timeClient.getEpochTime().toCharArray(date, 50); = convertir un string en char //timeClient.getEpochTime().toCharArray(date, 50); = convertir un string en char
/*
For test the esp32 signal emission without server Working/fixed/updated :
mosquitto_sub -h cohabit-capteurs.aquilenet.fr -u capteurs -P Fablab -t test-alex
*/
#endif #endif

View file

@ -1,5 +1,5 @@
#define MQTT_USER "NomUtilisateurMQTT" #define MQTT_USER "capteurs"
#define MQTT_MDP "MotDePasseMQTT" #define MQTT_MDP "Fablab"
#define SSID "NomDuWifi" #define SSID "Thermo-Bibli"
#define PWD "MotDePasseWifi" #define PWD "17413278"

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Pangodream
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,2 @@
# 18650CL
Library to calculate 18650 charge in Arduino environment

View file

@ -0,0 +1,28 @@
#include <Pangodream_18650_CL.h>
//#define ADC_PIN 34
//#define CONV_FACTOR 1.7
//#define READS 20
Pangodream_18650_CL BL;
/**
* If you need to change default values you can use it as
* Pangodream_18650_CL BL(ADC_PIN, CONV_FACTOR, READS);
*/
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.print("Value from pin: ");
Serial.println(analogRead(34));
Serial.print("Average value from pin: ");
Serial.println(BL.pinRead());
Serial.print("Volts: ");
Serial.println(BL.getBatteryVolts());
Serial.print("Charge level: ");
Serial.println(BL.getBatteryChargeLevel());
Serial.println("");
delay(1000);
}

View file

@ -0,0 +1,18 @@
#######################################
# Syntax Coloring Map BH1750FVI
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Pangodream_18650_CL KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
getBatteryChargeLevel KEYWORD2
getBatteryVolts KEYWORD2
getAnalogPin KEYWORD2
pinRead KEYWORD2
getConvFactor KEYWORD2

View file

@ -0,0 +1,9 @@
name=Pangodream_18650_CL
version=1.0.1
author=Pangodream
maintainer=Pangodream
sentence=Pangodream Library to calculate 18650 charge
paragraph=Pangodream Library to calculate 18650 Ion-Li battery charge
category=Uncategorized
url=https://github.com/pangodream/18650CL
architectures=esp32

View file

@ -0,0 +1,154 @@
/*
MIT License
Copyright (c) 2019 Pangodream
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "Arduino.h"
#include "Pangodream_18650_CL.h"
Pangodream_18650_CL::Pangodream_18650_CL(int addressPin, double convFactor, int reads)
{
_reads = reads;
_convFactor = convFactor;
_addressPin = addressPin;
_initVoltsArray();
}
Pangodream_18650_CL::Pangodream_18650_CL(int addressPin, double convFactor)
{
_reads = DEF_READS;
_convFactor = convFactor;
_addressPin = addressPin;
_initVoltsArray();
}
Pangodream_18650_CL::Pangodream_18650_CL(int addressPin)
{
_reads = DEF_READS;
_convFactor = DEF_CONV_FACTOR;
_addressPin = addressPin;
_initVoltsArray();
}
Pangodream_18650_CL::Pangodream_18650_CL()
{
_reads = DEF_READS;
_convFactor = DEF_CONV_FACTOR;
_addressPin = DEF_PIN;
_initVoltsArray();
}
int Pangodream_18650_CL::getAnalogPin()
{
return _addressPin;
}
double Pangodream_18650_CL::getConvFactor()
{
return _convFactor;
}
/**
* Loads each voltage value in its matching charge element (index)
*/
void Pangodream_18650_CL::_initVoltsArray(){
_vs[0] = 3.200;
_vs[1] = 3.250; _vs[2] = 3.300; _vs[3] = 3.350; _vs[4] = 3.400; _vs[5] = 3.450;
_vs[6] = 3.500; _vs[7] = 3.550; _vs[8] = 3.600; _vs[9] = 3.650; _vs[10] = 3.700;
_vs[11] = 3.703; _vs[12] = 3.706; _vs[13] = 3.710; _vs[14] = 3.713; _vs[15] = 3.716;
_vs[16] = 3.719; _vs[17] = 3.723; _vs[18] = 3.726; _vs[19] = 3.729; _vs[20] = 3.732;
_vs[21] = 3.735; _vs[22] = 3.739; _vs[23] = 3.742; _vs[24] = 3.745; _vs[25] = 3.748;
_vs[26] = 3.752; _vs[27] = 3.755; _vs[28] = 3.758; _vs[29] = 3.761; _vs[30] = 3.765;
_vs[31] = 3.768; _vs[32] = 3.771; _vs[33] = 3.774; _vs[34] = 3.777; _vs[35] = 3.781;
_vs[36] = 3.784; _vs[37] = 3.787; _vs[38] = 3.790; _vs[39] = 3.794; _vs[40] = 3.797;
_vs[41] = 3.800; _vs[42] = 3.805; _vs[43] = 3.811; _vs[44] = 3.816; _vs[45] = 3.821;
_vs[46] = 3.826; _vs[47] = 3.832; _vs[48] = 3.837; _vs[49] = 3.842; _vs[50] = 3.847;
_vs[51] = 3.853; _vs[52] = 3.858; _vs[53] = 3.863; _vs[54] = 3.868; _vs[55] = 3.874;
_vs[56] = 3.879; _vs[57] = 3.884; _vs[58] = 3.889; _vs[59] = 3.895; _vs[60] = 3.900;
_vs[61] = 3.906; _vs[62] = 3.911; _vs[63] = 3.917; _vs[64] = 3.922; _vs[65] = 3.928;
_vs[66] = 3.933; _vs[67] = 3.939; _vs[68] = 3.944; _vs[69] = 3.950; _vs[70] = 3.956;
_vs[71] = 3.961; _vs[72] = 3.967; _vs[73] = 3.972; _vs[74] = 3.978; _vs[75] = 3.983;
_vs[76] = 3.989; _vs[77] = 3.994; _vs[78] = 4.000; _vs[79] = 4.008; _vs[80] = 4.015;
_vs[81] = 4.023; _vs[82] = 4.031; _vs[83] = 4.038; _vs[84] = 4.046; _vs[85] = 4.054;
_vs[86] = 4.062; _vs[87] = 4.069; _vs[88] = 4.077; _vs[89] = 4.085; _vs[90] = 4.092;
_vs[91] = 4.100; _vs[92] = 4.111; _vs[93] = 4.122; _vs[94] = 4.133; _vs[95] = 4.144;
_vs[96] = 4.156; _vs[97] = 4.167; _vs[98] = 4.178; _vs[99] = 4.189; _vs[100] = 4.200;
}
int Pangodream_18650_CL::getBatteryChargeLevel()
{
int readValue = _analogRead(_addressPin);
double volts = _analogReadToVolts(readValue);
int chargeLevel = _getChargeLevel(volts);
return chargeLevel;
}
int Pangodream_18650_CL::pinRead(){
return _analogRead(_addressPin);
}
int Pangodream_18650_CL::_analogRead(int pinNumber){
int totalValue = 0;
int averageValue = 0;
for(int i = 0; i < _reads; i++){
totalValue += analogRead(pinNumber);
}
averageValue = totalValue / _reads;
return averageValue;
}
/**
* Performs a binary search to find the index corresponding to a voltage.
* The index of the array is the charge %
*/
int Pangodream_18650_CL::_getChargeLevel(double volts){
int idx = 50;
int prev = 0;
int half = 0;
if (volts >= 4.2){
return 100;
}
if (volts <= 3.2){
return 0;
}
while(true){
half = abs(idx - prev) / 2;
prev = idx;
if(volts >= _vs[idx]){
idx = idx + half;
}else{
idx = idx - half;
}
if (prev == idx){
break;
}
}
return idx;
}
double Pangodream_18650_CL::_analogReadToVolts(int readValue){
double volts;
volts = readValue * _convFactor / 1000;
return volts;
}
double Pangodream_18650_CL::getBatteryVolts(){
int readValue = analogRead(_addressPin);
return _analogReadToVolts(readValue);
}

View file

@ -0,0 +1,94 @@
/*
MIT License
Copyright (c) 2019 Pangodream
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* 18650 Ion-Li battery charge
*/
#ifndef Pangodream_18650_CL_h
#define Pangodream_18650_CL_h
#include "Arduino.h"
#define DEF_PIN 34
#define DEF_CONV_FACTOR 1.7
#define DEF_READS 20
/*
* 18650 Ion-Li battery charge
* Calculates charge level of an 18650 Ion-Li battery
*/
class Pangodream_18650_CL {
public:
/*
* Constructor
* @param addressPin, ADC pin number where the voltage divider is connected to
*/
Pangodream_18650_CL(int addressPin);
/*
* Constructor
* @param addressPin, ADC pin number where the voltage divider is connected to
* @param convFactor, Convertion factor for analog read units to volts
*/
Pangodream_18650_CL(int addressPin, double convFactor);
/*
* Constructor
* @param addressPin, ADC pin number where the voltage divider is connected to
* @param convFactor, Convertion factor for analog read units to volts
* @param reads, Number of reads of analog pin to calculate an average value
*/
Pangodream_18650_CL(int addressPin, double convFactor, int reads);
/*
* Constructor
*/
Pangodream_18650_CL();
/*
* Get the battery charge level (0-100)
* @return The calculated battery charge level
*/
int getBatteryChargeLevel();
double getBatteryVolts();
int getAnalogPin();
int pinRead();
double getConvFactor();
private:
int _addressPin; //!< ADC pin used, default is GPIO34 - ADC1_6
int _reads; //Number of reads of ADC pin to calculate an average value
double _convFactor; //!< Convertion factor to translate analog units to volts
double _vs[101]; //Array with voltage - charge definitions
void _initVoltsArray();
int _getChargeLevel(double volts);
int _analogRead(int pinNumber);
double _analogReadToVolts(int readValue);
};
#endif

View file

@ -8,9 +8,9 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:esp32dev] [env:esp32 olimex devKit Lipo]
platform = espressif32 platform = espressif32
board = esp32dev board = esp32-devkitlipo
framework = arduino framework = arduino
lib_deps = lib_deps =
adafruit/DHT sensor library@^1.4.2 adafruit/DHT sensor library@^1.4.2

View file

@ -1,8 +1,11 @@
#include "main.h" #include "main.h"
#include <ctime>
#include <iostream>
#include <chrono>
using namespace std; using namespace std;
DHT sensors[SENSORS_NUMBER] = {DHT(23, DHT22), DHT(22, DHT22), DHT(21, DHT22), DHT(17, DHT22), DHT(2, DHT22)}; DHT sensors[SENSORS_NUMBER] = {DHT(04, DHT22), DHT(18, DHT22), DHT(05, DHT22), DHT(17, DHT22), DHT(16, DHT22)};
float temp[SENSORS_NUMBER]; float temp[SENSORS_NUMBER];
float hum[SENSORS_NUMBER]; float hum[SENSORS_NUMBER];
@ -46,7 +49,7 @@ void reconnect(void)
while (!MqttClient.connected()) while (!MqttClient.connected())
{ {
Serial.print("."); Serial.print(".");
if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_PWD)) if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_MDP)) // MQTT_MDP (mot de passe)
{ {
Serial.println("Connected."); Serial.println("Connected.");
} }
@ -95,11 +98,24 @@ std::tuple<int, int, int> getDate()
return std::make_tuple(year, month, day); return std::make_tuple(year, month, day);
} }
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println("Failed to obtain time");
return(0);
}
time(&now);
return now;
}
//-------------------- Création de trames --------------------// //-------------------- Création de trames --------------------//
void writeMessage(char *txt, float *temp, float *hum, int number) void writeMessage(char *txt, float *temp, float *hum, int number)
{ {
int chargelvl = Battery.getBatteryChargeLevel(); int chargelvl = Battery.getBatteryChargeLevel();
//float homeValue = 3.3 * analogRead(ADC_PIN) / 2048; for test measurement
//float homeValue1 = 1.7 * analogRead(ADC_PIN) / 1000;
switch (number) switch (number)
{ {
case 1: case 1:
@ -115,7 +131,7 @@ void writeMessage(char *txt, float *temp, float *hum, int number)
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f", CLUSTER, temp[0], temp[1], temp[2], temp[3], hum[0], hum[1], hum[2], hum[3]); sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f", CLUSTER, temp[0], temp[1], temp[2], temp[3], hum[0], hum[1], hum[2], hum[3]);
break; break;
case 5: case 5:
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f|%d", CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], hum[0], hum[1], hum[2], hum[3], hum[4], chargelvl); sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f|%d|%f|%f", CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], hum[0], hum[1], hum[2], hum[3], hum[4], chargelvl, homeValue, homeValue1);
break; break;
case 6: case 6:
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f", CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], hum[0], hum[1], hum[2], hum[3], hum[4], hum[5]); sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f", CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], hum[0], hum[1], hum[2], hum[3], hum[4], hum[5]);
@ -137,7 +153,7 @@ void setup()
TimeClient.begin(); TimeClient.begin();
} }
//-------------------- Boucle pricipale --------------------// //-------------------- Boucle principale --------------------//
void loop() void loop()
{ {