Last active
January 1, 2016 15:30
-
-
Save compilerexe/78abd38c68911abddad5 to your computer and use it in GitHub Desktop.
MQTT Restaurant
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> | |
const char *ssid = "__YOUR_WIFI_SSID__"; | |
const char *pass = "__YOUR_WIFI_PASSWORD__"; | |
String MQTT_server("__YOUR_MQTT_SERVER__"); | |
/* MQTT_clienId USE ONE NUMBER TO ONE BOARD EXAMPLE TABLE-1 NOT USE DOUBLE NUMBER TABLE OTHER BOARD */ | |
String MQTT_clienId = "__NUMBER_TABLE__"; | |
String MQTT_username = "__YOUR_MQTT_USERNAME__"; | |
String MQTT_password = "__YOUR_MQTT_PASSWORD__"; | |
String MQTT_topic = "__YOUR_MQTT_TOPIC"; | |
int MQTT_port = 14878; // CHANGE YOUR PORT | |
int pushButton_Pin = 12; | |
int led_Pin = 14; | |
int value = 0; | |
WiFiClient wclient; | |
PubSubClient client(wclient, MQTT_server, MQTT_port); | |
void setup() { | |
Serial.begin(9600); | |
pinMode(pushButton_Pin, INPUT); | |
pinMode(led_Pin, OUTPUT); | |
} | |
void loop() { | |
if (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
WiFi.begin(ssid, pass); | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
Serial.println(); | |
int RETRY_CONNECTION = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
if (RETRY_CONNECTION == 30) { | |
Serial.print("."); | |
RETRY_CONNECTION = 0; | |
} | |
delay(50); | |
RETRY_CONNECTION++; | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
} | |
if (!client.connected()) { | |
digitalWrite(led_Pin, 0); | |
Serial.print("Connecting to MQTT server : "); | |
Serial.print(MQTT_server); | |
Serial.println(); | |
while (!client.connect(MQTT::Connect("ESP8266 TABLE " + MQTT_clientId).set_auth(MQTT_username, MQTT_password))) { | |
Serial.print("."); | |
delay(10); | |
} | |
Serial.println(); | |
Serial.print("ClientId : "); | |
Serial.print(MQTT_clientId); | |
Serial.println(); | |
Serial.print("Topic : "); | |
Serial.print(MQTT_topic); | |
Serial.println(); | |
Serial.println("Connected MQTT"); | |
Serial.println(); | |
} else { | |
digitalWrite(led_Pin, 1); | |
value = digitalRead(pushButton_Pin); | |
while (value) { | |
value = digitalRead(pushButton_Pin); | |
if (value == LOW) { | |
while (!client.publish(MQTT_topic, MQTT_clientId)) { | |
delay(10); | |
} | |
Serial.print("Publish Data : "); | |
Serial.print(MQTT_clientId); | |
Serial.print(" -> "); | |
Serial.print(MQTT_topic); | |
Serial.println(); | |
Serial.println("=============================="); | |
} | |
delay(50); | |
} | |
} | |
if (client.connected()){ | |
delay(20); | |
client.loop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment