Help to get down current consumption

Hello all
i have this code i need iderars how i can get down current consumption
battery is empty after 24 H
i use a 1100 Mak 3.7 volt battery

// Load libraries
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Replace with your network credentials
const char* ssid = "***";
const char
password = "
";

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "192.168.1.248";
// MQTT Broker IP example
//const char* mqtt_server = "192.168.1.144";

// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient1;
PubSubClient client(espClient1);

// Variable to hold the temperature reading
String temperatureString = "";

// Set GPIOs for: output variable, Soil Sensor, Temperature sensor
const int output = 15;
//int WET= 16; // Wet Indicator at Digital PIN D0
//int DRY= 2; // Dry Indicator at Digital PIN D4
int sense_Pin= 0; // Soil Sensor input at Analog PIN A0
int value= 0;

//const int ldr = A0;
// Store the current output state
String outputState = "off";

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

// Timers - Auxiliary variables
unsigned long now = millis();
unsigned long lastMeasure = 0;
boolean startTimer = false;
unsigned long currentTime = millis();
unsigned long previousTime = 0;

// Don't change the function below.
// This function connects your ESP8266 to your router
void setup_wifi() {
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) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}

// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;

for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();

// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp8266/output, you check if the message is either on or off.
// Turns the output according to the message received
if(topic=="esp8266/output"){
Serial.print("Changing output to ");
if(messageTemp == "on"){
digitalWrite(output, LOW);
Serial.print("on");
}
else if(messageTemp == "off"){
digitalWrite(output, HIGH);
Serial.print("off");
}
}
Serial.println();
}

// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more outputs)
client.subscribe("esp8266/output");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
// Start the DS18B20 sensor
sensors.begin();

// Serial port for debugging purposes
Serial.begin(115200);

setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}

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

// Timer variable with current time
now = millis();

// Publishes new temperature readings every 30 seconds
if (now - lastMeasure > 30000) {
lastMeasure = now;
sensors.requestTemperatures();
// Temperature in Celsius degrees
temperatureString = String(sensors.getTempCByIndex(0));
// Uncomment the next line for temperature in Fahrenheit degrees
//temperatureString = String(sensors.getTempFByIndex(0));
// Publishes Temperature values
client.publish("esp8266/temperature", temperatureString.c_str());
Serial.println("Temperature published");
}

//buttonState = digitalRead(buttonPin);
delay(10000);
// Serial.print("MOISTURE LEVEL : ");

   // Publishes MOISTURE LEVEL values

client.publish("smart/garden/message/soil/value", String(analogRead(sense_Pin)).c_str());

}

Please reformat using the </> tag and post a schematic so we can see what we're dealing with. Hand drawn is fine (actually preferred for most stuff)

Send data by data changes only.

I'd start with figuring out a way of managing peripheral power by finding a low power way to switch off power to those units that can be switched on/off and swapping to low power versions of peripherals.

Such as it may cost more but a BME680 has a low power mode, allowing the module to be put into a low power state while the MCU is put to sleep. Looking at your code, which is missing code tags, I see that a capacitive soil sensor is being used and has its own power connection and will consume battery power when the CPU is sleeping.

Expecting a 1100mAh battery to last for a long time when a capacitive soil sensor is drawing current 24/7 may not be reasonable. The same can be said for the DallasTemperature sensor.

Got a multi-meter? If you can use it to measure the current draw of your project to get an idea of how long the project will run on batteries. Let us know what the current draw is, if you do measure it.

First time poster? OK, read and follow the instructions to edit your first post using the "pencil" icon below it.

Why does the code refer to a Raspberry Pi? :dizzy_face:

I think that's where the MQTT broker is running.

Yes is send to a Raspberry pi with MQTT broker running

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