Skip to content

Instantly share code, notes, and snippets.

@raspberrytipsnl
Created March 27, 2018 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspberrytipsnl/72aa66302c8278eac34fa5c76291acd0 to your computer and use it in GitHub Desktop.
Save raspberrytipsnl/72aa66302c8278eac34fa5c76291acd0 to your computer and use it in GitHub Desktop.
Homekit (Homebridge) bewegingsmelder(motion sensor) script Arduino/Wemos
// Homekit motions sensor voor HomeBridge
// raspberrytips.nl
#include <ESP8266WiFi.h>
const char* ssid = "naamnetwerk"; // SSID Wifi (naam)
const char* password = "watchwoord"; // Wachtwoord wifi
IPAddress ip(192, 168, 2, 99); // IP address
IPAddress dns(192, 168, 2, 254); // IP address DNS server
IPAddress gateway(192, 168, 2, 254); // IP adres gateway/router.
IPAddress subnet(255, 255, 255, 0); // Subnetmask
WiFiClient client;
IPAddress motionServer(192, 168, 2, 60); // IP adres Homebridge server
#define REMOTE_PORT_NUMBER 18089
#define TIMESPAN_MOTION 10000
#define hcsr501pin 12
unsigned long lastMotionDetected = -1;
void setup() {
Serial.begin(9600);
while (!Serial); // wait for serial attach
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(ip, dns, gateway, subnet);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Connect Failed! Rebooting...");
delay(1000);
ESP.restart();
}
pinMode(hcsr501pin, INPUT);
}
void loop() {
int buttonState = digitalRead(hcsr501pin);
unsigned long now = millis();
if (buttonState == 1 && (now - lastMotionDetected) > TIMESPAN_MOTION){
lastMotionDetected = now;
Serial.println("Motion detected");
if (client.connect(motionServer, REMOTE_PORT_NUMBER)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /motion HTTP/1.1");
client.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment