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,99 +1,56 @@
|
||||
#include "Arduinoboardwifi.h"
|
||||
|
||||
#include "ArduinoBoardSketch.h"
|
||||
#include <Adafruit_CC3000.h>
|
||||
#include <SPI.h>
|
||||
|
||||
#include <avr/wdt.h>
|
||||
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();
|
||||
wdt_enable(WDTO_8S);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
if (pushClient.connected() && pollClient.connected()) {
|
||||
pushData();
|
||||
delay(POLL_INTERVAL);
|
||||
|
||||
boolean valid = readControls();
|
||||
|
||||
responseMsg="";
|
||||
} else {
|
||||
if(DEBUG) {
|
||||
Serial.println("client not found...");
|
||||
Serial.println("disconnecting.");
|
||||
}
|
||||
pushClient.close();
|
||||
pollClient.close();
|
||||
cc3000.disconnect();
|
||||
|
||||
while( !cc3000.checkConnected() ){
|
||||
connectHttp();
|
||||
}
|
||||
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
cpuTemperature=getBoardTemp();
|
||||
|
||||
|
||||
if(millis() - pushTimestamp > PUSH_INTERVAL){
|
||||
while (!client.connected()) {
|
||||
setupClient();
|
||||
}
|
||||
pushData();
|
||||
|
||||
pushTimestamp = millis();
|
||||
}
|
||||
|
||||
//Serial.println("PUSHED");
|
||||
|
||||
|
||||
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;
|
||||
if(millis() - pollTimestamp > POLL_INTERVAL){
|
||||
while (!client.connected()) {
|
||||
setupClient();
|
||||
}
|
||||
readControls();
|
||||
|
||||
// The offset of 324.31 could be wrong. It is just an indication.
|
||||
t = (wADC - 324.31 ) / 1.22;
|
||||
pollTimestamp = millis();
|
||||
|
||||
// The returned temperature is in degrees Celcius.
|
||||
return (t);
|
||||
}
|
||||
|
||||
|
||||
//Serial.println("LOOPING");
|
||||
wdt_reset();
|
||||
}
|
||||
|
@ -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,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