Advertisement
zykes

Untitled

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