Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <PubSubClient.h>
  2. #include <ESP8266WiFi.h>
  3. #include <SoftwareSerial.h>
  4. // Update these with values suitable for your network.
  5.  
  6. //const char* ssid = "Automation";
  7. //const char* password = "1234567890";
  8. const char* ssid = "Office";
  9. const char* password = "Office2018";
  10. const char* mqtt_server = "192.168.16.141";
  11.  
  12.  
  13.  
  14. char msg[50];
  15.  
  16. WiFiClient espClient;
  17. PubSubClient client(espClient);
  18.  
  19. byte mac[6];
  20. String MACAddress;
  21.  
  22.  
  23.  
  24. const int LED = D5;
  25.  
  26. void setup() {
  27. pinMode(LED, OUTPUT);
  28.  
  29. Serial.begin(115200);
  30. setup_wifi();
  31. printMACAddress();
  32. client.setServer(mqtt_server, 1883);
  33. client.setCallback(callback);
  34. }
  35.  
  36. void setup_wifi() {
  37.  
  38. delay(10);
  39. // We start by connecting to a WiFi network
  40. Serial.println();
  41. Serial.print("Connecting to ");
  42. Serial.println(ssid);
  43.  
  44. WiFi.begin(ssid, password);
  45.  
  46. while (WiFi.status() != WL_CONNECTED) {
  47. delay(500);
  48. Serial.print(".");
  49. }
  50.  
  51. Serial.println("");
  52. Serial.println("WiFi connected");
  53. Serial.println("IP address: ");
  54. Serial.println(WiFi.localIP());
  55. }
  56.  
  57. String mac2String(byte ar[]) {
  58. String s;
  59. for (byte i = 0; i < 6; ++i)
  60. {
  61. char buf[3];
  62. sprintf(buf, "%2X", ar[i]);
  63. s += buf;
  64. if (i < 5) s += ':';
  65. }
  66. return s;
  67. }
  68.  
  69. void printMACAddress() {
  70. WiFi.macAddress(mac);
  71. MACAddress = mac2String(mac);
  72. Serial.println(MACAddress);
  73. }
  74.  
  75. void callback(char* topic, byte* payload, unsigned int length) {
  76. Serial.print("Message arrived [");
  77. Serial.print(topic);
  78. Serial.print("] ");
  79. for (int i = 0; i < length; i++) {
  80. Serial.print((char)payload[i]);
  81. }
  82. Serial.println();
  83.  
  84. // Switch on the LED if an 1 was received as first character
  85. if ((char)payload[0] == "On") {
  86. digitalWrite(LED, HIGH); // Turn the LED on (Note that HIGH is the voltage level
  87. }
  88. if ((char)payload[0] == "Off") {
  89. digitalWrite(LED, LOW); // Turn the LED off by making the voltage LOW
  90. }
  91.  
  92. }
  93.  
  94. void reconnect() {
  95. // Loop until we're reconnected
  96.  
  97. printMACAddress();
  98. const char* CL;
  99. CL = MACAddress.c_str();
  100. Serial.println(CL);
  101. while (!client.connected()) {
  102. Serial.print("Attempting MQTT connection...");
  103. // Attempt to connect
  104. if (client.connect(CL)) {
  105. Serial.println("connected");
  106. client.subscribe("terima");
  107. } else {
  108. Serial.print("failed, rc=");
  109. Serial.print(client.state());
  110. Serial.println(" try again in 5 seconds");
  111. ESP.restart();
  112. delay(5000);
  113.  
  114. }
  115. }
  116. }
  117.  
  118. void loop() {
  119.  
  120. if (!client.connected()) {
  121. reconnect();
  122. }
  123. client.loop();
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement