Created
September 26, 2017 16:27
-
-
Save nbluis/79faac30d6917c1d93c696f812230e6e 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
| #include <ESP8266WiFi.h> | |
| #include <PubSubClient.h> | |
| // MQTT | |
| #define MQTT_BROKER "10.0.0.1" //MOSQUITTO IP | |
| #define MQTT_BROKER_PORT 1883 | |
| #define MQTT_ID "ID" //UNIQUE MOSQUITTO THING ID | |
| #define MQTT_PRIVATE_TOPIC "iot/office/"MQTT_ID | |
| #define MQTT_BROADCAST_TOPIC "iot/office" | |
| // WIFI | |
| #define WIFI_SSID "WIFI_SSID" | |
| #define WIFI_PASSWORD "WIFI_PASS" | |
| //defines - mapeamento de pinos do NodeMCU | |
| #define D0 16 | |
| #define D1 5 | |
| #define D2 4 | |
| #define D3 0 | |
| #define D4 2 | |
| #define D5 14 | |
| #define D6 12 | |
| #define D7 13 | |
| #define D8 15 | |
| #define D9 3 | |
| #define D10 1 | |
| WiFiClient espClient; | |
| PubSubClient MQTT(espClient); | |
| char state = '0'; | |
| void initSerial(); | |
| void initWiFi(); | |
| void initMQTT(); | |
| void reconectWiFi(); | |
| void mqttCallback(char* topic, byte* payload, unsigned int length); | |
| void verifyConnections(); | |
| void initOutput(); | |
| void setup() { | |
| initOutput(); | |
| initSerial(); | |
| initWiFi(); | |
| initMQTT(); | |
| } | |
| void initOutput() { | |
| pinMode(D0, OUTPUT); | |
| digitalWrite(D0, HIGH); | |
| } | |
| void initSerial() { | |
| Serial.begin(115200); | |
| } | |
| void initWiFi() { | |
| delay(10); | |
| Serial.println("------Conexao WI-FI------"); | |
| Serial.print("Conectando-se na rede: "); | |
| Serial.println(WIFI_SSID); | |
| Serial.println("Aguarde"); | |
| } | |
| void initMQTT() { | |
| MQTT.setServer(MQTT_BROKER, MQTT_BROKER_PORT); | |
| MQTT.setCallback(mqttCallback); | |
| } | |
| void reconectWiFi() { | |
| if (WiFi.status() == WL_CONNECTED) | |
| return; | |
| WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(100); | |
| Serial.print("."); | |
| } | |
| Serial.println(); | |
| Serial.print("Conectado com sucesso na rede "); | |
| Serial.print(WIFI_SSID); | |
| Serial.println("IP obtido: "); | |
| Serial.println(WiFi.localIP()); | |
| } | |
| void reconnectMQTT() { | |
| while (!MQTT.connected()) { | |
| Serial.print("* Tentando se conectar ao Broker MQTT: "); | |
| Serial.println(MQTT_BROKER); | |
| if (MQTT.connect(MQTT_ID)) { | |
| Serial.println("Conectado com sucesso ao broker MQTT!"); | |
| MQTT.subscribe(MQTT_PRIVATE_TOPIC); | |
| MQTT.subscribe(MQTT_BROADCAST_TOPIC); | |
| } else { | |
| Serial.println("Falha ao reconectar no broker."); | |
| Serial.println("Havera nova tentatica de conexao em 2s"); | |
| delay(2000); | |
| } | |
| } | |
| } | |
| void mqttCallback(char* topic, byte* payload, unsigned int length) { | |
| String msg = buildMessage(payload, length); | |
| if (msg.equals("L")) { | |
| digitalWrite(D0, LOW); | |
| state = '1'; | |
| } | |
| if (msg.equals("D")) { | |
| digitalWrite(D0, HIGH); | |
| state = '0'; | |
| } | |
| } | |
| String buildMessage(byte* payload, unsigned int length) { | |
| String msg; | |
| for (int i = 0; i < length; i++) { | |
| char c = (char) payload[i]; | |
| msg += c; | |
| } | |
| return msg; | |
| } | |
| void verifyConnections() { | |
| if (!MQTT.connected()) | |
| reconnectMQTT(); | |
| reconectWiFi(); | |
| } | |
| void loop() { | |
| verifyConnections(); | |
| MQTT.loop(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment