diff --git a/modules/distribution/pom.xml b/modules/distribution/pom.xml index de2bed56..9d0cc36a 100644 --- a/modules/distribution/pom.xml +++ b/modules/distribution/pom.xml @@ -343,6 +343,33 @@ + + + create-android-schema + package + + run + + + + + + + + + + + + + + + + + + + + + create-raspberrypi-schema diff --git a/modules/distribution/src/assembly/bin.xml b/modules/distribution/src/assembly/bin.xml index 300f2ef1..2a09163b 100644 --- a/modules/distribution/src/assembly/bin.xml +++ b/modules/distribution/src/assembly/bin.xml @@ -280,6 +280,7 @@ devicecloud.war firealarm.war arduino.war + android.war sensebot.war raspberrypi.war digitaldisplay.war diff --git a/modules/distribution/src/repository/conf/datasources/master-datasources.xml b/modules/distribution/src/repository/conf/datasources/master-datasources.xml index bb48c7bf..73bfe1cc 100755 --- a/modules/distribution/src/repository/conf/datasources/master-datasources.xml +++ b/modules/distribution/src/repository/conf/datasources/master-datasources.xml @@ -140,6 +140,27 @@ + + AndroidDM_DS + The datasource used for Android database + + jdbc/AndroidDM_DB + + + + jdbc:h2:repository/database/AndroidDM_DB;DB_CLOSE_ON_EXIT=FALSE + wso2carbon + wso2carbon + org.h2.Driver + 50 + 60000 + true + SELECT 1 + 30000 + + + + SensebotDM_DS The datasource used for Sensebot database diff --git a/modules/distribution/src/sketches/arduino/ArduinoBoardSketch.h b/modules/distribution/src/sketches/arduino/ArduinoBoardSketch.h new file mode 100644 index 00000000..541672ea --- /dev/null +++ b/modules/distribution/src/sketches/arduino/ArduinoBoardSketch.h @@ -0,0 +1,47 @@ +#ifndef ArduinoWifiAgent_H +#define ArduinoWifiAgent_H + +#include "Arduino.h" + +// 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 "SSID" // cannot be longer than 32 characters! +#define WLAN_PASS "password" + +#define WLAN_SECURITY WLAN_SEC_WPA +// 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_TOKEN "${DEVICE_TOKEN}" + + + +#define SERVICE_PORT 9763 +#define SERVICE_EPOINT "/arduino/controller/" + + +#define POLL_INTERVAL 1000 +#define PUSH_INTERVAL 10000 +#define DEBUG true +#define CON_DEBUG true + + + +byte server[4] = { 192, 168, 1, 101 }; +String host, jsonPayLoad, replyMsg; +String responseMsg, subStrn; +double cpuTemperature =0; +static unsigned long pushTimestamp = 0; +static unsigned long pollTimestamp = 0; + + +#endif + + + diff --git a/modules/distribution/src/sketches/arduino/ArduinoBoardSketch.ino b/modules/distribution/src/sketches/arduino/ArduinoBoardSketch.ino index 2c1b87b0..1b8c6f85 100644 --- a/modules/distribution/src/sketches/arduino/ArduinoBoardSketch.ino +++ b/modules/distribution/src/sketches/arduino/ArduinoBoardSketch.ino @@ -1,99 +1,56 @@ -#include "Arduinoboardwifi.h" - +#include "ArduinoBoardSketch.h" #include #include - +#include 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 pushClient; -Adafruit_CC3000_Client pollClient; +Adafruit_CC3000_Client client; uint32_t sserver; - /********************************************************************************************** - 0. Check with a sample Wifi code of the Adafruit_CC3000 library to ensure that the sheild is working - 1. Set the ip of the server(byte array below) where the Web-Rest API for the FireAlarm is running - 2. Check whether the "SERVICE_EPOINT" is correct in the 'FireAlarmWifiAgent.h' file - 3. Check whether the "SERVICE_PORT" is the same (9763) for the server running. Change it if needed - 4. Check whether the pins have been attached accordingly in the Arduino - 5. Check whether all reqquired pins are added to the 'digitalPins' array - ***********************************************************************************************/ - -byte server[4] = { 192, 168, 1, 101 }; -String host, jsonPayLoad, replyMsg; -String responseMsg, subStrn; void setup() { - Serial.begin(9600); - - Serial.println(F("Internal Temperature Sensor")); - pinMode(6, OUTPUT); - connectHttp(); - setupResource(); + Serial.begin(9600); + Serial.println(F("Internal Temperature Sensor")); + pinMode(6, OUTPUT); + connectHttp(); + setupResource(); + wdt_enable(WDTO_8S); } void loop() { - - if (pushClient.connected() && pollClient.connected()) { - pushData(); - delay(POLL_INTERVAL); - - boolean valid = readControls(); + while( !cc3000.checkConnected() ){ + connectHttp(); - responseMsg=""; - } else { - if(DEBUG) { - Serial.println("client not found..."); - Serial.println("disconnecting."); } - pushClient.close(); - pollClient.close(); - cc3000.disconnect(); - - connectHttp(); - } - - - delay(1000); -} - - - + cpuTemperature=getBoardTemp(); + if(millis() - pushTimestamp > PUSH_INTERVAL){ + while (!client.connected()) { + setupClient(); + } + pushData(); + + pushTimestamp = millis(); + } -double getBoardTemp(void) -{ - unsigned int wADC; - double t; - - // The internal temperature has to be used - // with the internal reference of 1.1V. - // Channel 8 can not be selected with - // the analogRead function yet. - - // Set the internal reference and mux. - ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3)); - ADCSRA |= _BV(ADEN); // enable the ADC - - delay(20); // wait for voltages to become stable. - - ADCSRA |= _BV(ADSC); // Start the ADC - - // Detect end-of-conversion - while (bit_is_set(ADCSRA,ADSC)); + //Serial.println("PUSHED"); - // Reading register "ADCW" takes care of how to read ADCL and ADCH. - wADC = ADCW; - // The offset of 324.31 could be wrong. It is just an indication. - t = (wADC - 324.31 ) / 1.22; + if(millis() - pollTimestamp > POLL_INTERVAL){ + while (!client.connected()) { + setupClient(); + } + readControls(); + + pollTimestamp = millis(); + + } - // The returned temperature is in degrees Celcius. - return (t); + //Serial.println("LOOPING"); + wdt_reset(); } - - diff --git a/modules/distribution/src/sketches/arduino/Arduinoboardwifi.h b/modules/distribution/src/sketches/arduino/Arduinoboardwifi.h deleted file mode 100644 index a9f26427..00000000 --- a/modules/distribution/src/sketches/arduino/Arduinoboardwifi.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef ArduinoWifiAgent_H -#define ArduinoWifiAgent_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 "SSID" // cannot be longer than 32 characters! -#define WLAN_PASS "Password" - -#define WLAN_SECURITY WLAN_SEC_WPA - // 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_TOKEN "${DEVICE_TOKEN}" - - -#define SERVICE_PORT 9763 -#define SERVICE_EPOINT "/arduino/controller/" - - - -#define POLL_INTERVAL 1000 -#define DEBUG false -#define CON_DEBUG true - -#endif - - diff --git a/modules/distribution/src/sketches/arduino/Connect.ino b/modules/distribution/src/sketches/arduino/Connect.ino index 5661aade..3f31224d 100644 --- a/modules/distribution/src/sketches/arduino/Connect.ino +++ b/modules/distribution/src/sketches/arduino/Connect.ino @@ -1,6 +1,7 @@ - /********************************************************************************************** - Use the below variables when required to set a static IP for the WifiSheild - ***********************************************************************************************/ +#include "ArduinoBoardSketch.h" +/********************************************************************************************** + 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 }; @@ -10,141 +11,147 @@ // 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 +// 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() { - /* Initialise the module */ - if(DEBUG) Serial.println(F("\nInitializing...")); - if (!cc3000.begin()) - { - 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 - - /***********************************************************************************************/ - - sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); - - if(CON_DEBUG) { - Serial.print(F("\nAttempting to connect to ")); - Serial.println(WLAN_SSID); - } - - if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { - if(CON_DEBUG) Serial.println(F("Failed!")); - while(1); - } - - if(CON_DEBUG) Serial.println(F("Connected to Wifi network!")); - - if(CON_DEBUG) Serial.println(F("Request DHCP")); + /* Initialise the module */ + if(DEBUG) Serial.println(F("\nInitializing...")); + if (!cc3000.begin()) + { + 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 + + /***********************************************************************************************/ + + sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); + + if(CON_DEBUG) { + Serial.print(F("\nAttempting to connect to ")); + Serial.println(WLAN_SSID); + } + + if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { + if(CON_DEBUG) Serial.println(F("Failed!")); + while(1); + } + + if(CON_DEBUG) Serial.println(F("Connected to Wifi network!")); + + if(CON_DEBUG) Serial.println(F("Request DHCP")); while (!cc3000.checkDHCP()) - { - delay(100); // ToDo: Insert a DHCP timeout! - } - - /* Display the IP address DNS, Gateway, etc. */ - while (! displayConnectionDetails()) { - delay(1000); - } - - pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT - if (pushClient.connected()) { - if(CON_DEBUG) Serial.println("PushClient Connected to server"); - } else { - cc3000.disconnect(); - if(CON_DEBUG) Serial.println(F("PushClient Connection failed")); - } - - - pollClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT - if (pollClient.connected()) { - if(CON_DEBUG) Serial.println("PollClient Connected to server"); - } else { - cc3000.disconnect(); - if(CON_DEBUG) Serial.println(F("PollClient Connection failed")); - } - - if(CON_DEBUG) Serial.println(F("-------------------------------------")); + { + delay(100); // ToDo: Insert a DHCP timeout! + } + + /* 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(CON_DEBUG) 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 += "\",\"reply\":\""; - - if(DEBUG) { - Serial.print("JSON Payload: "); - Serial.println(jsonPayLoad); - Serial.println("-------------------------------"); - } +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 += "\",\"reply\":\""; + + if(DEBUG) { + Serial.print("JSON Payload: "); + Serial.println(jsonPayLoad); + Serial.println("-------------------------------"); + } } String getHostIP(byte server[4]){ - String hostIP = String(server[0]); - - for ( int index = 1; index < 4; index++) { - hostIP += "." + String(server[index]); - } - - return hostIP; + String hostIP = String(server[0]); + + for ( int index = 1; index < 4; index++) { + hostIP += "." + String(server[index]); + } + + return hostIP; } bool displayConnectionDetails(void) { - uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; - - if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) - { - if(DEBUG) Serial.println(F("Unable to retrieve the IP Address!\r\n")); - return false; - } - else - { - if(CON_DEBUG) { - Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); - Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); - Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); - Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); - Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); - Serial.println(); + uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; + + if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) + { + if(DEBUG) Serial.println(F("Unable to retrieve the IP Address!\r\n")); + return false; + } + else + { + if(CON_DEBUG) { + Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); + Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); + Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); + Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); + Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); + Serial.println(); + } + return true; + } +} + +void setupClient(){ + client = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT + if (client.connected()) { + if(CON_DEBUG) Serial.println("client Connected to server"); + } else { + while( !cc3000.checkConnected() ){ + connectHttp(); + + } + if(CON_DEBUG) Serial.println(F("client Connection failed")); } - return true; - } } diff --git a/modules/distribution/src/sketches/arduino/PollServer.ino b/modules/distribution/src/sketches/arduino/PollServer.ino index 31e5f97f..3cecf098 100644 --- a/modules/distribution/src/sketches/arduino/PollServer.ino +++ b/modules/distribution/src/sketches/arduino/PollServer.ino @@ -1,55 +1,48 @@ -boolean readControls() { -// String responseMsg; - - pollClient.fastrprint(F("GET ")); - pollClient.fastrprint(SERVICE_EPOINT); - pollClient.fastrprint(F("readcontrols/")); - pollClient.fastrprint(DEVICE_ID); - pollClient.fastrprint(F("?owner=")); - pollClient.fastrprint(DEVICE_OWNER); - pollClient.fastrprint(F(" HTTP/1.1")); pollClient.fastrprint(F("\n")); - pollClient.fastrprint(host.c_str()); pollClient.fastrprint(F("\n")); - pollClient.println(); - - delay(1000); - - - while (pollClient.available()) { - char response = pollClient.read(); - responseMsg += response; - +#include "ArduinoBoardSketch.h" +void readControls() { + // String responseMsg; + + client.fastrprint(F("GET ")); + client.fastrprint(SERVICE_EPOINT); + client.fastrprint(F("readcontrols/")); + client.fastrprint(DEVICE_ID); + 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.println(); + + delay(1000); + + + while (client.available()) { + char response = client.read(); + responseMsg += response; + + } + int index = responseMsg.lastIndexOf(":"); + int newLine = responseMsg.lastIndexOf("\n"); + subStrn = responseMsg.substring(index + 1); + responseMsg = responseMsg.substring(newLine + 1, index); + if(DEBUG) { + Serial.print(responseMsg); + Serial.println(); + Serial.println("-------------------------------"); } - - - int index = responseMsg.lastIndexOf(":"); - int newLine = responseMsg.lastIndexOf("\n"); - subStrn = responseMsg.substring(index + 1); - responseMsg = responseMsg.substring(newLine + 1, index); - if(DEBUG) { - Serial.print(responseMsg); - Serial.println(); - Serial.println("-------------------------------"); - } - - if (subStrn.equals("ON")) { + + 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(6, LOW); - - } - if (responseMsg.equals("BULB")) { - return false; - } - - - return true; + } else if (subStrn.equals("OFF")){ + + Serial.println("ITS OFF"); + //digitalWrite(13, LOW); + digitalWrite(6, LOW); + + } + } - diff --git a/modules/distribution/src/sketches/arduino/PushData.ino b/modules/distribution/src/sketches/arduino/PushData.ino index 9eaf33ac..0879c14a 100644 --- a/modules/distribution/src/sketches/arduino/PushData.ino +++ b/modules/distribution/src/sketches/arduino/PushData.ino @@ -1,82 +1,84 @@ +#include "ArduinoBoardSketch.h" - /********************************************************************************************** - This method will traverse the array of digital pins and batch the data from the those pins together. - It makes a single call to the server and sends all pin values as a batch. - Server dis-assembles it accordingly and makes multiple publish calls for each sensor type. - ***********************************************************************************************/ +/********************************************************************************************** + This method will traverse the array of digital pins and batch the data from the those pins together. + It makes a single call to the server and sends all pin values as a batch. + Server dis-assembles it accordingly and makes multiple publish calls for each sensor type. + ***********************************************************************************************/ void pushData(){ - String payLoad = "Data"; - payLoad = payLoad + "\",\"value\":\""; - - - payLoad+=getBoardTemp(); + String payLoad = "Data"; + payLoad = payLoad + "\",\"value\":\""; + + + payLoad+=cpuTemperature; + - payLoad += "\"}"; - - pushClient.fastrprint(F("POST ")); - pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("pushdata")); - pushClient.fastrprint(F(" HTTP/1.1")); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(host.c_str()); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(F("Content-Type: application/json")); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(F("Content-Length: ")); - + + client.fastrprint(F("POST ")); + client.fastrprint(SERVICE_EPOINT); 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")); + client.fastrprint(F("Content-Length: ")); + int payLength = jsonPayLoad.length() + payLoad.length(); - pushClient.fastrprint(String(payLength).c_str()); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(F("\n")); - + client.fastrprint(String(payLength).c_str()); client.fastrprint(F("\n")); + client.fastrprint(F("\n")); + if(DEBUG) { - Serial.print("POST "); - 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(); - Serial.print("Content-Length: "); - Serial.print(payLength); Serial.println(); - Serial.println(); + Serial.print("POST "); + 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(); + 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()) { - pushClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); - if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); - i = jsonPayLoad.length(); - } else { - pushClient.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize)); - if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize)); - } - } + if ( (i+1)*chunkSize > jsonPayLoad.length()) { + client.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); + if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); + i = jsonPayLoad.length(); + } else { + client.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()) { - pushClient.print(payLoad.substring(i*chunkSize, payLoad.length())); - if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length())); - i = payLoad.length(); - } else { - pushClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); - if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); - } - } - - pushClient.fastrprint(F("\n")); + if ( (i+1)*chunkSize > payLoad.length()) { + client.print(payLoad.substring(i*chunkSize, payLoad.length())); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length())); + i = payLoad.length(); + } else { + client.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + } + } + + client.fastrprint(F("\n")); if(DEBUG) Serial.println(); - + delay(1000); - - - while (pushClient.available()) { - char response = pushClient.read(); + + + while (client.available()) { + char response = client.read(); if(DEBUG) Serial.print(response); - } + } + - if(DEBUG) { - Serial.println(); - Serial.println("-------------------------------"); + Serial.println(); + Serial.println("-------------------------------"); } payLoad = ""; @@ -84,3 +86,35 @@ void pushData(){ + +double getBoardTemp(void) +{ + unsigned int wADC; + double t; + + // The internal temperature has to be used + // with the internal reference of 1.1V. + // Channel 8 can not be selected with + // the analogRead function yet. + + // Set the internal reference and mux. + ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3)); + ADCSRA |= _BV(ADEN); // enable the ADC + + delay(20); // wait for voltages to become stable. + + ADCSRA |= _BV(ADSC); // Start the ADC + + // Detect end-of-conversion + while (bit_is_set(ADCSRA,ADSC)); + + // Reading register "ADCW" takes care of how to read ADCL and ADCH. + wADC = ADCW; + + // The offset of 324.31 could be wrong. It is just an indication. + t = (wADC - 324.31 ) / 1.22; + + // The returned temperature is in degrees Celcius. + return (t); +} + diff --git a/modules/distribution/src/sketches/firealarm_wifi/Connect.ino b/modules/distribution/src/sketches/firealarm_wifi/Connect.ino index 5661aade..3e62652a 100644 --- a/modules/distribution/src/sketches/firealarm_wifi/Connect.ino +++ b/modules/distribution/src/sketches/firealarm_wifi/Connect.ino @@ -1,6 +1,7 @@ - /********************************************************************************************** - Use the below variables when required to set a static IP for the WifiSheild - ***********************************************************************************************/ + +/********************************************************************************************** + 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 }; @@ -10,111 +11,106 @@ // 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.... "; + +//mac - c0:4a:00:1a:08:da +// c0:4a:00:1a:03:f8 +// b8:27:eb:88:37:7a -void connectHttp() { - /* Initialise the module */ - if(DEBUG) Serial.println(F("\nInitializing...")); - if (!cc3000.begin()) - { - 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 - - /***********************************************************************************************/ - - sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); - - if(CON_DEBUG) { - Serial.print(F("\nAttempting to connect to ")); - Serial.println(WLAN_SSID); - } - - if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { - if(CON_DEBUG) Serial.println(F("Failed!")); - while(1); - } - - if(CON_DEBUG) Serial.println(F("Connected to Wifi network!")); - if(CON_DEBUG) Serial.println(F("Request DHCP")); +void connectHttp() { + /* Initialise the module */ + if(DEBUG) Serial.println(F("\nInitializing...")); + if (!cc3000.begin()) + { + if(DEBUG) Serial.println(F("Couldn't begin()! Check your wiring?")); + while(1); + } + + 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 + + /***********************************************************************************************/ + + sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); + + if(CON_DEBUG) { + Serial.print(F("\nAttempting to connect to ")); + Serial.println(WLAN_SSID); + } + + if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { + if(CON_DEBUG) Serial.println(F("Failed!")); + while(1); + } + + if(CON_DEBUG) Serial.println(F("Connected to Wifi network!")); + + if(true) Serial.println(F("Request DHCP")); while (!cc3000.checkDHCP()) - { - delay(100); // ToDo: Insert a DHCP timeout! - } - - /* Display the IP address DNS, Gateway, etc. */ - while (! displayConnectionDetails()) { - delay(1000); - } - - pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT - if (pushClient.connected()) { - if(CON_DEBUG) Serial.println("PushClient Connected to server"); - } else { - cc3000.disconnect(); - if(CON_DEBUG) Serial.println(F("PushClient Connection failed")); - } - - - pollClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT - if (pollClient.connected()) { - if(CON_DEBUG) Serial.println("PollClient Connected to server"); - } else { - cc3000.disconnect(); - if(CON_DEBUG) Serial.println(F("PollClient Connection failed")); - } - - if(CON_DEBUG) Serial.println(F("-------------------------------------")); + { + delay(100); // ToDo: Insert a DHCP timeout! + } + + /* Display the IP address DNS, Gateway, etc. */ + while (! displayConnectionDetails()) { + delay(1000); + } + + pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT + if (pushClient.connected()) { + if(true) Serial.println(F("pushClient Connected to server")); + } else { + if(CON_DEBUG) Serial.println(F("pushClient Connection failed")); + } + + + + + if(CON_DEBUG) 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 += "\",\"reply\":\""; - - if(DEBUG) { - Serial.print("JSON Payload: "); - Serial.println(jsonPayLoad); - Serial.println("-------------------------------"); - } +void setupResource(){ + String hostIP = getHostIP(server); + String port = String(SERVICE_PORT); +// + host = "Host: " + hostIP + ":" + port; +// if(DEBUG) Serial.println(host); + + jsonPayLoad = "{\"owner\":\""; + jsonPayLoad += DEVICE_OWNER; + jsonPayLoad += "\",\"deviceId\":\""; + jsonPayLoad += DEVICE_ID; + jsonPayLoad += "\",\"reply\":\""; + + if(DEBUG) { + Serial.print("JSON Payload: "); + Serial.println(jsonPayLoad); + Serial.println("-------------------------------"); + } } + String getHostIP(byte server[4]){ String hostIP = String(server[0]); @@ -128,23 +124,36 @@ String getHostIP(byte server[4]){ bool displayConnectionDetails(void) { - uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; - - if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) - { - if(DEBUG) Serial.println(F("Unable to retrieve the IP Address!\r\n")); - return false; - } - else - { - if(CON_DEBUG) { - Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); - Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); - Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); - Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); - Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); - Serial.println(); + uint32_t netmask, gateway, dhcpserv, dnsserv; + + if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) + { + if(DEBUG) Serial.println(F("Unable to retrieve the IP Address!\r\n")); + return false; + } + else + { + if(CON_DEBUG) { + Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); + Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); + Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); + Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); + Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); + Serial.println(); + } + return true; + } +} + +void setupClient(){ + pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT + if (pushClient.connected()) { + if(CON_DEBUG) Serial.println(F("client Connected to server")); + } else { + while( !cc3000.checkConnected() ){ + connectHttp(); + + } + if(CON_DEBUG) Serial.println(F("client Connection failed")); } - return true; - } } diff --git a/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h index ef322104..340b70f4 100644 --- a/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h +++ b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.h @@ -13,32 +13,57 @@ #define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_CS 10 -#define WLAN_SSID "YourAP-SSID" // cannot be longer than 32 characters! -#define WLAN_PASS "Your-Password" +#define LISTEN_PORT 80 // What TCP port to listen on for connections. + // The HTTP protocol uses port 80 by default. -#define WLAN_SECURITY WLAN_SEC_WPA2 +#define MAX_ACTION 6 // Maximum length of the HTTP action that can be parsed. + +#define MAX_PATH 10 // Maximum length of the HTTP request path that can be parsed. + // There isn't much memory available so keep this short! + +#define BUFFER_SIZE MAX_ACTION + MAX_PATH + 10 // Size of buffer for incoming request data. + // Since only the first line is parsed this + // needs to be as large as the maximum action + // and path plus a little for whitespace and + // HTTP version. + +#define TIMEOUT_MS 500 // Amount of time in milliseconds to wait for + // an incoming request to finish. Don't set this + // too high or your server could be slow to respond. + + + + +#define WLAN_SSID "SSID" // cannot be longer than 32 characters! +#define WLAN_PASS "PASSWORD" + +#define WLAN_SECURITY WLAN_SEC_WPA // 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}" //"SHABIRMEAN" -#define DEVICE_ID "${DEVICE_ID}" //"vbhenqyt85yq" +#define DEVICE_OWNER "${DEVICE_OWNER}" +#define DEVICE_ID "${DEVICE_ID}" #define DEVICE_TOKEN "${DEVICE_TOKEN}" - #define SERVICE_PORT 9763 -#define SERVICE_EPOINT "/firealarm/controller/" - // 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 SERVICE_EPOINT "/firealarm/controller/" -#define POLL_INTERVAL 1000 +#define TEMP_PIN A5 +#define BULB_PIN A4 +#define FAN_PIN A3 #define DEBUG false #define CON_DEBUG true -#endif +static unsigned long pushTimestamp = 0; + +byte server[4] = { 192, 168, 1, 101 }; +String host, jsonPayLoad; +String responseMsg, subStrn; +uint32_t ipAddress; +#define PUSH_INTERVAL 30000 + +byte mac[6] = { 0xc0, 0x4a, 0x00, 0x1a, 0xb8, 0xbb }; + +#endif diff --git a/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino index 93e0b8ff..bae133f2 100644 --- a/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino +++ b/modules/distribution/src/sketches/firealarm_wifi/FireAlarmWifiAgent.ino @@ -1,156 +1,81 @@ #include "FireAlarmWifiAgent.h" #include +#include #include #include "dht.h" -#include 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 pushClient; -Adafruit_CC3000_Client pollClient; -static struct pt pushThread; - +Adafruit_CC3000_Server httpServer(LISTEN_PORT); uint32_t sserver; - /********************************************************************************************** - 0. Check with a sample Wifi code of the Adafruit_CC3000 library to ensure that the sheild is working - 1. Set the ip of the server(byte array below) where the Web-Rest API for the FireAlarm is running - 2. Check whether the "SERVICE_EPOINT" is correct in the 'FireAlarmWifiAgent.h' file - 3. Check whether the "SERVICE_PORT" is the same (9763) for the server running. Change it if needed - 4. Check whether the pins have been attached accordingly in the Arduino - 5. Check whether all reqquired pins are added to the 'digitalPins' array - ***********************************************************************************************/ +/********************************************************************************************** + 0. Check with a sample Wifi code of the Adafruit_CC3000 library to ensure that the sheild is working + 1. Set the ip of the server(byte array below) where the Web-Rest API for the FireAlarm is running + 2. Check whether the "SERVICE_EPOINT" is correct in the 'FireAlarmWifiAgent.h' file + 3. Check whether the "SERVICE_PORT" is the same (9763) for the server running. Change it if needed + 4. Check whether the pins have been attached accordingly in the Arduino + 5. Check whether all reqquired pins are added to the 'digitalPins' array +***********************************************************************************************/ +void setup() { + if (CON_DEBUG) Serial.begin(9600); -byte server[4] = { 10, 100, 7, 38 }; + pinMode(BULB_PIN, OUTPUT); + pinMode(FAN_PIN, OUTPUT); + pinMode(TEMP_PIN, INPUT); + setupResource(); -int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN }; -String host, jsonPayLoad, replyMsg; -String responseMsg, subStrn; + do { + connectHttp(); + } while (!cc3000.checkConnected()); -void setup() { - if(true) Serial.begin(115200); - pinMode(BULB_PIN, OUTPUT); - pinMode(FAN_PIN, OUTPUT); - - PT_INIT(&pushThread); - - connectHttp(); - setupResource(); + while (!pushClient.connected()) { + setupClient(); + } + + registerIP(); + + httpServer.begin(); + wdt_enable(WDTO_8S); } void loop() { - if (pushClient.connected() && pollClient.connected()) { - pushData(); // batches all the required pin values together and pushes once -// pushDigitalPinData(); // pushes pin data via multiple calls with a single pin data per call -// protothread1(&pushThread, 1000); // Pushes data and waits for control signals to be received - delay(POLL_INTERVAL); - - boolean valid = readControls(); - - if (!valid) { - if (responseMsg.equals("TEMPERATURE")) { - 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."); - } - pushClient.close(); - pollClient.close(); - cc3000.disconnect(); - - connectHttp(); - } -} + while (!cc3000.checkConnected()) { + connectHttp(); -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"; + wdt_reset(); + + if (millis() - pushTimestamp > PUSH_INTERVAL) { + wdt_reset(); + while (!pushClient.connected()) { + setupClient(); + } + pushData(); + pushTimestamp = millis(); } -} - - -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; -} - + readControls(); + wdt_reset(); + + if (subStrn.equals("ON")) { + if (responseMsg.equals("BULB")) { + digitalWrite(BULB_PIN, HIGH); + } else if (responseMsg.equals("FAN")) { + digitalWrite(FAN_PIN, HIGH); + } + } else if (subStrn.equals("OFF")) { + if (responseMsg.equals("BULB")) { + digitalWrite(BULB_PIN, LOW); + } else if (responseMsg.equals("FAN")) { + digitalWrite(FAN_PIN, LOW); + } + } + + wdt_reset(); -static int protothread1(struct pt *pt, int interval) { - static unsigned long timestamp = 0; - PT_BEGIN(pt); - while(1) { // never stop - /* each time the function it is checked whether any control signals are sent - * if so exit this proto thread - */ - PT_WAIT_UNTIL(pt, readControls() ); - pushData(); - } - PT_END(pt); } diff --git a/modules/distribution/src/sketches/firealarm_wifi/Listener.ino b/modules/distribution/src/sketches/firealarm_wifi/Listener.ino new file mode 100644 index 00000000..b94df8c8 --- /dev/null +++ b/modules/distribution/src/sketches/firealarm_wifi/Listener.ino @@ -0,0 +1,94 @@ +uint8_t buffer[BUFFER_SIZE + 1]; +int bufindex = 0; +char action[MAX_ACTION + 1]; +char path[MAX_PATH + 1]; + +void readControls() { + // Try to get a client which is connected. + Adafruit_CC3000_ClientRef client = httpServer.available(); + if (client) { + + bufindex = 0; + memset(&buffer, 0, sizeof(buffer)); + + // Clear action and path strings. + memset(&action, 0, sizeof(action)); + memset(&path, 0, sizeof(path)); + + // Set a timeout for reading all the incoming data. + unsigned long endtime = millis() + TIMEOUT_MS; + + // Read all the incoming data until it can be parsed or the timeout expires. + bool parsed = false; + while (!parsed && (millis() < endtime) && (bufindex < BUFFER_SIZE)) { + if (client.available()) { + buffer[bufindex++] = client.read(); + } + parsed = parseRequest(buffer, bufindex, action, path); + } + + wdt_reset(); + + if (parsed) { + if (strcmp(action, "GET") == 0) { + responseMsg = path; + + if (DEBUG) { + Serial.println(responseMsg); + } + + int index = responseMsg.lastIndexOf("/"); + int newLine = responseMsg.indexOf("/"); + subStrn = responseMsg.substring(index + 1); + responseMsg = responseMsg.substring(newLine + 1, index); + + if (DEBUG) { + Serial.print(responseMsg); + Serial.print(" - "); + Serial.println(subStrn); + Serial.println("-------------------------------"); + } + } + + if (responseMsg == "TEMP") { + int temperature = (uint8_t) getTemperature(); + + client.fastrprintln(F("HTTP/1.1 200 OK")); + client.fastrprintln(F("Connection: close")); + client.fastrprintln(F("")); + client.fastrprint(String(temperature).c_str()); + } + } + + delay(100); + + // Close the connection when done. + Serial.println(F("Client disconnected")); + client.close(); + wdt_reset(); + + } +} + +bool parseRequest(uint8_t *buf, int bufSize, char *action, char *path) { + // Check if the request ends with \r\n to signal end of first line. + if (bufSize < 2) + return false; + if (buf[bufSize - 2] == '\r' && buf[bufSize - 1] == '\n') { + parseFirstLine((char *) buf, action, path); + return true; + } + return false; +} + +// Parse the action and path from the first line of an HTTP request. +void parseFirstLine(char *line, char *action, char *path) { + // Parse first word up to whitespace as action. + char *lineaction = strtok(line, " "); + if (lineaction != NULL) + strncpy(action, lineaction, MAX_ACTION); + // Parse second word up to whitespace as path. + char *linepath = strtok(NULL, " "); + if (linepath != NULL) + strncpy(path, linepath, MAX_PATH); +} diff --git a/modules/distribution/src/sketches/firealarm_wifi/PollServer.ino b/modules/distribution/src/sketches/firealarm_wifi/PollServer.ino index 665f8791..e852a6bd 100644 --- a/modules/distribution/src/sketches/firealarm_wifi/PollServer.ino +++ b/modules/distribution/src/sketches/firealarm_wifi/PollServer.ino @@ -1,120 +1,33 @@ -boolean readControls() { -// String responseMsg; - - pollClient.fastrprint(F("GET ")); - pollClient.fastrprint(SERVICE_EPOINT); pollClient.fastrprint(F("readcontrols/")); - pollClient.fastrprint(DEVICE_OWNER); pollClient.fastrprint(F("/")); pollClient.fastrprint(DEVICE_ID); - pollClient.fastrprint(F(" HTTP/1.1")); pollClient.fastrprint(F("\n")); - pollClient.fastrprint(host.c_str()); pollClient.fastrprint(F("\n")); - pollClient.println(); - - delay(1000); - - if (true) { - while (pollClient.available()) { - char response = pollClient.read(); - responseMsg += response; - } - } - - - int index = responseMsg.lastIndexOf(":"); - int newLine = responseMsg.lastIndexOf("\n"); - subStrn = responseMsg.substring(index + 1); - responseMsg = responseMsg.substring(newLine + 1, index); - - if(DEBUG) { - Serial.print(responseMsg); - Serial.println(); - Serial.println("-------------------------------"); - } - - - if (subStrn.equals("IN")) { - return false; - } - - return true; -} - - - -void reply() { - String payLoad = replyMsg + "\"}"; - - if(DEBUG) { - Serial.print(jsonPayLoad); Serial.println(payLoad); - } - - pollClient.fastrprint(F("POST ")); - pollClient.fastrprint(SERVICE_EPOINT); pollClient.fastrprint(F("reply")); - pollClient.fastrprint(F(" HTTP/1.1")); pollClient.fastrprint(F("\n")); - pollClient.fastrprint(host.c_str()); pollClient.fastrprint(F("\n")); - pollClient.fastrprint(F("Content-Type: application/json")); pollClient.fastrprint(F("\n")); - pollClient.fastrprint(F("Content-Length: ")); - - int payLength = jsonPayLoad.length() + payLoad.length(); - - pollClient.fastrprint(String(payLength).c_str()); pollClient.fastrprint(F("\n")); - pollClient.fastrprint(F("\n")); - - if(DEBUG) { - Serial.print("POST "); - Serial.print(SERVICE_EPOINT); Serial.print("reply"); +void registerIP() { + pushClient.fastrprint(F("GET ")); + pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("register/")); + pushClient.fastrprint(DEVICE_OWNER); pushClient.fastrprint(F("/")); + pushClient.fastrprint(DEVICE_ID); pushClient.fastrprint(F("/")); + +// pushClient.fastrprint(deviceIP.c_str()); + pushClient.fastrprint(String((uint8_t)(ipAddress >> 24)).c_str()); + pushClient.fastrprint("."); + pushClient.fastrprint(String((uint8_t)(ipAddress >> 16)).c_str()); + pushClient.fastrprint("."); + pushClient.fastrprint(String((uint8_t)(ipAddress >> 8)).c_str()); + pushClient.fastrprint("."); + pushClient.fastrprint(String((uint8_t)ipAddress).c_str()); + + pushClient.fastrprint(F(" HTTP/1.1")); pushClient.fastrprint(F("\n")); + pushClient.fastrprint(host.c_str()); pushClient.fastrprint(F("\n")); + pushClient.println(); + + if (DEBUG) { + Serial.print("GET "); Serial.print(SERVICE_EPOINT); Serial.print("register/"); + Serial.print(DEVICE_OWNER); Serial.print("/"); Serial.print(DEVICE_ID); + Serial.print("/"); +// Serial.print(deviceIP); 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()) { - pollClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); - if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); - i = jsonPayLoad.length(); - } else { - pollClient.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()) { - pollClient.print(payLoad.substring(i*chunkSize, payLoad.length())); - if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length())); - i = payLoad.length(); - } else { - pollClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); - if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); - } + Serial.print(host); Serial.println(); Serial.println(); } - pollClient.fastrprint(F("\n")); - if(DEBUG) Serial.println(); - - delay(1000); - - if(true) { - while (pollClient.available()) { - char response = pollClient.read(); - if(DEBUG) Serial.print(response); - } - } - - if(DEBUG) { - Serial.println(); - Serial.println("-------------------------------"); - } - - payLoad = ""; -// delay(1000); + delay(100); + pushClient.flush(); + pushClient.close(); } - - - - diff --git a/modules/distribution/src/sketches/firealarm_wifi/PushData.ino b/modules/distribution/src/sketches/firealarm_wifi/PushData.ino index ea5bfc6e..4532ea31 100644 --- a/modules/distribution/src/sketches/firealarm_wifi/PushData.ino +++ b/modules/distribution/src/sketches/firealarm_wifi/PushData.ino @@ -1,30 +1,16 @@ - /********************************************************************************************** - This method will traverse the array of digital pins and batch the data from the those pins together. - It makes a single call to the server and sends all pin values as a batch. - Server dis-assembles it accordingly and makes multiple publish calls for each sensor type. - ***********************************************************************************************/ void pushData(){ String payLoad = "Data"; payLoad = payLoad + "\",\"value\":\""; - for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) { - 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"; - } - - if ( ((sizeof(digitalPins)/sizeof(int)) - 1) != pin ) { - payLoad += "-"; - } - - } - payLoad += "\"}"; + + payLoad += (uint8_t)getTemperature(); + payLoad += ":"; + payLoad += ( digitalRead(BULB_PIN) == HIGH )?"ON":"OFF"; + payLoad += ":"; + payLoad += ( digitalRead(FAN_PIN) == HIGH )?"ON":"OFF"; + payLoad += "\"}"; pushClient.fastrprint(F("POST ")); pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("pushalarmdata")); @@ -76,14 +62,18 @@ void pushData(){ pushClient.fastrprint(F("\n")); if(DEBUG) Serial.println(); - delay(1000); - - if(true) { - while (pushClient.available()) { - char response = pushClient.read(); - if(DEBUG) Serial.print(response); - } - } + delay(100); + + pushClient.flush(); + pushClient.close(); + wdt_reset(); + +// if(true) { +// while (pushClient.available()) { +// char response = pushClient.read(); +// if(DEBUG) Serial.print(response); +// } +// } if(DEBUG) { Serial.println(); @@ -93,186 +83,48 @@ void pushData(){ payLoad = ""; } - - /********************************************************************************************** - This method will traverse the array of digital pins and publish the data from the those pins. - It differs from the above method such that the pin data is published one after the other in - seperate calls to the server - ***********************************************************************************************/ - - -//void pushDigitalPinData(){ -// for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) { -// String payLoad = 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 += "\"}"; -// -// pushClient.fastrprint(F("POST ")); -// pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("pushalarmdata")); -// pushClient.fastrprint(F(" HTTP/1.1")); pushClient.fastrprint(F("\n")); -// pushClient.fastrprint(host.c_str()); pushClient.fastrprint(F("\n")); -// pushClient.fastrprint(F("Content-Type: application/json")); pushClient.fastrprint(F("\n")); -// pushClient.fastrprint(F("Content-Length: ")); -// -// int payLength = jsonPayLoad.length() + payLoad.length(); -// -// pushClient.fastrprint(String(payLength).c_str()); pushClient.fastrprint(F("\n")); -// pushClient.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 < jsonPayLoad.length(); i++) { -// if ( (i+1)*chunkSize > jsonPayLoad.length()) { -// pushClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); -// if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); -// i = jsonPayLoad.length(); -// } else { -// pushClient.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()) { -// pushClient.print(payLoad.substring(i*chunkSize, payLoad.length())); -// if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length())); -// i = payLoad.length(); -// } else { -// pushClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); -// if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); -// } -// } -// -// pushClient.fastrprint(F("\n")); -// if(DEBUG) Serial.println(); -// -// delay(1000); -// -// if(true) { -// while (pushClient.available()) { -// char response = pushClient.read(); -// if(DEBUG) Serial.print(response); -// } -// } -// -// if(DEBUG) { -// Serial.println(); -// Serial.println("-------------------------------"); -// } -// -// payLoad = ""; -//// delay(1000); -// } -//} - - /********************************************************************************************** - Only required for cases of reading analog pin values. - An int Array of analog pins that needs to be read has to be initialised. - This method will traverse the array and publish the data from the selected pins - ***********************************************************************************************/ - -//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 + "\"}"; -// -// pushClient.fastrprint(F("POST ")); -// pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("pushalarmdata")); -// pushClient.fastrprint(F(" HTTP/1.1")); pushClient.fastrprint(F("\n")); -// pushClient.fastrprint(host.c_str()); pushClient.fastrprint(F("\n")); -// pushClient.fastrprint(F("Content-Type: application/json")); pushClient.fastrprint(F("\n")); -// pushClient.fastrprint(F("Content-Length: ")); -// -// int payLength = payLoad.length(); -// -// pushClient.fastrprint(String(payLength).c_str()); pushClient.fastrprint(F("\n")); -// pushClient.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) { -// pushClient.print(payLoad.substring(i*chunkSize, payLength)); -// if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLength)); -// i = payLength; -// } else { -// pushClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); -// if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); -// } -// } -// -// pushClient.fastrprint(F("\n")); -// if(DEBUG) Serial.println(); -// -// delay(1000); -// -// if(true) { -// while (pushClient.available()) { -// char response = pushClient.read(); -// if(DEBUG) Serial.print(response); -// } -// } -// -// if(DEBUG) { -// Serial.println(); -// Serial.println("-------------------------------"); -// } -// -// payLoad = ""; -// delay(1000); -// } -//} - - - - -String getDataType(int pin){ - switch(pin){ - case TEMP_PIN: - return "TEMP"; - case BULB_PIN: - return "BULB"; - case FAN_PIN: - return "FAN"; - default: - return String(pin); +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/sketch.properties b/modules/distribution/src/sketches/firealarm_wifi/sketch.properties old mode 100644 new mode 100755 diff --git a/modules/distribution/src/sketches/sensebot_wifi/Connect.ino b/modules/distribution/src/sketches/sensebot_wifi/Connect.ino index 4f088d2b..5afdac35 100644 --- a/modules/distribution/src/sketches/sensebot_wifi/Connect.ino +++ b/modules/distribution/src/sketches/sensebot_wifi/Connect.ino @@ -1,6 +1,7 @@ - /********************************************************************************************** - Use the below variables when required to set a static IP for the WifiSheild - ***********************************************************************************************/ + +/********************************************************************************************** + 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 }; @@ -10,106 +11,106 @@ // 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() { - /* Initialise the module */ - if(DEBUG) Serial.println(F("\nInitializing...")); - if (!cc3000.begin()) - { - 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 +byte mac[6] = { 0xc0, 0x4a, 0x00, 0x1a, 0x08, 0xaa }; +//mac - c0:4a:00:1a:08:da +// c0:4a:00:1a:03:f8 +// b8:27:eb:88:37:7a - /***********************************************************************************************/ - - sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); - - if(CON_DEBUG) { - Serial.print(F("\nAttempting to connect to ")); - Serial.println(WLAN_SSID); - } - - if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { - if(CON_DEBUG) Serial.println(F("Failed!")); - while(1); - } - - if(CON_DEBUG) Serial.println(F("Connected to Wifi network!")); - if(CON_DEBUG) Serial.println(F("Request DHCP")); +void connectHttp() { + /* Initialise the module */ + if(DEBUG) Serial.println(F("\nInitializing...")); + if (!cc3000.begin()) + { + if(DEBUG) Serial.println(F("Couldn't begin()! Check your wiring?")); + while(1); + } + + 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 + + /***********************************************************************************************/ + + sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); + + if(CON_DEBUG) { + Serial.print(F("\nAttempting to connect to ")); + Serial.println(WLAN_SSID); + } + + if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { + if(CON_DEBUG) Serial.println(F("Failed!")); + while(1); + } + + if(CON_DEBUG) Serial.println(F("Connected to Wifi network!")); + + if(true) Serial.println(F("Request DHCP")); while (!cc3000.checkDHCP()) - { - delay(100); // ToDo: Insert a DHCP timeout! - } - - /* Display the IP address DNS, Gateway, etc. */ - while (! displayConnectionDetails()) { - delay(1000); - } - - pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT - - if (pushClient.connected()) { - if(CON_DEBUG) Serial.println("PushClient Connected to server"); - } else { - cc3000.disconnect(); - if(CON_DEBUG) Serial.println(F("PushClient Connection failed")); - } - - httpServer.begin(); - Serial.println(F("Listening for connections...")); - - if(CON_DEBUG) Serial.println(F("-------------------------------------")); + { + delay(100); // ToDo: Insert a DHCP timeout! + } + + /* Display the IP address DNS, Gateway, etc. */ + while (! displayConnectionDetails()) { + delay(1000); + } + + client = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT + if (client.connected()) { + if(true) Serial.println(F("client Connected to server")); + } else { + if(CON_DEBUG) Serial.println(F("client Connection failed")); + } + + + + + if(CON_DEBUG) 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 += "\",\"reply\":\""; - - if(DEBUG) { - Serial.print("JSON Payload: "); - Serial.println(jsonPayLoad); - Serial.println("-------------------------------"); - } +void setupResource(){ + String hostIP = getHostIP(server); + String port = String(SERVICE_PORT); +// + host = "Host: " + hostIP + ":" + port; +// if(DEBUG) Serial.println(host); + + jsonPayLoad = "{\"owner\":\""; + jsonPayLoad += DEVICE_OWNER; + jsonPayLoad += "\",\"deviceId\":\""; + jsonPayLoad += DEVICE_ID; + jsonPayLoad += "\",\"reply\":\""; + + if(DEBUG) { + Serial.print("JSON Payload: "); + Serial.println(jsonPayLoad); + Serial.println("-------------------------------"); + } } + String getHostIP(byte server[4]){ String hostIP = String(server[0]); @@ -123,23 +124,36 @@ String getHostIP(byte server[4]){ bool displayConnectionDetails(void) { - uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; - - if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) - { - if(DEBUG) Serial.println(F("Unable to retrieve the IP Address!\r\n")); - return false; - } - else - { - if(CON_DEBUG) { - Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); -// Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); -// Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); -// Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); -// Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); - Serial.println(); + uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; + + if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) + { + if(DEBUG) Serial.println(F("Unable to retrieve the IP Address!\r\n")); + return false; + } + else + { + if(CON_DEBUG) { + Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); + Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); + Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); + Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); + Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); + Serial.println(); + } + return true; + } +} + +void setupClient(){ + client = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT + if (client.connected()) { + if(CON_DEBUG) Serial.println(F("client Connected to server")); + } else { + while( !cc3000.checkConnected() ){ + connectHttp(); + + } + if(CON_DEBUG) Serial.println(F("client Connection failed")); } - return true; - } } diff --git a/modules/distribution/src/sketches/sensebot_wifi/MotorControls.ino b/modules/distribution/src/sketches/sensebot_wifi/Drive.ino similarity index 71% rename from modules/distribution/src/sketches/sensebot_wifi/MotorControls.ino rename to modules/distribution/src/sketches/sensebot_wifi/Drive.ino index 64622c3d..ee1bbd1b 100644 --- a/modules/distribution/src/sketches/sensebot_wifi/MotorControls.ino +++ b/modules/distribution/src/sketches/sensebot_wifi/Drive.ino @@ -1,4 +1,11 @@ + + +void updateDirectionVariable(int motionDir){ + motion_global = motionDir; +} + + void motor_stop(){ digitalWrite(motor_left[0], LOW); digitalWrite(motor_left[1], LOW); @@ -53,3 +60,19 @@ void turn_left(){ updateDirectionVariable(0); motor_stop(); } + +void drive(){ + switch(motion_global){ + case 1 : drive_forward(); + break; + case 2 : drive_backward(); + break; + case 3 : turn_left(); + break; + case 4 : turn_right(); + break; + case 5 : + motor_stop(); + break; + } +} diff --git a/modules/distribution/src/sketches/sensebot_wifi/Listener.ino b/modules/distribution/src/sketches/sensebot_wifi/HttpServer.ino similarity index 83% rename from modules/distribution/src/sketches/sensebot_wifi/Listener.ino rename to modules/distribution/src/sketches/sensebot_wifi/HttpServer.ino index 44636e93..972c1b7e 100644 --- a/modules/distribution/src/sketches/sensebot_wifi/Listener.ino +++ b/modules/distribution/src/sketches/sensebot_wifi/HttpServer.ino @@ -3,17 +3,12 @@ int bufindex = 0; char action[MAX_ACTION+1]; char path[MAX_PATH+1]; -boolean listen() +void readControls() { // Try to get a client which is connected. Adafruit_CC3000_ClientRef client = httpServer.available(); - if (client) { - //Serial.println(F("Client connected.")); - // Process this request until it completes or times out. - // Note that this is explicitly limited to handling one request at a time! - - // Clear the incoming data buffer and point to the beginning of it. + if(client){ bufindex = 0; memset(&buffer, 0, sizeof(buffer)); @@ -37,7 +32,7 @@ boolean listen() // Serial.print(F("Path: ")); Serial.println(path); if (strcmp(action, "GET") == 0) { - String urlPath = path; + urlPath = path; urlPath.replace("/move/",""); urlPath.replace("/",""); @@ -61,15 +56,11 @@ boolean listen() // Wait a short period to make sure the response had time to send before // the connection is closed (the CC3000 sends data asyncronously). - delay(100); - - // Close the connection when done. - Serial.println(F("Client disconnected")); + delay(200); client.close(); - return false; - } else { - return true; - } + + } + } diff --git a/modules/distribution/src/sketches/sensebot_wifi/PushData.ino b/modules/distribution/src/sketches/sensebot_wifi/PushData.ino index 6d59c56f..04ccb3ac 100644 --- a/modules/distribution/src/sketches/sensebot_wifi/PushData.ino +++ b/modules/distribution/src/sketches/sensebot_wifi/PushData.ino @@ -1,36 +1,31 @@ - /********************************************************************************************** - This method will traverse the array of digital pins and batch the data from the those pins together. - It makes a single call to the server and sends all pin values as a batch. - Server dis-assembles it accordingly and makes multiple publish calls for each sensor type. - ***********************************************************************************************/ void pushData(){ String payLoad = "Data"; payLoad = payLoad + "\",\"value\":\""; - int temperature = (uint8_t)getTemperature(); + payLoad += temperature; payLoad += ":"; - payLoad += digitalRead(PIR_PIN); + payLoad += pir; payLoad += ":"; - payLoad += getSonar(); // returns distance if < MAX_DISTANCE else returns -1, - // Pushed accordingly inside JAX-RS + payLoad += sonar; // returns distance if < MAX_DISTANCE else returns -1, + sonar=-1; // Pushed accordingly inside JAX-RS payLoad += ":"; - payLoad += analogRead(LDR_PIN); + payLoad += ldr; payLoad += "\"}"; - pushClient.fastrprint(F("POST ")); - pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("pushsensordata")); - pushClient.fastrprint(F(" HTTP/1.1")); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(host.c_str()); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(F("Content-Type: application/json")); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(F("Content-Length: ")); + client.fastrprint(F("POST ")); + client.fastrprint(SERVICE_EPOINT); client.fastrprint(F("pushsensordata")); + 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")); + client.fastrprint(F("Content-Length: ")); int payLength = jsonPayLoad.length() + payLoad.length() + 2; - pushClient.fastrprint(String(payLength).c_str()); pushClient.fastrprint(F("\n")); - pushClient.fastrprint(F("\n")); + client.fastrprint(String(payLength).c_str()); client.fastrprint(F("\n")); + client.fastrprint(F("\n")); if(DEBUG) { Serial.print("POST "); @@ -43,41 +38,45 @@ void pushData(){ Serial.println(); } + int chunkSize = 50; for (int i = 0; i < jsonPayLoad.length(); i++) { if ( (i+1)*chunkSize > jsonPayLoad.length()) { - pushClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); + client.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); i = jsonPayLoad.length(); } else { - pushClient.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize)); + client.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()) { - pushClient.print(payLoad.substring(i*chunkSize, payLoad.length())); + client.print(payLoad.substring(i*chunkSize, payLoad.length())); if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length())); i = payLoad.length(); } else { - pushClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); + client.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); } } - pushClient.fastrprint(F("\r\n")); + client.fastrprint(F("\r\n")); + if(DEBUG) Serial.println(); - delay(1000); - - if(true) { - while (pushClient.available()) { - char response = pushClient.read(); - if(DEBUG) Serial.print(response); - } - } + delay(100); + client.flush(); + client.close(); + wdt_reset(); +// if(true) { +// while (client.available()) { +// char response = client.read(); +// if(DEBUG) Serial.print(response); +// } +// } if(DEBUG) { Serial.println(); @@ -90,3 +89,98 @@ void pushData(){ + + + + + +double getTemperature(){ + int chk = DHT.read11(TEMP_PIN); + if(DEBUG){ + Serial.println("-------------------------------"); + Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)"); + Serial.print("DHT11, \t"); + + 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("-------------------------------"); + } + + temperature= DHT.temperature; +} + + +void getSonar() +{ + long duration, inches, cm; + + pinMode(SONAR_TRIG, OUTPUT);// attach pin 3 to Trig + digitalWrite(SONAR_TRIG, LOW); + delayMicroseconds(2); + digitalWrite(SONAR_TRIG, HIGH); + delayMicroseconds(5); + digitalWrite(SONAR_TRIG, LOW); + + pinMode (SONAR_ECHO, INPUT);//attach pin 4 to Echo + duration = pulseIn(SONAR_ECHO, HIGH); + + // convert the time into a distance + inches = microsecondsToInches(duration); + cm = microsecondsToCentimeters(duration); + + if(DEBUG){ + Serial.print("SONAR : "); + Serial.print(cm); + Serial.print(" , "); + Serial.println(inches); + Serial.println("-----------------------------------"); + } + + if (cm > MAX_DISTANCE || cm <= 0){ + //Serial.println("Out of range"); + noTone(BUZZER); + + + + } else { + tone(BUZZER, BUZZER_SOUND); + sonar= (uint8_t)cm; + } +} + +long microsecondsToInches(long microseconds){ + return microseconds / 74 / 2; +} + +long microsecondsToCentimeters(long microseconds){ + return microseconds / 29 / 2; +} + + diff --git a/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.h b/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.h index 7fce96d6..163316eb 100644 --- a/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.h +++ b/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.h @@ -1,37 +1,27 @@ -#ifndef SenseBotWifiAgent_H -#define SenseBotWifiAgent_H +#ifndef ArduinoWifiAgent_H +#define ArduinoWifiAgent_H -#if (ARDUINO >= 100) - #include "Arduino.h" -#else - #include "WProgram.h" -#endif +#include "Arduino.h" // 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 "YourAccessPointSSID" // cannot be longer than 32 characters! -#define WLAN_PASS "APPassword" - -#define WLAN_SECURITY WLAN_SEC_WPA2 - // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 +#define WLAN_SSID "SSID" // cannot be longer than 32 characters! +#define WLAN_PASS "password" -#define DEVICE_OWNER "${DEVICE_OWNER}" -#define DEVICE_ID "${DEVICE_ID}" +#define WLAN_SECURITY WLAN_SEC_WPA +// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 +#define IDLE_TIMEOUT_MS 3000 -/********************************************************************************************** - The following two parameters are used when pushing data via the API in the API Manager - ***********************************************************************************************/ -#define SERVICE_PORT 8281 -#define SERVICE_EPOINT "/sensebotdata/1.0/controller/" +#define DEVICE_OWNER "${DEVICE_OWNER}" +#define DEVICE_ID "${DEVICE_ID}" +#define DEVICE_TOKEN "${DEVICE_TOKEN}" -/********************************************************************************************** - Use the following two parameters when pushing data directly to the JAX-RS Service - ***********************************************************************************************/ -//#define SERVICE_PORT 9763 -//#define SERVICE_EPOINT "/sensebot/controller/" +#define SERVICE_PORT 9763 +#define SERVICE_EPOINT "/sensebot/controller/" #define BUZZER A0 #define LDR_PIN A1 @@ -45,27 +35,40 @@ #define TURN_DELAY 100 -#define LISTEN_PORT 80 // What TCP port to listen on for connections. - // The HTTP protocol uses port 80 by default. - +#define PUSH_INTERVAL 30000 #define MAX_ACTION 6 // Maximum length of the HTTP action that can be parsed. -#define MAX_PATH 10 // Maximum length of the HTTP request path that can be parsed. - // There isn't much memory available so keep this short! - +#define MAX_PATH 10 #define BUFFER_SIZE MAX_ACTION + MAX_PATH + 10 // Size of buffer for incoming request data. - // Since only the first line is parsed this - // needs to be as large as the maximum action - // and path plus a little for whitespace and - // HTTP version. -#define TIMEOUT_MS 500 // Amount of time in milliseconds to wait for - // an incoming request to finish. Don't set this - // too high or your server could be slow to respond. +#define TIMEOUT_MS 500 #define DEBUG false -#define CON_DEBUG true +#define CON_DEBUG false + + + +byte server[4] = { 192, 168, 1, 101 }; +String host, jsonPayLoad; + unsigned long pushTimestamp = 0; + unsigned long pollTimestamp = 0; + +#define LISTEN_PORT 80 + +byte motion_global = 0; +byte temperature = 0; +int pir =0; +int sonar=0; +int ldr =0; + +const byte motor_left[] = {7, 8}; +const byte enA = 12; + +const byte motor_right[] = {4, 6}; +const byte enB = 11; +String urlPath; #endif + diff --git a/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.ino b/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.ino index 95d0f8e0..ea675b40 100644 --- a/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.ino +++ b/modules/distribution/src/sketches/sensebot_wifi/SenseBotWifiAgent.ino @@ -1,134 +1,77 @@ #include "SenseBotWifiAgent.h" - #include -#include #include +#include #include "dht.h" -#include - 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 pushClient; +Adafruit_CC3000_Client client; Adafruit_CC3000_Server httpServer(LISTEN_PORT); -static struct pt pushThread; - -int motor_left[] = {7, 8}; -int enA = 12; - -int motor_right[] = {4, 6}; -int enB = 11; - - -int motion_global = 0; - - /********************************************************************************************** - 0. Check with a sample Wifi code of the Adafruit_CC3000 library to ensure that the sheild is working - 1. Set the ip of the server(byte array below) where the Web-Rest API for the FireAlarm is running - 2. Check whether the "SERVICE_EPOINT" is correct in the 'EUHackothonRobot.h.h' file - 3. Check whether the "SERVICE_PORT" is the same (9763) for the server running. Change it if needed - 4. Check whether the pins have been attached accordingly in the Arduino - 5. Check whether all reqquired pins are added to the 'digitalPins' array - ***********************************************************************************************/ - uint32_t sserver; -byte server[4] = { XX, XX, XX, XX }; - -String host, jsonPayLoad; dht DHT; - - - -void setup() { - Serial.begin(115200); - pinMode(PIR_PIN, INPUT); - - for(int i = 0; i < 2; i++){ - pinMode(motor_left[i], OUTPUT); - pinMode(motor_right[i], OUTPUT); - } - - pinMode(enA, OUTPUT); - pinMode(enB, OUTPUT); - digitalWrite(enA, 100); - digitalWrite(enB, 100); - motor_stop(); - - PT_INIT(&pushThread); - - connectHttp(); - setupResource(); - wdt_enable(WDTO_4S); -} - - -void loop() { - - protothread1(&pushThread, 1000); - -// listen(); -// wdt_reset(); - // Check connection - if( !cc3000.checkConnected() ){ - while(1){ +void setup() +{ + if(CON_DEBUG) Serial.begin(9600); + + pinMode(PIR_PIN, INPUT); + pinMode(LDR_PIN, INPUT); + pinMode(TEMP_PIN, INPUT); + for(int i = 0; i < 2; i++){ + pinMode(motor_left[i], OUTPUT); + pinMode(motor_right[i], OUTPUT); } - } + + pinMode(enA, OUTPUT); + pinMode(enB, OUTPUT); + digitalWrite(enA, 100); + digitalWrite(enB, 100); + motor_stop(); - wdt_reset(); - drive(); - -} - - -void drive(){ - switch(motion_global){ - case 1 : drive_forward(); - break; - case 2 : drive_backward(); - break; - case 3 : turn_left(); - break; - case 4 : turn_right(); - break; - case 5 : - motor_stop(); - break; - } -} - - -void updateDirectionVariable(int motionDir){ - motion_global = motionDir; + do{ + connectHttp(); + }while( !cc3000.checkConnected() ); + + setupResource(); + httpServer.begin(); + wdt_enable(WDTO_8S); } +void loop() +{ + while( !cc3000.checkConnected() ){ + connectHttp(); + + } + + if(millis() - pollTimestamp > TURN_DELAY){ + wdt_reset(); + readControls(); + drive(); + pollTimestamp = millis(); + + } + //Serial.println(F("looping")); + getTemperature(); + pir =digitalRead(PIR_PIN); + getSonar(); + ldr =analogRead(LDR_PIN); + + if(millis() - pushTimestamp > PUSH_INTERVAL){ + wdt_reset(); + while (!client.connected()) { + setupClient(); + } + pushData(); + // Serial.print("looping"); + pushTimestamp = millis(); + } - -static int protothread1(struct pt *pt, int interval) { - PT_BEGIN(pt); - while(1) { // never stop - /* each time the function is called the second boolean - * argument "millis() - timestamp > interval" is re-evaluated - * and if false the function exits after that. */ - PT_WAIT_UNTIL(pt, listen() ); - - if (pushClient.connected()) { - // batches all the required pin values together and pushes once - // Pushes data in 1 second interval - pushData(); - wdt_reset(); - } else { - pushClient.close(); - cc3000.disconnect(); - connectHttp(); - } - } - PT_END(pt); + wdt_reset(); } - - diff --git a/modules/distribution/src/sketches/sensebot_wifi/Sensors.ino b/modules/distribution/src/sketches/sensebot_wifi/Sensors.ino deleted file mode 100644 index 6aea97b4..00000000 --- a/modules/distribution/src/sketches/sensebot_wifi/Sensors.ino +++ /dev/null @@ -1,107 +0,0 @@ -//int motionSense(){ -// int motionDetect = digitalRead(PIR_PIN); -// if(DEBUG){ -// Serial.print("MOTION : "); -// Serial.println(motionDetect); -// } -// return motionDetect; -//} - - -//int lightSense(){ -// int lightLevel = analogRead(LDR_PIN); -// if(DEBUG){ -// Serial.print("LIGHT : "); -// Serial.println(lightLevel); -// } -// return lightLevel; -//} - -double getTemperature(){ - int chk = DHT.read11(TEMP_PIN); - if(DEBUG){ - Serial.println("-------------------------------"); - Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)"); - Serial.print("DHT11, \t"); - - 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; -} - - -int getSonar() -{ - long duration, inches, cm; - - pinMode(SONAR_TRIG, OUTPUT);// attach pin 3 to Trig - digitalWrite(SONAR_TRIG, LOW); - delayMicroseconds(2); - digitalWrite(SONAR_TRIG, HIGH); - delayMicroseconds(5); - digitalWrite(SONAR_TRIG, LOW); - - pinMode (SONAR_ECHO, INPUT);//attach pin 4 to Echo - duration = pulseIn(SONAR_ECHO, HIGH); - - // convert the time into a distance - inches = microsecondsToInches(duration); - cm = microsecondsToCentimeters(duration); - - if(DEBUG){ - Serial.print("SONAR : "); - Serial.print(cm); - Serial.print(" , "); - Serial.println(inches); - Serial.println("-----------------------------------"); - } - - if (cm > MAX_DISTANCE || cm <= 0){ - //Serial.println("Out of range"); - noTone(BUZZER); - return -1; - } else { - tone(BUZZER, BUZZER_SOUND); - return cm; - } -} - -long microsecondsToInches(long microseconds){ - return microseconds / 74 / 2; -} - -long microsecondsToCentimeters(long microseconds){ - return microseconds / 29 / 2; -} - - diff --git a/pom.xml b/pom.xml index 032f35fa..e0e54177 100644 --- a/pom.xml +++ b/pom.xml @@ -372,6 +372,11 @@ org.wso2.carbon.device.mgt.iot.arduino.impl ${carbon.iot.device.mgt.version} + + org.wso2.carbon.iot + org.wso2.carbon.device.mgt.iot.android.impl + ${carbon.iot.device.mgt.version} + org.wso2.carbon.iot org.wso2.carbon.device.mgt.iot.digitaldisplay.impl