From 9f4d2fed4b88c6ad4189af47620c248b95f284d5 Mon Sep 17 00:00:00 2001 From: Rasika Date: Sun, 24 May 2015 20:56:00 +0530 Subject: [PATCH] changes to the sketches --- .../distribution/src/sketches/archives/.zip | Bin 0 -> 3846 bytes .../src/sketches/firealarm/Connect.ino | 63 +++++++-- .../src/sketches/firealarm/FireAlarmAgent.h | 47 +++++++ .../src/sketches/firealarm/FireAlarmAgent.ino | 130 ++++++++++++++++++ .../src/sketches/firealarm/IoTArduinoAgent.h | 39 ------ .../sketches/firealarm/IoTArduinoAgent.ino | 36 ----- .../src/sketches/firealarm/MQTTConnect.ino | 75 ---------- .../src/sketches/firealarm/PollServer.ino | 52 +++++++ .../src/sketches/firealarm/PushData.ino | 107 +++++++------- 9 files changed, 328 insertions(+), 221 deletions(-) create mode 100644 modules/distribution/src/sketches/archives/.zip create mode 100755 modules/distribution/src/sketches/firealarm/FireAlarmAgent.h create mode 100755 modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino delete mode 100755 modules/distribution/src/sketches/firealarm/IoTArduinoAgent.h delete mode 100755 modules/distribution/src/sketches/firealarm/IoTArduinoAgent.ino delete mode 100755 modules/distribution/src/sketches/firealarm/MQTTConnect.ino create mode 100755 modules/distribution/src/sketches/firealarm/PollServer.ino diff --git a/modules/distribution/src/sketches/archives/.zip b/modules/distribution/src/sketches/archives/.zip new file mode 100644 index 0000000000000000000000000000000000000000..c99440ae3586a53951252b665d15ce3858c114fc GIT binary patch literal 3846 zcmaJ^by!sG_8k~{fT0yB0RgFjp+t~whLMo&p*v&%KR`+vQM$WRBnG5K5T#q`ZlnYW z5xB$idwy50-@V^D|GeipXTQ&R_d09uwbfuiED8V~9v;BU5hV|}0etitBKyST3DWtg zjH2xmFvcT2f)_JJ{e}5BCV#-P-6-FEB1$Wgr6;~Dqv#& zoryo;-S#Mc@z@Efpf^GTj6o#QVb9IqAZI*iq{Ha=vcSP_kZv?txFR0rd`26}59+H- zvY9hBRBknn^!Lm3-WGq!Nkb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h!~o^PuTZ66tgFKNB8fg zFX140wamzmXfC_i9Z10IES74z;rVpwR0(OT4PU=JWHso*@I9 zPM{(om16ubldL#Fu+126Sa2kM6}I>BLMvk1U3Y}u9aZsmLc1!?fh=P5Za1xps~D-{ zE>pLzRWDVf0dMIFZui%&QvA{(wW-^*j2I4OTaP)jNT9SC#2Yf91|mo|sPI0&oh05Jg(1MT8Q=?yg zDTT7CQ#^X^nisxz_go+!=8e$@|=v%G>7(u-pd-C>kO~eD#3!XEma?kQ2R1=_4G@ifHpNwV5`&SWGQPzx$ zAI>gc`dXR<4Mw@yr3dEjy8_E?LP_&m-@i$93gX%+UNTNbT|StMYOFTkdh>rCj8`?_EL1`PLa-Sf}j ho2kDp0KZXB@So*C4Tg<#{S+5{rJx(P4$<}9zW@y0RzCm$ literal 0 HcmV?d00001 diff --git a/modules/distribution/src/sketches/firealarm/Connect.ino b/modules/distribution/src/sketches/firealarm/Connect.ino index 892ee01c..b6e8bed6 100755 --- a/modules/distribution/src/sketches/firealarm/Connect.ino +++ b/modules/distribution/src/sketches/firealarm/Connect.ino @@ -9,30 +9,71 @@ byte server[4] = { 192, 168, 1, 216 }; String connecting = "connecting.... "; void connectHttp() { - Serial.println("-------------------------------"); +// Serial.println("-------------------------------"); Ethernet.begin(mac, deviceIP, dns2, gateway, subnet); delay(2000); - Serial.print("My IP: "); - Serial.println(Ethernet.localIP()); +// Serial.print("My IP: "); +// Serial.println(Ethernet.localIP()); connecting += httpClient.connect(server, SERVICE_PORT); delay(2000); - Serial.println(connecting); +// Serial.println(connecting); if (httpClient.connected()) { - Serial.println("connected"); +// Serial.println("connected"); } else { - Serial.println("connection failed"); - Serial.println("retrying to connect......"); - +// Serial.println("connection failed"); + while(!httpClient.connected()){ +// Serial.println("retrying to connect......"); httpClient.connect(server, SERVICE_PORT); delay(2000); } - Serial.println("connected to server!"); +// Serial.println("connected to server!"); + } +// Serial.println("-------------------------------"); +} + + +void setupResource(){ + String hostIP = getHostIP(server); + String port = String(SERVICE_PORT); + + host = "Host: " + hostIP + ":" + port; +// Serial.println(host); + + jsonPayLoad = String(OWNER_JSON); + jsonPayLoad += String(DEVICE_OWNER); + jsonPayLoad += String(DEVICE_ID_JSON); + jsonPayLoad += String(DEVICE_ID); + jsonPayLoad += String(REPLY_JSON); + +// Serial.print("JSON Payload: "); +// Serial.println(jsonPayLoad); +// Serial.println("-------------------------------"); +} + + +String getMyIP(){ + String myIP = ""; + myIP = String(Ethernet.localIP()[0]); + + for ( int index = 1; index < 4; index++) { + myIP += "." + String(Ethernet.localIP()[index]); + } + return myIP; +} + + +String getHostIP(byte server[4]){ + String hostIP = String(server[0]); + + for ( int index = 1; index < 4; index++) { + hostIP += "." + String(server[index]); } - Serial.println("-------------------------------"); -} \ No newline at end of file + + return hostIP; +} diff --git a/modules/distribution/src/sketches/firealarm/FireAlarmAgent.h b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.h new file mode 100755 index 00000000..c34b118d --- /dev/null +++ b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.h @@ -0,0 +1,47 @@ +#[[ +#ifndef FireAlarmAgent_H +#define FireAlarmAgent_H + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#define HTTP_POST "POST" +#define HTTP_GET "GET" +#define HTTP_VERSION "HTTP/1.1" +#define HTTP_CONTENT_TYPE "Content-Type: application/json" +#define HTTP_CONTENT_LEN "Content-Length: " +#define DEVICE_TYPE "FireAlarm" +]]# +\#define DEVICE_OWNER "${DEVICE_OWNER}" +\#define DEVICE_ID "${DEVICE_ID}" +#[[ +#define PUSH_ALARM_DATA "pushalarmdata" +#define READ_CONTROLS "readcontrols/" +#define REPLY "reply" + +#define OWNER_JSON "{\"owner\":\"" +#define DEVICE_ID_JSON "\",\"deviceId\":\"" +#define REPLY_JSON "\",\"replyMessage\":\"" +#define TIME_JSON "\",\"time\":\"" +#define KEY_JSON "\",\"key\":\"" +#define VALUE_JSON "\",\"value\":\"" +#define END_JSON "\"}" + +#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 3 +#define BULB_PIN 4 +#define FAN_PIN 5 + +#define POLL_INTERVAL 1000 + +#endif +]]# + diff --git a/modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino new file mode 100755 index 00000000..1e028a5d --- /dev/null +++ b/modules/distribution/src/sketches/firealarm/FireAlarmAgent.ino @@ -0,0 +1,130 @@ +#include "FireAlarmAgent.h" + +#include +#include +#include "dht.h" + +int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN }; +int analogPins[] = { 0, 1, 2, 3, 4, 5 }; + +EthernetClient httpClient; +String host, jsonPayLoad, replyMsg; + + +void setup() { +// Serial.begin(9600); + 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("TEMPERATURE")) { + replyMsg = "Temperature is " + String(getTemperature()) + "C."; + reply(replyMsg); + } else if (responseMsg.equals("BULB")) { + replyMsg = "Bulb was switched " + switchBulb(); + } else if (responseMsg.equals("FAN")) { + replyMsg = "Bulb was switched " + switchFan(); + } + } + +// delay(POLL_INTERVAL); + + } else { +// 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; +// 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"); + break; + case DHTLIB_ERROR_CHECKSUM: +// Serial.print("Checksum error,\t"); + break; + case DHTLIB_ERROR_TIMEOUT: +// Serial.print("Time out error,\t"); + break; + case DHTLIB_ERROR_CONNECT: +// Serial.print("Connect error,\t"); + break; + case DHTLIB_ERROR_ACK_L: +// Serial.print("Ack Low error,\t"); + break; + case DHTLIB_ERROR_ACK_H: +// Serial.print("Ack High error,\t"); + break; + default: +// 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("-------------------------------"); + return DHT.temperature; +} diff --git a/modules/distribution/src/sketches/firealarm/IoTArduinoAgent.h b/modules/distribution/src/sketches/firealarm/IoTArduinoAgent.h deleted file mode 100755 index e44f9e57..00000000 --- a/modules/distribution/src/sketches/firealarm/IoTArduinoAgent.h +++ /dev/null @@ -1,39 +0,0 @@ -#[[ -#ifndef WSO2ArduinoAgent_H -#define WSO2ArduinoAgent_H - -#if (ARDUINO >= 100) - #include "Arduino.h" -#else - #include "WProgram.h" -#endif - -#define HTTP_METHOD "POST" -#define HTTP_VERSION "HTTP/1.1" - -#define DEVICE_IP "192.168.1.219" -#define DEVICE_TYPE "arduino" -]]# -\#define DEVICE_OWNER "${DEVICE_OWNER}" -\#define DEVICE_ID "${DEVICE_ID}" -#[[ -#define SERVICE_IP "192.168.1.216" -#define SERVICE_PORT 9763 -#define SERVICE_EPOINT "/WSO2ConnectedDevices/DeviceController/pushdata/" - // {ip}/{owner}/{type}/{mac}/90/{key}/{value} - -#define MQTT_PROXY SERVICE_IP -#define MQTT_PORT 1883 -#define MQTT_CLIENTID "wso2:iot:arduino:xxxxxxxx" -#define MQTT_TOPIC "wso2/iot/shabirmean/arduino/xxxxxxxx" - -#define MQTTCLIENT_QOS2 1 -#define PUBLISH_INTERVAL 30000 - -enum IP_TYPE{ - SERVER, - DEVICE -}; - -#endif -]]# diff --git a/modules/distribution/src/sketches/firealarm/IoTArduinoAgent.ino b/modules/distribution/src/sketches/firealarm/IoTArduinoAgent.ino deleted file mode 100755 index d3002c1f..00000000 --- a/modules/distribution/src/sketches/firealarm/IoTArduinoAgent.ino +++ /dev/null @@ -1,36 +0,0 @@ -#include "IoTArduinoAgent.h" - -#include -#include -#include -#include -#include - -EthernetClient httpClient; -int digitalPins[] = { 2, 5, 8, 9, 12 }; -int analogPins[] = { 0, 1, 2, 3, 4, 5 }; - -void setup() { - Serial.begin(9600); - connectHttp(); - setupResource(); - MQTTConnect(); -} - -void loop() { - if (httpClient.connected()) { -// pushDigitalPinData(); -// pushAnalogPinData(); - } else { - Serial.println("client not found..."); - Serial.println("disconnecting."); - httpClient.stop(); -// connectHttp(); -// for(;;) -// ; - } - delay(PUBLISH_INTERVAL); - -} - - diff --git a/modules/distribution/src/sketches/firealarm/MQTTConnect.ino b/modules/distribution/src/sketches/firealarm/MQTTConnect.ino deleted file mode 100755 index 4c4ad924..00000000 --- a/modules/distribution/src/sketches/firealarm/MQTTConnect.ino +++ /dev/null @@ -1,75 +0,0 @@ -void messageArrived(MQTT::MessageData& md); -int arrivedcount = 0; -MQTT::Message message; - -EthernetClient mqtt; // replace by a YunClient if running on a Yun -IPStack mqttIPStack(mqtt); -MQTT::Client mqttClient = MQTT::Client(mqttIPStack); - - -void MQTTConnect() { - int rc = -1; - if (!mqttClient.isConnected()) { - Serial.print("Connecting using Registered mode with clientid : "); - Serial.println(MQTT_CLIENTID); - Serial.print("\tto MQTT Broker : "); - Serial.println(MQTT_PROXY); - Serial.print("\ton topic : "); - Serial.println(MQTT_TOPIC); - while (rc != 0) { - rc = mqttIPStack.connect(MQTT_PROXY, MQTT_PORT); -// rc = mqtt.connect(server, MQTT_PORT); - Serial.print("rc from TCP connect is "); - Serial.println(rc); - } - - Serial.println("MQTT connecting"); - MQTTPacket_connectData options = MQTTPacket_connectData_initializer; - options.MQTTVersion = 3; - options.clientID.cstring = MQTT_CLIENTID; -// options.username.cstring = "admin"; -// options.password.cstring = "admin"; -// options.keepAliveInterval = 10; - rc = -1; - while ((rc = mqttClient.connect(options)) != 0){ - Serial.print("rc from MQTT connect is "); - Serial.println(rc); - } - //unsubscribe the topic, if it had subscribed it before. - Serial.println("MQTT connected"); - mqttClient.unsubscribe(MQTT_TOPIC); - //Try to subscribe for commands - if ((rc = mqttClient.subscribe(MQTT_TOPIC, MQTT::QOS2, messageArrived)) != 0) { - Serial.print("Subscribe failed with return code : "); - Serial.println(rc); - } else { - Serial.println("Subscribed\n"); - } - Serial.println("Subscription tried......"); - Serial.println("Connected successfully\n"); - Serial.println("____________________________________________________________________________"); - } - - -} - - -void messageArrived(MQTT::MessageData& md) -{ - MQTT::Message &message = md.message; - Serial.println("======================================="); - Serial.print("Message "); -// Serial.print(++arrivedcount); - Serial.print(" arrived: qos "); - Serial.print(message.qos); - Serial.print(", retained "); - Serial.print(message.retained); - Serial.print(", dup "); - Serial.print(message.dup); - Serial.print(", packetid "); - Serial.println(message.id); - Serial.print("Payload "); - Serial.println((char*)message.payload); - Serial.println("======================================="); - delay(1000); -} \ No newline at end of file diff --git a/modules/distribution/src/sketches/firealarm/PollServer.ino b/modules/distribution/src/sketches/firealarm/PollServer.ino new file mode 100755 index 00000000..1d279821 --- /dev/null +++ b/modules/distribution/src/sketches/firealarm/PollServer.ino @@ -0,0 +1,52 @@ +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(); + delay(1000); + + while (httpClient.available()) { + char response = httpClient.read(); + responseMsg += response; + } + +// Serial.print(responseMsg); +// Serial.println(); +// Serial.println("-------------------------------"); + delay(1000); + return responseMsg; +} + +void reply(String replyMsg) { + String resource = " " + String(SERVICE_EPOINT) + String(REPLY) + " "; + String payLoad = jsonPayLoad + replyMsg + String(END_JSON); + + httpClient.print(HTTP_POST); + httpClient.print(resource); + httpClient.println(HTTP_VERSION); + httpClient.println(host); + httpClient.println(HTTP_CONTENT_TYPE); + httpClient.print(HTTP_CONTENT_LEN); + httpClient.println(payLoad.length()); + httpClient.println(); + httpClient.println(payLoad); + httpClient.println(); + delay(1000); + + while (httpClient.available()) { + char response = httpClient.read(); +// Serial.print(response); + } + +// Serial.println(); +// Serial.println("-------------------------------"); + delay(1000); +} + + + + diff --git a/modules/distribution/src/sketches/firealarm/PushData.ino b/modules/distribution/src/sketches/firealarm/PushData.ino index 3e7c0e4b..340feea0 100755 --- a/modules/distribution/src/sketches/firealarm/PushData.ino +++ b/modules/distribution/src/sketches/firealarm/PushData.ino @@ -1,91 +1,78 @@ -String resource, tempResource, host; -String myIP = ""; -String hostIP = ""; - -void setupResource(){ - - setHostIP(server); - String port = String(SERVICE_PORT); - - host = "Host: " + hostIP + ":" + port; - Serial.println(host); - - myIP = String(Ethernet.localIP()[0]); - - for ( int index = 1; index < 4; index++) { - myIP += "." + String(Ethernet.localIP()[index]); - } - - resource = String(SERVICE_EPOINT) + myIP + "/" + String(DEVICE_OWNER) + "/" + - String(DEVICE_TYPE) + "/" + String(DEVICE_ID) + "/"; - - resource = resource + 30; - Serial.println(resource); - Serial.println("-------------------------------"); -} - - - -void setHostIP(byte server[4]){ - hostIP = String(server[0]); - - for ( int index = 1; index < 4; index++) { - hostIP += "." + String(server[index]); - } -} - - - void pushDigitalPinData(){ for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) { - tempResource = " " + resource + "/D" + digitalPins[pin] + "/"; + String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " "; - if ( digitalRead(digitalPins[pin]) == HIGH) { - tempResource += "HIGH "; - } else if ( digitalRead(digitalPins[pin]) == LOW) { - tempResource += "LOW "; + String payLoad = jsonPayLoad + "DigitalPinData"; + payLoad = payLoad + String(TIME_JSON) + "9999"; + payLoad = payLoad + String(KEY_JSON) + getDataType(digitalPins[pin]); + payLoad += String(VALUE_JSON); + + + if ( digitalPins[pin] == TEMP_PIN ) { + payLoad += String(getTemperature()); + } else if ( digitalRead(digitalPins[pin]) == HIGH ) { + payLoad += "ON"; + } else if ( digitalRead(digitalPins[pin]) == LOW ) { + payLoad += "OFF"; } - httpClient.print(HTTP_METHOD); - httpClient.print(tempResource); + payLoad = payLoad + String(END_JSON); + httpClient.print(HTTP_POST); + httpClient.print(resource); httpClient.println(HTTP_VERSION); httpClient.println(host); + httpClient.println(HTTP_CONTENT_TYPE); + httpClient.print(HTTP_CONTENT_LEN); + httpClient.println(payLoad.length()); + httpClient.println(); + httpClient.println(payLoad); httpClient.println(); - delay(2000); + delay(1000); while (httpClient.available()) { char response = httpClient.read(); - Serial.print(response); +// Serial.print(response); } - Serial.println(); - Serial.println("-------------------------------"); - tempResource = ""; - delay(2000); +// Serial.println(); +// Serial.println("-------------------------------"); + delay(1000); } } - void pushAnalogPinData(){ for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) { - tempResource = " " + resource + "/A" + analogPins[pin] + "/" + analogRead(analogPins[pin]) + " "; + String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " "; + + String payLoad = jsonPayLoad + "AnalogPinData"; + payLoad = payLoad + String(TIME_JSON) + "9999"; + payLoad = payLoad + String(KEY_JSON) + getDataType(analogPins[pin]); + payLoad = payLoad + String(VALUE_JSON) + analogRead(analogPins[pin]); + payLoad = payLoad + String(END_JSON); - httpClient.print(HTTP_METHOD); - httpClient.print(tempResource); + httpClient.print(HTTP_POST); + httpClient.print(resource); httpClient.println(HTTP_VERSION); httpClient.println(host); + httpClient.println(HTTP_CONTENT_TYPE); + httpClient.print(HTTP_CONTENT_LEN); + httpClient.println(payLoad.length()); + httpClient.println(); + httpClient.println(payLoad); httpClient.println(); delay(1000); while (httpClient.available()) { char response = httpClient.read(); - Serial.print(response); +// Serial.print(response); } - Serial.println(); - Serial.println("-------------------------------"); - tempResource = ""; +// Serial.println(); +// Serial.println("-------------------------------"); delay(1000); } -} \ No newline at end of file +} + + +