changes to the configs

merge-requests/1/head
Rasika 10 years ago
parent fa35fe52c0
commit c3aff9c4eb

@ -402,14 +402,22 @@
<filtered>true</filtered> <filtered>true</filtered>
<fileMode>644</fileMode> <fileMode>644</fileMode>
</file> </file>
<file> <file>
<source> <source>
../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/iot-config.xml ../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/iot-config.xml
</source> </source>
<outputDirectory>${pom.artifactId}-${pom.version}/repository/conf/etc/device-mgt-plugin-configs/iot</outputDirectory> <outputDirectory>${pom.artifactId}-${pom.version}/repository/conf/iot</outputDirectory>
<filtered>true</filtered> <filtered>true</filtered>
<fileMode>644</fileMode> <fileMode>644</fileMode>
</file> </file>
<file>
<source>
../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/devicecloud-config.xml
</source>
<outputDirectory>${pom.artifactId}-${pom.version}/repository/conf/iot</outputDirectory>
<filtered>true</filtered>
<fileMode>644</fileMode>
</file>
<!-- <file> <!-- <file>
<source>../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/event-broker.xml <source>../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/conf/event-broker.xml
</source> </source>

@ -1,5 +1,5 @@
#ifndef FireAlarmAgent_H #ifndef FireAlarmEthernetAgent_H
#define FireAlarmAgent_H #define FireAlarmEthernetAgent_H
#if (ARDUINO >= 100) #if (ARDUINO >= 100)
#include "Arduino.h" #include "Arduino.h"
@ -13,10 +13,9 @@
#define HTTP_CONTENT_TYPE "Content-Type: application/json" #define HTTP_CONTENT_TYPE "Content-Type: application/json"
#define HTTP_CONTENT_LEN "Content-Length: " #define HTTP_CONTENT_LEN "Content-Length: "
#define DEVICE_OWNER "${DEVICE_OWNER}" #define DEVICE_OWNER "${DEVICE_OWNER}" //"Smeansbeer"
#define DEVICE_TYPE "FireAlarm" #define DEVICE_ID "${DEVICE_ID}" //"vbhenqyt85yq"
#define DEVICE_ID "${DEVICE_ID}" #define DEVICE_TOKEN "${DEVICE_TOKEN}"
#define DEVICE_TOKEN "${DEVICE_TOKEN}"
#define PUSH_ALARM_DATA "pushalarmdata" #define PUSH_ALARM_DATA "pushalarmdata"
#define READ_CONTROLS "readcontrols/" #define READ_CONTROLS "readcontrols/"
@ -31,7 +30,7 @@
#define END_JSON "\"}" #define END_JSON "\"}"
#define SERVICE_PORT 9763 #define SERVICE_PORT 9763
#define SERVICE_EPOINT "/WSO2ConnectedDevices/FireAlarmController/" #define SERVICE_EPOINT "/iotdevices/FireAlarmController/"
// pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""} // pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""}
// readcontrols/{owner}/{deviceId} // readcontrols/{owner}/{deviceId}
// reply - application/json - {"owner":"","deviceId":"","replyMessage":""} // reply - application/json - {"owner":"","deviceId":"","replyMessage":""}

@ -1,9 +1,18 @@
#include "FireAlarmAgent.h" #include "FireAlarmEthernetAgent.h"
#include <Ethernet.h> #include <Ethernet.h>
#include <SPI.h> #include <SPI.h>
#include "dht.h" #include "dht.h"
/**********************************************************************************************
0. Check with a sample Ethernet code of the Ethernet 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
***********************************************************************************************/
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN }; int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
int analogPins[] = { 0, 1, 2, 3, 4, 5 }; int analogPins[] = { 0, 1, 2, 3, 4, 5 };
@ -21,6 +30,7 @@ void setup() {
void loop() { void loop() {
if (httpClient.connected()) { if (httpClient.connected()) {
pushDigitalPinData(); pushDigitalPinData();
// pushData(); // Use this method to batch all data together and send in one call
delay(POLL_INTERVAL); delay(POLL_INTERVAL);

@ -1,3 +1,81 @@
/**********************************************************************************************
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 resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " ";
String payLoad = jsonPayLoad + "DigitalPinData";
payLoad = payLoad + String(TIME_JSON) + "9999";
payLoad = payLoad + String(KEY_JSON) + "Data";
payLoad += String(VALUE_JSON);
for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) {
if ( digitalPins[pin] == TEMP_PIN ) {
payLoad += String(getTemperature());
} 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 + String(END_JSON);
if(DEBUG) Serial.println(payLoad);
httpClient.print(HTTP_POST);
httpClient.print(resource);
httpClient.println(HTTP_VERSION);
httpClient.println(host);
httpClient.println(HTTP_CONTENT_TYPE);
httpClient.print(HTTP_CONTENT_LEN);
httpClient.println(payLoad.length());
httpClient.println();
httpClient.println(payLoad);
httpClient.println();
if(DEBUG) {
Serial.print(HTTP_POST);
Serial.print(resource);
Serial.println(HTTP_VERSION);
Serial.println(host);
Serial.println(HTTP_CONTENT_TYPE);
Serial.print(HTTP_CONTENT_LEN);
Serial.println(payLoad.length());
Serial.println();
Serial.println(payLoad);
Serial.println();
}
delay(1000);
while (httpClient.available()) {
char response = httpClient.read();
if(DEBUG) Serial.print(response);
}
if(DEBUG) {
Serial.println();
Serial.println("-------------------------------");
}
delay(1000);
}
/**********************************************************************************************
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(){ void pushDigitalPinData(){
for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) { for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) {
String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " "; String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " ";
@ -59,6 +137,12 @@ void pushDigitalPinData(){
} }
} }
/**********************************************************************************************
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(){ void pushAnalogPinData(){
for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) { for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) {

@ -1 +1 @@
templates=FireAlarmAgent.h templates=FireAlarmEthernetAgent.h

@ -1,57 +1,97 @@
byte mac[6] = { 0x90, 0xA2, 0xDA, 0x0D, 0x30, 0xD7}; //mac - 90a2da0d30d7 /**********************************************************************************************
byte dns2[] = { 8, 8, 8, 8 }; Use the below variables when required to set a static IP for the WifiSheild
byte subnet[] = { 255, 255, 255, 0 }; ***********************************************************************************************/
byte gateway[] = { 192, 168, 1, 1 }; // byte dns2[] = { 8, 8, 8, 8 };
byte deviceIP[4] = { 192, 168, 1, 219 }; // byte subnet[] = { 255, 255, 255, 0 };
byte server[4] = { 192, 168, 1, 216 }; // byte gateway[] = { 10, 100, 9, 254 };
//byte server[4] = { 207, 58, 139, 247 }; // byte deviceIP[4] = { 10, 100, 9, 9 };
// byte gateway[] = { 192, 168, 1, 1 };
uint32_t ip, ddns, ssubnet, ggateway, sserver; // byte deviceIP[4] = { 192, 168, 1, 219 };
// 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.... "; String connecting = "connecting.... ";
void connectHttp() { void connectHttp() {
/* Initialise the module */ /* Initialise the module */
if(true) Serial.println(F("\nInitializing...")); if(DEBUG) Serial.println(F("\nInitializing..."));
if (!cc3000.begin()) if (!cc3000.begin())
{ {
if(true) Serial.println(F("Couldn't begin()! Check your wiring?")); if(DEBUG) Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1); while(1);
} }
ip = cc3000.IP2U32(deviceIP[0], deviceIP[1], deviceIP[2], deviceIP[3]); // if( cc3000.setMacAddress(mac) ) { // Set your own mac and print it to re-check
ddns = cc3000.IP2U32(dns2[0], dns2[1], dns2[2], dns2[3]); // uint8_t address[6];
ssubnet = cc3000.IP2U32(subnet[0], subnet[1], subnet[2], subnet[3]); // cc3000.getMacAddress(address);
ggateway = cc3000.IP2U32(gateway[0], gateway[1], gateway[2], gateway[3]); // if(DEBUG){
sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]); // 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
/***********************************************************************************************/
cc3000.setStaticIPAddress(ip, ssubnet, ggateway, ddns); sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]);
if(true) { if(CON_DEBUG) {
Serial.print(F("\nAttempting to connect to ")); Serial.print(F("\nAttempting to connect to "));
Serial.println(WLAN_SSID); Serial.println(WLAN_SSID);
} }
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
if(true) Serial.println(F("Failed!")); if(CON_DEBUG) Serial.println(F("Failed!"));
while(1); while(1);
} }
if(true) Serial.println(F("Connected to Wifi network!")); 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);
}
httpClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT
if (httpClient.connected()) { if (pushClient.connected()) {
if(true) Serial.println("Connected to server"); if(CON_DEBUG) Serial.println("PushClient Connected to server");
} else { } else {
if(true) Serial.println(F("Connection failed")); cc3000.disconnect();
if(CON_DEBUG) Serial.println(F("PushClient Connection failed"));
}
while(!httpClient.connected()){
if(true) Serial.println("retrying to connect......"); pollClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT
httpClient = cc3000.connectTCP(sserver, SERVICE_PORT); if (pollClient.connected()) {
delay(1000); if(CON_DEBUG) Serial.println("PollClient Connected to server");
} } else {
cc3000.disconnect();
if(CON_DEBUG) Serial.println(F("PollClient Connection failed"));
} }
if(true) Serial.println(F("-------------------------------------")); if(CON_DEBUG) Serial.println(F("-------------------------------------"));
} }
@ -66,7 +106,7 @@ void setupResource(){
jsonPayLoad += String(DEVICE_OWNER); jsonPayLoad += String(DEVICE_OWNER);
jsonPayLoad += "\",\"deviceId\":\""; jsonPayLoad += "\",\"deviceId\":\"";
jsonPayLoad += String(DEVICE_ID); jsonPayLoad += String(DEVICE_ID);
jsonPayLoad += "\",\"replyMessage\":\""; jsonPayLoad += "\",\"reply\":\"";
if(DEBUG) { if(DEBUG) {
Serial.print("JSON Payload: "); Serial.print("JSON Payload: ");
@ -75,24 +115,36 @@ void setupResource(){
} }
} }
String getHostIP(byte server[4]){
String getMyIP(byte deviceIP[4]){ String hostIP = String(server[0]);
String myIP = String(deviceIP[0]);
for ( int index = 1; index < 4; index++) { for ( int index = 1; index < 4; index++) {
myIP += "." + String(deviceIP[index]); hostIP += "." + String(server[index]);
} }
return myIP; return hostIP;
} }
String getHostIP(byte server[4]){ bool displayConnectionDetails(void)
String hostIP = String(server[0]); {
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
for ( int index = 1; index < 4; index++) { if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
hostIP += "." + String(server[index]); {
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;
} }
return hostIP;
} }

@ -13,19 +13,17 @@
#define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10 #define ADAFRUIT_CC3000_CS 10
#define WLAN_SSID "Dialog 4G" // cannot be longer than 32 characters!
#define WLAN_PASS "FA09C543"
#define WLAN_SECURITY WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA2
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or 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 IDLE_TIMEOUT_MS 3000
#define DEVICE_OWNER "${DEVICE_OWNER}" #define DEVICE_OWNER "${DEVICE_OWNER}" //"SHABIRMEAN"
#define DEVICE_TYPE "FireAlarm" #define DEVICE_ID "${DEVICE_ID}" //"vbhenqyt85yq"
#define DEVICE_ID "${DEVICE_ID}" #define DEVICE_TOKEN "${DEVICE_TOKEN}"
#define DEVICE_TOKEN "${DEVICE_TOKEN}"
#define SERVICE_PORT 9763 #define SERVICE_PORT 9763
#define SERVICE_EPOINT "/WSO2ConnectedDevices/FireAlarmController/" #define SERVICE_EPOINT "/iotdevices/FireAlarmController/"
// pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""} // pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""}
// readcontrols/{owner}/{deviceId} // readcontrols/{owner}/{deviceId}
// reply - application/json - {"owner":"","deviceId":"","replyMessage":""} // reply - application/json - {"owner":"","deviceId":"","replyMessage":""}
@ -36,6 +34,7 @@
#define POLL_INTERVAL 1000 #define POLL_INTERVAL 1000
#define DEBUG false #define DEBUG false
#define CON_DEBUG true
#endif #endif

@ -1,43 +1,57 @@
#include "FireAlarmWifiAgent.h" #include "FireAlarmWifiAgent.h"
#include <Adafruit_CC3000.h> #include <Adafruit_CC3000.h>
//#include <ccspi.h>
//#include <string.h>
//#include "utility/debug.h"
#include <SPI.h> #include <SPI.h>
#include "dht.h" #include "dht.h"
#include <pt.h>
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
int analogPins[] = { 0, 1, 2, 3, 4, 5 };
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed SPI_CLOCK_DIVIDER); // you can change this clock speed
Adafruit_CC3000_Client httpClient; Adafruit_CC3000_Client pushClient;
Adafruit_CC3000_Client pollClient;
static struct pt pushThread;
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] = { 10, 100, 7, 38 };
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
String host, jsonPayLoad, replyMsg; String host, jsonPayLoad, replyMsg;
String responseMsg, subStrn;
void setup() { void setup() {
if(true) Serial.begin(115200); if(true) Serial.begin(115200);
pinMode(BULB_PIN, OUTPUT); pinMode(BULB_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT); pinMode(FAN_PIN, OUTPUT);
PT_INIT(&pushThread);
connectHttp(); connectHttp();
setupResource(); setupResource();
} }
void loop() { void loop() {
if (httpClient.connected()) { if (pushClient.connected() && pollClient.connected()) {
pushDigitalPinData(); 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); delay(POLL_INTERVAL);
String responseMsg = readControls(); boolean valid = readControls();
int index = responseMsg.lastIndexOf(":");
int newLine = responseMsg.lastIndexOf("\n");
String subStrn = responseMsg.substring(index + 1);
if (subStrn.equals("IN")) { if (!valid) {
responseMsg = responseMsg.substring(newLine + 1, index); if (responseMsg.equals("TEMPERATURE")) {
if (responseMsg.equals("TEMP")) {
int temperature = (uint8_t)getTemperature(); int temperature = (uint8_t)getTemperature();
replyMsg = "Temperature is " + String(temperature) + " C"; replyMsg = "Temperature is " + String(temperature) + " C";
reply(); reply();
@ -52,28 +66,15 @@ void loop() {
Serial.println("client not found..."); Serial.println("client not found...");
Serial.println("disconnecting."); Serial.println("disconnecting.");
} }
pushClient.close();
pollClient.close();
cc3000.disconnect();
httpClient.stop();
connectHttp(); connectHttp();
} }
} }
String getDataType(int pin){
switch(pin){
case TEMP_PIN:
return "Temperature";
case BULB_PIN:
return "Bulb";
case FAN_PIN:
return "Fan";
default:
return String(pin);
}
}
String switchBulb() { String switchBulb() {
if (digitalRead(BULB_PIN) == HIGH) { if (digitalRead(BULB_PIN) == HIGH) {
digitalWrite(BULB_PIN, LOW); digitalWrite(BULB_PIN, LOW);
@ -139,3 +140,17 @@ double getTemperature(){
} }
return DHT.temperature; return DHT.temperature;
} }
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);
}

@ -1,30 +1,44 @@
String readControls() { boolean readControls() {
String responseMsg; // String responseMsg;
httpClient.fastrprint(F("GET ")); pollClient.fastrprint(F("GET "));
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("readcontrols/")); pollClient.fastrprint(SERVICE_EPOINT); pollClient.fastrprint(F("readcontrols/"));
httpClient.fastrprint(DEVICE_OWNER); httpClient.fastrprint(F("/")); httpClient.fastrprint(DEVICE_ID); pollClient.fastrprint(DEVICE_OWNER); pollClient.fastrprint(F("/")); pollClient.fastrprint(DEVICE_ID);
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n")); pollClient.fastrprint(F(" HTTP/1.1")); pollClient.fastrprint(F("\n"));
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n")); pollClient.fastrprint(host.c_str()); pollClient.fastrprint(F("\n"));
httpClient.println(); pollClient.println();
delay(1000); delay(1000);
while (httpClient.available()) { if (true) {
char response = httpClient.read(); while (pollClient.available()) {
responseMsg += response; 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) { if(DEBUG) {
Serial.print(responseMsg); Serial.print(responseMsg);
Serial.println(); Serial.println();
Serial.println("-------------------------------"); Serial.println("-------------------------------");
} }
delay(1000);
return responseMsg; if (subStrn.equals("IN")) {
return false;
}
return true;
} }
void reply() { void reply() {
String payLoad = replyMsg + "\"}"; String payLoad = replyMsg + "\"}";
@ -32,17 +46,17 @@ void reply() {
Serial.print(jsonPayLoad); Serial.println(payLoad); Serial.print(jsonPayLoad); Serial.println(payLoad);
} }
httpClient.fastrprint(F("POST ")); pollClient.fastrprint(F("POST "));
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("reply")); pollClient.fastrprint(SERVICE_EPOINT); pollClient.fastrprint(F("reply"));
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n")); pollClient.fastrprint(F(" HTTP/1.1")); pollClient.fastrprint(F("\n"));
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n")); pollClient.fastrprint(host.c_str()); pollClient.fastrprint(F("\n"));
httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n")); pollClient.fastrprint(F("Content-Type: application/json")); pollClient.fastrprint(F("\n"));
httpClient.fastrprint(F("Content-Length: ")); pollClient.fastrprint(F("Content-Length: "));
int payLength = jsonPayLoad.length() + payLoad.length(); int payLength = jsonPayLoad.length() + payLoad.length();
httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n")); pollClient.fastrprint(String(payLength).c_str()); pollClient.fastrprint(F("\n"));
httpClient.fastrprint(F("\n")); pollClient.fastrprint(F("\n"));
if(DEBUG) { if(DEBUG) {
Serial.print("POST "); Serial.print("POST ");
@ -59,34 +73,36 @@ void reply() {
for (int i = 0; i < jsonPayLoad.length(); i++) { for (int i = 0; i < jsonPayLoad.length(); i++) {
if ( (i+1)*chunkSize > jsonPayLoad.length()) { if ( (i+1)*chunkSize > jsonPayLoad.length()) {
httpClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); pollClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length())); if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
i = jsonPayLoad.length(); i = jsonPayLoad.length();
} else { } else {
httpClient.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize)); pollClient.print(jsonPayLoad.substring(i*chunkSize, (i+1)*chunkSize));
if(DEBUG) Serial.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++) { for (int i = 0; i < payLoad.length(); i++) {
if ( (i+1)*chunkSize > payLoad.length()) { if ( (i+1)*chunkSize > payLoad.length()) {
httpClient.print(payLoad.substring(i*chunkSize, payLoad.length())); pollClient.print(payLoad.substring(i*chunkSize, payLoad.length()));
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length())); if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length()));
i = payLoad.length(); i = payLoad.length();
} else { } else {
httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); pollClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
} }
} }
httpClient.fastrprint(F("\n")); pollClient.fastrprint(F("\n"));
if(DEBUG) Serial.println(); if(DEBUG) Serial.println();
delay(1000); delay(1000);
while (httpClient.available()) { if(true) {
char response = httpClient.read(); while (pollClient.available()) {
if(DEBUG) Serial.print(response); char response = pollClient.read();
if(DEBUG) Serial.print(response);
}
} }
if(DEBUG) { if(DEBUG) {
@ -95,7 +111,7 @@ void reply() {
} }
payLoad = ""; payLoad = "";
delay(1000); // delay(1000);
} }

@ -1,10 +1,15 @@
void pushDigitalPinData(){
for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) {
String payLoad = jsonPayLoad + "DigitalPinData";
payLoad = payLoad + "\",\"time\":\"" + "9999";
payLoad = payLoad + "\",\"key\":\"" + getDataType(digitalPins[pin]);
payLoad = payLoad + "\",\"value\":\"";
/**********************************************************************************************
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 ) { if ( digitalPins[pin] == TEMP_PIN ) {
int temperature = (uint8_t)getTemperature(); int temperature = (uint8_t)getTemperature();
payLoad += temperature; payLoad += temperature;
@ -14,19 +19,24 @@ void pushDigitalPinData(){
payLoad += "OFF"; payLoad += "OFF";
} }
if ( ((sizeof(digitalPins)/sizeof(int)) - 1) != pin ) {
payLoad += "-";
}
}
payLoad += "\"}"; payLoad += "\"}";
httpClient.fastrprint(F("POST ")); pushClient.fastrprint(F("POST "));
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("pushalarmdata")); pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("pushalarmdata"));
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n")); pushClient.fastrprint(F(" HTTP/1.1")); pushClient.fastrprint(F("\n"));
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n")); pushClient.fastrprint(host.c_str()); pushClient.fastrprint(F("\n"));
httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n")); pushClient.fastrprint(F("Content-Type: application/json")); pushClient.fastrprint(F("\n"));
httpClient.fastrprint(F("Content-Length: ")); pushClient.fastrprint(F("Content-Length: "));
int payLength = payLoad.length(); int payLength = jsonPayLoad.length() + payLoad.length();
httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n")); pushClient.fastrprint(String(payLength).c_str()); pushClient.fastrprint(F("\n"));
httpClient.fastrprint(F("\n")); pushClient.fastrprint(F("\n"));
if(DEBUG) { if(DEBUG) {
Serial.print("POST "); Serial.print("POST ");
@ -41,25 +51,38 @@ void pushDigitalPinData(){
int chunkSize = 50; int chunkSize = 50;
for (int i = 0; i < payLength; i++) { for (int i = 0; i < jsonPayLoad.length(); i++) {
if ( (i+1)*chunkSize > payLength) { if ( (i+1)*chunkSize > jsonPayLoad.length()) {
httpClient.print(payLoad.substring(i*chunkSize, payLength)); pushClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLength)); if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
i = payLength; 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 { } else {
httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); pushClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize)); if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
} }
} }
httpClient.fastrprint(F("\n")); pushClient.fastrprint(F("\n"));
if(DEBUG) Serial.println(); if(DEBUG) Serial.println();
delay(1000); delay(1000);
while (httpClient.available()) { if(true) {
char response = httpClient.read(); while (pushClient.available()) {
if(DEBUG) Serial.print(response); char response = pushClient.read();
if(DEBUG) Serial.print(response);
}
} }
if(DEBUG) { if(DEBUG) {
@ -68,73 +91,188 @@ void pushDigitalPinData(){
} }
payLoad = ""; payLoad = "";
delay(1000);
}
} }
void pushAnalogPinData(){ /**********************************************************************************************
for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) { This method will traverse the array of digital pins and publish the data from the those pins.
String payLoad = jsonPayLoad + "AnalogPinData"; It differs from the above method such that the pin data is published one after the other in
payLoad = payLoad + "\",\"time\":\"" + "9999"; seperate calls to the server
payLoad = payLoad + "\",\"key\":\"" + getDataType(analogPins[pin]); ***********************************************************************************************/
payLoad = payLoad + "\",\"value\":\"" + analogRead(analogPins[pin]);
payLoad = payLoad + "\"}";
httpClient.fastrprint(F("POST "));
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("pushalarmdata"));
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n"));
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n"));
httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n"));
httpClient.fastrprint(F("Content-Length: "));
int payLength = payLoad.length();
httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n"));
httpClient.fastrprint(F("\n"));
if(DEBUG) {
Serial.print("POST ");
Serial.print(SERVICE_EPOINT); Serial.print("pushalarmdata");
Serial.print(" HTTP/1.1"); Serial.println();
Serial.print(host); Serial.println();
Serial.print("Content-Type: application/json"); Serial.println();
Serial.print("Content-Length: ");
Serial.print(payLength); Serial.println();
Serial.println();
}
int chunkSize = 50; //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);
// }
//}
for (int i = 0; i < payLength; i++) { /**********************************************************************************************
if ( (i+1)*chunkSize > payLength) { Only required for cases of reading analog pin values.
httpClient.print(payLoad.substring(i*chunkSize, payLength)); An int Array of analog pins that needs to be read has to be initialised.
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLength)); This method will traverse the array and publish the data from the selected pins
i = payLength; ***********************************************************************************************/
} else {
httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
}
}
httpClient.fastrprint(F("\n")); //void pushAnalogPinData(){
if(DEBUG) Serial.println(); // 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);
// }
//}
delay(1000);
while (httpClient.available()) {
char response = httpClient.read();
if(DEBUG) Serial.print(response);
}
if(DEBUG) {
Serial.println();
Serial.println("-------------------------------");
}
payLoad = ""; String getDataType(int pin){
delay(1000); switch(pin){
case TEMP_PIN:
return "TEMP";
case BULB_PIN:
return "BULB";
case FAN_PIN:
return "FAN";
default:
return String(pin);
} }
} }

Loading…
Cancel
Save