forked from community/product-iots
parent
dd138ebad5
commit
5e94332a50
@ -1,48 +0,0 @@
|
||||
#ifndef FireAlarmEthernetAgent_H
|
||||
#define FireAlarmEthernetAgent_H
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#define HTTP_POST "POST"
|
||||
#define HTTP_GET "GET"
|
||||
#define HTTP_VERSION "HTTP/1.1"
|
||||
#define HTTP_CONTENT_TYPE "Content-Type: application/json"
|
||||
#define HTTP_CONTENT_LEN "Content-Length: "
|
||||
|
||||
#define DEVICE_OWNER "${DEVICE_OWNER}" //"Smeansbeer"
|
||||
#define DEVICE_ID "${DEVICE_ID}" //"vbhenqyt85yq"
|
||||
#define DEVICE_TOKEN "${DEVICE_TOKEN}"
|
||||
|
||||
#define PUSH_ALARM_DATA "pushalarmdata"
|
||||
#define READ_CONTROLS "readcontrols/"
|
||||
#define REPLY "reply"
|
||||
|
||||
#define OWNER_JSON "{\"owner\":\""
|
||||
#define DEVICE_ID_JSON "\",\"deviceId\":\""
|
||||
#define REPLY_JSON "\",\"replyMessage\":\""
|
||||
#define TIME_JSON "\",\"time\":\""
|
||||
#define KEY_JSON "\",\"key\":\""
|
||||
#define VALUE_JSON "\",\"value\":\""
|
||||
#define END_JSON "\"}"
|
||||
|
||||
#define SERVICE_PORT 9763
|
||||
#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 3
|
||||
#define BULB_PIN 4
|
||||
#define FAN_PIN 5
|
||||
|
||||
#define DEBUG false
|
||||
#define POLL_INTERVAL 1000
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,143 +0,0 @@
|
||||
#include "FireAlarmEthernetAgent.h"
|
||||
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
#include "dht.h"
|
||||
|
||||
/**********************************************************************************************
|
||||
0. Check with a sample Ethernet code of the Ethernet 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
|
||||
***********************************************************************************************/
|
||||
|
||||
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
|
||||
int analogPins[] = { 0, 1, 2, 3, 4, 5 };
|
||||
|
||||
EthernetClient httpClient;
|
||||
String host, jsonPayLoad, replyMsg;
|
||||
|
||||
void setup() {
|
||||
if(DEBUG) Serial.begin(9600);
|
||||
pinMode(BULB_PIN, OUTPUT);
|
||||
pinMode(FAN_PIN, OUTPUT);
|
||||
connectHttp();
|
||||
setupResource();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (httpClient.connected()) {
|
||||
pushDigitalPinData();
|
||||
// pushData(); // Use this method to batch all data together and send in one call
|
||||
|
||||
delay(POLL_INTERVAL);
|
||||
|
||||
String responseMsg = readControls();
|
||||
int index = responseMsg.lastIndexOf(":");
|
||||
int newLine = responseMsg.lastIndexOf("\n");
|
||||
String subStrn = responseMsg.substring(index + 1);
|
||||
|
||||
if (subStrn.equals("IN")) {
|
||||
responseMsg = responseMsg.substring(newLine + 1, index);
|
||||
if (responseMsg.equals("TEMPERATURE")) {
|
||||
replyMsg = "Temperature is " + String(getTemperature()) + "C.";
|
||||
reply(replyMsg);
|
||||
} else if (responseMsg.equals("BULB")) {
|
||||
replyMsg = "Bulb was switched " + switchBulb();
|
||||
} else if (responseMsg.equals("FAN")) {
|
||||
replyMsg = "Bulb was switched " + switchFan();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(DEBUG) {
|
||||
Serial.println("client not found...");
|
||||
Serial.println("disconnecting.");
|
||||
}
|
||||
httpClient.stop();
|
||||
connectHttp();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String getDataType(int pin){
|
||||
switch(pin){
|
||||
case TEMP_PIN:
|
||||
return "Temperature";
|
||||
case BULB_PIN:
|
||||
return "Bulb";
|
||||
case FAN_PIN:
|
||||
return "Fan";
|
||||
default:
|
||||
return String(pin);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
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);
|
||||
}
|
||||
|
||||
if (parsed) {
|
||||
// Serial.print(F("Path: ")); Serial.println(path);
|
||||
|
||||
if (strcmp(action, "GET") == 0) {
|
||||
String urlPath = path;
|
||||
urlPath.replace("/move/","");
|
||||
urlPath.replace("/","");
|
||||
|
||||
if(urlPath.endsWith("F")){
|
||||
updateDirectionVariable(1);
|
||||
|
||||
}else if(urlPath.endsWith("B")){
|
||||
updateDirectionVariable(2);
|
||||
|
||||
}else if(urlPath.endsWith("L")){
|
||||
updateDirectionVariable(3);
|
||||
|
||||
}else if(urlPath.endsWith("R")){
|
||||
updateDirectionVariable(4);
|
||||
|
||||
}else if(urlPath.endsWith("S")){
|
||||
updateDirectionVariable(5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a short period to make sure the response had time to send before
|
||||
// the connection is closed (the CC3000 sends data asyncronously).
|
||||
delay(100);
|
||||
|
||||
// Close the connection when done.
|
||||
Serial.println(F("Client disconnected"));
|
||||
client.close();
|
||||
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);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
|
||||
void motor_stop(){
|
||||
digitalWrite(motor_left[0], LOW);
|
||||
digitalWrite(motor_left[1], LOW);
|
||||
|
||||
digitalWrite(motor_right[0], LOW);
|
||||
digitalWrite(motor_right[1], LOW);
|
||||
unsigned long motorStop= millis() + 25;
|
||||
while (!(motorStop<= millis())){
|
||||
//delay 25ms
|
||||
}
|
||||
}
|
||||
|
||||
void drive_backward(){
|
||||
//motor_stop();
|
||||
digitalWrite(motor_left[0], LOW);
|
||||
digitalWrite(motor_left[1], HIGH);
|
||||
|
||||
digitalWrite(motor_right[0], LOW);
|
||||
digitalWrite(motor_right[1], HIGH);
|
||||
}
|
||||
|
||||
void drive_forward(){
|
||||
//motor_stop();
|
||||
digitalWrite(motor_left[0], HIGH);
|
||||
digitalWrite(motor_left[1], LOW);
|
||||
|
||||
digitalWrite(motor_right[0], HIGH);
|
||||
digitalWrite(motor_right[1], LOW);
|
||||
}
|
||||
|
||||
|
||||
void turn_right(){
|
||||
motor_stop();
|
||||
digitalWrite(motor_left[0], HIGH);
|
||||
digitalWrite(motor_left[1], LOW);
|
||||
unsigned long motorStop= millis() + TURN_DELAY;
|
||||
while (!(motorStop<= millis())){
|
||||
//delay 300ms
|
||||
}
|
||||
updateDirectionVariable(0);
|
||||
motor_stop();
|
||||
}
|
||||
|
||||
void turn_left(){
|
||||
motor_stop();
|
||||
digitalWrite(motor_right[0], HIGH);
|
||||
digitalWrite(motor_right[1], LOW);
|
||||
unsigned long motorStop= millis() + TURN_DELAY;
|
||||
while (!(motorStop<= millis())){
|
||||
//delay 300ms
|
||||
}
|
||||
updateDirectionVariable(0);
|
||||
motor_stop();
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
String readControls() {
|
||||
String responseMsg;
|
||||
String resource = " " + String(SERVICE_EPOINT) + String(READ_CONTROLS) + String(DEVICE_OWNER) + "/" + String(DEVICE_ID) + " ";
|
||||
|
||||
httpClient.print(HTTP_GET);
|
||||
httpClient.print(resource);
|
||||
httpClient.println(HTTP_VERSION);
|
||||
httpClient.println(host);
|
||||
httpClient.println();
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.print(HTTP_GET);
|
||||
Serial.print(resource);
|
||||
Serial.println(HTTP_VERSION);
|
||||
Serial.println(host);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
responseMsg += response;
|
||||
}
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.print(responseMsg);
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
delay(1000);
|
||||
return responseMsg;
|
||||
}
|
||||
|
||||
void reply(String replyMsg) {
|
||||
String resource = " " + String(SERVICE_EPOINT) + String(REPLY) + " ";
|
||||
String payLoad = jsonPayLoad + replyMsg + String(END_JSON);
|
||||
|
||||
httpClient.print(HTTP_POST);
|
||||
httpClient.print(resource);
|
||||
httpClient.println(HTTP_VERSION);
|
||||
httpClient.println(host);
|
||||
httpClient.println(HTTP_CONTENT_TYPE);
|
||||
httpClient.print(HTTP_CONTENT_LEN);
|
||||
httpClient.println(payLoad.length());
|
||||
httpClient.println();
|
||||
httpClient.println(payLoad);
|
||||
httpClient.println();
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.print(HTTP_POST);
|
||||
Serial.print(resource);
|
||||
Serial.println(HTTP_VERSION);
|
||||
Serial.println(host);
|
||||
Serial.println(HTTP_CONTENT_TYPE);
|
||||
Serial.print(HTTP_CONTENT_LEN);
|
||||
Serial.println(payLoad.length());
|
||||
Serial.println();
|
||||
Serial.println(payLoad);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
if(DEBUG) Serial.print(response);
|
||||
}
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,64 @@
|
||||
#ifndef SenseBotEthernetAgent_H
|
||||
#define SenseBotEthernetAgent_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!
|
||||
#define ADAFRUIT_CC3000_VBAT 5
|
||||
#define ADAFRUIT_CC3000_CS 10
|
||||
|
||||
#define WLAN_SSID "YourAccessPointSSID" // cannot be longer than 32 characters!
|
||||
#define WLAN_PASS "APPassword"
|
||||
|
||||
#define WLAN_SECURITY WLAN_SEC_WPA2
|
||||
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
|
||||
|
||||
#define DEVICE_OWNER "Test" //"${DEVICE_OWNER}"
|
||||
#define DEVICE_ID "Test" //"${DEVICE_ID}"
|
||||
|
||||
#define SERVICE_PORT 9763
|
||||
#define SERVICE_EPOINT "/iotdevices/SenseBotController/"
|
||||
// pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""}
|
||||
// readcontrols/{owner}/{deviceId}
|
||||
// reply - application/json - {"owner":"","deviceId":"","replyMessage":""}
|
||||
#define BUZZER A0
|
||||
#define LDR_PIN A1
|
||||
#define TEMP_PIN A2
|
||||
#define PIR_PIN A3
|
||||
#define SONAR_TRIG A4
|
||||
#define SONAR_ECHO A5
|
||||
|
||||
#define BUZZER_SOUND 100
|
||||
#define MAX_DISTANCE 30
|
||||
|
||||
#define TURN_DELAY 100
|
||||
|
||||
#define LISTEN_PORT 80 // What TCP port to listen on for connections.
|
||||
// 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 DEBUG false
|
||||
#define CON_DEBUG true
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -0,0 +1,134 @@
|
||||
#include "SenseBotEthernetAgent.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_Server httpServer(LISTEN_PORT);
|
||||
|
||||
static struct pt pushThread;
|
||||
|
||||
int motor_left[] = {7, 8};
|
||||
int enA = 12;
|
||||
|
||||
int motor_right[] = {4, 6};
|
||||
int enB = 11;
|
||||
|
||||
|
||||
int motion_global = 0;
|
||||
|
||||
/**********************************************************************************************
|
||||
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 'EUHackothonRobot.h.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
|
||||
***********************************************************************************************/
|
||||
|
||||
uint32_t sserver;
|
||||
byte server[4] = { XX, XX, XX, XX };
|
||||
|
||||
String host, jsonPayLoad;
|
||||
dht DHT;
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
pinMode(PIR_PIN, INPUT);
|
||||
|
||||
for(int i = 0; i < 2; i++){
|
||||
pinMode(motor_left[i], OUTPUT);
|
||||
pinMode(motor_right[i], OUTPUT);
|
||||
}
|
||||
|
||||
pinMode(enA, OUTPUT);
|
||||
pinMode(enB, OUTPUT);
|
||||
digitalWrite(enA, 100);
|
||||
digitalWrite(enB, 100);
|
||||
motor_stop();
|
||||
|
||||
PT_INIT(&pushThread);
|
||||
|
||||
connectHttp();
|
||||
setupResource();
|
||||
wdt_enable(WDTO_4S);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
protothread1(&pushThread, 1000);
|
||||
|
||||
listen();
|
||||
// wdt_reset();
|
||||
// Check connection
|
||||
if( !cc3000.checkConnected() ){
|
||||
while(1){
|
||||
}
|
||||
}
|
||||
|
||||
wdt_reset();
|
||||
drive();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void drive(){
|
||||
switch(motion_global){
|
||||
case 1 : drive_forward();
|
||||
break;
|
||||
case 2 : drive_backward();
|
||||
break;
|
||||
case 3 : turn_left();
|
||||
break;
|
||||
case 4 : turn_right();
|
||||
break;
|
||||
case 5 :
|
||||
motor_stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void updateDirectionVariable(int motionDir){
|
||||
motion_global = motionDir;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int protothread1(struct pt *pt, int interval) {
|
||||
PT_BEGIN(pt);
|
||||
while(1) { // never stop
|
||||
/* each time the function is called the second boolean
|
||||
* argument "millis() - timestamp > interval" is re-evaluated
|
||||
* and if false the function exits after that. */
|
||||
PT_WAIT_UNTIL(pt, listen() );
|
||||
|
||||
if (pushClient.connected()) {
|
||||
// batches all the required pin values together and pushes once
|
||||
// Pushes data in 1 second interval
|
||||
pushData();
|
||||
wdt_reset();
|
||||
} else {
|
||||
pushClient.close();
|
||||
cc3000.disconnect();
|
||||
connectHttp();
|
||||
}
|
||||
}
|
||||
PT_END(pt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,107 @@
|
||||
//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;
|
||||
}
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
templates=FireAlarmEthernetAgent.h
|
||||
zipfilename=SensebotEthernetAgent.zip
|
||||
templates=SenseBotEthernetAgent.h
|
||||
zipfilename=SenseBotEthernetAgent.zip
|
@ -1,44 +0,0 @@
|
||||
#ifndef FireAlarmWifiAgent_H
|
||||
#define FireAlarmWifiAgent_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 "YourAP-SSID" // cannot be longer than 32 characters!
|
||||
#define WLAN_PASS "Your-Password"
|
||||
|
||||
#define WLAN_SECURITY WLAN_SEC_WPA2
|
||||
// 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}" //"SHABIRMEAN"
|
||||
#define DEVICE_ID "${DEVICE_ID}" //"vbhenqyt85yq"
|
||||
#define DEVICE_TOKEN "${DEVICE_TOKEN}"
|
||||
|
||||
|
||||
#define SERVICE_PORT 9763
|
||||
#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 BULB_PIN 7
|
||||
#define FAN_PIN 8
|
||||
|
||||
#define POLL_INTERVAL 1000
|
||||
#define DEBUG false
|
||||
#define CON_DEBUG true
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -1,156 +0,0 @@
|
||||
#include "FireAlarmWifiAgent.h"
|
||||
|
||||
#include <Adafruit_CC3000.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;
|
||||
|
||||
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] = { 10, 100, 7, 38 };
|
||||
|
||||
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
|
||||
String host, jsonPayLoad, replyMsg;
|
||||
String responseMsg, subStrn;
|
||||
|
||||
void setup() {
|
||||
if(true) Serial.begin(115200);
|
||||
pinMode(BULB_PIN, OUTPUT);
|
||||
pinMode(FAN_PIN, OUTPUT);
|
||||
|
||||
PT_INIT(&pushThread);
|
||||
|
||||
connectHttp();
|
||||
setupResource();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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,98 @@
|
||||
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);
|
||||
}
|
||||
|
||||
if (parsed) {
|
||||
// Serial.print(F("Path: ")); Serial.println(path);
|
||||
|
||||
if (strcmp(action, "GET") == 0) {
|
||||
String urlPath = path;
|
||||
urlPath.replace("/move/","");
|
||||
urlPath.replace("/","");
|
||||
|
||||
if(urlPath.endsWith("F")){
|
||||
updateDirectionVariable(1);
|
||||
|
||||
}else if(urlPath.endsWith("B")){
|
||||
updateDirectionVariable(2);
|
||||
|
||||
}else if(urlPath.endsWith("L")){
|
||||
updateDirectionVariable(3);
|
||||
|
||||
}else if(urlPath.endsWith("R")){
|
||||
updateDirectionVariable(4);
|
||||
|
||||
}else if(urlPath.endsWith("S")){
|
||||
updateDirectionVariable(5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a short period to make sure the response had time to send before
|
||||
// the connection is closed (the CC3000 sends data asyncronously).
|
||||
delay(100);
|
||||
|
||||
// Close the connection when done.
|
||||
Serial.println(F("Client disconnected"));
|
||||
client.close();
|
||||
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);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
|
||||
void motor_stop(){
|
||||
digitalWrite(motor_left[0], LOW);
|
||||
digitalWrite(motor_left[1], LOW);
|
||||
|
||||
digitalWrite(motor_right[0], LOW);
|
||||
digitalWrite(motor_right[1], LOW);
|
||||
unsigned long motorStop= millis() + 25;
|
||||
while (!(motorStop<= millis())){
|
||||
//delay 25ms
|
||||
}
|
||||
}
|
||||
|
||||
void drive_backward(){
|
||||
//motor_stop();
|
||||
digitalWrite(motor_left[0], LOW);
|
||||
digitalWrite(motor_left[1], HIGH);
|
||||
|
||||
digitalWrite(motor_right[0], LOW);
|
||||
digitalWrite(motor_right[1], HIGH);
|
||||
}
|
||||
|
||||
void drive_forward(){
|
||||
//motor_stop();
|
||||
digitalWrite(motor_left[0], HIGH);
|
||||
digitalWrite(motor_left[1], LOW);
|
||||
|
||||
digitalWrite(motor_right[0], HIGH);
|
||||
digitalWrite(motor_right[1], LOW);
|
||||
}
|
||||
|
||||
|
||||
void turn_right(){
|
||||
motor_stop();
|
||||
digitalWrite(motor_left[0], HIGH);
|
||||
digitalWrite(motor_left[1], LOW);
|
||||
unsigned long motorStop= millis() + TURN_DELAY;
|
||||
while (!(motorStop<= millis())){
|
||||
//delay 300ms
|
||||
}
|
||||
updateDirectionVariable(0);
|
||||
motor_stop();
|
||||
}
|
||||
|
||||
void turn_left(){
|
||||
motor_stop();
|
||||
digitalWrite(motor_right[0], HIGH);
|
||||
digitalWrite(motor_right[1], LOW);
|
||||
unsigned long motorStop= millis() + TURN_DELAY;
|
||||
while (!(motorStop<= millis())){
|
||||
//delay 300ms
|
||||
}
|
||||
updateDirectionVariable(0);
|
||||
motor_stop();
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
boolean readControls() {
|
||||
// String responseMsg;
|
||||
|
||||
pollClient.fastrprint(F("GET "));
|
||||
pollClient.fastrprint(SERVICE_EPOINT); pollClient.fastrprint(F("readcontrols/"));
|
||||
pollClient.fastrprint(DEVICE_OWNER); pollClient.fastrprint(F("/")); pollClient.fastrprint(DEVICE_ID);
|
||||
pollClient.fastrprint(F(" HTTP/1.1")); pollClient.fastrprint(F("\n"));
|
||||
pollClient.fastrprint(host.c_str()); pollClient.fastrprint(F("\n"));
|
||||
pollClient.println();
|
||||
|
||||
delay(1000);
|
||||
|
||||
if (true) {
|
||||
while (pollClient.available()) {
|
||||
char response = pollClient.read();
|
||||
responseMsg += response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int index = responseMsg.lastIndexOf(":");
|
||||
int newLine = responseMsg.lastIndexOf("\n");
|
||||
subStrn = responseMsg.substring(index + 1);
|
||||
responseMsg = responseMsg.substring(newLine + 1, index);
|
||||
|
||||
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(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()) {
|
||||
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);
|
||||
|
||||
if(true) {
|
||||
while (pollClient.available()) {
|
||||
char response = pollClient.read();
|
||||
if(DEBUG) Serial.print(response);
|
||||
}
|
||||
}
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
|
||||
payLoad = "";
|
||||
// delay(1000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,64 @@
|
||||
#ifndef SenseBotWifiAgent_H
|
||||
#define SenseBotWifiAgent_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!
|
||||
#define ADAFRUIT_CC3000_VBAT 5
|
||||
#define ADAFRUIT_CC3000_CS 10
|
||||
|
||||
#define WLAN_SSID "YourAccessPointSSID" // cannot be longer than 32 characters!
|
||||
#define WLAN_PASS "APPassword"
|
||||
|
||||
#define WLAN_SECURITY WLAN_SEC_WPA2
|
||||
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
|
||||
|
||||
#define DEVICE_OWNER "Test" //"${DEVICE_OWNER}"
|
||||
#define DEVICE_ID "Test" //"${DEVICE_ID}"
|
||||
|
||||
#define SERVICE_PORT 9763
|
||||
#define SERVICE_EPOINT "/iotdevices/SenseBotController/"
|
||||
// pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""}
|
||||
// readcontrols/{owner}/{deviceId}
|
||||
// reply - application/json - {"owner":"","deviceId":"","replyMessage":""}
|
||||
#define BUZZER A0
|
||||
#define LDR_PIN A1
|
||||
#define TEMP_PIN A2
|
||||
#define PIR_PIN A3
|
||||
#define SONAR_TRIG A4
|
||||
#define SONAR_ECHO A5
|
||||
|
||||
#define BUZZER_SOUND 100
|
||||
#define MAX_DISTANCE 30
|
||||
|
||||
#define TURN_DELAY 100
|
||||
|
||||
#define LISTEN_PORT 80 // What TCP port to listen on for connections.
|
||||
// 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 DEBUG false
|
||||
#define CON_DEBUG true
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -0,0 +1,134 @@
|
||||
#include "SenseBotWifiAgent.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_Server httpServer(LISTEN_PORT);
|
||||
|
||||
static struct pt pushThread;
|
||||
|
||||
int motor_left[] = {7, 8};
|
||||
int enA = 12;
|
||||
|
||||
int motor_right[] = {4, 6};
|
||||
int enB = 11;
|
||||
|
||||
|
||||
int motion_global = 0;
|
||||
|
||||
/**********************************************************************************************
|
||||
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 'EUHackothonRobot.h.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
|
||||
***********************************************************************************************/
|
||||
|
||||
uint32_t sserver;
|
||||
byte server[4] = { XX, XX, XX, XX };
|
||||
|
||||
String host, jsonPayLoad;
|
||||
dht DHT;
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
pinMode(PIR_PIN, INPUT);
|
||||
|
||||
for(int i = 0; i < 2; i++){
|
||||
pinMode(motor_left[i], OUTPUT);
|
||||
pinMode(motor_right[i], OUTPUT);
|
||||
}
|
||||
|
||||
pinMode(enA, OUTPUT);
|
||||
pinMode(enB, OUTPUT);
|
||||
digitalWrite(enA, 100);
|
||||
digitalWrite(enB, 100);
|
||||
motor_stop();
|
||||
|
||||
PT_INIT(&pushThread);
|
||||
|
||||
connectHttp();
|
||||
setupResource();
|
||||
wdt_enable(WDTO_4S);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
protothread1(&pushThread, 1000);
|
||||
|
||||
listen();
|
||||
// wdt_reset();
|
||||
// Check connection
|
||||
if( !cc3000.checkConnected() ){
|
||||
while(1){
|
||||
}
|
||||
}
|
||||
|
||||
wdt_reset();
|
||||
drive();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void drive(){
|
||||
switch(motion_global){
|
||||
case 1 : drive_forward();
|
||||
break;
|
||||
case 2 : drive_backward();
|
||||
break;
|
||||
case 3 : turn_left();
|
||||
break;
|
||||
case 4 : turn_right();
|
||||
break;
|
||||
case 5 :
|
||||
motor_stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void updateDirectionVariable(int motionDir){
|
||||
motion_global = motionDir;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int protothread1(struct pt *pt, int interval) {
|
||||
PT_BEGIN(pt);
|
||||
while(1) { // never stop
|
||||
/* each time the function is called the second boolean
|
||||
* argument "millis() - timestamp > interval" is re-evaluated
|
||||
* and if false the function exits after that. */
|
||||
PT_WAIT_UNTIL(pt, listen() );
|
||||
|
||||
if (pushClient.connected()) {
|
||||
// batches all the required pin values together and pushes once
|
||||
// Pushes data in 1 second interval
|
||||
pushData();
|
||||
wdt_reset();
|
||||
} else {
|
||||
pushClient.close();
|
||||
cc3000.disconnect();
|
||||
connectHttp();
|
||||
}
|
||||
}
|
||||
PT_END(pt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,107 @@
|
||||
//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;
|
||||
}
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
templates=FireAlarmWifiAgent.h
|
||||
zipfilename=SensebotWifiAgent.zip
|
||||
templates=SenseBotWifiAgent.h
|
||||
zipfilename=SenseBotWifiAgent.zip
|
Loading…
Reference in new issue