Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1.  
  2.  
  3. //MQTT
  4. #include <PubSubClient.h>
  5. //WIFIMANAGER
  6. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  7. #include <DNSServer.h>
  8. #include <ESP8266WebServer.h>
  9. #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
  10. #define MQTT_AUTH false
  11. #define MQTT_USERNAME ""
  12. #define MQTT_PASSWORD ""
  13. #define LIGHT D2
  14. const char* MQTT_CONTROL_TOPIC = "/bhmaker/luz/set";
  15. const char* MQTT_STATE_TOPIC = "/bhmaker/luz";
  16. //MQTT BROKERS GRATUITOS PARA TESTES https://github.com/mqtt/mqtt.github.io/wiki/public_brokers
  17. const char* MQTT_SERVER = "test.mosquitto.org";
  18. String const HOSTNAME = "bhMaker";
  19. //INIT MQTT
  20. WiFiClient wclient;
  21. PubSubClient client(MQTT_SERVER,1883,wclient);
  22.  
  23. void setup() {
  24. // put your setup code here, to run once:
  25. Serial.begin(115200);
  26. pinMode(LIGHT,OUTPUT);
  27. //WiFiManager
  28. //Local intialization. Once its business is done, there is no need to keep it around
  29. WiFiManager wifiManager;
  30. //reset settings - for testing
  31. //wifiManager.resetSettings();
  32.  
  33. //sets timeout until configuration portal gets turned off
  34. //useful to make it all retry or go to sleep
  35. //in seconds
  36. wifiManager.setTimeout(180);
  37.  
  38. //fetches ssid and pass and tries to connect
  39. //if it does not connect it starts an access point with the specified name
  40. //here "AutoConnectAP"
  41. //and goes into a blocking loop awaiting configuration
  42. if(!wifiManager.autoConnect(HOSTNAME.c_str(),"xptoxpto")) {
  43. Serial.println("failed to connect and hit timeout");
  44. delay(3000);
  45. //reset and try again, or maybe put it to deep sleep
  46. ESP.restart();
  47. delay(5000);
  48. }
  49.  
  50. //if you get here you have connected to the WiFi
  51. Serial.println("connected...yeey :)");
  52. client.setCallback(callback);
  53. }
  54. //Chamada de recepção de mensagem
  55. void callback(char* topic, byte* payload, unsigned int length) {
  56. String payloadStr = "";
  57. for (int i=0; i<length; i++) {
  58. payloadStr += (char)payload[i];
  59. }
  60. Serial.println(payloadStr);
  61. String topicStr = String(topic);
  62. if(topicStr.equals(MQTT_CONTROL_TOPIC)){
  63. if(payloadStr.equals("ON")){
  64. turnOn();
  65. }else if(payloadStr.equals("OFF")) {
  66. turnOff();
  67. }
  68. }
  69. }
  70.  
  71. void turnOn(){
  72. digitalWrite(LIGHT,HIGH);
  73. }
  74. void turnOff(){
  75. digitalWrite(LIGHT,LOW);
  76.  
  77. }
  78. bool checkMqttConnection(){
  79. if (!client.connected()) {
  80. if (MQTT_AUTH ? client.connect(HOSTNAME.c_str(),MQTT_USERNAME, MQTT_PASSWORD) : client.connect(HOSTNAME.c_str())) {
  81. //SUBSCRIÇÃO DE TOPICOS
  82. Serial.println("CONNECTED");
  83. client.subscribe(MQTT_CONTROL_TOPIC);
  84. }
  85. }
  86. return client.connected();
  87. }
  88. void loop() {
  89. if (WiFi.status() == WL_CONNECTED) {
  90. if (checkMqttConnection()){
  91. client.loop();
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement