Created
February 1, 2018 21:39
-
-
Save valvoline/6351cdfcf9e148c4fd863f645047d99a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A simple HomeBridgeHub example for HomeBridge | |
Author: Costantino Pistagna <[email protected]> | |
*/ | |
#include <dht.h> | |
#include <Bridge.h> | |
#include <BridgeServer.h> | |
#include <BridgeClient.h> | |
dht DHT; | |
BridgeServer server; | |
String startString; | |
#define DHT22_PIN 7 | |
#define REMOTEDOOR_PIN 4 | |
void setup() | |
{ | |
pinMode(REMOTEDOOR_PIN, OUTPUT); | |
digitalWrite(REMOTEDOOR_PIN, LOW); | |
Bridge.begin(); | |
server.listenOnLocalhost(); | |
server.begin(); | |
Process startTime; | |
startTime.runShellCommand("date"); | |
while (startTime.available()) { | |
char c = startTime.read(); | |
startString += c; | |
} | |
} | |
void loop() | |
{ | |
BridgeClient client = server.accept(); | |
if (client) { | |
String command = client.readString(); | |
command.trim(); | |
// Console.println(command); | |
if (command == "temp") { | |
int chk = DHT.read22(DHT22_PIN); | |
switch (chk) | |
{ | |
case DHTLIB_OK: | |
client.print("{ \"temperature\":"); | |
client.print(DHT.temperature-2); | |
client.print(", \"humidity\":"); | |
client.print(DHT.humidity); | |
client.println("}"); | |
break; | |
case DHTLIB_ERROR_CHECKSUM: | |
client.println("{ \"status\" : \"Checksum error\""); | |
break; | |
case DHTLIB_ERROR_TIMEOUT: | |
client.println("{ \"status\" : \"Timeout error\""); | |
break; | |
default: | |
client.println("{ \"status\" : \"Unknown error\""); | |
break; | |
} | |
} | |
else if (command == "switchOn") { | |
client.println("{\"status\" : \"OK\"}"); | |
digitalWrite(REMOTEDOOR_PIN, HIGH); | |
delay(2000); | |
digitalWrite(REMOTEDOOR_PIN, LOW); | |
} | |
client.stop(); | |
} | |
delay(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment