Advertisement
Guest User

MQTT

a guest
May 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4.  
  5. // Update these with values suitable for your network.
  6.  
  7. char msg1[20];
  8. char msgFresh[20];
  9. char* subtop;
  10. //char[] laggay = "FID";
  11. //String str(laggay);
  12. const char* ssid = "***";
  13. const char* password = "***";
  14. const char* mqtt_server = "192.168.1.1";
  15. //String str1 = "";
  16.  
  17. WiFiClient espClient;
  18. PubSubClient client(espClient);
  19.  
  20. long lastMsg = 0;
  21. char msg[50];
  22. int value = 0;
  23.  
  24. void setup_wifi() {
  25.  
  26. delay(10);
  27. // We start by connecting to a WiFi network
  28. Serial.println();
  29. Serial.print("Connecting to ");
  30. Serial.println(ssid);
  31.  
  32. WiFi.begin(ssid, password);
  33.  
  34. while (WiFi.status() != WL_CONNECTED) {
  35. delay(500);
  36. Serial.print(".");
  37. }
  38.  
  39. randomSeed(micros());
  40.  
  41. Serial.println("");
  42. Serial.println("WiFi connected");
  43. Serial.println("IP address: ");
  44. Serial.println(WiFi.localIP());
  45. }
  46.  
  47. void callback(char* topic, byte* payload, unsigned int length) {
  48. //Serial.print("Message arrived [");
  49. // Serial.print(topic);
  50. //Serial.print("] ");
  51. // for (int i = 0; i < length; i++) {
  52. // Serial.print((char)payload[i]);
  53. // }
  54. String str(topic);
  55. delay(500);
  56. // Serial.print(length);
  57. if(str == "FID")
  58. {
  59. String str1;
  60. for (int i = 0; i < length; i++)
  61. {
  62. str1 += String((char *)payload)[i];
  63. }
  64. Serial.println(str1);
  65. Serial.print("---");
  66. if (str1 == "Denis")
  67. {
  68. Serial.println(" That's Denis");
  69. str1 = "";
  70. }
  71. else if(str1 == "Egor")
  72. {
  73. Serial.println(" That's Egor");
  74. str1 = "";
  75. }
  76. else if(str1 == "Varava")
  77. {
  78. Serial.println(" That's Sasha");
  79. str1 = "";
  80. }
  81. else if (str1 == "Unknown")
  82. {
  83. Serial.println("Somebody");
  84. str1 = "";
  85. }
  86.  
  87. str1 = "";
  88.  
  89. }
  90. str = "";
  91. }
  92.  
  93.  
  94. void reconnect() {
  95. // Loop until we're reconnected
  96. while (!client.connected()) {
  97. Serial.print("Attempting MQTT connection...");
  98. // Create a random client ID
  99. String clientId = "ESP8266Client-";
  100. clientId += String(random(0xffff), HEX);
  101. // Attempt to connect
  102. if (client.connect(clientId.c_str())) {
  103. Serial.println("connected");
  104. // Once connected, publish an announcement...
  105. // client.publish("outTopic", "hello world");
  106. // ... and resubscribe
  107. client.subscribe("FID");
  108. } else {
  109. Serial.print("failed, rc=");
  110. Serial.print(client.state());
  111. Serial.println(" try again in 5 seconds");
  112. // Wait 5 seconds before retrying
  113. delay(5000);
  114. }
  115. }
  116. }
  117.  
  118. void setup() {
  119. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  120. Serial.begin(115200);
  121. setup_wifi();
  122. client.setServer(mqtt_server, 1883);
  123. client.setCallback(callback);
  124. client.subscribe("FID");
  125. }
  126.  
  127. void loop() {
  128.  
  129. if (!client.connected()) {
  130. reconnect();
  131. }
  132. client.loop();
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement