forked from community/product-iots
parent
be5e12deb9
commit
07bae69062
@ -0,0 +1,99 @@
|
||||
#include "Arduinoboardwifi.h"
|
||||
|
||||
#include <Adafruit_CC3000.h>
|
||||
#include <SPI.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;
|
||||
|
||||
uint32_t sserver;
|
||||
|
||||
/**********************************************************************************************
|
||||
0. Check with a sample Wifi code of the Adafruit_CC3000 library to ensure that the sheild is working
|
||||
1. Set the ip of the server(byte array below) where the Web-Rest API for the FireAlarm is running
|
||||
2. Check whether the "SERVICE_EPOINT" is correct in the 'FireAlarmWifiAgent.h' file
|
||||
3. Check whether the "SERVICE_PORT" is the same (9763) for the server running. Change it if needed
|
||||
4. Check whether the pins have been attached accordingly in the Arduino
|
||||
5. Check whether all reqquired pins are added to the 'digitalPins' array
|
||||
***********************************************************************************************/
|
||||
|
||||
byte server[4] = { 192, 168, 1, 101 };
|
||||
String host, jsonPayLoad, replyMsg;
|
||||
String responseMsg, subStrn;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
Serial.println(F("Internal Temperature Sensor"));
|
||||
pinMode(6, OUTPUT);
|
||||
connectHttp();
|
||||
setupResource();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
if (pushClient.connected() && pollClient.connected()) {
|
||||
pushData();
|
||||
delay(POLL_INTERVAL);
|
||||
|
||||
boolean valid = readControls();
|
||||
|
||||
responseMsg="";
|
||||
} else {
|
||||
if(DEBUG) {
|
||||
Serial.println("client not found...");
|
||||
Serial.println("disconnecting.");
|
||||
}
|
||||
pushClient.close();
|
||||
pollClient.close();
|
||||
cc3000.disconnect();
|
||||
|
||||
connectHttp();
|
||||
}
|
||||
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
double getBoardTemp(void)
|
||||
{
|
||||
unsigned int wADC;
|
||||
double t;
|
||||
|
||||
// The internal temperature has to be used
|
||||
// with the internal reference of 1.1V.
|
||||
// Channel 8 can not be selected with
|
||||
// the analogRead function yet.
|
||||
|
||||
// Set the internal reference and mux.
|
||||
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
|
||||
ADCSRA |= _BV(ADEN); // enable the ADC
|
||||
|
||||
delay(20); // wait for voltages to become stable.
|
||||
|
||||
ADCSRA |= _BV(ADSC); // Start the ADC
|
||||
|
||||
// Detect end-of-conversion
|
||||
while (bit_is_set(ADCSRA,ADSC));
|
||||
|
||||
// Reading register "ADCW" takes care of how to read ADCL and ADCH.
|
||||
wADC = ADCW;
|
||||
|
||||
// The offset of 324.31 could be wrong. It is just an indication.
|
||||
t = (wADC - 324.31 ) / 1.22;
|
||||
|
||||
// The returned temperature is in degrees Celcius.
|
||||
return (t);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,39 @@
|
||||
#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,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;
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
templates=FireAlarmEthernetAgent.h
|
||||
zipfilename=Arduino.zip
|
||||
templates=Arduinoboardwifi.h
|
||||
zipfilename=ArduinoBoardSketch.zip
|
Loading…
Reference in new issue