Last active
May 31, 2018 15:46
-
-
Save RianWardana/7a0736bb1f9cdcc0d9fcb14e3a9c8f17 to your computer and use it in GitHub Desktop.
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 <MQTT.h> | |
#define pin_led 2 | |
#define pin_button 14 | |
#define wifi_ssid "ChewingGum" | |
#define wifi_password "Chewing.Gum" | |
#define mqtt_server "52.230.69.177" | |
#define mqtt_server_port 1883 | |
#define mqtt_data_topic "mikon/daya" | |
#define mqtt_publish_topic "mikon/toServer" | |
#define mqtt_subscribe_topic "mikon/fromServer" | |
WiFiClient net; | |
MQTTClient client; | |
boolean relayON = false; | |
boolean buttonPressed = false; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(pin_led, OUTPUT); | |
pinMode(pin_button, INPUT); | |
connectWifi(); | |
setupMqtt(); | |
} | |
void loop() { | |
if (!client.connected()) connectMqtt(); | |
client.loop(); | |
if (digitalRead(pin_button) == HIGH) { | |
delay(100); | |
if (digitalRead(pin_button) == HIGH) handleButton(); | |
} | |
checkSerial(); | |
delay(5); | |
} | |
void checkSerial() { | |
if (Serial.available() > 0) { | |
//char buffer[] = {' ',' '}; | |
String data = Serial.readStringUntil(','); | |
//int incomingValue = atoi(buffer); | |
client.publish(mqtt_data_topic, data); | |
} | |
} | |
void connectWifi() { | |
// Serial.println("Connecting to WiFi"); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(wifi_ssid, wifi_password); | |
boolean ledState = true; | |
while (WiFi.status() != WL_CONNECTED) { | |
// Serial.print("."); | |
delay(500); | |
(ledState ? digitalWrite(pin_led, 1) : digitalWrite(pin_led, 0)); | |
ledState != ledState; | |
} | |
// Serial.println("Connected!"); | |
digitalWrite(pin_led, 1); | |
} | |
void connectMqtt() { | |
while (!client.connected()) { | |
if (client.connect("ProyekMikon15")) { | |
// Serial.println("Trying to connect MQTT."); | |
client.subscribe(mqtt_subscribe_topic); | |
(relayON ? client.publish(mqtt_publish_topic, "1") : client.publish(mqtt_publish_topic, "0")); | |
} | |
else { | |
// Serial.println("Fail to connect MQTT."); | |
delay(3000); | |
} | |
} | |
// Serial.println("MQTT connected."); | |
} | |
void setupMqtt() { | |
client.begin(mqtt_server, mqtt_server_port, net); | |
client.onMessage(onMessage); | |
} | |
void onMessage(String &topic, String &data) { | |
// Serial.println("Message arrived."); | |
if (data == "0") { | |
Serial.print('0'); | |
relayON = false; | |
} | |
else if (data == "1") { | |
Serial.print('1'); | |
relayON = true; | |
} | |
client.publish(mqtt_publish_topic, data); | |
} | |
long int lastHandled; | |
void handleButton() { | |
if ((millis() - lastHandled) < 1000) return; | |
if (relayON) { | |
Serial.print('0'); | |
} else { | |
Serial.print('1'); | |
} | |
relayON = !relayON; | |
(relayON ? client.publish(mqtt_publish_topic, "1") : client.publish(mqtt_publish_topic, "0")); | |
lastHandled = millis(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment