Advertisement
Guest User

HTTP Arduino

a guest
Jan 19th, 2018
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. /**
  2. Carriots - Sending Streams to Carriots from a Lolin 32 (NodeMcu v3)
  3. when the LDR changes status between Light/Night and sending SMS/Email
  4. using a Listener in Carriots
  5. **/
  6.  
  7. #include <ESP8266WiFi.h>
  8.  
  9. // WiFi Setup
  10. const char* ssid = "carriotsLAB"; // Wifi SSID
  11. const char* password = "carriotsLAB052017"; // Wifi Password
  12. WiFiClient client_wifi;
  13.  
  14. // Lolin LED Setup
  15. const int LED_pin = 16; // LED Pin (16)
  16. #define LED_BUILTIN 2 // Internal Led Pin of the NodeMcu v3
  17.  
  18. // Lolin LDR Setup
  19. const int serial_seed = 115200; // Serial Bus Speed
  20. const unsigned int LDR_pin = A0; // LDR Pin (14)
  21. const int LDR_flag = 25; // Control Flag
  22. int LDR_value = 0; // Current Value of LDR
  23. String LDR_old = "OFF"; // Previous Status of LDR
  24. String LDR_new = "OFF"; // Current Status of LDR
  25.  
  26. // Carriots Setup
  27. IPAddress server(82, 223, 244, 60); // HTTP IP Server
  28. const int http_server_port = 80; // HTTP Port Server
  29. const String apikey = "0f63ed320ad494f2e096a088b8aafc07a4ddd8e897ea84324f5d9db9aee592be"; // Carriots API_Key
  30. const String device = "defaultDevice@edumrtnz.edumrtnz"; // Carriots Device
  31. const String at = "now"; // Stream Date
  32. const String protocol = "v2"; // Stream Protocol
  33. String data = ""; // Stream Data
  34.  
  35. const short delayTime = 2500;
  36.  
  37. void setup()
  38. {
  39.  
  40. Serial.begin(serial_seed);
  41.  
  42. // LED PIN setup
  43. pinMode(LED_pin, OUTPUT);
  44. pinMode(LED_BUILTIN, OUTPUT);
  45.  
  46. // Start Wifi
  47. conectWifi(ssid, password);
  48. printWifiStatus();
  49.  
  50. }
  51.  
  52. void loop()
  53. {
  54.  
  55. // Connection Status Evaluation
  56. evalWifiStatusConnected();
  57. evalHTTPConnection(client_wifi, server, http_server_port);
  58.  
  59. // Reading and Evaluation of LDR
  60. LDR_value = analogRead(LDR_pin);
  61. printLDR_value();
  62. LDR_new = (LDR_value > LDR_flag) ? "OFF" : "ON";
  63. digitalWrite(LED_pin, (LDR_new == "ON") ? HIGH : LOW);
  64.  
  65. // Sending the Stream
  66. if (LDR_old != LDR_new) {
  67. data = "{\"ldr_value\": \"" + LDR_new + "\"}";
  68. sendStream(client_wifi, apikey, device, at, protocol, data);
  69. LDR_old = LDR_new;
  70. internalLedBlink();
  71. }
  72.  
  73. delay(delayTime);
  74.  
  75. }
  76.  
  77. /**
  78. LDR
  79. **/
  80. void printLDR_value() {
  81.  
  82. Serial.println("LDR VALUE: " + String(LDR_value));
  83.  
  84. }
  85.  
  86. void internalLedBlink () {
  87. digitalWrite(LED_BUILTIN, LOW);
  88. delay(1000);
  89. digitalWrite(LED_BUILTIN, HIGH);
  90. }
  91.  
  92. /**
  93. Stream
  94. **/
  95. void sendStream(WiFiClient &client_wifi, String apikey, String device, String at, String protocol, String data) {
  96. Serial.println();
  97. // Build the data field
  98. String json = "{\"protocol\":\"" + protocol + "\",\"device\":\"" + device + "\",\"at\":\"" + at + "\",\"data\":" + data + "}";
  99.  
  100. String post_request = "POST /streams HTTP/1.1\nHost: api.carriots.com\nAccept: application/json\nContent-Type: application/json\nUser-Agent: Arduino-Carriots\ncarriots.apikey: " +
  101. apikey + "\nContent-Length: " + json.length() + "\n\n" + json;
  102. Serial.println(post_request);
  103.  
  104. // HTTP Request
  105. client_wifi.println(post_request);
  106.  
  107. Serial.println();
  108. }
  109.  
  110. /**
  111. Utils
  112. **/
  113. char* to_char_array(String characters) {
  114.  
  115. if (characters.length() != 0) {
  116. char *total_buffer = const_cast<char*>(characters.c_str());
  117. return total_buffer;
  118. }
  119.  
  120. }
  121.  
  122. /**
  123. WIFI
  124. **/
  125. void conectWifi(const char* ssid, const char* password) {
  126.  
  127. // WiFi SSID information to connect
  128. Serial.println();
  129. Serial.println("--------------------------------------------------- [[Trying to Connect to WiFi]] ---------------------------------------------------");
  130.  
  131. // Start the connection
  132. WiFi.begin(ssid, password);
  133.  
  134. // Connecting...
  135. Serial.print("Connecting");
  136. evalWifiStatusConnected();
  137.  
  138. Serial.println("\n\n");
  139.  
  140. }
  141.  
  142. void evalWifiStatusConnected() {
  143.  
  144. // Connecting to Wifi...
  145. while (WiFi.status() != WL_CONNECTED) {
  146. delay(500);
  147. Serial.print(".");
  148. }
  149.  
  150. Serial.println("\nWiFi Connection Status: -CONNECTED-\n");
  151.  
  152. }
  153.  
  154. void printWifiStatus() {
  155.  
  156. Serial.println("\n------------------------------------------------------------ WiFi Status ------------------------------------------------------------");
  157. Serial.println("SSID: " + WiFi.SSID());
  158.  
  159. IPAddress ip = WiFi.localIP();
  160. Serial.println("IP Address: " + ip.toString());
  161.  
  162. // WiFi Signal Strength
  163. long rssi = WiFi.RSSI();
  164. Serial.print("Signal strength (RSSI): ");
  165. Serial.print(rssi);
  166. Serial.println(" dBm");
  167. Serial.println("-------------------------------------------------------------------------------------------------------------------------------------\n");
  168.  
  169. }
  170.  
  171. /**
  172. HTTP
  173. **/
  174. void evalHTTPConnection (WiFiClient &client_wifi, IPAddress server, int port) {
  175.  
  176. // Try Wifi Connection
  177. if (!client_wifi.connect(server, port)) {
  178. Serial.println("--------------------------------------------------------- Connection Failed ---------------------------------------------------------\n");
  179. return;
  180. }
  181.  
  182. }
  183.  
  184. /**
  185. Listener Code
  186.  
  187. - Name:
  188.  
  189. listenerLDRMailSMS
  190.  
  191. - If expression:
  192.  
  193. context.data.ldr_value == "ON" || context.data.ldr_value == "OFF"
  194.  
  195. - Then expression:
  196.  
  197. import com.carriots.sdk.utils.Email
  198. import com.carriots.sdk.utils.Sms
  199.  
  200. def ldr_value = context.data.ldr_value
  201.  
  202. new Email([to: "example@mail.com", subject: "LDR - Dayligth " + ldr_value, message: "Lights are " + ldr_value]).send()
  203.  
  204. new Sms([to: "0034123456789", message: "Lights are " + ldr_value]).send()
  205.  
  206. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement