forked from community/product-iots
parent
9f4d2fed4b
commit
c24e6edea1
@ -0,0 +1 @@
|
||||
templates=FireAlarmAgent.h
|
@ -0,0 +1,98 @@
|
||||
byte mac[6] = { 0x90, 0xA2, 0xDA, 0x0D, 0x30, 0xD7}; //mac - 90a2da0d30d7
|
||||
byte dns2[] = { 8, 8, 8, 8 };
|
||||
byte subnet[] = { 255, 255, 255, 0 };
|
||||
byte gateway[] = { 192, 168, 1, 1 };
|
||||
byte deviceIP[4] = { 192, 168, 1, 219 };
|
||||
byte server[4] = { 192, 168, 1, 216 };
|
||||
//byte server[4] = { 207, 58, 139, 247 };
|
||||
|
||||
uint32_t ip, ddns, ssubnet, ggateway, sserver;
|
||||
String connecting = "connecting.... ";
|
||||
|
||||
void connectHttp() {
|
||||
/* Initialise the module */
|
||||
if(true) Serial.println(F("\nInitializing..."));
|
||||
if (!cc3000.begin())
|
||||
{
|
||||
if(true) Serial.println(F("Couldn't begin()! Check your wiring?"));
|
||||
while(1);
|
||||
}
|
||||
|
||||
ip = cc3000.IP2U32(deviceIP[0], deviceIP[1], deviceIP[2], deviceIP[3]);
|
||||
ddns = cc3000.IP2U32(dns2[0], dns2[1], dns2[2], dns2[3]);
|
||||
ssubnet = cc3000.IP2U32(subnet[0], subnet[1], subnet[2], subnet[3]);
|
||||
ggateway = cc3000.IP2U32(gateway[0], gateway[1], gateway[2], gateway[3]);
|
||||
sserver = cc3000.IP2U32(server[0], server[1], server[2], server[3]);
|
||||
|
||||
cc3000.setStaticIPAddress(ip, ssubnet, ggateway, ddns);
|
||||
|
||||
if(true) {
|
||||
Serial.print(F("\nAttempting to connect to "));
|
||||
Serial.println(WLAN_SSID);
|
||||
}
|
||||
|
||||
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
|
||||
if(true) Serial.println(F("Failed!"));
|
||||
while(1);
|
||||
}
|
||||
|
||||
if(true) Serial.println(F("Connected to Wifi network!"));
|
||||
|
||||
httpClient = cc3000.connectTCP(sserver, SERVICE_PORT); //SERVICE_PORT
|
||||
if (httpClient.connected()) {
|
||||
if(true) Serial.println("Connected to server");
|
||||
} else {
|
||||
if(true) Serial.println(F("Connection failed"));
|
||||
|
||||
while(!httpClient.connected()){
|
||||
if(true) Serial.println("retrying to connect......");
|
||||
httpClient = cc3000.connectTCP(sserver, SERVICE_PORT);
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
if(true) Serial.println(F("-------------------------------------"));
|
||||
}
|
||||
|
||||
|
||||
void setupResource(){
|
||||
String hostIP = getHostIP(server);
|
||||
String port = String(SERVICE_PORT);
|
||||
|
||||
host = "Host: " + hostIP + ":" + port;
|
||||
if(DEBUG) Serial.println(host);
|
||||
|
||||
jsonPayLoad = "{\"owner\":\"";
|
||||
jsonPayLoad += String(DEVICE_OWNER);
|
||||
jsonPayLoad += "\",\"deviceId\":\"";
|
||||
jsonPayLoad += String(DEVICE_ID);
|
||||
jsonPayLoad += "\",\"replyMessage\":\"";
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.print("JSON Payload: ");
|
||||
Serial.println(jsonPayLoad);
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String getMyIP(byte deviceIP[4]){
|
||||
String myIP = String(deviceIP[0]);
|
||||
|
||||
for ( int index = 1; index < 4; index++) {
|
||||
myIP += "." + String(deviceIP[index]);
|
||||
}
|
||||
|
||||
return myIP;
|
||||
}
|
||||
|
||||
|
||||
String getHostIP(byte server[4]){
|
||||
String hostIP = String(server[0]);
|
||||
|
||||
for ( int index = 1; index < 4; index++) {
|
||||
hostIP += "." + String(server[index]);
|
||||
}
|
||||
|
||||
return hostIP;
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#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 "Dialog 4G" // cannot be longer than 32 characters!
|
||||
#define WLAN_PASS "FA09C543"
|
||||
#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}"
|
||||
#define DEVICE_TYPE "FireAlarm"
|
||||
#define DEVICE_ID "${DEVICE_ID}"
|
||||
|
||||
#define SERVICE_PORT 9763
|
||||
#define SERVICE_EPOINT "/WSO2ConnectedDevices/FireAlarmController/"
|
||||
// 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
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -0,0 +1,141 @@
|
||||
#include "FireAlarmWifiAgent.h"
|
||||
|
||||
#include <Adafruit_CC3000.h>
|
||||
//#include <ccspi.h>
|
||||
//#include <string.h>
|
||||
//#include "utility/debug.h"
|
||||
#include <SPI.h>
|
||||
#include "dht.h"
|
||||
|
||||
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
|
||||
int analogPins[] = { 0, 1, 2, 3, 4, 5 };
|
||||
|
||||
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 httpClient;
|
||||
String host, jsonPayLoad, replyMsg;
|
||||
|
||||
void setup() {
|
||||
if(true) Serial.begin(115200);
|
||||
pinMode(BULB_PIN, OUTPUT);
|
||||
pinMode(FAN_PIN, OUTPUT);
|
||||
connectHttp();
|
||||
setupResource();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (httpClient.connected()) {
|
||||
pushDigitalPinData();
|
||||
|
||||
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("TEMP")) {
|
||||
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.");
|
||||
}
|
||||
|
||||
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,104 @@
|
||||
String readControls() {
|
||||
String responseMsg;
|
||||
|
||||
httpClient.fastrprint(F("GET "));
|
||||
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("readcontrols/"));
|
||||
httpClient.fastrprint(DEVICE_OWNER); httpClient.fastrprint(F("/")); httpClient.fastrprint(DEVICE_ID);
|
||||
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n"));
|
||||
httpClient.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 payLoad = replyMsg + "\"}";
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.print(jsonPayLoad); Serial.println(payLoad);
|
||||
}
|
||||
|
||||
httpClient.fastrprint(F("POST "));
|
||||
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("reply"));
|
||||
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("Content-Length: "));
|
||||
|
||||
int payLength = jsonPayLoad.length() + payLoad.length();
|
||||
|
||||
httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n"));
|
||||
httpClient.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()) {
|
||||
httpClient.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
|
||||
if(DEBUG) Serial.print(jsonPayLoad.substring(i*chunkSize, jsonPayLoad.length()));
|
||||
i = jsonPayLoad.length();
|
||||
} else {
|
||||
httpClient.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()) {
|
||||
httpClient.print(payLoad.substring(i*chunkSize, payLoad.length()));
|
||||
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLoad.length()));
|
||||
i = payLoad.length();
|
||||
} else {
|
||||
httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
|
||||
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
|
||||
}
|
||||
}
|
||||
|
||||
httpClient.fastrprint(F("\n"));
|
||||
if(DEBUG) Serial.println();
|
||||
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
if(DEBUG) Serial.print(response);
|
||||
}
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
|
||||
payLoad = "";
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,141 @@
|
||||
void pushDigitalPinData(){
|
||||
for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) {
|
||||
String payLoad = jsonPayLoad + "DigitalPinData";
|
||||
payLoad = payLoad + "\",\"time\":\"" + "9999";
|
||||
payLoad = payLoad + "\",\"key\":\"" + getDataType(digitalPins[pin]);
|
||||
payLoad = payLoad + "\",\"value\":\"";
|
||||
|
||||
if ( digitalPins[pin] == TEMP_PIN ) {
|
||||
int temperature = (uint8_t)getTemperature();
|
||||
payLoad += temperature;
|
||||
} else if ( digitalRead(digitalPins[pin]) == HIGH ) {
|
||||
payLoad += "ON";
|
||||
} else if ( digitalRead(digitalPins[pin]) == LOW ) {
|
||||
payLoad += "OFF";
|
||||
}
|
||||
|
||||
payLoad += "\"}";
|
||||
|
||||
httpClient.fastrprint(F("POST "));
|
||||
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("pushalarmdata"));
|
||||
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("Content-Length: "));
|
||||
|
||||
int payLength = payLoad.length();
|
||||
|
||||
httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("\n"));
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.print("POST ");
|
||||
Serial.print(SERVICE_EPOINT); Serial.print("pushalarmdata");
|
||||
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 < payLength; i++) {
|
||||
if ( (i+1)*chunkSize > payLength) {
|
||||
httpClient.print(payLoad.substring(i*chunkSize, payLength));
|
||||
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLength));
|
||||
i = payLength;
|
||||
} else {
|
||||
httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
|
||||
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
|
||||
}
|
||||
}
|
||||
|
||||
httpClient.fastrprint(F("\n"));
|
||||
if(DEBUG) Serial.println();
|
||||
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
if(DEBUG) Serial.print(response);
|
||||
}
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
|
||||
payLoad = "";
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void pushAnalogPinData(){
|
||||
for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) {
|
||||
String payLoad = jsonPayLoad + "AnalogPinData";
|
||||
payLoad = payLoad + "\",\"time\":\"" + "9999";
|
||||
payLoad = payLoad + "\",\"key\":\"" + getDataType(analogPins[pin]);
|
||||
payLoad = payLoad + "\",\"value\":\"" + analogRead(analogPins[pin]);
|
||||
payLoad = payLoad + "\"}";
|
||||
|
||||
httpClient.fastrprint(F("POST "));
|
||||
httpClient.fastrprint(SERVICE_EPOINT); httpClient.fastrprint(F("pushalarmdata"));
|
||||
httpClient.fastrprint(F(" HTTP/1.1")); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(host.c_str()); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("Content-Type: application/json")); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("Content-Length: "));
|
||||
|
||||
int payLength = payLoad.length();
|
||||
|
||||
httpClient.fastrprint(String(payLength).c_str()); httpClient.fastrprint(F("\n"));
|
||||
httpClient.fastrprint(F("\n"));
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.print("POST ");
|
||||
Serial.print(SERVICE_EPOINT); Serial.print("pushalarmdata");
|
||||
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 < payLength; i++) {
|
||||
if ( (i+1)*chunkSize > payLength) {
|
||||
httpClient.print(payLoad.substring(i*chunkSize, payLength));
|
||||
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, payLength));
|
||||
i = payLength;
|
||||
} else {
|
||||
httpClient.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
|
||||
if(DEBUG) Serial.print(payLoad.substring(i*chunkSize, (i+1)*chunkSize));
|
||||
}
|
||||
}
|
||||
|
||||
httpClient.fastrprint(F("\n"));
|
||||
if(DEBUG) Serial.println();
|
||||
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
if(DEBUG) Serial.print(response);
|
||||
}
|
||||
|
||||
if(DEBUG) {
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
|
||||
payLoad = "";
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1 @@
|
||||
templates=FireAlarmWifiAgent.h
|
Loading…
Reference in new issue