forked from community/product-iots
parent
789b6f3a42
commit
9f4d2fed4b
Binary file not shown.
@ -0,0 +1,47 @@
|
||||
#[[
|
||||
#ifndef FireAlarmAgent_H
|
||||
#define FireAlarmAgent_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_TYPE "FireAlarm"
|
||||
]]#
|
||||
\#define DEVICE_OWNER "${DEVICE_OWNER}"
|
||||
\#define DEVICE_ID "${DEVICE_ID}"
|
||||
#[[
|
||||
#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 "/WSO2ConnectedDevices/FireAlarmController/"
|
||||
// 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 POLL_INTERVAL 1000
|
||||
|
||||
#endif
|
||||
]]#
|
||||
|
@ -0,0 +1,130 @@
|
||||
#include "FireAlarmAgent.h"
|
||||
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
#include "dht.h"
|
||||
|
||||
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
|
||||
int analogPins[] = { 0, 1, 2, 3, 4, 5 };
|
||||
|
||||
EthernetClient httpClient;
|
||||
String host, jsonPayLoad, replyMsg;
|
||||
|
||||
|
||||
void setup() {
|
||||
// Serial.begin(9600);
|
||||
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("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();
|
||||
}
|
||||
}
|
||||
|
||||
// delay(POLL_INTERVAL);
|
||||
|
||||
} else {
|
||||
// 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;
|
||||
// Serial.println("-------------------------------");
|
||||
// Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
|
||||
// Serial.print("DHT11, \t");
|
||||
int chk = DHT.read11(TEMP_PIN);
|
||||
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;
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
#[[
|
||||
#ifndef WSO2ArduinoAgent_H
|
||||
#define WSO2ArduinoAgent_H
|
||||
|
||||
#if (ARDUINO >= 100)
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#define HTTP_METHOD "POST"
|
||||
#define HTTP_VERSION "HTTP/1.1"
|
||||
|
||||
#define DEVICE_IP "192.168.1.219"
|
||||
#define DEVICE_TYPE "arduino"
|
||||
]]#
|
||||
\#define DEVICE_OWNER "${DEVICE_OWNER}"
|
||||
\#define DEVICE_ID "${DEVICE_ID}"
|
||||
#[[
|
||||
#define SERVICE_IP "192.168.1.216"
|
||||
#define SERVICE_PORT 9763
|
||||
#define SERVICE_EPOINT "/WSO2ConnectedDevices/DeviceController/pushdata/"
|
||||
// {ip}/{owner}/{type}/{mac}/90/{key}/{value}
|
||||
|
||||
#define MQTT_PROXY SERVICE_IP
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_CLIENTID "wso2:iot:arduino:xxxxxxxx"
|
||||
#define MQTT_TOPIC "wso2/iot/shabirmean/arduino/xxxxxxxx"
|
||||
|
||||
#define MQTTCLIENT_QOS2 1
|
||||
#define PUBLISH_INTERVAL 30000
|
||||
|
||||
enum IP_TYPE{
|
||||
SERVER,
|
||||
DEVICE
|
||||
};
|
||||
|
||||
#endif
|
||||
]]#
|
@ -1,36 +0,0 @@
|
||||
#include "IoTArduinoAgent.h"
|
||||
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
#include <IPStack.h>
|
||||
#include <Countdown.h>
|
||||
#include <MQTTClient.h>
|
||||
|
||||
EthernetClient httpClient;
|
||||
int digitalPins[] = { 2, 5, 8, 9, 12 };
|
||||
int analogPins[] = { 0, 1, 2, 3, 4, 5 };
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
connectHttp();
|
||||
setupResource();
|
||||
MQTTConnect();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (httpClient.connected()) {
|
||||
// pushDigitalPinData();
|
||||
// pushAnalogPinData();
|
||||
} else {
|
||||
Serial.println("client not found...");
|
||||
Serial.println("disconnecting.");
|
||||
httpClient.stop();
|
||||
// connectHttp();
|
||||
// for(;;)
|
||||
// ;
|
||||
}
|
||||
delay(PUBLISH_INTERVAL);
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,75 +0,0 @@
|
||||
void messageArrived(MQTT::MessageData& md);
|
||||
int arrivedcount = 0;
|
||||
MQTT::Message message;
|
||||
|
||||
EthernetClient mqtt; // replace by a YunClient if running on a Yun
|
||||
IPStack mqttIPStack(mqtt);
|
||||
MQTT::Client<IPStack, Countdown, 100, 1> mqttClient = MQTT::Client<IPStack, Countdown, 100, 1>(mqttIPStack);
|
||||
|
||||
|
||||
void MQTTConnect() {
|
||||
int rc = -1;
|
||||
if (!mqttClient.isConnected()) {
|
||||
Serial.print("Connecting using Registered mode with clientid : ");
|
||||
Serial.println(MQTT_CLIENTID);
|
||||
Serial.print("\tto MQTT Broker : ");
|
||||
Serial.println(MQTT_PROXY);
|
||||
Serial.print("\ton topic : ");
|
||||
Serial.println(MQTT_TOPIC);
|
||||
while (rc != 0) {
|
||||
rc = mqttIPStack.connect(MQTT_PROXY, MQTT_PORT);
|
||||
// rc = mqtt.connect(server, MQTT_PORT);
|
||||
Serial.print("rc from TCP connect is ");
|
||||
Serial.println(rc);
|
||||
}
|
||||
|
||||
Serial.println("MQTT connecting");
|
||||
MQTTPacket_connectData options = MQTTPacket_connectData_initializer;
|
||||
options.MQTTVersion = 3;
|
||||
options.clientID.cstring = MQTT_CLIENTID;
|
||||
// options.username.cstring = "admin";
|
||||
// options.password.cstring = "admin";
|
||||
// options.keepAliveInterval = 10;
|
||||
rc = -1;
|
||||
while ((rc = mqttClient.connect(options)) != 0){
|
||||
Serial.print("rc from MQTT connect is ");
|
||||
Serial.println(rc);
|
||||
}
|
||||
//unsubscribe the topic, if it had subscribed it before.
|
||||
Serial.println("MQTT connected");
|
||||
mqttClient.unsubscribe(MQTT_TOPIC);
|
||||
//Try to subscribe for commands
|
||||
if ((rc = mqttClient.subscribe(MQTT_TOPIC, MQTT::QOS2, messageArrived)) != 0) {
|
||||
Serial.print("Subscribe failed with return code : ");
|
||||
Serial.println(rc);
|
||||
} else {
|
||||
Serial.println("Subscribed\n");
|
||||
}
|
||||
Serial.println("Subscription tried......");
|
||||
Serial.println("Connected successfully\n");
|
||||
Serial.println("____________________________________________________________________________");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void messageArrived(MQTT::MessageData& md)
|
||||
{
|
||||
MQTT::Message &message = md.message;
|
||||
Serial.println("=======================================");
|
||||
Serial.print("Message ");
|
||||
// Serial.print(++arrivedcount);
|
||||
Serial.print(" arrived: qos ");
|
||||
Serial.print(message.qos);
|
||||
Serial.print(", retained ");
|
||||
Serial.print(message.retained);
|
||||
Serial.print(", dup ");
|
||||
Serial.print(message.dup);
|
||||
Serial.print(", packetid ");
|
||||
Serial.println(message.id);
|
||||
Serial.print("Payload ");
|
||||
Serial.println((char*)message.payload);
|
||||
Serial.println("=======================================");
|
||||
delay(1000);
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
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();
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
responseMsg += response;
|
||||
}
|
||||
|
||||
// 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();
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
// Serial.print(response);
|
||||
}
|
||||
|
||||
// Serial.println();
|
||||
// Serial.println("-------------------------------");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,91 +1,78 @@
|
||||
String resource, tempResource, host;
|
||||
String myIP = "";
|
||||
String hostIP = "";
|
||||
|
||||
void setupResource(){
|
||||
|
||||
setHostIP(server);
|
||||
String port = String(SERVICE_PORT);
|
||||
|
||||
host = "Host: " + hostIP + ":" + port;
|
||||
Serial.println(host);
|
||||
|
||||
myIP = String(Ethernet.localIP()[0]);
|
||||
|
||||
for ( int index = 1; index < 4; index++) {
|
||||
myIP += "." + String(Ethernet.localIP()[index]);
|
||||
}
|
||||
|
||||
resource = String(SERVICE_EPOINT) + myIP + "/" + String(DEVICE_OWNER) + "/" +
|
||||
String(DEVICE_TYPE) + "/" + String(DEVICE_ID) + "/";
|
||||
|
||||
resource = resource + 30;
|
||||
Serial.println(resource);
|
||||
Serial.println("-------------------------------");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setHostIP(byte server[4]){
|
||||
hostIP = String(server[0]);
|
||||
|
||||
for ( int index = 1; index < 4; index++) {
|
||||
hostIP += "." + String(server[index]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void pushDigitalPinData(){
|
||||
for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) {
|
||||
tempResource = " " + resource + "/D" + digitalPins[pin] + "/";
|
||||
String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " ";
|
||||
|
||||
if ( digitalRead(digitalPins[pin]) == HIGH) {
|
||||
tempResource += "HIGH ";
|
||||
} else if ( digitalRead(digitalPins[pin]) == LOW) {
|
||||
tempResource += "LOW ";
|
||||
String payLoad = jsonPayLoad + "DigitalPinData";
|
||||
payLoad = payLoad + String(TIME_JSON) + "9999";
|
||||
payLoad = payLoad + String(KEY_JSON) + getDataType(digitalPins[pin]);
|
||||
payLoad += String(VALUE_JSON);
|
||||
|
||||
|
||||
if ( digitalPins[pin] == TEMP_PIN ) {
|
||||
payLoad += String(getTemperature());
|
||||
} else if ( digitalRead(digitalPins[pin]) == HIGH ) {
|
||||
payLoad += "ON";
|
||||
} else if ( digitalRead(digitalPins[pin]) == LOW ) {
|
||||
payLoad += "OFF";
|
||||
}
|
||||
|
||||
httpClient.print(HTTP_METHOD);
|
||||
httpClient.print(tempResource);
|
||||
payLoad = payLoad + 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();
|
||||
delay(2000);
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
Serial.print(response);
|
||||
// Serial.print(response);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
tempResource = "";
|
||||
delay(2000);
|
||||
// Serial.println();
|
||||
// Serial.println("-------------------------------");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void pushAnalogPinData(){
|
||||
for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) {
|
||||
tempResource = " " + resource + "/A" + analogPins[pin] + "/" + analogRead(analogPins[pin]) + " ";
|
||||
String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " ";
|
||||
|
||||
String payLoad = jsonPayLoad + "AnalogPinData";
|
||||
payLoad = payLoad + String(TIME_JSON) + "9999";
|
||||
payLoad = payLoad + String(KEY_JSON) + getDataType(analogPins[pin]);
|
||||
payLoad = payLoad + String(VALUE_JSON) + analogRead(analogPins[pin]);
|
||||
payLoad = payLoad + String(END_JSON);
|
||||
|
||||
httpClient.print(HTTP_METHOD);
|
||||
httpClient.print(tempResource);
|
||||
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();
|
||||
delay(1000);
|
||||
|
||||
while (httpClient.available()) {
|
||||
char response = httpClient.read();
|
||||
Serial.print(response);
|
||||
// Serial.print(response);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println("-------------------------------");
|
||||
tempResource = "";
|
||||
// Serial.println();
|
||||
// Serial.println("-------------------------------");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue