Esp8266 + mqtt + jsn-sr04t

Hello,

I’m trying to do the same thing with an ESP8266 + MQTT + JSN-SR04T ultrasonic sensor to measure the capacity of my rainwater tank.

My program seems to work fine without the sensor connected. However, as soon as I connect the sensor, nothing works.

The question I have is, which pin should I connect it to? 5V or 3.3V?

A little help for a beginner would be welcome :sweat_smile:

Thanks :stuck_out_tongue_winking_eye:

Hello, based on this plan, I would say 5V.

However, HC-SR04P is 3.3V, but HC-SR04 is 5V.

In fact, I have a problem I think following the different connections I have this message on the Arduino IDE: Hard resetting via RTS pin…

I’m looking for how to do it on the net.

This is a normal message when you upload your code :slight_smile:

good evening,
is this the module you have? https://fr.aliexpress.com/item/1005001442055224.html?spm=a2g0o.detail.1000014.1.55474e2dLM75V8\u0026gps-id=pcDetailBottomMoreOtherSeller\u0026scm=1007.13338.183346.0\u0026scm_id=1007.13338.183346.0\u0026scm-url=1007.13338.183346.0\u0026pvid=2a0eed97-d9e0-4469-8a54-2b8ca98b7325\u0026_t=gps-id:pcDetailBottomMoreOtherSeller,scm-url:1007.13338.183346.0,pvid:2a0eed97-d9e0-4469-8a54-2b8ca98b7325,tpp_buckets:668%230%23131923%230_668%23888%233325%2310_3338%230%23183346%230_3338%233142%239889%231_668%232846%238110%23330_668%232717%237558%23183_668%231000022185%231000066058%230_668%233422%2315392%23770

Not quite, I have this one

Here is my program that was working before connecting.

Let me explain: when I connected the wemos, the serial monitor displayed that the WiFi connection was successful as well as the MQTT connection.

Since I tried to connect my sensor to my wemos, I only have symbols in the serial monitor.

Any ideas?

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

/* WIFI */

#define wifi_ssid "******box"
#define wifi_password "****************"

/* MQTT */

#define mqtt_server "192.168.0.7"
#define mqtt_user "gladys"            // username
#define mqtt_password "****************"        // password
#define gladys_topic "gladys/master/device/mqtt:jardin:capteur-ultrason/feature/mqtt: jardin:capteur-ultrason:quantite/state"
#define mqtt_cuve "mqtt:jardin:capteur-ultrason"     //Topic capteur cuve

/* Buffer that allows decoding received MQTT messages */

char message_buff[100];

long lastMsg = 0;   //Timestamp of the last message published on MQTT
long lastRecu = 0;
bool debug = false;  //Displays on the console if True

/* Constants for the pins */

const byte TRIGGER_PIN = 7; // TRIGGER pin
const byte ECHO_PIN = 6;    // ECHO pin

/* Constants for the timeout */

const unsigned long MEASURE_TIMEOUT = 25000UL; // 25ms = ~8m at 340m/s

/* Speed of sound in air in mm/us */

const float SOUND_SPEED = 340.0 / 1000;

//Object creation

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  /* Initialize the serial port */

  Serial.begin(9600);     //Optional for debugging

  /* Initialize the pins */

  pinMode(TRIGGER_PIN, OUTPUT);
  digitalWrite(TRIGGER_PIN, LOW); // The TRIGGER pin must be LOW at rest
  pinMode(ECHO_PIN, INPUT);

  setup_wifi();           //Connect to the WiFi network
  client.setServer(mqtt_server, 1883);    //Configuration of the connection to the MQTT server
}

//WiFi network connection
void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connection established ");
  Serial.print("=> IP Address: ");
  Serial.print(WiFi.localIP());
}

//Reconnection
void reconnect() {
  //Loop until reconnection is obtained
  while (!client.connected()) {
    Serial.print("Connecting to MQTT server...");
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("OK");
    } else {
      Serial.print("KO, error: ");
      Serial.print(client.state());
      Serial.println(" Waiting 5 seconds before retrying");
      delay(5000);
    }
  }
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();

  /*Send a message every minute*/

  if (now - lastMsg > 1000 * 60) {
    lastMsg = now;

    /* 1. Start a distance measurement by sending a HIGH pulse of 10µs on the TRIGGER pin */

    digitalWrite(TRIGGER_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIGGER_PIN, LOW);

    /* 2. Measure the time between the sending of the ultrasonic pulse and its echo (if it exists) */

    long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);

    /* 3. Calculate the distance from the measured time */

    int distance_mm = measure / 2.0 * SOUND_SPEED;
    float c = 2000 - distance_mm;
    c = c / 2000;
    c = c * 100;

    //No need to go any further if the sensor returns nothing
    if ( isnan(c)) {
      Serial.println("Reading failure! Check your HRC-04 sensor");
      return;
    }

    if ( debug ) {
      Serial.print("Tank: ");
      Serial.print(c);
    }
    client.publish(gladys_topic, String(c).c_str(), true);   //Publish the temperature on the temperature_topic
  }
  if (now - lastRecu > 100 ) {
    lastRecu = now;
    client.subscribe("homeassistant/switch1");
  }
}

Good evening Jerome,
What voltage are you powering your sensor with?

Yeah, it smells like a power supply issue, the voltage is probably not sufficient. Or the baud rate is incorrect.

If you power your sensor with 5v, you need a 5v to 3.3v voltage adapter on each input used, as the Wemos inputs and outputs only support 3.3v. Secondly, in your program, you use pins 6 and 7. I think you should put D6 and D7, because if you don’t put the « D », the Wemos understands GPIO 6 and GPIO 7 which correspond to the Flash memory management pins. That’s it

Hello @Psoy

I modified the program by adding the Ds

I bought this as an adapter

Regarding the connection, I connect the GND to GND and the other ports from the wemos to LOW LV: 3.3, LV1: D6, LV2: D7 and on the sensor side to HV: 5V HV1: Echo and HV2: trigger

Is that correct?

Issue resolved, the « D » changes everything.

Adapter not necessary.

Program functional and back in Gladys ok. I just have to install it in my tank now.

Thanks @Psoy

Great, :+1:
but check your Wemos documentation to see if it accepts 5V on its inputs, as they can burn out over time
keep going,

It would have been nice to create your own topic about your issue, even if it’s related to the article by @pierre-gilles.

@VonOx, the splitting pro if you’re passing by ^^

The sensor you’re using looks really cool, it’s going to be useful for me this summer I think.

It’s done, I’ll make you a little tutorial

You totally got that I didn’t want to cause any trouble :joy:

To get back to the topic, I tested tamosta with an sr04 and an esp12e, well I’m not bored writing code anymore, it’s hassle-free. (PR in progress to manage distance sensors)

I think we should mention that you are not using the current version of Gladys, but an evolution, which correctly integrates the SR04.

Just a quick update to let you know that everything is working fine, but I have a question about the MQTT API. I had to choose the « Humidity - percentage » feature

which is the closest to what I wanted, but would it be possible to have a specific label for my needs like « Tank capacity - percentage » or, even better, a gauge?

Distance sensor units are coming (cm to start with, but percentage could also be good)

The gauge is another topic, I need that too :wink: (it’s on my to-do list)