zykes

Untitled

Dec 9th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <WiFiClientSecure.h>
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <PubSubClient.h>
  5.  
  6. #include "mqtt_crt.h"
  7. #include "mqtt_key.h"
  8.  
  9. #include "time.h"
  10.  
  11. const char* ssid = "x";
  12. const char* password = "x";
  13. const char* mqtt_server = "192.168.254.8";
  14. const char* my_name = "esp-ctrl-5ccf7f0167f0";
  15.  
  16. const uint8_t IP[4] = {192, 168, 254, 15};
  17. const uint8_t GATEWAY[4] = {192, 168, 254, 1};
  18. const uint8_t SUBNET[4] = {255, 255, 255, 0};
  19. const uint8_t DNS[4] = {8, 8, 8, 8};
  20.  
  21. int timezone = 0;
  22. int dst = 0;
  23.  
  24. WiFiClientSecure espClient;
  25. PubSubClient client(espClient);
  26. long lastMsg = 0;
  27. char msg[50];
  28. int value = 0;
  29.  
  30. void callback(char* topic, byte* payload, unsigned int length);
  31.  
  32. void setup() {
  33. Serial.begin(115200);
  34. Serial.setDebugOutput(true);
  35.  
  36. WiFi.mode(WIFI_STA);
  37.  
  38. WiFi.hostname(my_name);
  39. WiFi.config(IP, GATEWAY, SUBNET, DNS);
  40. WiFi.begin(ssid, password);
  41.  
  42. Serial.println("\nConnecting to WiFi");
  43. while (WiFi.status() != WL_CONNECTED) {
  44. Serial.print(".");
  45. delay(1000);
  46. }
  47.  
  48. configTime(timezone, dst, "pool.ntp.org", "time.nist.gov");
  49. Serial.println("\nWaiting for time");
  50. while (!time(nullptr)) {
  51. Serial.print(".");
  52. delay(100);
  53. }
  54. Serial.println("");
  55.  
  56.  
  57. espClient.setCertificate(esp_crt, esp_crt_len);
  58. espClient.setPrivateKey(esp_key, esp_key_len);
  59.  
  60. client.setServer(mqtt_server, 1883);
  61. client.setCallback(callback);
  62. }
  63.  
  64. void callback(char* topic, byte* payload, unsigned int length) {
  65. Serial.print("Message arrived [");
  66. Serial.print(topic);
  67. Serial.print("] ");
  68. for (int i = 0; i < length; i++) {
  69. Serial.print((char)payload[i]);
  70. }
  71. Serial.println();
  72.  
  73. // Switch on the LED if an 1 was received as first character
  74. if ((char)payload[0] == '1') {
  75. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  76. // but actually the LED is on; this is because
  77. // it is acive low on the ESP-01)
  78. } else {
  79. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  80. }
  81.  
  82. }
  83.  
  84. void reconnect() {
  85. // Loop until we're reconnected
  86. while (!client.connected()) {
  87. Serial.print("Attempting MQTT connection...");
  88. // Attempt to connect
  89. if (client.connect("ESP8266Client")) {
  90. Serial.println("connected");
  91. // Once connected, publish an announcement...
  92. client.publish("outTopic", "hello world");
  93. // ... and resubscribe
  94. client.subscribe("inTopic");
  95. } else {
  96. Serial.print("failed, rc=");
  97. Serial.print(client.state());
  98. Serial.println(" try again in 5 seconds");
  99. // Wait 5 seconds before retrying
  100. delay(5000);
  101. }
  102. }
  103. }
  104.  
  105. void loop() {
  106. time_t n = time(nullptr);
  107. Serial.println(ctime(&n));
  108. delay(1000);
  109.  
  110. if (!client.connected()) {
  111. reconnect();
  112. }
  113. client.loop();
  114.  
  115. long now = millis();
  116. if (now - lastMsg > 2000) {
  117. lastMsg = now;
  118. ++value;
  119. snprintf (msg, 75, "hello world #%ld", value);
  120. Serial.print("Publish message: ");
  121. Serial.println(msg);
  122. client.publish("outTopic", msg);
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment