From c24e6edea1ab0e3858c590472656ec8d1041b168 Mon Sep 17 00:00:00 2001 From: Rasika Date: Tue, 26 May 2015 15:40:32 +0530 Subject: [PATCH] changes to the jaggery app --- .../jaggeryapps/iot/api/device-api.jag | 3 +- .../iot/units/firealarm/firealarm.hbs | 15 +- .../src/sketches/firealarm/Connect.ino | 30 ++-- .../src/sketches/firealarm/FireAlarmAgent.h | 13 +- .../src/sketches/firealarm/FireAlarmAgent.ino | 53 +++---- .../src/sketches/firealarm/PollServer.ino | 41 ++++- .../src/sketches/firealarm/PushData.ino | 51 ++++++- .../src/sketches/firealarm/sketch.properties | 1 + .../src/sketches/firealarm_wifi/Connect.ino | 98 ++++++++++++ .../firealarm_wifi/FireAlarmWifiAgent.h | 41 +++++ .../firealarm_wifi/FireAlarmWifiAgent.ino | 141 ++++++++++++++++++ .../sketches/firealarm_wifi/PollServer.ino | 104 +++++++++++++ .../src/sketches/firealarm_wifi/PushData.ino | 141 ++++++++++++++++++ .../sketches/firealarm_wifi/sketch.properties | 1 + 14 files changed, 671 insertions(+), 62 deletions(-) mode change 100755 => 100644 modules/distribution/src/sketches/firealarm/Connect.ino mode change 100755 => 100644 modules/distribution/src/sketches/firealarm/FireAlarmAgent.h mode change 100755 => 100644 modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino mode change 100755 => 100644 modules/distribution/src/sketches/firealarm/PollServer.ino mode change 100755 => 100644 modules/distribution/src/sketches/firealarm/PushData.ino create mode 100644 modules/distribution/src/sketches/firealarm/sketch.properties create mode 100644 modules/distribution/src/sketches/firealarm_wifi/Connect.ino create mode 100644 modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h create mode 100644 modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino create mode 100644 modules/distribution/src/sketches/firealarm_wifi/PollServer.ino create mode 100644 modules/distribution/src/sketches/firealarm_wifi/PushData.ino create mode 100644 modules/distribution/src/sketches/firealarm_wifi/sketch.properties diff --git a/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag b/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag index dd2d39e4..2eb45651 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iot/api/device-api.jag @@ -41,7 +41,8 @@ if (uriMatcher.match("/{context}/api/device/sketch/")) { exit(); } - serviceUrl = "http://localhost:9763/WSO2ConnectedDevices-1.0.0/FireAlarmDeviceManager/DownloadSketch"; + serviceUrl = "http://localhost:9763/WSO2ConnectedDevices-1.0" + + ".0/FireAlarmDeviceManager/downloadSketch"; response.sendRedirect(serviceUrl + "?type="+sketchType+"&owner="+user.username); exit(); } diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/firealarm/firealarm.hbs b/modules/distribution/src/repository/jaggeryapps/iot/units/firealarm/firealarm.hbs index fa35f8ad..ce2c1544 100644 --- a/modules/distribution/src/repository/jaggeryapps/iot/units/firealarm/firealarm.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/firealarm/firealarm.hbs @@ -27,11 +27,20 @@ Arduino Ethernet / WiFi Shield

-
+ + +
+
- - + diff --git a/modules/distribution/src/sketches/firealarm/Connect.ino b/modules/distribution/src/sketches/firealarm/Connect.ino old mode 100755 new mode 100644 index b6e8bed6..808fbe7e --- a/modules/distribution/src/sketches/firealarm/Connect.ino +++ b/modules/distribution/src/sketches/firealarm/Connect.ino @@ -9,32 +9,34 @@ byte server[4] = { 192, 168, 1, 216 }; String connecting = "connecting.... "; void connectHttp() { -// Serial.println("-------------------------------"); + if(DEBUG) Serial.println("-------------------------------"); Ethernet.begin(mac, deviceIP, dns2, gateway, subnet); delay(2000); -// Serial.print("My IP: "); -// Serial.println(Ethernet.localIP()); + if(DEBUG) { + Serial.print("My IP: "); + Serial.println(Ethernet.localIP()); + } connecting += httpClient.connect(server, SERVICE_PORT); delay(2000); -// Serial.println(connecting); + if(DEBUG) Serial.println(connecting); if (httpClient.connected()) { -// Serial.println("connected"); + if(DEBUG) Serial.println("connected"); } else { -// Serial.println("connection failed"); + if(DEBUG) Serial.println("connection failed"); while(!httpClient.connected()){ -// Serial.println("retrying to connect......"); + if(DEBUG) Serial.println("retrying to connect......"); httpClient.connect(server, SERVICE_PORT); delay(2000); } -// Serial.println("connected to server!"); + if(DEBUG) Serial.println("connected to server!"); } -// Serial.println("-------------------------------"); + if(DEBUG) Serial.println("-------------------------------"); } @@ -43,7 +45,7 @@ void setupResource(){ String port = String(SERVICE_PORT); host = "Host: " + hostIP + ":" + port; -// Serial.println(host); + if(DEBUG) Serial.println(host); jsonPayLoad = String(OWNER_JSON); jsonPayLoad += String(DEVICE_OWNER); @@ -51,9 +53,11 @@ void setupResource(){ jsonPayLoad += String(DEVICE_ID); jsonPayLoad += String(REPLY_JSON); -// Serial.print("JSON Payload: "); -// Serial.println(jsonPayLoad); -// Serial.println("-------------------------------"); + if(DEBUG) { + Serial.print("JSON Payload: "); + Serial.println(jsonPayLoad); + Serial.println("-------------------------------"); + } } diff --git a/modules/distribution/src/sketches/firealarm/FireAlarmAgent.h b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.h old mode 100755 new mode 100644 index c34b118d..f32c3eff --- a/modules/distribution/src/sketches/firealarm/FireAlarmAgent.h +++ b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.h @@ -1,4 +1,3 @@ -#[[ #ifndef FireAlarmAgent_H #define FireAlarmAgent_H @@ -13,11 +12,11 @@ #define HTTP_VERSION "HTTP/1.1" #define HTTP_CONTENT_TYPE "Content-Type: application/json" #define HTTP_CONTENT_LEN "Content-Length: " + +#define DEVICE_OWNER "${DEVICE_OWNER}" #define DEVICE_TYPE "FireAlarm" -]]# -\#define DEVICE_OWNER "${DEVICE_OWNER}" -\#define DEVICE_ID "${DEVICE_ID}" -#[[ +#define DEVICE_ID "${DEVICE_ID}" + #define PUSH_ALARM_DATA "pushalarmdata" #define READ_CONTROLS "readcontrols/" #define REPLY "reply" @@ -40,8 +39,10 @@ #define BULB_PIN 4 #define FAN_PIN 5 +#define DEBUG false #define POLL_INTERVAL 1000 + #endif -]]# + diff --git a/modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino old mode 100755 new mode 100644 index 1e028a5d..e3d2c25d --- a/modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino +++ b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino @@ -10,9 +10,8 @@ int analogPins[] = { 0, 1, 2, 3, 4, 5 }; EthernetClient httpClient; String host, jsonPayLoad, replyMsg; - void setup() { -// Serial.begin(9600); + if(DEBUG) Serial.begin(9600); pinMode(BULB_PIN, OUTPUT); pinMode(FAN_PIN, OUTPUT); connectHttp(); @@ -41,12 +40,11 @@ void loop() { replyMsg = "Bulb was switched " + switchFan(); } } - -// delay(POLL_INTERVAL); - } else { -// Serial.println("client not found..."); -// Serial.println("disconnecting."); + if(DEBUG) { + Serial.println("client not found..."); + Serial.println("disconnecting."); + } httpClient.stop(); connectHttp(); @@ -91,40 +89,45 @@ String switchFan() { double getTemperature(){ dht DHT; -// Serial.println("-------------------------------"); -// Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)"); -// Serial.print("DHT11, \t"); + if(DEBUG) { + Serial.println("-------------------------------"); + Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)"); + Serial.print("DHT11, \t"); + } + int chk = DHT.read11(TEMP_PIN); switch (chk) { case DHTLIB_OK: -// Serial.print("OK,\t"); + if(DEBUG) Serial.print("OK,\t"); break; case DHTLIB_ERROR_CHECKSUM: -// Serial.print("Checksum error,\t"); + if(DEBUG) Serial.print("Checksum error,\t"); break; case DHTLIB_ERROR_TIMEOUT: -// Serial.print("Time out error,\t"); + if(DEBUG) Serial.print("Time out error,\t"); break; case DHTLIB_ERROR_CONNECT: -// Serial.print("Connect error,\t"); - break; + if(DEBUG) Serial.print("Connect error,\t"); + break; case DHTLIB_ERROR_ACK_L: -// Serial.print("Ack Low error,\t"); - break; + if(DEBUG) Serial.print("Ack Low error,\t"); + break; case DHTLIB_ERROR_ACK_H: -// Serial.print("Ack High error,\t"); - break; + if(DEBUG) Serial.print("Ack High error,\t"); + break; default: -// Serial.print("Unknown error,\t"); + if(DEBUG) Serial.print("Unknown error,\t"); break; } // DISPLAY DATA -// Serial.print("\t"); -// Serial.print(DHT.temperature, 1); -// Serial.print(",\t\t"); -// Serial.println(DHT.humidity, 1); -// Serial.println("-------------------------------"); + if(DEBUG) { + Serial.print("\t"); + Serial.print(DHT.temperature, 1); + Serial.print(",\t\t"); + Serial.println(DHT.humidity, 1); + Serial.println("-------------------------------"); + } return DHT.temperature; } diff --git a/modules/distribution/src/sketches/firealarm/PollServer.ino b/modules/distribution/src/sketches/firealarm/PollServer.ino old mode 100755 new mode 100644 index 1d279821..fc36ce60 --- a/modules/distribution/src/sketches/firealarm/PollServer.ino +++ b/modules/distribution/src/sketches/firealarm/PollServer.ino @@ -1,12 +1,21 @@ String readControls() { String responseMsg; String resource = " " + String(SERVICE_EPOINT) + String(READ_CONTROLS) + String(DEVICE_OWNER) + "/" + String(DEVICE_ID) + " "; - + httpClient.print(HTTP_GET); httpClient.print(resource); httpClient.println(HTTP_VERSION); httpClient.println(host); httpClient.println(); + + if(DEBUG) { + Serial.print(HTTP_GET); + Serial.print(resource); + Serial.println(HTTP_VERSION); + Serial.println(host); + Serial.println(); + } + delay(1000); while (httpClient.available()) { @@ -14,9 +23,11 @@ String readControls() { responseMsg += response; } -// Serial.print(responseMsg); -// Serial.println(); -// Serial.println("-------------------------------"); + if(DEBUG) { + Serial.print(responseMsg); + Serial.println(); + Serial.println("-------------------------------"); + } delay(1000); return responseMsg; } @@ -35,15 +46,31 @@ void reply(String replyMsg) { httpClient.println(); httpClient.println(payLoad); httpClient.println(); + + if(DEBUG) { + Serial.print(HTTP_POST); + Serial.print(resource); + Serial.println(HTTP_VERSION); + Serial.println(host); + Serial.println(HTTP_CONTENT_TYPE); + Serial.print(HTTP_CONTENT_LEN); + Serial.println(payLoad.length()); + Serial.println(); + Serial.println(payLoad); + Serial.println(); + } + delay(1000); while (httpClient.available()) { char response = httpClient.read(); -// Serial.print(response); + if(DEBUG) Serial.print(response); } -// Serial.println(); -// Serial.println("-------------------------------"); + if(DEBUG) { + Serial.println(); + Serial.println("-------------------------------"); + } delay(1000); } diff --git a/modules/distribution/src/sketches/firealarm/PushData.ino b/modules/distribution/src/sketches/firealarm/PushData.ino old mode 100755 new mode 100644 index 340feea0..10825659 --- a/modules/distribution/src/sketches/firealarm/PushData.ino +++ b/modules/distribution/src/sketches/firealarm/PushData.ino @@ -17,6 +17,9 @@ void pushDigitalPinData(){ } payLoad = payLoad + String(END_JSON); + + if(DEBUG) Serial.println(payLoad); + httpClient.print(HTTP_POST); httpClient.print(resource); httpClient.println(HTTP_VERSION); @@ -27,15 +30,31 @@ void pushDigitalPinData(){ httpClient.println(); httpClient.println(payLoad); httpClient.println(); + + if(DEBUG) { + Serial.print(HTTP_POST); + Serial.print(resource); + Serial.println(HTTP_VERSION); + Serial.println(host); + Serial.println(HTTP_CONTENT_TYPE); + Serial.print(HTTP_CONTENT_LEN); + Serial.println(payLoad.length()); + Serial.println(); + Serial.println(payLoad); + Serial.println(); + } + delay(1000); while (httpClient.available()) { char response = httpClient.read(); -// Serial.print(response); + if(DEBUG) Serial.print(response); } -// Serial.println(); -// Serial.println("-------------------------------"); + if(DEBUG) { + Serial.println(); + Serial.println("-------------------------------"); + } delay(1000); } } @@ -50,7 +69,9 @@ void pushAnalogPinData(){ payLoad = payLoad + String(KEY_JSON) + getDataType(analogPins[pin]); payLoad = payLoad + String(VALUE_JSON) + analogRead(analogPins[pin]); payLoad = payLoad + String(END_JSON); - + + if(DEBUG) Serial.println(payLoad); + httpClient.print(HTTP_POST); httpClient.print(resource); httpClient.println(HTTP_VERSION); @@ -61,15 +82,31 @@ void pushAnalogPinData(){ httpClient.println(); httpClient.println(payLoad); httpClient.println(); + + if(DEBUG) { + Serial.print(HTTP_POST); + Serial.print(resource); + Serial.println(HTTP_VERSION); + Serial.println(host); + Serial.println(HTTP_CONTENT_TYPE); + Serial.print(HTTP_CONTENT_LEN); + Serial.println(payLoad.length()); + Serial.println(); + Serial.println(payLoad); + Serial.println(); + } + delay(1000); while (httpClient.available()) { char response = httpClient.read(); -// Serial.print(response); + if(DEBUG) Serial.print(response); } -// Serial.println(); -// Serial.println("-------------------------------"); + if(DEBUG) { + Serial.println(); + Serial.println("-------------------------------"); + } delay(1000); } } diff --git a/modules/distribution/src/sketches/firealarm/sketch.properties b/modules/distribution/src/sketches/firealarm/sketch.properties new file mode 100644 index 00000000..37a58821 --- /dev/null +++ b/modules/distribution/src/sketches/firealarm/sketch.properties @@ -0,0 +1 @@ +templates=FireAlarmAgent.h \ No newline at end of file diff --git a/modules/distribution/src/sketches/firealarm_wifi/Connect.ino b/modules/distribution/src/sketches/firealarm_wifi/Connect.ino new file mode 100644 index 00000000..b26c86b2 --- /dev/null +++ b/modules/distribution/src/sketches/firealarm_wifi/Connect.ino @@ -0,0 +1,98 @@ +byte mac[6] = { 0x90, 0xA2, 0xDA, 0x0D, 0x30, 0xD7}; //mac - 90a2da0d30d7 +byte dns2[] = { 8, 8, 8, 8 }; +byte subnet[] = { 255, 255, 255, 0 }; +byte gateway[] = { 192, 168, 1, 1 }; +byte deviceIP[4] = { 192, 168, 1, 219 }; +byte server[4] = { 192, 168, 1, 216 }; +//byte server[4] = { 207, 58, 139, 247 }; + +uint32_t ip, ddns, ssubnet, ggateway, sserver; +String connecting = "connecting.... "; + +void connectHttp() { + /* Initialise the module */ + if(true) Serial.println(F("\nInitializing...")); + if (!cc3000.begin()) + { + if(true) Serial.println(F("Couldn't begin()! Check your wiring?")); + while(1); + } + + ip = cc3000.IP2U32(deviceIP[0], deviceIP[1], deviceIP[2], deviceIP[3]); + ddns = cc3000.IP2U32(dns2[0], dns2[1], dns2[2], dns2[3]); + ssubnet = cc3000.IP2U32(subnet[0], subnet[1], subnet[2], subnet[3]); + ggateway = cc3000.IP2U32(gateway[0], gateway[1], gateway[2], gateway[3]); + sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); + + cc3000.setStaticIPAddress(ip, ssubnet, ggateway, ddns); + + if(true) { + Serial.print(F("\nAttempting to connect to ")); + Serial.println(WLAN_SSID); + } + + if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { + if(true) Serial.println(F("Failed!")); + while(1); + } + + if(true) Serial.println(F("Connected to Wifi network!")); + + httpClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT + if (httpClient.connected()) { + if(true) Serial.println("Connected to server"); + } else { + if(true) Serial.println(F("Connection failed")); + + while(!httpClient.connected()){ + if(true) Serial.println("retrying to connect......"); + httpClient = cc3000.connectTCP(sserver, SERVICE_PORT); + delay(1000); + } + } + + if(true) Serial.println(F("-------------------------------------")); +} + + +void setupResource(){ + String hostIP = getHostIP(server); + String port = String(SERVICE_PORT); + + host = "Host: " + hostIP + ":" + port; + if(DEBUG) Serial.println(host); + + jsonPayLoad = "{\"owner\":\""; + jsonPayLoad += String(DEVICE_OWNER); + jsonPayLoad += "\",\"deviceId\":\""; + jsonPayLoad += String(DEVICE_ID); + jsonPayLoad += "\",\"replyMessage\":\""; + + if(DEBUG) { + Serial.print("JSON Payload: "); + Serial.println(jsonPayLoad); + Serial.println("-------------------------------"); + } +} + + +String getMyIP(byte deviceIP[4]){ + String myIP = String(deviceIP[0]); + + for ( int index = 1; index < 4; index++) { + myIP += "." + String(deviceIP[index]); + } + + return myIP; +} + + +String getHostIP(byte server[4]){ + String hostIP = String(server[0]); + + for ( int index = 1; index < 4; index++) { + hostIP += "." + String(server[index]); + } + + return hostIP; +} diff --git a/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h new file mode 100644 index 00000000..9640f819 --- /dev/null +++ b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h @@ -0,0 +1,41 @@ +#ifndef FireAlarmWifiAgent_H +#define FireAlarmWifiAgent_H + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +// These are the interrupt and control pins +#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin! +// These can be any two pins +#define ADAFRUIT_CC3000_VBAT 5 +#define ADAFRUIT_CC3000_CS 10 + +#define WLAN_SSID "Dialog 4G" // cannot be longer than 32 characters! +#define WLAN_PASS "FA09C543" +#define WLAN_SECURITY WLAN_SEC_WPA2 + // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 +#define IDLE_TIMEOUT_MS 3000 + +#define DEVICE_OWNER "${DEVICE_OWNER}" +#define DEVICE_TYPE "FireAlarm" +#define DEVICE_ID "${DEVICE_ID}" + +#define SERVICE_PORT 9763 +#define SERVICE_EPOINT "/WSO2ConnectedDevices/FireAlarmController/" + // pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""} + // readcontrols/{owner}/{deviceId} + // reply - application/json - {"owner":"","deviceId":"","replyMessage":""} + +#define TEMP_PIN 6 +#define BULB_PIN 7 +#define FAN_PIN 8 + +#define POLL_INTERVAL 1000 +#define DEBUG false + +#endif + + diff --git a/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino new file mode 100644 index 00000000..85c7c068 --- /dev/null +++ b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino @@ -0,0 +1,141 @@ +#include "FireAlarmWifiAgent.h" + +#include +//#include +//#include +//#include "utility/debug.h" +#include +#include "dht.h" + +int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN }; +int analogPins[] = { 0, 1, 2, 3, 4, 5 }; + +Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, + SPI_CLOCK_DIVIDER); // you can change this clock speed + +Adafruit_CC3000_Client httpClient; +String host, jsonPayLoad, replyMsg; + +void setup() { + if(true) Serial.begin(115200); + pinMode(BULB_PIN, OUTPUT); + pinMode(FAN_PIN, OUTPUT); + connectHttp(); + setupResource(); +} + +void loop() { + if (httpClient.connected()) { + pushDigitalPinData(); + + delay(POLL_INTERVAL); + + String responseMsg = readControls(); + int index = responseMsg.lastIndexOf(":"); + int newLine = responseMsg.lastIndexOf("\n"); + String subStrn = responseMsg.substring(index + 1); + + if (subStrn.equals("IN")) { + responseMsg = responseMsg.substring(newLine + 1, index); + if (responseMsg.equals("TEMP")) { + int temperature = (uint8_t)getTemperature(); + replyMsg = "Temperature is " + String(temperature) + " C"; + reply(); + } else if (responseMsg.equals("BULB")) { + replyMsg = "Bulb was switched " + switchBulb(); + } else if (responseMsg.equals("FAN")) { + replyMsg = "Buzzer was switched " + switchFan(); + } + } + } else { + if(DEBUG) { + Serial.println("client not found..."); + Serial.println("disconnecting."); + } + + httpClient.stop(); + connectHttp(); + + } +} + + +String getDataType(int pin){ + switch(pin){ + case TEMP_PIN: + return "Temperature"; + case BULB_PIN: + return "Bulb"; + case FAN_PIN: + return "Fan"; + default: + return String(pin); + } + +} + +String switchBulb() { + if (digitalRead(BULB_PIN) == HIGH) { + digitalWrite(BULB_PIN, LOW); + return "OFF"; + } else { + digitalWrite(BULB_PIN, HIGH); + return "ON"; + } +} + +String switchFan() { + if (digitalRead(FAN_PIN) == HIGH) { + digitalWrite(FAN_PIN, LOW); + return "OFF"; + } else { + digitalWrite(FAN_PIN, HIGH); + return "ON"; + } +} + + +double getTemperature(){ + dht DHT; + + if(DEBUG) { + Serial.println("-------------------------------"); + Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)"); + Serial.print("DHT11, \t"); + } + int chk = DHT.read11(TEMP_PIN); + switch (chk) + { + case DHTLIB_OK: + if(DEBUG) Serial.print("OK,\t"); + break; + case DHTLIB_ERROR_CHECKSUM: + if(DEBUG) Serial.print("Checksum error,\t"); + break; + case DHTLIB_ERROR_TIMEOUT: + if(DEBUG) Serial.print("Time out error,\t"); + break; + case DHTLIB_ERROR_CONNECT: + if(DEBUG) Serial.print("Connect error,\t"); + break; + case DHTLIB_ERROR_ACK_L: + if(DEBUG) Serial.print("Ack Low error,\t"); + break; + case DHTLIB_ERROR_ACK_H: + if(DEBUG) Serial.print("Ack High error,\t"); + break; + default: + if(DEBUG) Serial.print("Unknown error,\t"); + break; + } + + // DISPLAY DATA + if(DEBUG) { + Serial.print("\t"); + Serial.print(DHT.temperature, 1); + Serial.print(",\t\t"); + Serial.println(DHT.humidity, 1); + Serial.println("-------------------------------"); + } + return DHT.temperature; +} diff --git a/modules/distribution/src/sketches/firealarm_wifi/PollServer.ino b/modules/distribution/src/sketches/firealarm_wifi/PollServer.ino new file mode 100644 index 00000000..f403daed --- /dev/null +++ b/modules/distribution/src/sketches/firealarm_wifi/PollServer.ino @@ -0,0 +1,104 @@ +String readControls() { + String responseMsg; + + httpClient.fastrprint(F("GET ")); + httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("readcontrols/")); + httpClient.fastrprint(DEVICE_OWNER); httpClient.fastrprint(F("/")); httpClient.fastrprint(DEVICE_ID); + httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n")); + httpClient.println(); + + delay(1000); + + while (httpClient.available()) { + char response = httpClient.read(); + responseMsg += response; + } + + if(DEBUG) { + Serial.print(responseMsg); + Serial.println(); + Serial.println("-------------------------------"); + } + + delay(1000); + return responseMsg; +} + +void reply() { + String payLoad = replyMsg + "\"}"; + + if(DEBUG) { + Serial.print(jsonPayLoad); Serial.println(payLoad); + } + + httpClient.fastrprint(F("POST ")); + httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("reply")); + httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("Content-Length: ")); + + int payLength = jsonPayLoad.length() + payLoad.length(); + + httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("\n")); + + if(DEBUG) { + Serial.print("POST "); + Serial.print(SERVICE_EPOINT); Serial.print("reply"); + Serial.print(" HTTP/1.1"); Serial.println(); + Serial.print(host); Serial.println(); + Serial.print("Content-Type: application/json"); Serial.println(); + Serial.print("Content-Length: "); + Serial.print(payLength); Serial.println(); + Serial.println(); + } + + int chunkSize = 50; + + for (int i = 0; i < jsonPayLoad.length(); i++) { + if ( (i+1)*chunkSize > jsonPayLoad.length()) { + httpClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); + if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); + i = jsonPayLoad.length(); + } else { + httpClient.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize)); + if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize)); + } + } + + for (int i = 0; i < payLoad.length(); i++) { + if ( (i+1)*chunkSize > payLoad.length()) { + httpClient.print(payLoad.substring(i*chunkSize, payLoad.length())); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length())); + i = payLoad.length(); + } else { + httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + } + } + + httpClient.fastrprint(F("\n")); + if(DEBUG) Serial.println(); + + delay(1000); + + while (httpClient.available()) { + char response = httpClient.read(); + if(DEBUG) Serial.print(response); + } + + if(DEBUG) { + Serial.println(); + Serial.println("-------------------------------"); + } + + payLoad = ""; + delay(1000); + +} + + + + diff --git a/modules/distribution/src/sketches/firealarm_wifi/PushData.ino b/modules/distribution/src/sketches/firealarm_wifi/PushData.ino new file mode 100644 index 00000000..86725e1d --- /dev/null +++ b/modules/distribution/src/sketches/firealarm_wifi/PushData.ino @@ -0,0 +1,141 @@ +void pushDigitalPinData(){ + for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) { + String payLoad = jsonPayLoad + "DigitalPinData"; + payLoad = payLoad + "\",\"time\":\"" + "9999"; + payLoad = payLoad + "\",\"key\":\"" + getDataType(digitalPins[pin]); + payLoad = payLoad + "\",\"value\":\""; + + if ( digitalPins[pin] == TEMP_PIN ) { + int temperature = (uint8_t)getTemperature(); + payLoad += temperature; + } else if ( digitalRead(digitalPins[pin]) == HIGH ) { + payLoad += "ON"; + } else if ( digitalRead(digitalPins[pin]) == LOW ) { + payLoad += "OFF"; + } + + payLoad += "\"}"; + + httpClient.fastrprint(F("POST ")); + httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("pushalarmdata")); + httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("Content-Length: ")); + + int payLength = payLoad.length(); + + httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("\n")); + + if(DEBUG) { + Serial.print("POST "); + Serial.print(SERVICE_EPOINT); Serial.print("pushalarmdata"); + Serial.print(" HTTP/1.1"); Serial.println(); + Serial.print(host); Serial.println(); + Serial.print("Content-Type: application/json"); Serial.println(); + Serial.print("Content-Length: "); + Serial.print(payLength); Serial.println(); + Serial.println(); + } + + int chunkSize = 50; + + for (int i = 0; i < payLength; i++) { + if ( (i+1)*chunkSize > payLength) { + httpClient.print(payLoad.substring(i*chunkSize, payLength)); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLength)); + i = payLength; + } else { + httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + } + } + + httpClient.fastrprint(F("\n")); + if(DEBUG) Serial.println(); + + delay(1000); + + while (httpClient.available()) { + char response = httpClient.read(); + if(DEBUG) Serial.print(response); + } + + if(DEBUG) { + Serial.println(); + Serial.println("-------------------------------"); + } + + payLoad = ""; + delay(1000); + } +} + + +void pushAnalogPinData(){ + for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) { + String payLoad = jsonPayLoad + "AnalogPinData"; + payLoad = payLoad + "\",\"time\":\"" + "9999"; + payLoad = payLoad + "\",\"key\":\"" + getDataType(analogPins[pin]); + payLoad = payLoad + "\",\"value\":\"" + analogRead(analogPins[pin]); + payLoad = payLoad + "\"}"; + + httpClient.fastrprint(F("POST ")); + httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("pushalarmdata")); + httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("Content-Length: ")); + + int payLength = payLoad.length(); + + httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n")); + httpClient.fastrprint(F("\n")); + + if(DEBUG) { + Serial.print("POST "); + Serial.print(SERVICE_EPOINT); Serial.print("pushalarmdata"); + Serial.print(" HTTP/1.1"); Serial.println(); + Serial.print(host); Serial.println(); + Serial.print("Content-Type: application/json"); Serial.println(); + Serial.print("Content-Length: "); + Serial.print(payLength); Serial.println(); + Serial.println(); + } + + int chunkSize = 50; + + for (int i = 0; i < payLength; i++) { + if ( (i+1)*chunkSize > payLength) { + httpClient.print(payLoad.substring(i*chunkSize, payLength)); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLength)); + i = payLength; + } else { + httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + } + } + + httpClient.fastrprint(F("\n")); + if(DEBUG) Serial.println(); + + delay(1000); + + while (httpClient.available()) { + char response = httpClient.read(); + if(DEBUG) Serial.print(response); + } + + if(DEBUG) { + Serial.println(); + Serial.println("-------------------------------"); + } + + payLoad = ""; + delay(1000); + } +} + + + diff --git a/modules/distribution/src/sketches/firealarm_wifi/sketch.properties b/modules/distribution/src/sketches/firealarm_wifi/sketch.properties new file mode 100644 index 00000000..bb141ac9 --- /dev/null +++ b/modules/distribution/src/sketches/firealarm_wifi/sketch.properties @@ -0,0 +1 @@ +templates=FireAlarmWifiAgent.h \ No newline at end of file