JSN-SR04T shows just 18cm after a few hours (Need a priest!)

Hi,
I have a JSN-SR04T on a Wemos D1 mini for measure the level in an IBC.
I used it with some library. It worked great for a few hours then it only shows 18 cm.
Tried everything so it seems to be damaged.
I ordered one again and this time I used the example from arduino. Works also great but it also stopped working the same way as the other after ~3-4hours.

Anyone else expected this?
The cable between D1 mini 5V out and JSN-SR04T unit is ~5 meters long 0.75mm². I just thought that this could be a problem, but only because I am out of ideas..That should be okay.
Any other ideas?

EDIT: The IBC is empty, should show around 99 cm, what it did the first hours.

I guess if the power wire is 5m the other signals have the same length. These signal wires shouldn't be more than 0.5-1m at most.

Why do you power the unit with 5V? That means the output signal also has 5V which damages the D1 mini, at least if it's treated so for a longer time frame.

Anyway, in your post I miss a complete wiring diagram. If I'm wrong and the hardware is correctly wired we need your code to look for errors there.

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);
}


A google of

JSN-SR04T extension cable

seems to indicate that it likes its own 2.5m cable and not much else.

I use the 2,5 meter cable from sensor to evaluation unit. And after the unit (which communicates digitalread or RS485 to my D1 mini) I use a twisted pair of a cat7 cable. The length should be no problem especially with RS485 I had the same problems.

Oh.

So you have the JSN-SR04T head connected to the JSN board with its standard 2.5m cable, and the four pins of the JSN board are connected to your D1 or DC-DC converter via cat7 cable that is 5m long. All three grounds are connected (JSN, D1, and DC-DC).

Not sure why you mention RS485, since the JSN doesn't use that protocol.

I uninstalled the DCDC Converter as it doesn´t fixed the problem.
But I keep the 0,5mm² cable (mentioned in first post) as extra powercable and do not use VIN on CAT7.
On the CAT7 I connected one twisted pair for Echo/Trig. Tried to put the Cat7 Shield to ground today. (Tried only onesided) Does also not helped. This thing made me crazy.

The JSN-SR04T can talk serial, like mentioned here https://www.roboter-bausatz.de/media/pdf/83/0f/93/JSN-SR04T_outputmode.pdf. Tried Mode 2&3. I am now not 100% sure if it is really called RS485 for this device or if it´s just some kind of. But it is 8n1 UART serial. Should make a long distance without problems.

The strange thing is, it will not work one simple measure the whole night, but tomorrow at 10 clock i will get a signal the whole day.

Don´t be confused by the values 5 and 83, I mapped: LI = map(LI, 0, 98, 100, 0); to get percentage

Since forum priests are off performing other exorcisms and a miracle seems unlikely, this lowly alter boy would suggest the usual problem solving steps, such as simplifying your code (eliminate everything except reading the JSN and posting the results in the simplest possible way) and simplifying your physical configuration (connect the JSN board to your ESP with 3" or 6" jumpers). If that works, then go back to your current code and configuration, in steps.

But I need that priest! :zipper_mouth_face:
Hm, yes, your tips are the next logical way.
Also my idea was to install an INA219 at the end of line just to prove that there are really, really, really no strange voltage drops.
But I didn´t mentioned, I also changed the powercable 2 days before.
And all other sensors working flawless at night. Even when I turn on a 12V pump which drain the Voltage down to 12V: the Ultrasonic is working stable.
Sadly, tomorrow I have to go to work a few hundred kilometer away for one month. I just can look at the values and modify the program in this time
So I have no time left for your ideas. If miracles do not exist (what??) I will do that in the BIG and deserved holidays when I am back.
I also hold the JST failures and working entry points agains lux and temperature of my weather station. But I have not enough datadays at the moment. Temperature is falling to 6-8 DegC when JST-failure and rising to 5-6 DegC when sensor is back. Down to -2 DegC at night. (Yes, I am out of good ideas.)

It was sunny since I installed and experienced these curses. I am very interested if that also happens at very cloudy/dark days. Since covering the solarpanel yesterday noon didn´t change anything.

By the way: Here we go again.
Screenshot 2022-03-06 103613
(I temporary do not upload the values 83 and 84 because it drives me crazy to see.)

Where do you have the RS485? I asked about the length of the blue wires in your wiring diagram.

Just because others does the same doesn't make it better. This might fry your reception pin on the ESP8266. The JSN-SR04T works with 3.3V so I don't see a reason to over-voltage the ESP.

Interrupt handlers should be as fast as possible and shouldn't use any functionality that itself depends on interrupts to work (as using the serial interface).

That wiring diagram only shows the HC-SR04T connected but your sketch implies that you have much more connected.

First by the way, yesterday the sensor was working every 2-3rd measurement until 19h. The days before it stopped completely between 17-17.30h

There is no RS485 right now, i get back to normal Digital I/O after I did not get any better results. Cable length ist about 5 meters on both, Power source and CAT7.

Seriously, i KNEW that this will be an answer. And what should I say...that´s right.
But I have a bunch of Wemos D1 mini in operation and I never expect any problems when I used 5V as Powersource for DI or I2C. One of them is running since 3 years outside and every IO on this device is used. It is just running.
I don´t want to say that this is not true. But serious, I know, I read often about that it could damage a D1 mini and I never followed the complete circuit diagram of the D1 internals if it could be really a big problem. Do i sound stupid right now? Yes, probably, but I drank a few beer and this it what I experienced.

I think I have to do some homework to understand exactly what you mean. But That Interrupt is just to switch the pump off if the interrupt LAL (This is a Hardware IO Level float switch on the bootom of the IBC just for pump protection) The JSN-SRT04T is just for information and never had a interrupt and/or switching function. Did I miss something else there?

Yes, that´s true. Will take some time to make a complete diagram since I am not at home. At the moment the code is the best documentation I have. ^^

Move your ESP to the sensor! The blue wires shouldn't have more than about 20cm.

That's about the same as if you have a crane and regularly lift 900kg using a strap built for 500kg. That may work for months but be assured that I would never voluntarily stay in the range of your crane. Using something outside it's specification for a longer time is seldomly a clever idea.

You install the interrupt handler pumpinterlock() which then calls pumpOff() and that contains output to the serial interface as well as network interaction. Both must not be called inside an interrupt handler!

I will try that when I have time. Surprisingly I was able to drive home for this weekend but I have to delay this.

My experiences beside-at the end of day you are right with your arguments.
I rearranged the JST (and also the DHT22) to 3.3V and it seems that everything is working as before. So it was bullshit to argue for 5V anyway.
I will keep that in mind for later projects.

Yea ok, that´s right. I had other plans there which need the Interrupt to do other stuff while pump is on. In that case it doesnt matter if the interrupthandler will overtake the program for a longer period. It should switch the pump off by high priority, the rest doesn´t matter. That was my intention to build it like that.
I could remove it by time, the LAL is much higher installed than it should so there is no need to switch the pump off immediately.

Hi,
just an update. I tried a new sensor a few weeks ago. This time the JSN-SR04T in V3. And now it is working good.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.