forked from community/product-iots
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.2 KiB
93 lines
3.2 KiB
10 years ago
|
|
||
|
/**********************************************************************************************
|
||
|
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(){
|
||
10 years ago
|
String payLoad = "Data";
|
||
|
payLoad = payLoad + "\",\"value\":\"";
|
||
|
|
||
|
int temperature = (uint8_t)getTemperature();
|
||
|
payLoad += temperature;
|
||
|
payLoad += ":";
|
||
|
payLoad += digitalRead(PIR_PIN);
|
||
|
payLoad += ":";
|
||
|
payLoad += getSonar(); // returns distance if < MAX_DISTANCE else returns -1,
|
||
|
// Pushed accordingly inside JAX-RS
|
||
|
payLoad += ":";
|
||
|
payLoad += analogRead(LDR_PIN);
|
||
|
payLoad += "\"}";
|
||
10 years ago
|
|
||
10 years ago
|
pushClient.fastrprint(F("POST "));
|
||
10 years ago
|
pushClient.fastrprint(SERVICE_EPOINT); pushClient.fastrprint(F("pushsensordata"));
|
||
10 years ago
|
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: "));
|
||
|
|
||
10 years ago
|
int payLength = jsonPayLoad.length() + payLoad.length() + 2;
|
||
10 years ago
|
|
||
10 years ago
|
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("pushsensordata");
|
||
|
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();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
int chunkSize = 50;
|
||
10 years ago
|
|
||
10 years ago
|
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));
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
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));
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
pushClient.fastrprint(F("\r\n"));
|
||
10 years ago
|
if(DEBUG) Serial.println();
|
||
|
|
||
|
delay(1000);
|
||
10 years ago
|
|
||
10 years ago
|
if(true) {
|
||
|
while (pushClient.available()) {
|
||
|
char response = pushClient.read();
|
||
10 years ago
|
if(DEBUG) Serial.print(response);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
if(DEBUG) {
|
||
10 years ago
|
Serial.println();
|
||
|
Serial.println("-------------------------------");
|
||
|
}
|
||
10 years ago
|
|
||
|
payLoad = "";
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
|
|
||
10 years ago
|
|