Hello,
I’m French, I don’t speak English well, I use a translation software to write you.
Sorry if the translation is bad.
I am a newcomer in programming.
I have a “climate” project, here is the hardware used:
- ESP32
- DHT22 (temperature, humidity)
- TSL2561 (luminosity in “lux”)
I am using influx to view my saved data.
I’m not used to using influx, and I can’t get the TSL2561 to display these values (and curves).
I program with PlatformIO and VS Code (Mac version)
Could you help me?
Here is my code used:
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "DHT.h"
#include <WiFi.h>
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
// Heure Paris : https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"
// WiFi AP SSID
#define WIFI_SSID "*******************"
#define WIFI_PASSWORD "*******************"
#define INFLUXDB_URL "*******************"
#define INFLUXDB_TOKEN "*******************"
#define INFLUXDB_ORG "*******************"
#define INFLUXDB_BUCKET "Climat Bonsai Hiver"
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Data point
Point sensorReadings("measurements");
Point sensor("wifi_status");
// connexion de la pin digital du capteur sur la broche n°2
#define DHTPIN 4
#define DHTTYPE DHT22
// Déclaration DHT22
DHT dht(DHTPIN, DHTTYPE);
float temperature;
float humidity;
void setup() {
Serial.begin(115200);
// Setup wifi
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
//Init DHT22
dht.begin();
// Add tags
sensorReadings.addTag("device", DEVICE);
sensorReadings.addTag("location", "Salon");
sensorReadings.addTag("sensor", "DHT");
sensor.addTag("SSID", WiFi.SSID());
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
}
void loop() {
// Get latest sensor readings
temperature = dht.readTemperature();
humidity = dht.readHumidity();
// Add readings as fields to point
sensorReadings.addField("temperature", temperature);
sensorReadings.addField("humidity", humidity);
sensor.addField("rssi", WiFi.RSSI());
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.print(client.pointToLineProtocol(sensorReadings));
Serial.println(client.pointToLineProtocol(sensor));
// Write point into buffer
client.writePoint(sensorReadings);
client.writePoint(sensor);
// Clear fields for next usage. Tags remain the same.
sensorReadings.clearFields();
sensor.clearFields();
// If no Wifi signal, try to reconnect it
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// Wait 10s
Serial.println("Wait 10s");
delay(10000);
Here is the code for using the TSL2561:
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
/**************************************************************************/
/*
Displays some basic information on this sensor from the unified
sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
/**************************************************************************/
/*
Configure le gain et le temps d'intégration pour le TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
/* Vous pouvez également définir manuellement le gain ou activer la prise en charge du gain automatique */
// tsl.setGain(TSL2561_GAIN_1X); /* Pas de gain... à utiliser en pleine lumière pour éviter la saturation du capteur */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... utiliser en basse lumière pour augmenter la sensibilité */
tsl.enableAutoRange(true); /* Auto-gain ... bascule automatiquement entre 1x et 16x */
/* La modification du temps d'intégration vous donne une meilleure résolution du capteur (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* rapide mais faible résolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* résolution et vitesse moyennes */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data mais les conversions sont plus lentes */
/* Mettez à jour ces valeurs en fonction de ce que vous avez défini ci-dessus! */
Serial.println("------------------------------------");
Serial.print ("Gain: "); Serial.println("Auto");
Serial.print ("Timing: "); Serial.println("13 ms");
Serial.println("------------------------------------");
}
/**************************************************************************/
/*
Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(115200);
Serial.println("Light Sensor Test"); Serial.println("");
/* Initialiser le capteur */
//use tsl.begin() to default to Wire,
//tsl.begin(&Wire2) directs api to use Wire2, etc.
if(!tsl.begin())
{
/* There was a problem detecting the TSL2561 ... check your connections */
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Afficher quelques informations de base sur ce capteur */
displaySensorDetails();
/* Configurer le gain du capteur et le temps d'intégration */
configureSensor();
/* We're ready to go! */
Serial.println("");
}
/**************************************************************************/
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
/**************************************************************************/
void loop(void)
{
/* Obtenir un nouvel événement de capteur */
sensors_event_t event;
tsl.getEvent(&event);
/* Afficher les résultats (la lumière est mesurée en lux) */
if (event.light)
{
Serial.print(event.light); Serial.println(" lux");
}
else
{
/* If event.light = 0 lux le capteur est probablement saturé
et aucune donnée fiable n'a pu être générée ! */
Serial.println("Sensor overload");
}
delay(250);
}
Thank you for your help