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