Skip to content

Instantly share code, notes, and snippets.

@mbcharbonneau
Created November 24, 2016 03:53
Show Gist options
  • Save mbcharbonneau/4824b290bbf8468a721f300b8813e398 to your computer and use it in GitHub Desktop.
Save mbcharbonneau/4824b290bbf8468a721f300b8813e398 to your computer and use it in GitHub Desktop.
Example sketch for using an Arduino to send temperature and humidity data to HomeKit.
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include "Adafruit_HTU21DF.h"
// Program variables.
Adafruit_HTU21DF temp = Adafruit_HTU21DF();
BridgeServer server;
void setup() {
Serial.begin(9600);
Serial.println(F("Starting sensor..."));
Bridge.begin();
if (!temp.begin()) {
Serial.println(F("HTU21D-F communication error."));
while(1);
}
server.listenOnLocalhost();
server.begin();
Serial.println(F("Setup complete."));
}
void loop() {
BridgeClient client = server.accept();
if (client) {
client.setTimeout(5);
process(client);
client.stop();
}
delay(50);
}
void process(BridgeClient client) {
String command = client.readStringUntil('/');
command.trim();
if (command == "temperature") {
float temperature = temp.readTemperature();
float humidity = temp.readHumidity();
client.println(F("{"));
client.print(F("\t\"temperature\": "));
client.print(temperature);
client.println(F(","));
client.print(F("\t\"humidity\": "));
client.println(humidity);
client.println(F("}"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment