Skip to content

Instantly share code, notes, and snippets.

@wardpieters
Forked from rinkp/ipcamera.php
Last active August 22, 2019 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wardpieters/ed6806b0ca25791bd9c8e51601d934a0 to your computer and use it in GitHub Desktop.
Save wardpieters/ed6806b0ca25791bd9c8e51601d934a0 to your computer and use it in GitHub Desktop.
IPCamera
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define LED_BUILTIN 2
const char* ssid = "XP-820";
const char* pass = "MVhek2kKsxLHwc9";
const int pinAlarmIn1 = D1; //bedenk hier pinnummer
boolean AlarmIn1; //1 = niet verbonden; 0 = wel verbonden
boolean AlarmStatus; //1 = aan of storing; 0 = uit
boolean neverset = true;
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
WiFi.begin(ssid, pass); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!\n");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}
boolean IPcameraUpdate(boolean status) {
HTTPClient http;
if(status == 1) http.begin("http://nasc9bfcb/ipcamera.php?inschakelen"); //HTTP
else http.begin("http://nasc9bfcb/ipcamera.php?uitschakelen"); //HTTP
http.addHeader("Host", "nasc9bfcb");
http.setTimeout(10000);
int httpCode = http.GET();
if(httpCode == 200) return true;
else return false;
}
void loop() {
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED)) {
pinMode(pinAlarmIn1, INPUT_PULLUP);
AlarmIn1 = digitalRead(pinAlarmIn1); // HIGH (1) is niet verbonden want PULLUP; LOW (0) is wel verbonden.
Serial.print("Waarde van AlarmIn1: ");
Serial.println(AlarmIn1);
if (AlarmIn1 == 1) {
//Alarm aan of kabel los, lampje aan, IPcamera aan
if (AlarmStatus == 0 || neverset == true) {
digitalWrite(LED_BUILTIN, LOW);
if (IPcameraUpdate(1) == true) {
Serial.println("IPCamera ingeschakeld");
}
else {
Serial.println("IPCamera inschakelen mislukt");
neverset = true;
}
}
AlarmStatus = 1;
}
else {
//alarm uit, lampje uit, IPcamera uit
if (AlarmStatus == 1 || neverset == true) {
digitalWrite(LED_BUILTIN, HIGH);
if (IPcameraUpdate(0) == true) {
Serial.println("IPCamera uitgeschakeld");
}
else {
Serial.println("IPCamera uitschakelen mislukt");
neverset = true;
}
}
AlarmStatus = 0;
}
neverset = false;
}
else {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Not connected to wifi\n");
neverset = true;
}
delay(10000);
}
<?php
/* infraredstat=auto voor automatisch, infraredstat=open voor aan, infraredstat=close voor uit */
ini_set("log_errors", 1);
ini_set("error_log", "ipcamera.log");
ini_set('display_errors', false);
error_reporting(E_ALL);
date_default_timezone_set("Europe/Amsterdam");
$sensitivity = 60;
$notification_api_key = "[SECRET]";
if (substr($_SERVER['REMOTE_ADDR'],0,5) != "10.9." AND ($_SERVER['REMOTE_ADDR'] != "185.56.145.157" OR $_GET['key'] != "[SECRET]")) {
error_log($_SERVER['REMOTE_ADDR']);
exit("Dit IP heeft geen toegang tot deze pagina.");
}
$REMOTE_DEVICE = $_SERVER['REMOTE_ADDR'];
if ($_SERVER['REMOTE_ADDR'] == "10.9.9.162") $REMOTE_DEVICE = "alarmsysteem";
if (isset($_GET['inschakelen']) AND !isset($_GET['uitschakelen'])) $preset = 0;
elseif (isset($_GET['uitschakelen']) AND !isset($_GET['inschakelen'])) $preset = 3;
else exit("Geen keuze opgegeven");
if (request_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi?cmd=preset&-act=goto&-number=" . $preset) == true) {
if ($preset == 3) {
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setaudioalarmattr&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Falarmaudio.html&-aa_enable=0&-aa_value=50");
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setinfrared&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Fdisplay.html&-infraredstat=close&cmd=setircutattr&-saradc_switch_value=250&cmd=setimageattr&-brightness=50&-contrast=50&-saturation=120&-sharpness=70&-mirror=on&-flip=on&-shutter=65535&-night=off&-wdr=off&-noise=off&-gc=65535&-ae=128&-targety=110&-aemode=1&-image_type=1&-imgmode=1");
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setmdalarm&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Falarm.html&-aname=email&-switch=off&cmd=setmdalarm&-aname=ftpsnap&-switch=on&cmd=setmdalarm&-aname=ftprec&-switch=on&cmd=setmdalarm&-aname=snap&-switch=off&cmd=setmdalarm&-aname=record&-switch=off&cmd=setmdalarm&-aname=relay&-switch=off&cmd=setmdalarm&-aname=server&-switch=off&cmd=setmdalarm&-aname=preset&-switch=off&cmd=setrelayattr&-time=5&cmd=setmotorattr&-alarmpresetindex=1&cmd=setalarmsnapattr&-snap_count=3");
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setmdattr&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Fmd.html&-name=1&-x=0&-y=288&-w=1276&-h=716&-s=$sensitivity&-t=0&-enable=0&-chn=1&cmd=setmdattr&-name=2&-x=0&-y=288&-w=200&-h=120&-s=50&-t=0&-enable=0&-chn=1&cmd=setmdattr&-name=3&-x=0&-y=288&-w=200&-h=120&-s=50&-t=0&-enable=0&-chn=1&cmd=setmdattr&-name=4&-x=0&-y=288&-w=200&-h=120&-s=50&-t=0&-enable=0&-chn=1");
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setsnaptimerattrex&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Fautosnap.html&-as_interval=60&-as_type=snap&-as_enable=0&cmd=setsnaptimerattrex&-as_interval=60&-as_type=ftp&-as_enable=0&cmd=setscheduleex&-ename=snap&-week0=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week1=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week2=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week3=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week4=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week5=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week6=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP");
if(isset($_GET['key']) AND $_GET['key'] == "[SECRET]") {
send_notification("Automatisch uitgeschakeld vanaf " . $REMOTE_DEVICE);
trigger_error("IPCamera automatisch uitgeschakeld vanaf " . $REMOTE_DEVICE, E_USER_NOTICE);
} else {
send_notification("Uitgeschakeld vanaf " . $REMOTE_DEVICE);
trigger_error("IPCamera uitgeschakeld vanaf " . $REMOTE_DEVICE, E_USER_NOTICE);
}
}
else {
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setaudioalarmattr&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Falarmaudio.html&-aa_enable=0&-aa_value=50"); //geen alarm op audio
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setinfrared&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Fdisplay.html&-infraredstat=open&cmd=setircutattr&-saradc_switch_value=250&cmd=setimageattr&-brightness=50&-contrast=50&-saturation=120&-sharpness=70&-mirror=on&-flip=on&-shutter=65535&-night=off&-wdr=off&-noise=off&-gc=65535&-ae=128&-targety=175&-aemode=1&-image_type=1&-imgmode=0");
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setmdalarm&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Falarm.html&-aname=email&-switch=on&cmd=setmdalarm&-aname=ftpsnap&-switch=on&cmd=setmdalarm&-aname=ftprec&-switch=on&cmd=setmdalarm&-aname=snap&-switch=off&cmd=setmdalarm&-aname=record&-switch=off&cmd=setmdalarm&-aname=relay&-switch=off&cmd=setmdalarm&-aname=server&-switch=off&cmd=setmdalarm&-aname=preset&-switch=off&cmd=setrelayattr&-time=5&cmd=setmotorattr&-alarmpresetindex=1&cmd=setalarmsnapattr&-snap_count=3");
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setmdattr&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Fmd.html&-name=1&-x=0&-y=0&-w=1276&-h=716&-s=$sensitivity&-t=0&-enable=1&-chn=1&cmd=setmdattr&-name=2&-x=0&-y=364&-w=200&-h=120&-s=50&-t=0&-enable=0&-chn=1&cmd=setmdattr&-name=3&-x=0&-y=364&-w=200&-h=120&-s=50&-t=0&-enable=0&-chn=1&cmd=setmdattr&-name=4&-x=0&-y=364&-w=200&-h=120&-s=50&-t=0&-enable=0&-chn=1");
post_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/param.cgi","cmd=setsnaptimerattrex&cururl=http%3A%2F%2Fipcam%3A443%2Fweb%2Fautosnap.html&-as_interval=60&-as_type=snap&-as_enable=0&cmd=setsnaptimerattrex&-as_interval=60&-as_type=ftp&-as_enable=1&cmd=setscheduleex&-ename=snap&-week0=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week1=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week2=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week3=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week4=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week5=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP&-week6=PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP");
sleep(5);
request_basic_auth("http://10.9.9.163:443/web/cgi-bin/hi3510/ptzctrl.cgi?-step=0&-act=hscan");
if(isset($_GET['key']) AND $_GET['key'] == "[SECRET]") {
send_notification("Automatisch ingeschakeld naar $preset vanaf " . $REMOTE_DEVICE);
trigger_error("IPCamera automatisch ingeschakeld naar $preset vanaf " . $REMOTE_DEVICE, E_USER_NOTICE);
} else {
send_notification("Ingeschakeld naar $preset vanaf " . $REMOTE_DEVICE);
trigger_error("IPCamera ingeschakeld naar $preset vanaf " . $REMOTE_DEVICE, E_USER_NOTICE);
}
}
echo "Gelukt";
}
else {
http_response_code(503);
send_notification("Kon IPCamera niet naar $preset instellen vanaf $REMOTE_DEVICE");
trigger_error("Kon IPCamera niet naar $preset instellen vanaf $REMOTE_DEVICE", E_USER_WARNING);
echo "Mislukt";
}
function request_basic_auth($url, $attempt = 0) {
$bagebruikersnaam = "gebruiker";
$bawachtwoord = "[SECRET]";
$headers[] = "Authorization: Basic " . base64_encode($bagebruikersnaam . ":" . $bawachtwoord);
$curl = curl_init();
//$f = fopen('request.txt', 'w');
if (!empty($parameters)) curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
//curl_setopt($curl, CURLOPT_STDERR, $f);
$result = curl_exec($curl);
curl_close($curl);
if ($result == "[Succeed]set ok.") return true;
elseif ($attempt < 3) return request_basic_auth($url, $attempt + 1);
else return false;
}
function post_basic_auth($url,$content, $attempt = 0) {
$bagebruikersnaam = "admin";
$bawachtwoord = "[SECRET]";
$headers[] = "Authorization: Basic " . base64_encode($bagebruikersnaam . ":" . $bawachtwoord);
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$curl = curl_init();
//$f = fopen('request.txt', 'w');
if (!empty($content)) curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
//curl_setopt($curl, CURLOPT_STDERR, $f);
$result = curl_exec($curl);
curl_close($curl);
if ($result == "[Succeed]set ok.") return true;
elseif ($attempt < 3) return post_basic_auth($url, $content, $attempt + 1);
else return false;
}
function send_notification($message) {
global $notification_api_key;
$json = json_encode(
array(
"type" => "ipcamera",
"title" => "IP Camera",
"body" => $message
)
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://dashboard.wardpieters.nl/api/notification",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer " . $notification_api_key,
'Content-Length: ' . strlen($json)
),
));
$response = json_decode(curl_exec($curl),true);
$err = curl_error($curl);
curl_close($curl);
if(!$err && $response['status'] == true) return true;
return false;
}
?>
@wardpieters
Copy link
Author

@rinkp Vervang [API_KEY_NOTIFICATIONS] met de API key

@rinkp
Copy link

rinkp commented Jun 2, 2018

DE API-key?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment