added new firealarm sketch

merge-requests/1/head
Shabir Mohamed 10 years ago
parent 3ceed63107
commit 92358c4135

@ -13,10 +13,11 @@
// byte mac[6] = { 0xC0, 0x4A, 0x00, 0x1A, 0x08, 0xDA }; //mac - c0:4a:00:1a:08:da // byte mac[6] = { 0xC0, 0x4A, 0x00, 0x1A, 0x08, 0xDA }; //mac - c0:4a:00:1a:08:da
// c0:4a:00:1a:03:f8 // c0:4a:00:1a:03:f8
// b8:27:eb:88:37:7a // b8:27:eb:88:37:7a
uint32_t ipAddress;
String connecting = "connecting.... "; String connecting = "connecting.... ";
void connectHttp() { void connectWifi() {
/* Initialise the module */ /* Initialise the module */
if(DEBUG) Serial.println(F("\nInitializing...")); if(DEBUG) Serial.println(F("\nInitializing..."));
if (!cc3000.begin()) if (!cc3000.begin())
{ {
@ -61,6 +62,8 @@ void connectHttp() {
while(1); while(1);
} }
wdt_reset();
if(CON_DEBUG) 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")); if(CON_DEBUG) Serial.println(F("Request DHCP"));
@ -74,21 +77,40 @@ void connectHttp() {
delay(1000); delay(1000);
} }
wdt_reset();
// deviceIP = String((uint8_t)(ipAddress >> 24)) + "."
// + String((uint8_t)(ipAddress >> 16)) + "."
// + String((uint8_t)(ipAddress >> 8)) + "."
// + String((uint8_t)ipAddress);
if(DEBUG) {
Serial.print("(String) IP: ");
// Serial.println(deviceIP);
}
if( cc3000.checkConnected() ){
// pinMode(13, HIGH);
connectToAndUpServer();
}
}
void connectToAndUpServer() {
pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT pushClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT
wdt_reset();
httpServer.begin();
wdt_reset();
if(CON_DEBUG) Serial.println(F("Listening for connections..."));
if (pushClient.connected()) { if (pushClient.connected()) {
registerIP();
wdt_reset();
if(CON_DEBUG) Serial.println("PushClient Connected to server"); if(CON_DEBUG) Serial.println("PushClient Connected to server");
} else { } else {
cc3000.disconnect();
if(CON_DEBUG) Serial.println(F("PushClient Connection failed")); if(CON_DEBUG) Serial.println(F("PushClient Connection failed"));
} reconnectInterval = millis();
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("-------------------------------------")); if(CON_DEBUG) Serial.println(F("-------------------------------------"));
@ -128,7 +150,7 @@ String getHostIP(byte server[4]){
bool displayConnectionDetails(void) bool displayConnectionDetails(void)
{ {
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; uint32_t netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{ {
@ -139,10 +161,10 @@ bool displayConnectionDetails(void)
{ {
if(CON_DEBUG) { if(CON_DEBUG) {
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); // Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); // Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); // Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); // Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println(); Serial.println();
} }
return true; return true;

@ -13,8 +13,27 @@
#define ADAFRUIT_CC3000_VBAT 5 #define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10 #define ADAFRUIT_CC3000_CS 10
#define WLAN_SSID "YourAP-SSID" // cannot be longer than 32 characters! #define LISTEN_PORT 80 // What TCP port to listen on for connections.
#define WLAN_PASS "Your-Password" // The HTTP protocol uses port 80 by default.
#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 "YourAP-SSID" // cannot be longer than 32 characters!
//#define WLAN_PASS "Your-Password"
#define WLAN_SECURITY WLAN_SEC_WPA2 #define WLAN_SECURITY WLAN_SEC_WPA2
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
@ -27,18 +46,13 @@
#define SERVICE_PORT 9763 #define SERVICE_PORT 9763
#define SERVICE_EPOINT "/firealarm/controller/" #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 TEMP_PIN A5
#define BULB_PIN 7 #define BULB_PIN A4
#define FAN_PIN 8 #define FAN_PIN A3
#define POLL_INTERVAL 1000
#define DEBUG false #define DEBUG false
#define CON_DEBUG true #define CON_DEBUG true
#endif #endif

@ -1,6 +1,7 @@
#include "FireAlarmWifiAgent.h" #include "FireAlarmWifiAgent.h"
#include <Adafruit_CC3000.h> #include <Adafruit_CC3000.h>
#include <avr/wdt.h>
#include <SPI.h> #include <SPI.h>
#include "dht.h" #include "dht.h"
#include <pt.h> #include <pt.h>
@ -9,8 +10,12 @@ Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ
SPI_CLOCK_DIVIDER); // you can change this clock speed SPI_CLOCK_DIVIDER); // you can change this clock speed
Adafruit_CC3000_Client pushClient; Adafruit_CC3000_Client pushClient;
Adafruit_CC3000_Client pollClient; Adafruit_CC3000_Server httpServer(LISTEN_PORT);
static struct pt pushThread; static struct pt pushThread;
static unsigned long reconnectInterval = 0;
static unsigned long pushInterval = 0;
//String deviceIP;
uint32_t sserver; uint32_t sserver;
@ -23,75 +28,47 @@ uint32_t sserver;
5. Check whether all reqquired pins are added to the 'digitalPins' array 5. Check whether all reqquired pins are added to the 'digitalPins' array
***********************************************************************************************/ ***********************************************************************************************/
byte server[4] = { 10, 100, 7, 38 }; //byte server[4] = { 10, 100, 7, 38 };
byte server[4] = { 192, 168, 2, 1 };
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN }; int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
String host, jsonPayLoad, replyMsg; String host, jsonPayLoad;
String responseMsg, subStrn; String responseMsg, subStrn;
void setup() { void setup() {
if(true) Serial.begin(115200); if(CON_DEBUG) Serial.begin(115200);
pinMode(BULB_PIN, OUTPUT); pinMode(BULB_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT); pinMode(FAN_PIN, OUTPUT);
// pinMode(13, OUTPUT);
PT_INIT(&pushThread); PT_INIT(&pushThread);
connectHttp();
setupResource(); setupResource();
}
void loop() { while( !cc3000.checkConnected() ){
if (pushClient.connected() && pollClient.connected()) { connectWifi();
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();
} }
}
wdt_enable(WDTO_8S);
String switchBulb() {
if (digitalRead(BULB_PIN) == HIGH) {
digitalWrite(BULB_PIN, LOW);
return "OFF";
} else {
digitalWrite(BULB_PIN, HIGH);
return "ON";
}
} }
String switchFan() { void loop() {
if (digitalRead(FAN_PIN) == HIGH) { wdt_reset();
digitalWrite(FAN_PIN, LOW); protothread1(&pushThread); // Pushes data and waits for control signals to be received
return "OFF";
} else { if ( subStrn.equals("ON") ) {
digitalWrite(FAN_PIN, HIGH); if (responseMsg.equals("BULB")) {
return "ON"; 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);
}
} }
} }
@ -129,7 +106,6 @@ double getTemperature(){
if(DEBUG) Serial.print("Unknown error,\t"); if(DEBUG) Serial.print("Unknown error,\t");
break; break;
} }
// DISPLAY DATA // DISPLAY DATA
if(DEBUG) { if(DEBUG) {
Serial.print("\t"); Serial.print("\t");
@ -142,15 +118,39 @@ double getTemperature(){
} }
static int protothread1(struct pt *pt, int interval) { static int protothread1(struct pt *pt) {
static unsigned long timestamp = 0;
PT_BEGIN(pt); PT_BEGIN(pt);
while(1) { // never stop while(1) { // never stop
/* each time the function it is checked whether any control signals are sent /* each time the function it is checked whether any control signals are sent
* if so exit this proto thread * if so exit this proto thread
*/ */
PT_WAIT_UNTIL(pt, readControls() ); PT_WAIT_UNTIL(pt, listen() );
pushData();
if (pushClient.connected()) {
if ((millis() - pushInterval) > 2000) {
pushData();
pushInterval = millis();
wdt_reset();
}
} else {
if(DEBUG) {
Serial.println("client not found...");
Serial.println("disconnecting.");
}
pushClient.flush();
if(CON_DEBUG) Serial.println("Disconnection!!!!");
if ( (millis() - reconnectInterval) > 3000 ) {
if (cc3000.checkConnected()) {
connectToAndUpServer();
} else {
// pinMode(13, LOW);
while(1);
}
}
}
} }
PT_END(pt); PT_END(pt);
} }

@ -0,0 +1,100 @@
uint8_t buffer[BUFFER_SIZE+1];
int bufindex = 0;
char action[MAX_ACTION+1];
char path[MAX_PATH+1];
boolean listen() {
// 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.
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();
return false;
} else {
return true;
}
}
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);
}

@ -1,120 +1,32 @@
boolean readControls() { void registerIP() {
// String responseMsg; pushClient.fastrprint(F("GET "));
pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("register/"));
pollClient.fastrprint(F("GET ")); pushClient.fastrprint(DEVICE_OWNER); pushClient.fastrprint(F("/"));
pollClient.fastrprint(SERVICE_EPOINT); pollClient.fastrprint(F("readcontrols/")); pushClient.fastrprint(DEVICE_ID); pushClient.fastrprint(F("/"));
pollClient.fastrprint(DEVICE_OWNER); pollClient.fastrprint(F("/")); pollClient.fastrprint(DEVICE_ID);
pollClient.fastrprint(F(" HTTP/1.1")); pollClient.fastrprint(F("\n")); // pushClient.fastrprint(deviceIP.c_str());
pollClient.fastrprint(host.c_str()); pollClient.fastrprint(F("\n")); pushClient.fastrprint(String((uint8_t)(ipAddress >> 24)).c_str());
pollClient.println(); pushClient.fastrprint(".");
pushClient.fastrprint(String((uint8_t)(ipAddress >> 16)).c_str());
delay(1000); pushClient.fastrprint(".");
pushClient.fastrprint(String((uint8_t)(ipAddress >> 8)).c_str());
if (true) { pushClient.fastrprint(".");
while (pollClient.available()) { pushClient.fastrprint(String((uint8_t)ipAddress).c_str());
char response = pollClient.read();
responseMsg += response; pushClient.fastrprint(F(" HTTP/1.1")); pushClient.fastrprint(F("\n"));
} pushClient.fastrprint(host.c_str()); pushClient.fastrprint(F("\n"));
} pushClient.println();
if (DEBUG) {
int index = responseMsg.lastIndexOf(":"); Serial.print("GET "); Serial.print(SERVICE_EPOINT); Serial.print("register/");
int newLine = responseMsg.lastIndexOf("\n"); Serial.print(DEVICE_OWNER); Serial.print("/"); Serial.print(DEVICE_ID);
subStrn = responseMsg.substring(index + 1); Serial.print("/");
responseMsg = responseMsg.substring(newLine + 1, index); // Serial.print(deviceIP);
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");
Serial.print(" HTTP/1.1"); Serial.println(); Serial.print(" HTTP/1.1"); Serial.println();
Serial.print(host); Serial.println(); Serial.print(host); Serial.println(); 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));
}
}
pollClient.fastrprint(F("\n"));
if(DEBUG) Serial.println();
delay(1000); delay(1000);
if(true) { pushClient.flush();
while (pollClient.available()) {
char response = pollClient.read();
if(DEBUG) Serial.print(response);
}
}
if(DEBUG) {
Serial.println();
Serial.println("-------------------------------");
}
payLoad = "";
// delay(1000);
} }

@ -20,7 +20,7 @@ void pushData(){
} }
if ( ((sizeof(digitalPins)/sizeof(int)) - 1) != pin ) { if ( ((sizeof(digitalPins)/sizeof(int)) - 1) != pin ) {
payLoad += "-"; payLoad += ":";
} }
} }
@ -78,6 +78,8 @@ void pushData(){
delay(1000); delay(1000);
wdt_reset();
if(true) { if(true) {
while (pushClient.available()) { while (pushClient.available()) {
char response = pushClient.read(); char response = pushClient.read();
@ -94,186 +96,4 @@ void pushData(){
} }
/**********************************************************************************************
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);
}
}

Loading…
Cancel
Save