Merge branch 'master' of https://github.com/wso2-dev/product-device-cloud
commit
98dde99da1
@ -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 "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 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,156 +1,81 @@
|
||||
#include "FireAlarmWifiAgent.h"
|
||||
|
||||
#include <Adafruit_CC3000.h>
|
||||
#include <avr/wdt.h>
|
||||
#include <SPI.h>
|
||||
#include "dht.h"
|
||||
#include <pt.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;
|
||||
static struct pt pushThread;
|
||||
|
||||
Adafruit_CC3000_Server httpServer(LISTEN_PORT);
|
||||
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
|
||||
***********************************************************************************************/
|
||||
/**********************************************************************************************
|
||||
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
|
||||
***********************************************************************************************/
|
||||
void setup() {
|
||||
if (CON_DEBUG) Serial.begin(9600);
|
||||
|
||||
byte server[4] = { 10, 100, 7, 38 };
|
||||
pinMode(BULB_PIN, OUTPUT);
|
||||
pinMode(FAN_PIN, OUTPUT);
|
||||
pinMode(TEMP_PIN, INPUT);
|
||||
setupResource();
|
||||
|
||||
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
|
||||
String host, jsonPayLoad, replyMsg;
|
||||
String responseMsg, subStrn;
|
||||
do {
|
||||
connectHttp();
|
||||
} while (!cc3000.checkConnected());
|
||||
|
||||
void setup() {
|
||||
if(true) Serial.begin(115200);
|
||||
pinMode(BULB_PIN, OUTPUT);
|
||||
pinMode(FAN_PIN, OUTPUT);
|
||||
|
||||
PT_INIT(&pushThread);
|
||||
|
||||
connectHttp();
|
||||
setupResource();
|
||||
while (!pushClient.connected()) {
|
||||
setupClient();
|
||||
}
|
||||
|
||||
registerIP();
|
||||
|
||||
httpServer.begin();
|
||||
wdt_enable(WDTO_8S);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (pushClient.connected() && pollClient.connected()) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
while (!cc3000.checkConnected()) {
|
||||
connectHttp();
|
||||
|
||||
String switchBulb() {
|
||||
if (digitalRead(BULB_PIN) == HIGH) {
|
||||
digitalWrite(BULB_PIN, LOW);
|
||||
return "OFF";
|
||||
} else {
|
||||
digitalWrite(BULB_PIN, HIGH);
|
||||
return "ON";
|
||||
}
|
||||
}
|
||||
|
||||
String switchFan() {
|
||||
if (digitalRead(FAN_PIN) == HIGH) {
|
||||
digitalWrite(FAN_PIN, LOW);
|
||||
return "OFF";
|
||||
} else {
|
||||
digitalWrite(FAN_PIN, HIGH);
|
||||
return "ON";
|
||||
wdt_reset();
|
||||
|
||||
if (millis() - pushTimestamp > PUSH_INTERVAL) {
|
||||
wdt_reset();
|
||||
while (!pushClient.connected()) {
|
||||
setupClient();
|
||||
}
|
||||
pushData();
|
||||
pushTimestamp = millis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double getTemperature(){
|
||||
dht DHT;
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.println("-------------------------------");
|
||||
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
|
||||
Serial.print("DHT11, \t");
|
||||
}
|
||||
int chk = DHT.read11(TEMP_PIN);
|
||||
switch (chk)
|
||||
{
|
||||
case DHTLIB_OK:
|
||||
if(DEBUG) Serial.print("OK,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_CHECKSUM:
|
||||
if(DEBUG) Serial.print("Checksum error,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_TIMEOUT:
|
||||
if(DEBUG) Serial.print("Time out error,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_CONNECT:
|
||||
if(DEBUG) Serial.print("Connect error,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_ACK_L:
|
||||
if(DEBUG) Serial.print("Ack Low error,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_ACK_H:
|
||||
if(DEBUG) Serial.print("Ack High error,\t");
|
||||
break;
|
||||
default:
|
||||
if(DEBUG) Serial.print("Unknown error,\t");
|
||||
break;
|
||||
}
|
||||
|
||||
// DISPLAY DATA
|
||||
if(DEBUG) {
|
||||
Serial.print("\t");
|
||||
Serial.print(DHT.temperature, 1);
|
||||
Serial.print(",\t\t");
|
||||
Serial.println(DHT.humidity, 1);
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
return DHT.temperature;
|
||||
}
|
||||
|
||||
readControls();
|
||||
wdt_reset();
|
||||
|
||||
if (subStrn.equals("ON")) {
|
||||
if (responseMsg.equals("BULB")) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
wdt_reset();
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -0,0 +1,94 @@
|
||||
uint8_t buffer[BUFFER_SIZE + 1];
|
||||
int bufindex = 0;
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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,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