parent
92358c4135
commit
bbd09ae509
@ -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 "linksys" // cannot be longer than 32 characters!
|
||||
#define WLAN_PASS "ramsgate717"
|
||||
|
||||
#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
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -1,86 +0,0 @@
|
||||
|
||||
/**********************************************************************************************
|
||||
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();
|
||||
|
||||
|
||||
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: "));
|
||||
|
||||
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("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));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
||||
while (pushClient.available()) {
|
||||
char response = pushClient.read();
|
||||
if(DEBUG) Serial.print(response);
|
||||
}
|
||||
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
|
||||
payLoad = "";
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,100 +1,94 @@
|
||||
uint8_t buffer[BUFFER_SIZE+1];
|
||||
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);
|
||||
}
|
||||
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();
|
||||
|
||||
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)
|
||||
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;
|
||||
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);
|
||||
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,92 +0,0 @@
|
||||
|
||||
/**********************************************************************************************
|
||||
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 += ":";
|
||||
payLoad += getSonar(); // returns distance if < MAX_DISTANCE else returns -1,
|
||||
// Pushed accordingly inside JAX-RS
|
||||
payLoad += ":";
|
||||
payLoad += analogRead(LDR_PIN);
|
||||
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: "));
|
||||
|
||||
int payLength = jsonPayLoad.length() + payLoad.length() + 2;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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("\r\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 = "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue