Guest User

Untitled

a guest
Dec 17th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. // This #include statement was automatically added by the Particle IDE.
  2. #undef MBEDTLS_ECDSA_C
  3.  
  4. // This #include statement was automatically added by the Particle IDE.
  5. #include <MQTT-TLS.h>
  6.  
  7. // This #include statement was automatically added by the Particle IDE.
  8. #include <ArduinoJson.h>
  9. #include "certificate.h"
  10.  
  11. /*
  12. * Sync Settings
  13. *
  14. * Enter a Sync Key & Password, your document unique name,
  15. * and a device name.
  16. */
  17. const char* sync_key = "KYx";
  18. const char* sync_password = "nnnnnn";
  19. const char* sync_document = "sync/docs/BoardLED";
  20. const char* sync_device_name = "jreyes_owl_key";
  21.  
  22. /* LED Setting - on the Electron, the LED is Pin 7 */
  23. const uint8_t LED_PIN = D5;
  24.  
  25. /* Sync server and MQTT setup; you probably don't have to change these. */
  26. char* mqtt_server = "mqtt-sync.us1.twilio.com";
  27. uint16_t mqtt_port = 8883;
  28. const uint16_t maxMQTTpackageSize = 512;
  29.  
  30. void callback(char* topic, byte* payload, unsigned int length);
  31. MQTT client(
  32. mqtt_server,
  33. mqtt_port,
  34. callback
  35. );
  36.  
  37. /*
  38. * Our Twilio Connected Devices message handling callback. This is passed as a
  39. * callback function when we subscribe to the document, and any messages will
  40. * appear here.
  41. */
  42. void callback(char* topic, byte* payload, unsigned int length)
  43. {
  44. std::unique_ptr<char []> msg(new char[length+1]());
  45. memcpy (msg.get(), payload, length);
  46.  
  47. Serial.print("Message arrived on topic "); Serial.println(msg.get());
  48.  
  49. StaticJsonBuffer<maxMQTTpackageSize> jsonBuffer;
  50. JsonObject& root = jsonBuffer.parseObject(msg.get());
  51. const char* led_command_raw = root["led"];
  52. String led_command(led_command_raw);
  53.  
  54. if (led_command == "ON") {
  55. digitalWrite(LED_PIN, HIGH);
  56. Serial.println("LED turned on!");
  57. } else {
  58. digitalWrite(LED_PIN, LOW);
  59. Serial.println("LED turned off!");
  60. }
  61. }
  62.  
  63. /*
  64. * This function connects to Sync via MQTT. We connect using the device keys
  65. * from above, and immediately check the server's certificate fingerprint.
  66. *
  67. * If everything works, we subscribe to the document topic and return.
  68. */
  69. void connect_mqtt()
  70. {
  71.  
  72. // Connect to Sync
  73. Serial.println("Connecting to Sync...");
  74.  
  75. Serial.println(client.connect(
  76. "owl-jarod",
  77. sync_key,
  78. sync_password
  79. ));
  80. if (client.isConnected()) {
  81. client.subscribe(sync_document);
  82. Serial.print("Subscribed to "); Serial.println(sync_document);
  83. }
  84. Serial.print("Client Connected:");
  85. Serial.println(client.isConnected());
  86. }
  87.  
  88. /* In setup, we configure our LED, enabled serial, and connect to Sync */
  89. void setup()
  90. {
  91. pinMode(LED_PIN, OUTPUT);
  92. digitalWrite(LED_PIN, LOW);
  93. Serial.begin(115200);
  94. // Ensure we are talking to Twilio
  95. client.enableTls(root_ca, sizeof(root_ca));
  96.  
  97. // Connect to Sync
  98. Serial.println("Connecting to Sync...");
  99.  
  100. Serial.println(client.connect(
  101. "owl-jarod",
  102. sync_key,
  103. sync_password
  104. ));
  105. if (client.isConnected()) {
  106. client.subscribe(sync_document);
  107. Serial.print("Subscribed to "); Serial.println(sync_document);
  108. }
  109. Serial.print("Client Connected:");
  110. Serial.println(client.isConnected());
  111. }
  112.  
  113. /*
  114. * Our loop constantly checks we are still connected and runs the
  115. * heartbeat/yield function.
  116. */
  117. void loop()
  118. {
  119. if (client.isConnected()) {
  120. Serial.println("Connected Successfully!");
  121. client.loop();
  122. } else {
  123. delay(1000);
  124. connect_mqtt();
  125. }
  126. delay(1000);
  127. // Here's an example of publishing from the Particle Electron.
  128. // Uncomment until the end of the function to send a 'msg' back to Sync
  129. // every 2 minutes!
  130.  
  131. // static uint32_t now = millis();
  132. // if (millis() > now + (2*(1000*60))) {
  133. // Serial.println("Sending 2 minute ON message to Twilio!");
  134. // client.publish(
  135. // sync_document,
  136. // "{\"msg\":\"Ahoy World!\",\"led\":\"ON\"}"
  137. // );
  138. // now = millis();
  139. // }
  140. }
Add Comment
Please, Sign In to add comment