#include <Stepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <PubSubClient.h>
#include <WiFiMulti.h>
#include <WiFi.h>
#define WIFISSID "ERROR404"
#define PASSWORD "qwerty1234"
#define TOKEN "BBFF-jj8O42thcYOrdlp5MbbOmOnTfkC1Ar"
#define MQTT_CLIENT_NAME "Smart Irrigation System"
#define VARIABLE_LABEL_1 "Temperaturevalue"
#define VARIABLE_LABEL_2 "Humidity"
#define VARIABLE_LABEL_3 "Moisture"
#define DEVICE_LABEL "Smart Irrigation System"
char mqttBroker[]= "industrial.api.ubidots.com";
char payload[700];
char topic[150];
// Space to store values to send
char str_val_1[6];
char str_val_2[6];
char str_val_3[6];
int flag = 0;
WiFiMulti WifiMulti;
WiFiClient ubidots;
PubSubClient client (ubidots);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 4, 5, 18, 19);
const int DHT_PIN = 15;
DHTesp dhtSensor;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
int Temperaturevalue = 0;
int Humidity = 0;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Print a message to the LCD.
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SMART IRRIGATION");
lcd.setCursor(0,1);
lcd.print("SYSTEM");
Serial.begin(9600);
delay(2000);
lcd.clear();
WiFi.begin(WIFISSID, PASSWORD);
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}
void loop() {
if (flag == 0)
{
client.connect(MQTT_CLIENT_NAME, TOKEN, "");
Serial.println("MQTT connected again");
flag = 1;
}
if (!client.connected()) {
Serial.print("Reconnecting ... ");
reconnect();
}
// Make sure to call update as fast as possible
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float Temperaturevalue = data.temperature;
float Humidity = data.humidity;
Serial.println(Temperaturevalue);
Serial.println(Humidity);
if (Temperaturevalue <= 30 || Humidity >= 200 ) {
lcd.setCursor(0,0);
lcd.print("Tem=");
lcd.print(Temperaturevalue);
lcd.print("Mos=");
lcd.print(Humidity);
lcd.setCursor(0,1);
lcd.print("PUMP STATUS:OFF ");
}
else if (Temperaturevalue > 30 || Humidity < 200) {
lcd.setCursor(0,0);
lcd.print("Temp=");
lcd.print(Temperaturevalue);
lcd.print("Mos=");
lcd.print(Humidity);
lcd.setCursor(0,1);
lcd.print("PUMP Status:ON ");
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
delay(750); // Delay a little bit to improve simulation performance
int16_t i = analogRead(34);
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
lcd.clear();
lcd.print("Soil: ");
lcd.print(msg);
dtostrf(Temperaturevalue, 4, 2, str_val_1);
dtostrf(Humidity, 4, 2, str_val_2);
//dtostrf((double)msg, 4, 2, str_val_3);
sprintf(topic, "%s", ""); // Cleans the topic content
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload content
sprintf(payload, "{\"%s\":", VARIABLE_LABEL_1); // Adds the variable label
sprintf(payload, "%s {\"value\": %s}", payload, str_val_1); // Adds the value
sprintf(payload, "%s, \"%s\":", payload, VARIABLE_LABEL_2); // Adds the variable label
sprintf(payload, "%s {\"value\": %s}", payload, str_val_2); // Adds the value
//sprintf(payload, "%s, \"%s\":", payload, VARIABLE_LABEL_3); // Adds the variable label
//sprintf(payload, "%s {\"value\": %s}", payload, str_val_3); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
Serial.println(payload);
Serial.println(topic);
client.publish(topic, payload);
client.loop();
Serial.println("Data sent to Ubidots");
delay(5000); // Delay before sending the next set of data
}