Created
September 26, 2017 12:21
-
-
Save j15e/9a289304dc58a72fa43c54b246c7a805 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#define WLAN_SSID "..." | |
#define WLAN_PASS "..." | |
#define WLAN_HOSTNAME "gardenrelay-1" | |
#define IO_USERNAME "j15e" | |
#define IO_KEY "..." | |
#define FEED_PATH IO_USERNAME "/f/garden/" | |
#define SET_PIN 5 | |
#define UNSET_PIN 4 | |
#include <ESP8266WiFi.h> | |
#include <EthernetClient.h> | |
#include <SPI.h> | |
#include <PubSubClient.h> | |
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(); | |
int state = payload[0]; | |
if(state == 1) { | |
openRelay(); | |
} else { | |
closeRelay(); | |
} | |
Serial.println(""); | |
} | |
WiFiClient client; | |
PubSubClient mqttclient(client); | |
void setup() { | |
Serial.begin(115200); | |
delay(100); | |
// Close relay during boot | |
closeRelay(); | |
// Connecting to WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(WLAN_SSID); | |
WiFi.hostname(WLAN_HOSTNAME); | |
WiFi.begin(WLAN_SSID, WLAN_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(120); | |
} | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("Mac Address: "); | |
Serial.println(WiFi.macAddress()); | |
// Connect to MQTT and subscribe to a path | |
mqttclient.setServer("io.adafruit.com", 1883); | |
mqttclient.setCallback(callback); | |
if (mqttclient.connect("ESP8266Client", IO_USERNAME, IO_KEY)) { | |
Serial.println("MQTT Connected"); | |
if(mqttclient.subscribe(FEED_PATH)) { | |
Serial.println("MQTT Subcribed to : "); | |
Serial.println(FEED_PATH); | |
} | |
} else { | |
Serial.println("MQTT Not connected : "); | |
Serial.println(mqttclient.state()); | |
} | |
} | |
void loop() { | |
mqttclient.loop(); | |
if (!mqttclient.connected()) { | |
mqttclient.connect("ESP8266Client", IO_USERNAME, IO_KEY); | |
mqttclient.subscribe(FEED_PATH); | |
} else { | |
if(mqttclient.publish(FEED_PATH, "1")) { | |
Serial.println("Published!"); | |
} else { | |
Serial.println("Not published!"); | |
Serial.println("MQTT State : "); | |
Serial.println(mqttclient.state()); | |
}; | |
} | |
Serial.println("MQTT State : "); | |
Serial.println(mqttclient.state()); | |
delay(1000); | |
} | |
void openRelay() { | |
analogWrite(SET_PIN, PWMRANGE); | |
delay(15); | |
analogWrite(SET_PIN, 0); | |
} | |
void closeRelay() { | |
analogWrite(UNSET_PIN, PWMRANGE); | |
delay(15); | |
analogWrite(UNSET_PIN, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment