merkelck

wifirem_2.ino.txt

Feb 5th, 2019
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. int BUTTON_PIN = 5; //button is connected to GPIO pin D1
  5. // Update these with values suitable for your network.
  6. const char* ssid = "xx";
  7. const char* password = "xx";
  8. const char* mqtt_server = "192.168.1.x";
  9. //const char* mqtt_server = "iot.eclipse.org";
  10.  
  11. WiFiClient espClient;
  12. PubSubClient client(espClient);
  13. long lastMsg = 0;
  14. char msg[50];
  15.  
  16. void setup_wifi() {
  17. delay(100);
  18. // We start by connecting to a WiFi network
  19. Serial.print("Connecting to ");
  20. Serial.println(ssid);
  21. WiFi.begin(ssid, password);
  22. while (WiFi.status() != WL_CONNECTED)
  23. {
  24. delay(500);
  25. Serial.print(".");
  26. }
  27. randomSeed(micros());
  28. Serial.println("");
  29. Serial.println("WiFi connected");
  30. Serial.println("IP address: ");
  31. Serial.println(WiFi.localIP());
  32. }
  33.  
  34. void callback(char* topic, byte* payload, unsigned int length)
  35. {
  36.  
  37. } //end callback
  38.  
  39. void reconnect() {
  40. // Loop until we're reconnected
  41. while (!client.connected())
  42. {
  43. Serial.print("Attempting MQTT connection...");
  44. // Create a random client ID
  45. String clientId = "ESP8266Client-";
  46. clientId += String(random(0xffff), HEX);
  47. // Attempt to connect
  48. //if you MQTT broker has clientID,username and password
  49. //please change following line to if (client.connect(clientId,userName,passWord))
  50. if (client.connect(clientId.c_str()))
  51. {
  52. Serial.println("connected");
  53. //once connected to MQTT broker, subscribe command if any
  54. client.subscribe("home/office/sonoff2");
  55. } else {
  56. Serial.print("failed, rc=");
  57. Serial.print(client.state());
  58. Serial.println(" try again in 5 seconds");
  59. // Wait 5 seconds before retrying
  60. delay(5000);
  61. }
  62. }
  63. } //end reconnect()
  64.  
  65. void setup() {
  66. Serial.begin(115200);
  67. setup_wifi();
  68. client.setServer(mqtt_server, 1883);
  69. client.setCallback(callback);
  70. pinMode(2,INPUT);
  71. }
  72.  
  73. void loop() {
  74. if (!client.connected()) {
  75. reconnect();
  76. }
  77. client.loop();
  78. long now = millis();
  79. int status;
  80. //??0.5s??????
  81. if (now - lastMsg > 500) {
  82. lastMsg = now;
  83. status=digitalRead(BUTTON_PIN);
  84. String msg="";
  85. if(status==HIGH)
  86. {
  87. msg= msg+ "on";
  88. char message[58];
  89. msg.toCharArray(message,58);
  90. Serial.println(message);
  91. //publish sensor data to MQTT broker
  92. client.publish("home/office/sonoff2/on", message);
  93. }
  94. else
  95. {
  96. msg= msg+ "off";
  97. char message[58];
  98. msg.toCharArray(message,58);
  99. Serial.println(message);
  100. //publish sensor data to MQTT broker
  101. client.publish("/home/office/sonoff2/off", message);
  102. }
  103. }
  104.  
  105. }
RAW Paste Data