From 0993e5125a76c39861a18dbf76cf4bfd35bbc1de Mon Sep 17 00:00:00 2001 From: mharindu Date: Mon, 2 May 2016 16:31:48 +0530 Subject: [PATCH 1/4] Fixed issues in arduino temperature agent --- .../main/resources/agent/ArduinoBoardSketch.h | 8 ++++-- .../resources/agent/ArduinoBoardSketch.ino | 3 +- .../src/main/resources/agent/Connect.ino | 28 +++++++++++++++++-- .../src/main/resources/agent/PushData.ino | 13 ++------- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h index d52792dfca..3e731f5c97 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h @@ -39,8 +39,12 @@ #define DEVICE_ID "${DEVICE_ID}" #define DEVICE_TOKEN "${DEVICE_TOKEN}" #define REFRESH_DEVICE_TOKEN "${DEVICE_REFRESH_TOKEN}" +#define DEVICE_TYPE "arduino" +#define TIME 0 +#define SUPER_TENANT "carbon.super" -#define SERVICE_EPOINT "/arduino/controller/" +#define SERVICE_EPOINT "/endpoints" +#define EVENT_STREAM "/temperature-http" #define POLL_INTERVAL 1000 #define PUSH_INTERVAL 10000 @@ -57,7 +61,7 @@ byte subnet[] = { 255, 255, 255, 0 }; //Ststic subnet of arduino byte gateway[] = { 192, 168, 43, 1 }; //Ststic gateway of arduino -String host, jsonPayLoad, replyMsg; +String host, jsonPayLoad, replyMsg, endPoint; String responseMsg, subStrn; double cpuTemperature =0; static unsigned long pushTimestamp = 0; diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino index 50627d074d..f0461ce9b2 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino @@ -37,6 +37,7 @@ void setup() pinMode(13, OUTPUT); connectHttp(); setupResource(); + setupEndpoint(); wdt_enable(WDTO_8S); } @@ -80,4 +81,4 @@ void loop() // //Serial.println("LOOPING"); wdt_reset(); -} \ No newline at end of file +} diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino index dbc838a891..587658abe4 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino @@ -97,11 +97,16 @@ void setupResource(){ host = "Host: " + hostIP + ":" + port; if(DEBUG) Serial.println(host); - jsonPayLoad = "{\"owner\":\""; + jsonPayLoad = "{\"event\":" + jsonPayLoad += "{\"owner\":\""; jsonPayLoad += String(DEVICE_OWNER); + jsonPayLoad += "\",\"deviceType\":\""; + jsonPayLoad += String(DEVICE_TYPE); jsonPayLoad += "\",\"deviceId\":\""; jsonPayLoad += String(DEVICE_ID); - jsonPayLoad += "\",\"reply\":\""; + jsonPayLoad += "\",\"time\":"; + jsonPayLoad += String(TIME); + jsonPayLoad += "}, \"payloadData\":{"; if(DEBUG) { Serial.print("JSON Payload: "); @@ -155,4 +160,21 @@ void setupClient(){ } if(CON_DEBUG) Serial.println(F("client Connection failed")); } -} \ No newline at end of file +} + + +void setupEndpoint() { + endPoint = SERVICE_EPOINT; + + // check tenant domain + if (TENANT_DOMAIN.equals(SUPER_TENANT)) { + endPoint += EVENT_STREAM; + + } else { + endPoint += "/t/" + TENANT_DOMAIN; + endPoint += EVENT_STREAM; + } + endPoint += "?deviceId="; + endPoint += DEVICE_ID; + +} diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino index 6b95b41558..a4ff1674e3 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino @@ -25,17 +25,12 @@ ***********************************************************************************************/ void pushData(){ - String payLoad = "Temp"; - payLoad = payLoad + "\",\"value\":\""; - - + String payLoad = "\"temperature\":"; payLoad+=cpuTemperature; - - - payLoad += "\"}"; + payLoad += "}}}"; client.fastrprint(F("POST ")); - client.fastrprint(SERVICE_EPOINT); client.fastrprint(F("pushdata")); + client.fastrprint(endPoint); client.fastrprint(F("pushdata")); client.fastrprint(F(" HTTP/1.1")); client.fastrprint(F("\n")); client.fastrprint(host.c_str()); client.fastrprint(F("\n")); client.fastrprint(F("Content-Type: application/json")); client.fastrprint(F("\n")); @@ -104,8 +99,6 @@ void pushData(){ } - - double getBoardTemp(void) { unsigned int wADC; From de265f30acc01c3fdbd4ac333675a707efb05327 Mon Sep 17 00:00:00 2001 From: mharindu Date: Tue, 3 May 2016 23:53:06 +0530 Subject: [PATCH 2/4] Fixed issues in arduino temperature sensor --- .../main/resources/agent/ArduinoBoardSketch.h | 18 ++++++++++------- .../resources/agent/ArduinoBoardSketch.ino | 1 - .../src/main/resources/agent/Connect.ino | 18 +---------------- .../src/main/resources/agent/PollServer.ino | 20 ++++++++----------- .../src/main/resources/agent/PushData.ino | 20 ++++++++++++++----- 5 files changed, 35 insertions(+), 42 deletions(-) diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h index 3e731f5c97..62d01f1bcb 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h @@ -43,8 +43,10 @@ #define TIME 0 #define SUPER_TENANT "carbon.super" -#define SERVICE_EPOINT "/endpoints" -#define EVENT_STREAM "/temperature-http" +#define DAS_SERVICE_EPOINT "/endpoints/temperature-http?deviceId=${DEVICE_ID}" +#define DAS_SERVICE_TEPOINT "/endpoints/t/${TENANT_DOMAIN}/temperature-http?deviceId=${DEVICE_ID}" + +#define IOT_SERVICE_EPOINT "/arduino/device/${DEVICE_ID}/controls" #define POLL_INTERVAL 1000 #define PUSH_INTERVAL 10000 @@ -53,19 +55,21 @@ #define SERVICE_PORT 9763 //http port of iot server -byte server[4] = {192,168,43,168}; //Ip address of iot server -byte deviceIP[4] = { 192, 168, 43,11 }; //Ststic ip address of arduino +byte server[4] = {192,168,1,10}; //Ip address of iot server +byte deviceIP[4] = { 192, 168, 1,110 }; //Ststic ip address of arduino byte dns2[] = { 8, 8, 8, 8 }; //Ststic dns of arduino byte subnet[] = { 255, 255, 255, 0 }; //Ststic subnet of arduino -byte gateway[] = { 192, 168, 43, 1 }; //Ststic gateway of arduino +byte gateway[] = { 192, 168, 1, 1 }; //Ststic gateway of arduino -String host, jsonPayLoad, replyMsg, endPoint; +String host, jsonPayLoad, replyMsg; String responseMsg, subStrn; -double cpuTemperature =0; +double cpuTemperature = 0; static unsigned long pushTimestamp = 0; static unsigned long pollTimestamp = 0; +char charBuf[10]; +String payLoad; #endif diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino index f0461ce9b2..7f0e6941ab 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino @@ -37,7 +37,6 @@ void setup() pinMode(13, OUTPUT); connectHttp(); setupResource(); - setupEndpoint(); wdt_enable(WDTO_8S); } diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino index 587658abe4..a97852a68c 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino @@ -97,7 +97,7 @@ void setupResource(){ host = "Host: " + hostIP + ":" + port; if(DEBUG) Serial.println(host); - jsonPayLoad = "{\"event\":" + jsonPayLoad = "{\"event\":{\"metaData\":"; jsonPayLoad += "{\"owner\":\""; jsonPayLoad += String(DEVICE_OWNER); jsonPayLoad += "\",\"deviceType\":\""; @@ -162,19 +162,3 @@ void setupClient(){ } } - -void setupEndpoint() { - endPoint = SERVICE_EPOINT; - - // check tenant domain - if (TENANT_DOMAIN.equals(SUPER_TENANT)) { - endPoint += EVENT_STREAM; - - } else { - endPoint += "/t/" + TENANT_DOMAIN; - endPoint += EVENT_STREAM; - } - endPoint += "?deviceId="; - endPoint += DEVICE_ID; - -} diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino index 486fad7a17..85bbaa8e58 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino @@ -23,37 +23,33 @@ void readControls() { Serial.println("Started.."); client.fastrprint(F("GET ")); - client.fastrprint(SERVICE_EPOINT); - client.fastrprint(F("readcontrols")); + client.fastrprint(IOT_SERVICE_EPOINT); client.fastrprint(F(" HTTP/1.1")); client.fastrprint(F("\n")); client.fastrprint(host.c_str()); + client.fastrprint(F("Authorization: Bearer ")); client.fastrprint(F(DEVICE_TOKEN)); client.fastrprint(F("\n")); + client.fastrprint(F("Content-Type: application/json")); client.fastrprint(F("\n")); + client.fastrprint(F("Accept: application/json")); client.fastrprint(F("\n")); client.fastrprint(F("\n")); - client.fastrprint(DEVICE_ID); - client.fastrprint(F("owner: ")); - client.fastrprint(DEVICE_OWNER); - client.fastrprint(F("\n")); - client.fastrprint(F("deviceId: ")); - client.fastrprint(F(DEVICE_ID)); - client.fastrprint(F("\n")); - client.fastrprint(F("deviceId: ")); - client.fastrprint(F("protocol: HTTP\n")); + client.println(); delay(1000); - Serial.println("Ended.."); + while (client.available()) { char response = client.read(); responseMsg += response; } + Serial.println("Ended.."); int index = responseMsg.lastIndexOf(":"); int newLine = responseMsg.lastIndexOf("\n"); subStrn = responseMsg.substring(index + 1); responseMsg = responseMsg.substring(newLine + 1, index); if(DEBUG) { + Serial.print("Polling Response: "); Serial.print(responseMsg); Serial.println(); Serial.println("-------------------------------"); diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino index a4ff1674e3..6d4a529802 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino @@ -25,15 +25,22 @@ ***********************************************************************************************/ void pushData(){ - String payLoad = "\"temperature\":"; - payLoad+=cpuTemperature; + + payLoad = "\"temperature\":"; + payLoad += dtostrf(cpuTemperature, 3, 2, charBuf); payLoad += "}}}"; client.fastrprint(F("POST ")); - client.fastrprint(endPoint); client.fastrprint(F("pushdata")); + if (strcmp(TENANT_DOMAIN, SUPER_TENANT) == 0) { + client.fastrprint(DAS_SERVICE_EPOINT); + } else { + client.fastrprint(DAS_SERVICE_TEPOINT); + } client.fastrprint(F(" HTTP/1.1")); client.fastrprint(F("\n")); client.fastrprint(host.c_str()); client.fastrprint(F("\n")); + client.fastrprint(F("Authorization: Bearer ")); client.fastrprint(F(DEVICE_TOKEN)); client.fastrprint(F("\n")); client.fastrprint(F("Content-Type: application/json")); client.fastrprint(F("\n")); + client.fastrprint(F("Accept: application/json")); client.fastrprint(F("\n")); client.fastrprint(F("Content-Length: ")); int payLength = jsonPayLoad.length() + payLoad.length(); @@ -43,8 +50,11 @@ void pushData(){ if(DEBUG) { Serial.print("POST "); - Serial.print(SERVICE_EPOINT); - Serial.print("pushdata"); + if (strcmp(TENANT_DOMAIN, SUPER_TENANT) == 0) { + Serial.print(DAS_SERVICE_EPOINT); + } else { + Serial.print(DAS_SERVICE_TEPOINT); + } Serial.print(" HTTP/1.1"); Serial.println(); Serial.print(host); Serial.println(); Serial.print("Content-Type: application/json"); Serial.println(); From 381b4cc7e1fc3723d4441020118e793a276167f4 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 4 May 2016 00:24:05 +0530 Subject: [PATCH 3/4] Fixed issues in arduino temperature sensor --- .../src/main/resources/agent/PollServer.ino | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino index 85bbaa8e58..2c2019a107 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino @@ -26,22 +26,33 @@ void readControls() { client.fastrprint(IOT_SERVICE_EPOINT); client.fastrprint(F(" HTTP/1.1")); client.fastrprint(F("\n")); - client.fastrprint(host.c_str()); + client.fastrprint(host.c_str());client.fastrprint(F("\n")); client.fastrprint(F("Authorization: Bearer ")); client.fastrprint(F(DEVICE_TOKEN)); client.fastrprint(F("\n")); client.fastrprint(F("Content-Type: application/json")); client.fastrprint(F("\n")); client.fastrprint(F("Accept: application/json")); client.fastrprint(F("\n")); client.fastrprint(F("\n")); + client.fastrprint(F("protocol: HTTP\n")); client.println(); + if(DEBUG) { + Serial.print("GET "); + Serial.print(IOT_SERVICE_EPOINT); + Serial.print(" HTTP/1.1"); Serial.println(); + Serial.print(host); Serial.println(); + Serial.print("Content-Type: application/json"); Serial.println(); + Serial.println(); + } delay(1000); while (client.available()) { char response = client.read(); + if(DEBUG) Serial.print(response); responseMsg += response; } + Serial.println(); Serial.println("Ended.."); int index = responseMsg.lastIndexOf(":"); int newLine = responseMsg.lastIndexOf("\n"); From b03105f50b41e0228ac048ead00967a5f851e996 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 4 May 2016 01:15:31 +0530 Subject: [PATCH 4/4] Fixed issues in arduino temperature sensor --- .../src/main/webapp/META-INF/permissions.xml | 6 +++--- .../src/main/resources/agent/PollServer.ino | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml index c59a0b9798..4966eba2e2 100644 --- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml +++ b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml @@ -31,16 +31,16 @@ control bulb - device-mgt/user/operations + /device-mgt/user/operations /device/*/bulb POST arduino_user get controls - device-mgt/user/operations + /device-mgt/user/operations /device/*/controls - POST + GET arduino_device diff --git a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino index 2c2019a107..040790f2df 100644 --- a/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino +++ b/features/iot-plugins-feature/arduino-plugin-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino @@ -28,8 +28,6 @@ void readControls() { client.fastrprint(F("\n")); client.fastrprint(host.c_str());client.fastrprint(F("\n")); client.fastrprint(F("Authorization: Bearer ")); client.fastrprint(F(DEVICE_TOKEN)); client.fastrprint(F("\n")); - client.fastrprint(F("Content-Type: application/json")); client.fastrprint(F("\n")); - client.fastrprint(F("Accept: application/json")); client.fastrprint(F("\n")); client.fastrprint(F("\n")); client.fastrprint(F("protocol: HTTP\n")); @@ -77,6 +75,7 @@ void readControls() { digitalWrite(6, LOW); } + responseMsg = ""; }