It is completely strange! Only a priest or something could help me!
It seems when the sun slightly goes away (~17.00h) then my Ultrasonic stays at ~18cm
In an Empty IBC.
Some Infos:
- I changed the cable to a CAT7 doesn´t help
- I changed the protocol to RS485 doesn´t helped
- I mounted the Solarpanel more away from the Sensor doesn´t helped (Layed on the IBC)
- I covered the solarpanel, good values
- I mounted a DC-DC converter because I untrust the Converter in the Wemos D1 Mini
- Of course, the battery is full! 12V /Ah, @13,6 Volt (NEW!)
EDIT: Forgit to say:
-There NO powerline or anything that could interference in a radus of ~100 meter.*
- When the sun goes up, around 10 clock, then the readings are correct again until it´s ~17-18 clock
@pylon Because the problem also exists with RS485 on CAT 7 and only at evening/night I don´t believe that this is a range problem.. But it might be could have.
This sensor uses 5V, everyone on ESP8266 connected it to 5V afaik.
It was a general question a few days beforce but now things goes serios, wiring diagram and complete and messy code here:
Note: I have no JSN-SR04T in the fritzing lib so I used the JSN-SR04 in the picture.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266httpUpdate.h>
#include "DHT.h"
#define DHTPIN 2 // Pin 4
#define DHTTYPE DHT22
int LItrigPin = 13; // TRIG pin braun D7
int LIechoPin = 12; // ECHO pin D6
/********************************************************/
const char* ssid = "XXXX";
const char* password = "XXXX!";
/********************************************************/
const char* mqtt_server = "stueckgut";
const int port = 1883;
char* msg = "Gewaechshaus"; // msg to the broker
/********************************************************/
int msg_id = 0;
int msg_id_temp = 0;
int pumprelais = 5; //D1
float humidity = 0;
float temp = 0;
int erdfeuchte;
float LI;
long pumpcycletime = 60 * 1000; //Pumpdauer
long sleeptime = 1800 * 1000; //Pausenzeit in s
long rssi = 0;
WiFiClient espClient;
PubSubClient client(espClient);
const long utcOffsetInSeconds = 3600;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
int today;
int pumpcyclestoday;
int lastpumpday;
boolean LAL = 0;
const byte LALinterruptpin = 14; //D5
DHT dht(DHTPIN, DHTTYPE);
/********************************************************/
/* SETUP */
/********************************************************/
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
pinMode(A0, INPUT);
pinMode(pumprelais, OUTPUT);
startingProcedure(); // blinking BuildIn_LED as starting procedure
pinMode(LItrigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(LIechoPin, INPUT);
setup_wifi();
client.setServer(mqtt_server, port);
timeClient.begin(); //NTP
// LAL Fuellstand
pinMode(LALinterruptpin, INPUT);
dht.begin();
}
/********************************************************/
// MAINLOOP
/********************************************************/
void loop() {
today = timeClient.getDay(); //new day?
if (today != lastpumpday ) {
pumpcyclestoday = 0;
}
setup_wifi();
reconnect();
sensorStates();
senddata();
OTAUpdate();
stopwifi();
delay (sleeptime);
}
//=======================================================
//
//
//=======================================================
void sensorStates() {
// read the input on analog pin 0:
erdfeuchte = analogRead(A0);
humidity = dht.readHumidity(); // Lesen der Luftfeuchtigkeit und speichern in die Variable h
temp = dht.readTemperature(); // Lesen der Temperatur in °C und speichern in die Variable t
if (erdfeuchte > 0 && erdfeuchte < 340) {
ledOff();
msg = "Erde ist ZU nass";
msg_id = 1;
} else if (erdfeuchte >= 340 && erdfeuchte <= 900) {
ledOff();
msg = "Erde ist nass";
msg_id = 2;
} else if (erdfeuchte > 900 && erdfeuchte < 1023) {
ledOn();
msg = "Erde ist trocken";
msg_id = 3;
// pump();
} else {
ledOff();
msg = "Sensor defekt";
Serial.println(erdfeuchte);
msg_id = 4;
}
Serial.println(msg);
Serial.println(erdfeuchte);
LAL = digitalRead(LALinterruptpin);
digitalWrite(LItrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(LItrigPin, LOW);
float LIduration_us;
LIduration_us = pulseIn(LIechoPin, HIGH);
// calculate the distance
LI = 0.017 * LIduration_us;
LI = map(LI, 0, 98, 100, 0); // map(value, fromLow, fromHigh, toLow, toHigh)
LAL = digitalRead(LALinterruptpin);
// DRY?
if (msg_id == 3) {
//Turn pump on
pump();
}
}
/********************************************************/
/* Pump */
/********************************************************/
void pump() {
client.loop();
if ((timeClient.getHours() >= 6 && timeClient.getHours() < 24) && pumpcyclestoday < 5 && LAL == true) {
// Turn pump on
startingProcedure();
pumpOn();
delay(pumpcycletime);
pumpOff();
}
else Serial.println("Ausserhalb der Giesszeit oder Max Pumpcycles erreicht");
client.loop();
}
void pumpOn() {
attachInterrupt(digitalPinToInterrupt(LALinterruptpin), pumpinterlock, FALLING);
ledOn();
lastpumpday = today;
pumpcyclestoday++;
// pump on
Serial.println("Pumpe An!");
digitalWrite(pumprelais, true);
if (client.connected()) {
client.publish("Pumpe", String("1").c_str());
}
}
void pumpOff() {
ledOff();
// pump off
Serial.println("Pumpe Aus!");
digitalWrite(pumprelais, false);
client.publish("Pumpe", String("0").c_str());
detachInterrupt(digitalPinToInterrupt(LALinterruptpin));
}
ICACHE_RAM_ATTR void pumpinterlock() {
pumpOff();
//INTERLOCKS
}
/********************************************************/
/* LED */
/********************************************************/
void ledOn() {
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
delay(250);
}
void ledOff() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250);
}
// blinking procedure when device is switched on
void startingProcedure() {
ledOn();
ledOff();
ledOn();
ledOff();
}
/********************************************************/
/* MQTT */
/********************************************************/
void setup_wifi() {
int wifitimer = 0;
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED && wifitimer < 5) {
delay(1000);
Serial.print(".");
wifitimer++;
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
timeClient.update();
rssi = WiFi.RSSI();
Serial.print("RSSI:");
Serial.println(rssi);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266-Gewaechshaus";
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void stopwifi() {
// Serial.println("Wifi disconnect");
WiFi.disconnect();
delay(100);
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
}
void senddata() {
if (client.connected()) {
client.publish("Erdfeuchte", String(erdfeuchte).c_str());
if (LI != 82 && LI != 83 ) {
client.publish("LI", String(LI).c_str());
}
if (LAL == false ) {
Serial.println("LAL!");
client.publish("LAL", String("LL").c_str());
}
else {
client.publish("LAL", String("NORMAL").c_str());
}
client.publish("Message", msg);
client.publish("pumpcyclestoday", String(pumpcyclestoday).c_str());
client.publish("Luftfeuchte", String(humidity).c_str());
client.publish("Raumtemperatur", String(temp).c_str());
// client.publish("Wifi", rssi.c_str());
Serial.println(temp);
}
else Serial.print("MQTT Probleme, sende keine Daten");
}
void OTAUpdate() {
WiFiClient client;
while (ESPhttpUpdate.update(client, "10.10.10.254", 8000, "/latestgewaechshaus.bin"));
{
delay(10000);
}
delay(100);
}