Hello, I have a new problem with my code.
I have three temperature sensors connected in one wire. The value is displayed very well on my screen and on the application.
On the other hand, I cannot write the code for the Moisture Sensor, the value does not appear on my screen. Do you have an idea ?
I am with an esp32
#include <SPI.h>
#define BLYNK_TEMPLATE_ID "xxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxxxx"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
#include "BlynkEdgent.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#include <FastLED.h>
#define NUM_LEDS 4
#define DATA_PIN 13
CRGB leds[NUM_LEDS];
const int relais1 = 14;
const int btn1 = 26;
const int relais2 = 32;
const int btn2 = 27;
const int relais3 = 15;
const int btn3 = 25;
const int relais4 = 12;
const int btn4 = 19;
// one wire temperature
#define ONE_WIRE_BUS 23
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 3;
float tempC;
int relais1Sate = LOW;
int btn1State = HIGH;
int relais2Sate = LOW;
int btn2State = HIGH;
int relais3Sate = LOW;
int btn3State = HIGH;
int relais4Sate = LOW;
int btn4State = HIGH;
int brightness = 50;
int lcd_position[] = {16, 30, 44}; // position temperature sur l'écran
BLYNK_WRITE(V1) {
relais1Sate = param.asInt();
Serial.print("Widget Button V1 changed. Writing ");
Serial.print(relais1Sate);
Serial.print(" to Relay 1 and LED V1, and ");
Serial.print(!relais1Sate);
Serial.println(" to LED R1 ...");
digitalWrite(relais1, !relais1Sate);
if (!relais1Sate) {
leds[0] = CRGB::Red;
}
if (relais1Sate) {
leds[0] = CRGB::Green ;
}
FastLED.show();
}
BLYNK_WRITE(V2) {
relais2Sate = param.asInt();
digitalWrite(relais2, !relais2Sate);
if (!relais2Sate) {
leds[1] = CRGB::Red;
}
if (relais2Sate) {
leds[1] = CRGB::Green ;
}
FastLED.show();
}
BLYNK_WRITE(V3) {
relais3Sate = param.asInt();
digitalWrite(relais3, !relais3Sate);
if (!relais3Sate) {
leds[2] = CRGB::Red;
}
if (relais3Sate) {
leds[2] = CRGB::Green ;
}
FastLED.show();
}
BLYNK_WRITE(V4) {
relais4Sate = param.asInt();
digitalWrite(relais4, !relais4Sate);
if (!relais4Sate) {
leds[3] = CRGB::Red;
}
if (relais4Sate) {
leds[3] = CRGB::Green ;
}
FastLED.show();
}
const int dry = 3540; // value for dry sensor // Moisture Sensor
const int wet = 1246; // value for wet sensor // Moisture Sensor
int percentageHumididy; // Moisture Sensor
void calhum() // Moisture Sensor
{
int sensorVal = analogRead(33); // Moisture Sensor
int percentageHumididy = map(sensorVal, wet, dry, 100, 0); // Moisture Sensor
}
void humidite() // Moisture Sensor
{
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.setCursor(0, 0);
display.println("---------------------");
display.setCursor(0, 6);
display.print("| Humidite |");
display.setCursor(0, 12);
display.println("---------------------");
display.display();
display.setCursor(0, 16); //Moisture Sensor
display.print(percentageHumididy); //Moisture Sensor
display.println("%"); //Moisture Sensor
display.display(); //Moisture Sensor
Serial.print(percentageHumididy); //Moisture Sensor
Serial.println("%"); //Moisture Sensor
Blynk.virtualWrite(V10, percentageHumididy); //Moisture Sensor
}
void temperature()
{
display.clearDisplay();
display.setCursor(0, 0);
display.println("---------------------");
display.setCursor(0, 6);
display.print("| Temperature |");
display.setCursor(0, 12);
display.println("---------------------");
display.setCursor(0, 16); // position 0
display.println(" Interieur |");
display.print("---------------------");
display.display();
display.setCursor(0, 30); // position 1
display.println(" Exterrieur |");
display.print("---------------------");
display.display();
display.setCursor(0, 44); // position 2
display.println(" Eau |");
display.print("---------------------");
display.display();
// Send command to all the sensors for temperature conversion
sensors.requestTemperatures();
// Display temperature from each sensor
for (int i = 0; i < deviceCount; i++)
{
tempC = sensors.getTempCByIndex(i);
Blynk.virtualWrite(i + 21, tempC);
display.setCursor(80, lcd_position[i]);
display.print(tempC);
display.display();
delay(1000);
}
}
void checkPhysicalButton1()
{
if (digitalRead(btn1) == LOW)
{
Serial.print("Location A, btn1 = ");
Serial.print(digitalRead(btn1));
Serial.print(" btn1State = ");
Serial.println(btn1State);
// btnState is used to avoid sequential toggles
if (btn1State != LOW)
{
Serial.print("Location B, btn1 = ");
Serial.print(btn1);
Serial.print(", btn1State = ");
Serial.println(btn1State);
// Toggle LED state
relais1Sate = !relais1Sate;
Serial.print("Location C, writing ");
Serial.print(relais1Sate);
Serial.println(" to Relay 1 ...");
digitalWrite(relais1, !relais1Sate);
if (!relais1Sate){leds[0] = CRGB::Red;}
if (relais1Sate){leds[0] = CRGB::Green ;}
FastLED.show();
Serial.print("Location D, writing ");
Serial.print(!relais1Sate);
Serial.print(" to LED V1 and ");
Serial.print(relais1Sate);
Serial.println(" to LED R1 ...");
// Update Button Widget
Serial.print("Location E, writing ");
Serial.print(relais1Sate);
Serial.println(" to Button Widget on pin V1 ...");
Blynk.virtualWrite(V1, relais1Sate);
btn1State = LOW;
}
}
else
{
btn1State = HIGH;
}
if (digitalRead(btn2) == LOW)
{
// btnState is used to avoid sequential toggles
if (btn2State != LOW)
{
// Toggle LED state
relais2Sate = !relais2Sate;
digitalWrite(relais2, !relais2Sate);
if (!relais2Sate){leds[1] = CRGB::Red;}
if (relais2Sate){leds[1] = CRGB::Green ;}
FastLED.show();
// Update Button Widget
Blynk.virtualWrite(V2, relais2Sate);
btn2State = LOW;
}
}
else
{
btn2State = HIGH;
}
if (digitalRead(btn3) == LOW)
{
// btnState is used to avoid sequential toggles
if (btn3State != LOW)
{
// Toggle LED state
relais3Sate = !relais3Sate;
digitalWrite(relais3, !relais3Sate);
if (!relais3Sate){leds[2] = CRGB::Red;}
if (relais3Sate){leds[2] = CRGB::Green ;}
FastLED.show();
// Update Button Widget
Blynk.virtualWrite(V3, relais3Sate);
btn3State = LOW;
}
}
else
{
btn3State = HIGH;
}
if (digitalRead(btn4) == LOW)
{
// btnState is used to avoid sequential toggles
if (btn4State != LOW)
{
// Toggle LED state
relais4Sate = !relais4Sate;
digitalWrite(relais4, !relais4Sate);
if (!relais4Sate){leds[3] = CRGB::Red;}
if (relais4Sate){leds[3] = CRGB::Green ;}
FastLED.show();
// Update Button Widget
Blynk.virtualWrite(V4, relais4Sate);
btn4State = LOW;
}
}
else
{
btn4State = HIGH;
}
}
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);
}
void checkPhysicalButton1();
void temperature();
void humidite();
void setup()
{
Serial.begin(115200);
delay(100);
BlynkEdgent.begin();
sensors.begin(); // debut de la librairie capteur de température
// Debug console
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
leds[0] = CRGB(0, 0, 255);
leds[1] = CRGB(0, 0, 255);
leds[2] = CRGB(0, 0, 255);
leds[3] = CRGB(0, 0, 255);
FastLED.show();
pinMode(relais1, OUTPUT);
pinMode(btn1, INPUT_PULLUP);
digitalWrite(relais1, relais1Sate);
pinMode(relais2, OUTPUT);
pinMode(btn2, INPUT_PULLUP);
digitalWrite(relais2, relais2Sate);
pinMode(relais3, OUTPUT);
pinMode(btn3, INPUT_PULLUP);
digitalWrite(relais3, relais3Sate);
pinMode(relais4, OUTPUT);
pinMode(btn4, INPUT_PULLUP);
digitalWrite(relais4, relais4Sate);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
display.display();
timer.setInterval(100L, checkPhysicalButton1);
timer.setInterval(10000L, temperature);
timer.setInterval(10000L, humidite);
}
void loop() {
BlynkEdgent.run();
timer.run();
}