diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java index 9f3bc424fa..2fabc224e5 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java @@ -190,65 +190,18 @@ public class ArduinoService { @FormParam("state") String state, @Context HttpServletResponse response) { - try { - DeviceValidator deviceValidator = new DeviceValidator(); - if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, - ArduinoConstants.DEVICE_TYPE))) { - response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); - return; - } - } catch (DeviceManagementException e) { - log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner); - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return; - } - - String switchToState = state.toUpperCase(); - - if (!switchToState.equals(ArduinoConstants.STATE_ON) && !switchToState.equals(ArduinoConstants.STATE_OFF)) { - log.error("The requested state change shoud be either - 'ON' or 'OFF'"); - response.setStatus(Response.Status.BAD_REQUEST.getStatusCode()); - return; - } - - String protocolString = protocol.toUpperCase(); - String callUrlPattern = ArduinoConstants.BULB_CONTEXT + switchToState; + LinkedList deviceControlList = internalControlsQueue.get(deviceId); - if (log.isDebugEnabled()) { - log.debug("Sending request to switch-bulb of device [" + deviceId + "] via " + - protocolString); + String operation = "BULB:" + state.toUpperCase(); + log.info(operation); + if (deviceControlList == null) { + deviceControlList = new LinkedList<>(); + deviceControlList.add(operation); + internalControlsQueue.put(deviceId,deviceControlList); + } else { + deviceControlList.add(operation); } - try { - /*switch (protocolString) { - case HTTP_PROTOCOL: - String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); - if (deviceHTTPEndpoint == null) { - response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - return; - } - ArduinoServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, callUrlPattern, true); - break; - case MQTT_PROTOCOL: - String mqttMessage = ArduinoConstants.BULB_CONTEXT.replace("/", ""); - ArduinoServiceUtils.sendCommandViaMQTT(owner, deviceId, mqttMessage, switchToState); - break; - default: - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - return; - }*/ - String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); - if (deviceHTTPEndpoint == null) { - response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - return; - } - ArduinoServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, callUrlPattern, true); - - } catch (DeviceManagementException e) { - log.error("Failed to send switch-bulb request to device [" + deviceId + "] via " + protocolString); - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return; - } response.setStatus(Response.Status.OK.getStatusCode()); } @@ -271,40 +224,10 @@ public class ArduinoService { @Context HttpServletResponse response) { SensorRecord sensorRecord = null; - DeviceValidator deviceValidator = new DeviceValidator(); try { - if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, - ArduinoConstants.DEVICE_TYPE))) { - response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); - } - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - } - - String protocolString = protocol.toUpperCase(); - - if (log.isDebugEnabled()) { - log.debug( - "Sending request to read raspberrypi-temperature of device [" + deviceId + "] via " + - protocolString); - } - - try { - String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); - if (deviceHTTPEndpoint == null) { - response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - } - - String temperatureValue = ArduinoServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, - ArduinoConstants - .TEMPERATURE_CONTEXT, - false); - SensorDataManager.getInstance().setSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE, - temperatureValue, - Calendar.getInstance().getTimeInMillis()); sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE); - } catch (DeviceManagementException | DeviceControllerException e) { + } catch ( DeviceControllerException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } @@ -323,49 +246,18 @@ public class ArduinoService { String owner = dataMsg.owner; String deviceId = dataMsg.deviceId; - String deviceIp = dataMsg.reply; //TODO:: Get IP from request float pinData = dataMsg.value; - try { - DeviceValidator deviceValidator = new DeviceValidator(); - if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, - ArduinoConstants.DEVICE_TYPE))) { - response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); - log.warn("Data Received from unregistered Arduino device [" + deviceId + "] for owner [" + owner + "]"); - return; - } - - String registeredIp = deviceToIpMap.get(deviceId); - - if (registeredIp == null) { - log.warn("Unregistered IP: Arduino Pin Data Received from an un-registered IP " + deviceIp + - " for device ID - " + deviceId); - response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - return; - } else if (!registeredIp.equals(deviceIp)) { - log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId + - " is already registered under some other IP. Re-registration required"); - response.setStatus(Response.Status.CONFLICT.getStatusCode()); - return; - } - - if (log.isDebugEnabled()) { - log.debug("Received Pin Data Value: " + pinData + " degrees C"); - } - SensorDataManager.getInstance().setSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE, + SensorDataManager.getInstance().setSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE, String.valueOf(pinData), Calendar.getInstance().getTimeInMillis()); - if (!ArduinoServiceUtils.publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - log.warn("An error occured whilst trying to publish pin data of Arduino with ID [" + deviceId + + if (!ArduinoServiceUtils.publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + log.warn("An error occured whilst trying to publish pin data of Arduino with ID [" + deviceId + "] of owner [" + owner + "]"); - } - - } catch (DeviceManagementException e) { - String errorMsg = "Validation attempt for deviceId [" + deviceId + "] of owner [" + owner + "] failed.\n"; - log.error(errorMsg + Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase() + "\n" + e.getErrorMessage()); } + } /** @@ -397,6 +289,7 @@ public class ArduinoService { response.setStatus(HttpStatus.SC_NO_CONTENT); } } + if (log.isDebugEnabled()) { log.debug(result); } diff --git a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h index 3a1fe1ab9d..482c410599 100644 --- a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h +++ b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.h @@ -27,20 +27,19 @@ #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 -#define WLAN_SSID "SSID" // cannot be longer than 32 characters! -#define WLAN_PASS "password" +#define WLAN_SSID "ssid" // Your wifi network SSID (cannot be longer than 32 characters!) +#define WLAN_PASS "password" // Your wifi network password -#define WLAN_SECURITY WLAN_SEC_WPA2 +#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_ID "${DEVICE_ID}" + +#define DEVICE_OWNER "${DEVICE_OWNER}" +#define DEVICE_ID "${DEVICE_ID}" #define DEVICE_TOKEN "${DEVICE_TOKEN}" #define REFRESH_DEVICE_TOKEN "${DEVICE_REFRESH_TOKEN}" - -#define SERVICE_PORT 9763 #define SERVICE_EPOINT "/arduino/controller/" #define POLL_INTERVAL 1000 @@ -48,7 +47,14 @@ #define DEBUG true #define CON_DEBUG true -byte server[4] = { 192, 168, 1, 101 }; +byte server[4] = {192,168,43,168}; //Ip address of iot server +#define SERVICE_PORT 9763 //http port of iot server + +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 deviceIP[4] = { 192, 168, 43,11 }; //Ststic ip address of arduino + String host, jsonPayLoad, replyMsg; String responseMsg, subStrn; double cpuTemperature =0; @@ -59,4 +65,3 @@ static unsigned long pollTimestamp = 0; #endif - diff --git a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino index 50601b634a..50627d074d 100644 --- a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino +++ b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/ArduinoBoardSketch.ino @@ -31,20 +31,25 @@ uint32_t sserver; void setup() { - Serial.begin(9600); + Serial.begin(115200); Serial.println(F("Internal Temperature Sensor")); pinMode(6, OUTPUT); + pinMode(13, OUTPUT); connectHttp(); setupResource(); wdt_enable(WDTO_8S); + } void loop() { + wdt_reset(); while( !cc3000.checkConnected() ){ connectHttp(); } + + cpuTemperature=getBoardTemp(); @@ -60,16 +65,19 @@ void loop() //Serial.println("PUSHED"); + wdt_reset(); + if(millis() - pollTimestamp > POLL_INTERVAL){ while (!client.connected()) { setupClient(); } + Serial.println("Read Controls"); readControls(); pollTimestamp = millis(); } - //Serial.println("LOOPING"); +// //Serial.println("LOOPING"); wdt_reset(); -} +} \ No newline at end of file diff --git a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino index 38dfeacdaa..dbc838a891 100644 --- a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino +++ b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/Connect.ino @@ -20,18 +20,9 @@ /********************************************************************************************** Use the below variables when required to set a static IP for the WifiSheild ***********************************************************************************************/ -// byte dns2[] = { 8, 8, 8, 8 }; -// byte subnet[] = { 255, 255, 255, 0 }; -// byte gateway[] = { 10, 100, 9, 254 }; -// byte deviceIP[4] = { 10, 100, 9, 9 }; -// byte gateway[] = { 192, 168, 1, 1 }; -// byte deviceIP[4] = { 192, 168, 1, 219 }; + + uint32_t ip, ddns, ssubnet, ggateway; -// uint32_t ip, ddns, ssubnet, ggateway; - -// byte mac[6] = { 0xC0, 0x4A, 0x00, 0x1A, 0x08, 0xDA }; //mac - c0:4a:00:1a:08:da -// c0:4a:00:1a:03:f8 -// b8:27:eb:88:37:7a String connecting = "connecting.... "; void connectHttp() { @@ -42,31 +33,19 @@ void connectHttp() { if(DEBUG) Serial.println(F("Couldn't begin()! Check your wiring?")); while(1); } - - // if( cc3000.setMacAddress(mac) ) { // Set your own mac and print it to re-check - // uint8_t address[6]; - // cc3000.getMacAddress(address); - // if(DEBUG){ - // Serial.print(address[0], HEX); Serial.print(":"); - // Serial.print(address[1], HEX); Serial.print(":"); - // Serial.print(address[2], HEX); Serial.print(":"); - // Serial.print(address[3], HEX); Serial.print(":"); - // Serial.print(address[4], HEX); Serial.print(":"); - // Serial.println(address[5], HEX); - // } - // } - + /********************************************************************************************** Only required if using static IP for the WifiSheild ***********************************************************************************************/ - // 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]); - // cc3000.setStaticIPAddress(ip, ssubnet, ggateway, ddns); // required for setting static IP + 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]); + cc3000.setStaticIPAddress(ip, ssubnet, ggateway, ddns); // required for setting static IP /***********************************************************************************************/ + sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); @@ -74,6 +53,8 @@ void connectHttp() { Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID); } + + cc3000.deleteProfiles(); if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { if(CON_DEBUG) Serial.println(F("Failed!")); @@ -85,24 +66,26 @@ void connectHttp() { if(CON_DEBUG) Serial.println(F("Request DHCP")); while (!cc3000.checkDHCP()) { - delay(100); // ToDo: Insert a DHCP timeout! + delay(100); } /* Display the IP address DNS, Gateway, etc. */ while (! displayConnectionDetails()) { delay(1000); } - - client = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT - if (client.connected()) { - if(CON_DEBUG) Serial.println("client Connected to server"); - } else { - if(CON_DEBUG) Serial.println(F("client Connection failed")); - } - - - - + + if (cc3000.checkConnected()) { + Serial.println("client Connected to AP"); + client = cc3000.connectTCP(sserver, SERVICE_PORT); + if (client.connected()) { + if(CON_DEBUG) Serial.println("client Connected to server"); + } else { + if(CON_DEBUG) Serial.println(F("client Connection failed")); + } + } else { + Serial.println(F("client Connection to AP failed")); + } + if(CON_DEBUG) Serial.println(F("-------------------------------------")); } @@ -172,4 +155,4 @@ void setupClient(){ } if(CON_DEBUG) Serial.println(F("client Connection failed")); } -} +} \ No newline at end of file diff --git a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino index a3bfce0e18..486fad7a17 100644 --- a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino +++ b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PollServer.ino @@ -19,20 +19,30 @@ #include "ArduinoBoardSketch.h" void readControls() { // String responseMsg; + + Serial.println("Started.."); client.fastrprint(F("GET ")); client.fastrprint(SERVICE_EPOINT); - client.fastrprint(F("readcontrols/")); + client.fastrprint(F("readcontrols")); + client.fastrprint(F(" HTTP/1.1")); + client.fastrprint(F("\n")); + client.fastrprint(host.c_str()); + client.fastrprint(F("\n")); client.fastrprint(DEVICE_ID); - client.fastrprint(F("?owner=")); + client.fastrprint(F("owner: ")); client.fastrprint(DEVICE_OWNER); - client.fastrprint(F(" HTTP/1.1")); client.fastrprint(F("\n")); - client.fastrprint(host.c_str()); client.fastrprint(F("\n")); + 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); - + delay(1000); + Serial.println("Ended.."); while (client.available()) { char response = client.read(); responseMsg += response; @@ -42,6 +52,7 @@ void readControls() { int newLine = responseMsg.lastIndexOf("\n"); subStrn = responseMsg.substring(index + 1); responseMsg = responseMsg.substring(newLine + 1, index); + if(DEBUG) { Serial.print(responseMsg); Serial.println(); @@ -50,12 +61,12 @@ void readControls() { if (subStrn.equals("ON")) { Serial.println("ITS ON"); - //digitalWrite(13, HIGH); + digitalWrite(13, HIGH); digitalWrite(6, HIGH); } else if (subStrn.equals("OFF")){ Serial.println("ITS OFF"); - //digitalWrite(13, LOW); + digitalWrite(13, LOW); digitalWrite(6, LOW); } @@ -63,4 +74,3 @@ void readControls() { } - diff --git a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino index faaa72f01d..6b95b41558 100644 --- a/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino +++ b/features/device-mgt-iot-arduino-feature/org.wso2.carbon.device.mgt.iot.arduino.feature/src/main/resources/agent/PushData.ino @@ -25,7 +25,7 @@ ***********************************************************************************************/ void pushData(){ - String payLoad = "Data"; + String payLoad = "Temp"; payLoad = payLoad + "\",\"value\":\""; @@ -48,7 +48,8 @@ void pushData(){ if(DEBUG) { Serial.print("POST "); - Serial.print(SERVICE_EPOINT); Serial.print("pushdata"); + Serial.print(SERVICE_EPOINT); + Serial.print("pushdata"); Serial.print(" HTTP/1.1"); Serial.println(); Serial.print(host); Serial.println(); Serial.print("Content-Type: application/json"); Serial.println(); @@ -135,4 +136,3 @@ double getBoardTemp(void) // The returned temperature is in degrees Celcius. return (t); } -