Created
June 15, 2019 22:11
-
-
Save geek-at/346520639924cf391dc4836a8017342e to your computer and use it in GitHub Desktop.
Code for the DIY irrigation system -> https://blog.haschek.at/2019/diy-garden-irrigation-for-less-than-20-bucks.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#define wifi_ssid "YourWIfiName" | |
#define wifi_password "YourWifiPassword" | |
const char* mqttServer = "192.168.1.61"; //IP of your MQTT Server | |
const int mqttPort = 1883; // Port of your MQTT server | |
//pins change these if you used different pins | |
const int valve1=D1; | |
const int valve2=D2; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
int status = WL_IDLE_STATUS; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(valve1, OUTPUT); | |
pinMode(valve2, OUTPUT); | |
digitalWrite(valve1, HIGH); | |
digitalWrite(valve2, HIGH); | |
reconnectWifi(); | |
client.setServer(mqttServer, mqttPort); | |
client.setCallback(callback); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char message_buff[100]; | |
Serial.print("Message arrived in topic: "); | |
Serial.println(topic); | |
Serial.println("Message:"); | |
int i = 0; | |
for (i = 0; i < length; i++) { | |
message_buff[i] = (char)payload[i]; | |
} | |
message_buff[i] = '\0'; | |
String msgString = String(message_buff); | |
Serial.println(msgString); | |
if (strcmp(topic, "garden/valve1") == 0) | |
{ | |
if (msgString == "OFF") | |
digitalWrite(valve1, HIGH); | |
else if (msgString == "ON") | |
digitalWrite(valve1, LOW); | |
} | |
if (strcmp(topic, "garden/valve2") == 0) | |
{ | |
if (msgString == "OFF") | |
digitalWrite(valve2, HIGH); | |
else if (msgString == "ON") | |
digitalWrite(valve2, LOW); | |
} | |
Serial.println("-----------------------"); | |
} | |
void mqttreconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Connecting to server ..."); | |
if ( client.connect("Valves",NULL,NULL,"status/valvecontrol",1,1,"DEAD") ) { | |
Serial.println( "[DONE]" ); | |
client.publish("status/valvecontrol", "ALIVE",true); | |
client.subscribe("garden/#"); | |
} else { | |
Serial.print( "[FAILED] [ rc = " ); | |
Serial.print( client.state() ); | |
Serial.println( " : retrying in 5 seconds]" ); | |
// Wait 5 seconds before retrying | |
delay( 5000 ); | |
} | |
} | |
} | |
void reconnectWifi() | |
{ | |
if ( WiFi.status() != WL_CONNECTED) { | |
WiFi.begin(wifi_ssid, wifi_password); | |
int count = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
count++; | |
delay(500); | |
Serial.println("Connecting to WiFi.."); | |
delay(500); | |
if(count>50) reconnectWifi(); | |
} | |
} | |
} | |
void loop() { | |
reconnectWifi(); | |
if ( !client.connected() ) { | |
mqttreconnect(); | |
} | |
client.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment