Pump Motor DC is ON while it supposed to be OFF if meet desire above threshold moisture

thankyou, i have done it, so on my project i need this two of 6volts right? one for the relay and then one for the motor pump and its wiring still the same as i refer at first?

No just one. You can use the same battery pack for the motor and relay.

the motor pump and its wiring still the same as i refer at first?

Yes

Do you still need help with the wiring?

the wiring is done, but i have the problem into the serial monitor, i succesfully compile, but the serial monitor output like this

�����f��~����~~���f��xf���..................................................sdd��<�$�| �l�b|���e�r�c� c��oo�dno���cpx��dsl{${$$��|�l�| � d� c|����{�b�c��gn�lgg��� cpx��$sd{lsdd��|�l�| � d� c|����{�c�c��oo�dno���bxp��drl{d;ll��|�d�< �$� b|���e�r�c� c��oo�dno���c8x��dsl{$sdd��|�l�| � d� c|����{�c�c��no�l'o��� bxp��drl{d;llܞ|�d�| �l� b|���e�r�c� c��og�dno���b8p��drl{d;llܞ|�d�< �d� c|����{�c�c��no�l'n��� #xp��l;lsdsll��|�d�< �$� c|����{�b�c��'n�lg'��� cp8��${d;lrllܞ|�d�| � d� c|ǃ��s�#� b��g'�lgg��� cp8��$sd;lsdd��<�l�| � l�c|���e�r�c� c��oo�lno��� bxp��lrlsd{ll��|�l�| � d� c|ǃ��{�b�c��gn�lgg��� cpp��l{$rlsll��|�d�| �l�c|���e�s�c� #��gg�$gg��� cp8��l{d;lrllܞ|�d�< �d� c|����{�b�c��gn�lgg��� cp8��l{$rlrll��|�d�| �l�c|���e�r�c� c��og�doo���c8p��lrlsdsll��|�d�| �l�b|���e�;�c�c��no�l'o��� #xpćl{lsl{$d��|�l�| � d� c|����{�c�c��no�lno��� bxp��lrl{dsll��|�d�< �l�b|���e�s�c� c��og�doo���cpx��dsl{d{dd��|�l�| � l�b|���e�;�c�c��no�lg'��� cp8��l{d;lsll��|�$�| � l�c|���e�r�c� c��og�doo���cpx��dsd{l{$$��|�l�| � d� c|����{�b�c��no�l'n��� bxp��l;lsd{dd��|�l�| � d� c|����;�c� c��og�doo���cpx��dsd{lrll��|�$�| � l�c|ǃ��{�c�c��oo�doo���cpx��dsl{${dd��|�l�| � l�c|���e�s�c� #��gg�dog���cpx��dsd{${$d��|�l�| �d� c|����{�c�c��'o�lgn��� bxp��lrlsd{ll��|�l�| � d�c|����s�#� b��g'�lgg��� cp8��l{d;lsdd��<�$�| � l�c|���e�s�c� c��og�doo���cpx��dsd{l{dd��|�l�| �l� b|���e�s�c� #��g'�$gg��� cp8��l{drl{dd��|�l�| � d�c|���e�s�c� c��og�dog���cpx��dsl{${dd��|�l�| � l�c<���e�s�c� c��og�doo���bxp��l;lslsdl��|�d�| � d� c|ǃ��s�c� #��og�doo���bxp��l;lslsdd��<�$�| �l� b|����{�c�c��no�lno��� #xp��l;lslsdd��<�$�| � l�c|���e�r�c� c��oo�lno��� #xp��l;lsd;llܟ|�d�| � l�c|����{�b�b��gn�lg'��� cpp��l{$rl{dd��|�l�| � l�c|����s�#�b��g'�lgg��� cpp��l{lslsll��|�d�| �$� #|����;�c�c��oo�dno���c8p��lrlsd;ll��|�l�|� l�c<���e�s�c� c��oo�lno��� #xpćl{$rl{$d��|�l�| � l�c|���e�s�c� c��og�doo���cpx��dsl{d{dd��<�$�| �l� #|����{�c�c��'n�lgg��� cp8��${d;lsdd��|�l�| �d� #|���e�r�c� c��og�dog���cpx��dsd{${dd��|�l�|� d� c|����{�c�c��'n�lgn��� cxp��l{$slsdd��<�$�| �l�b|���e�r�c� c��og�doo���c8p��lrlsdsll��|�$�| � l�c|����s�#�c��'n�lgn��� #xp��l;lsd{$$��|�l�| �l�c|���e�s�c� c��og�dog���cpx��dsd;l{dd��|�l�| � l�c<���e�r�c� c��no�lgn��� cpp��$sd;lsdd��|�l�| �d� #|���e�r�c� #��g'�lgn��� b8p��dsd;lrll��|�d�| � l�c<����s�#�b��gn�lgn��� bxp��lrl{drll��|�d�| � l�c<����{�#�b��'n�lgn��� bxp��lrl{d;ll��|�d�| �d� c|����{�c�c��no�l'n��� bxp��lrl{drll��|�d�< �$� c|����{�b�c��'n�lg'��� cxp��l{$rlsdd��|�l�| � d� c|���e�r�c� #��g'�lgg��� cpp��l{lsdsll��|�$�| � l�c|���e�s�#�b��gn�lgn��� #xp��lrl{d{l$��|�l�| �d� c|����{�c�c��no�l'n��� bxp��dsl{dsdd��|�l�| � l�c<����{�b�c��'n�l'n��� bxp��lrl{dsll��|�$�| � l�c|���e�s�c� b��g'�lg'��� cxpćl{lslDOX��

Ln 43, Col 2

Generic ESP8266 Module

on COM7

2

Is your baud rate set at the same value on both ends (esp and your pc).

You have the baud rate set wrong on the serial monitor.

What does client.state() return?

yes, still the same at 9600 same as the code, but still give the same output like before problem

i have already suit the baudrate same as the code, but still give the same output

it is the state which indicates the status connect to the mqtt thingsboard

After this:

Add:

Serial.println("hello");
while(1) {
   ;
}

This should send hello to your monitor...
Does that work?
If so: baudrates and cables are good.
Your problem may be caused by a C-string without '\0'

Are you using the same code you show in post#1 ?

Did the original code print correctly?

Is it a char* or a String or a number or something else maybe?
I am looking for a source of a char* without terminating '\0'....

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

00:38:49.277 -> ?��1��5!��0U�hG�tHhello
00:38:48.994 -> >>>stack>>>
00:38:49.029 ->
00:38:49.029 -> ctx: cont
00:38:49.029 -> sp: 3ffffe50 end: 3fffffd0 offset: 0160
00:38:49.080 -> 3fffffb0: feefeffe feefeffe 3ffee850 402043a0
00:38:49.145 -> 3fffffc0: feefeffe feefeffe 3fffdab0 40100dc9
00:38:49.209 -> <<<stack<<<
00:38:49.209 ->

The output like this now

yes i use the same code, but the output come out like this now
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

00:38:49.277 -> ?��1��5!��0U�hG�tHhello
00:38:48.994 -> >>>stack>>>
00:38:49.029 ->
00:38:49.029 -> ctx: cont
00:38:49.029 -> sp: 3ffffe50 end: 3fffffd0 offset: 0160
00:38:49.080 -> 3fffffb0: feefeffe feefeffe 3ffee850 402043a0
00:38:49.145 -> 3fffffc0: feefeffe feefeffe 3fffdab0 40100dc9
00:38:49.209 -> <<<stack<<<
00:38:49.209 ->
and so on


it is still matter if the speed 115200, and don't have the 9600
but i try to change the baud rate at the code and the serial monitor into 115200 but not change at all

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

#define WIFI_SSID "Galaxy A51C9B4"
#define WIFI_PASSWORD "gchu2277"

#define THINGSBOARD_SERVER "thingsboard.cloud"
#define TOKEN "LLism8ZrsiHC3vkBPGR5"  // Access token dari ThingsBoard
#define MQTT_PORT 1883

#define SOIL_MOISTURE_PIN A0
#define THRESHOLD_MOISTURE 30  // Dalam %
#define PUMP_PIN 14  // GPIO0 = D5

WiFiClient espClient;
PubSubClient client(espClient);
LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long lastSend = 0;
bool isPumpOn = false;

void connectToWiFi() {
  Serial.print("Connecting to WiFi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("WiFi Connected");
  lcd.setCursor(0, 1);
  lcd.print(WIFI_SSID);
}

void connectToThingsBoard() {
  while (!client.connected()) {
    Serial.print("Connecting to ThingsBoard...");
    if (client.connect("ESP8266", TOKEN, NULL)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" retrying in 5 seconds");
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("hello");
  while(1) {
    ;
  }
  pinMode(PUMP_PIN, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Connecting...");

  connectToWiFi();
  client.setServer(THINGSBOARD_SERVER, MQTT_PORT);
}

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

  client.loop();

  if (millis() - lastSend > 3000) {
    sendTelemetry();
    lastSend = millis();
  }
}

void sendTelemetry() {
  int soilAnalog = analogRead(SOIL_MOISTURE_PIN);
  int soilMoisture = map(soilAnalog, 800, 300, 0, 100);
  soilMoisture = constrain(soilMoisture, 0, 100);

  // Kontrol pompa otomatis
  if (soilMoisture < THRESHOLD_MOISTURE) {
    digitalWrite(PUMP_PIN, HIGH);
    isPumpOn = true;
    Serial.println("Pompa ON (otomatis)");
  } else {
    digitalWrite(PUMP_PIN, LOW);
    isPumpOn = false;
    Serial.println("Pompa OFF");
  }

  // Tampilkan ke LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Moisture:");
  lcd.print(soilMoisture);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("Pump:");
  lcd.print(isPumpOn ? "ON " : "OFF");

  Serial.print("Soil analog: ");
  Serial.println(soilAnalog);
  Serial.print("Soil moisture (%): ");
  Serial.println(soilMoisture);

  // Kirim ke ThingsBoard
  String payload = "{";
  payload += "\"soilMoisture\":" + String(soilMoisture) + ",";
  payload += "\"pumpStatus\":\"" + String(isPumpOn ? "ON" : "OFF") + "\"";
  payload += "}";

  Serial.print("Sending: ");
  Serial.println(payload);

  client.publish("v1/devices/me/telemetry", payload.c_str());
}

I don't have an 8266 module so I can't help any further with the code.

You should write a simple test program without the WiFi to make sure the pump and soil moisture modules work as as expected

i have create without the wifi, but still work as before, should be off, but not off

How did you determine the mapping for the soil moisture?

void loop() {
  int soilAnalog = analogRead(SOIL_MOISTURE_PIN);
  int soilMoisture = map(soilAnalog, 1024, 450, 0, 100); 
  soilMoisture = constrain(soilMoisture, 0, 100);

  Serial.print("Soil Analog Value: ");
  Serial.print(soilAnalog);
  Serial.print(" | Moisture (%): ");
  Serial.println(soilMoisture);

  if (soilMoisture < THRESHOLD_MOISTURE) {
    digitalWrite(PUMP_PIN, HIGH);
    Serial.println("Pump ON");
  } else {
    digitalWrite(PUMP_PIN, LOW);
    Serial.println("Pump OFF");
  }

  delay(2000); 
}

i do test the soil sensor analog dip in water and see the soil analog value for the wet is around 450 but if its dry it's on 1024, so i map into the soil moisture