Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Carriots - Sending Streams to Carriots from a Lolin 32 (NodeMcu v3)
- when the LDR changes status between Light/Night and sending SMS/Email
- using a Listener in Carriots
- **/
- #include <ESP8266WiFi.h>
- // WiFi Setup
- const char* ssid = "carriotsLAB"; // Wifi SSID
- const char* password = "carriotsLAB052017"; // Wifi Password
- WiFiClient client_wifi;
- // Lolin LED Setup
- const int LED_pin = 16; // LED Pin (16)
- #define LED_BUILTIN 2 // Internal Led Pin of the NodeMcu v3
- // Lolin LDR Setup
- const int serial_seed = 115200; // Serial Bus Speed
- const unsigned int LDR_pin = A0; // LDR Pin (14)
- const int LDR_flag = 25; // Control Flag
- int LDR_value = 0; // Current Value of LDR
- String LDR_old = "OFF"; // Previous Status of LDR
- String LDR_new = "OFF"; // Current Status of LDR
- // Carriots Setup
- IPAddress server(82, 223, 244, 60); // HTTP IP Server
- const int http_server_port = 80; // HTTP Port Server
- const String apikey = "0f63ed320ad494f2e096a088b8aafc07a4ddd8e897ea84324f5d9db9aee592be"; // Carriots API_Key
- const String device = "defaultDevice@edumrtnz.edumrtnz"; // Carriots Device
- const String at = "now"; // Stream Date
- const String protocol = "v2"; // Stream Protocol
- String data = ""; // Stream Data
- const short delayTime = 2500;
- void setup()
- {
- Serial.begin(serial_seed);
- // LED PIN setup
- pinMode(LED_pin, OUTPUT);
- pinMode(LED_BUILTIN, OUTPUT);
- // Start Wifi
- conectWifi(ssid, password);
- printWifiStatus();
- }
- void loop()
- {
- // Connection Status Evaluation
- evalWifiStatusConnected();
- evalHTTPConnection(client_wifi, server, http_server_port);
- // Reading and Evaluation of LDR
- LDR_value = analogRead(LDR_pin);
- printLDR_value();
- LDR_new = (LDR_value > LDR_flag) ? "OFF" : "ON";
- digitalWrite(LED_pin, (LDR_new == "ON") ? HIGH : LOW);
- // Sending the Stream
- if (LDR_old != LDR_new) {
- data = "{\"ldr_value\": \"" + LDR_new + "\"}";
- sendStream(client_wifi, apikey, device, at, protocol, data);
- LDR_old = LDR_new;
- internalLedBlink();
- }
- delay(delayTime);
- }
- /**
- LDR
- **/
- void printLDR_value() {
- Serial.println("LDR VALUE: " + String(LDR_value));
- }
- void internalLedBlink () {
- digitalWrite(LED_BUILTIN, LOW);
- delay(1000);
- digitalWrite(LED_BUILTIN, HIGH);
- }
- /**
- Stream
- **/
- void sendStream(WiFiClient &client_wifi, String apikey, String device, String at, String protocol, String data) {
- Serial.println();
- // Build the data field
- String json = "{\"protocol\":\"" + protocol + "\",\"device\":\"" + device + "\",\"at\":\"" + at + "\",\"data\":" + data + "}";
- 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: " +
- apikey + "\nContent-Length: " + json.length() + "\n\n" + json;
- Serial.println(post_request);
- // HTTP Request
- client_wifi.println(post_request);
- Serial.println();
- }
- /**
- Utils
- **/
- char* to_char_array(String characters) {
- if (characters.length() != 0) {
- char *total_buffer = const_cast<char*>(characters.c_str());
- return total_buffer;
- }
- }
- /**
- WIFI
- **/
- void conectWifi(const char* ssid, const char* password) {
- // WiFi SSID information to connect
- Serial.println();
- Serial.println("--------------------------------------------------- [[Trying to Connect to WiFi]] ---------------------------------------------------");
- // Start the connection
- WiFi.begin(ssid, password);
- // Connecting...
- Serial.print("Connecting");
- evalWifiStatusConnected();
- Serial.println("\n\n");
- }
- void evalWifiStatusConnected() {
- // Connecting to Wifi...
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nWiFi Connection Status: -CONNECTED-\n");
- }
- void printWifiStatus() {
- Serial.println("\n------------------------------------------------------------ WiFi Status ------------------------------------------------------------");
- Serial.println("SSID: " + WiFi.SSID());
- IPAddress ip = WiFi.localIP();
- Serial.println("IP Address: " + ip.toString());
- // WiFi Signal Strength
- long rssi = WiFi.RSSI();
- Serial.print("Signal strength (RSSI): ");
- Serial.print(rssi);
- Serial.println(" dBm");
- Serial.println("-------------------------------------------------------------------------------------------------------------------------------------\n");
- }
- /**
- HTTP
- **/
- void evalHTTPConnection (WiFiClient &client_wifi, IPAddress server, int port) {
- // Try Wifi Connection
- if (!client_wifi.connect(server, port)) {
- Serial.println("--------------------------------------------------------- Connection Failed ---------------------------------------------------------\n");
- return;
- }
- }
- /**
- Listener Code
- - Name:
- listenerLDRMailSMS
- - If expression:
- context.data.ldr_value == "ON" || context.data.ldr_value == "OFF"
- - Then expression:
- import com.carriots.sdk.utils.Email
- import com.carriots.sdk.utils.Sms
- def ldr_value = context.data.ldr_value
- new Email([to: "example@mail.com", subject: "LDR - Dayligth " + ldr_value, message: "Lights are " + ldr_value]).send()
- new Sms([to: "0034123456789", message: "Lights are " + ldr_value]).send()
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement