Skip to content

Instantly share code, notes, and snippets.

@mohitkh7
Last active April 29, 2018 03:25
Show Gist options
  • Save mohitkh7/3a04aacdd6399a5bdb7745a3398ee2ee to your computer and use it in GitHub Desktop.
Save mohitkh7/3a04aacdd6399a5bdb7745a3398ee2ee to your computer and use it in GitHub Desktop.
Hardware code of Smart Irrigation System.
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
#include "ArduinoJson.h"
const int AnalogIn = A0;
const int Motor_Pin = D1;
const char* ssid = "JioFi3_1E8B14";
const char* password = "9d1u40ntfbb";
const char* host = "mohitkh7.pythonanywhere.com";
int value = 0;
int state;
void setup() {
Serial.begin(115200);
delay(10);
pinMode(Motor_Pin,OUTPUT);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //WiFi Connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP Address:");
Serial.println(WiFi.localIP());
}
void loop() {
delay(1000);
value = 1024-(analogRead(AnalogIn));
state = digitalRead(Motor_Pin);
Serial.print("Connecting to ");
Serial.println(host);
WiFiClient client; //WiFiClient to create TCP connections
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("Connection failed");
return;
}
String url = "/api/create/";
url += "?";
url += "moisture";
url += "=";
url += value;
url += "&";
url += "status";
url += "=";
url += state;
Serial.print("Requesting URL:");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host:" + host + "\r\n" + "Connection: close\r\n\r\n");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
}
}
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
HTTPClient http;
http.begin("http://mohitkh7.pythonanywhere.com/api/read/");
int httpCode = http.GET();
if (httpCode > 0) { // Get the request response payload
String payload = http.getString(); //to do parsing
Serial.println(payload);
const size_t bufferSize = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(payload);
int set_state = root["set_state"];
Serial.println(set_state);
if (set_state == 1) {
digitalWrite(Motor_Pin, HIGH);
Serial.println("Motor turned ON");
}
else {
digitalWrite(Motor_Pin, LOW);
Serial.println("Motor turned OFF");
}
}
http.end();
Serial.println("Closing connection");
delay(60000);
}
Ubidots Code:
#include "UbidotsESPMQTT.h"
int pin=D1;
int Sensor_Pin = A0;
int Sensor_Val;
int Motor_State;
int count;
char state;
#define TOKEN "A1E-ozFQrTzkWsmI3G4d8wxDWneYOD1tl2" // Your Ubidots TOKEN
#define WIFINAME "JioFi3_1E8B14" //Your SSID
#define WIFIPASS "9d1u40ntfbb" // Your Wifi Pass
Ubidots client(TOKEN);
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]);
}
if (topic[25] == 's') {
state = (char)payload[0];
}
else if (topic[25] == 't') {
for (int i = 0; i < length; i++) {
count = payload[i];
}
if (state == '1') {
digitalWrite(pin, HIGH);
Serial.println("Motor turned ON");
delay(count * 100);
digitalWrite(pin, LOW);
Serial.println("Motor turned OFF");
//t.pulse(LED_BUILTIN,count*1000,HIGH);
}
else {
digitalWrite(pin, LOW);
Serial.println("Motor turned OFF");
delay(count * 1000);
// t.pulse(LED_BUILTIN,count*1000,LOW);
}
}
Serial.println();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// client.setDebug(true); // Pass a true or false bool value to activate debug messages
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
pinMode(pin, OUTPUT);
client.ubidotsSubscribe("irrigation","switch"); //Insert the dataSource and Variable's Labels
client.ubidotsSubscribe("irrigation","timer");
}
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
client.ubidotsSubscribe("irrigation","switch"); //Insert the dataSource and Variable's Labels
client.ubidotsSubscribe("irrigation","timer");
}
Sensor_Val = analogRead(Sensor_Pin);
Motor_State = digitalRead(pin);
if (Sensor_Val >= 800) {
digitalWrite(pin, HIGH);
client.add("motor", Motor_State);
client.ubidotsPublish("irrigation");
//Serial.println("Motor turned ON");
}
else {
digitalWrite(pin, LOW);
client.add("motor", Motor_State);
client.ubidotsPublish("irrigation");
// Serial.println("Motor turned OFF");
}
client.add("moisture", Sensor_Val);
client.ubidotsPublish("irrigation");
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment