Skip to content

Instantly share code, notes, and snippets.

@wardpieters wardpieters/ESP8266.ino Secret forked from rinkp/ipcamera.php
Last active Jun 2, 2018

Embed
What would you like to do?
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
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");
if (substr($_SERVER['REMOTE_ADDR'],0,10) != "192.168.1." 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.");
}
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");
$sensitivity = 60;
$notification_api_key = "[API_KEY_NOTIFICATIONS]";
if (request_basic_auth("http://192.168.1.175:443/web/cgi-bin/hi3510/param.cgi?cmd=preset&-act=goto&-number=" . $preset) == true) {
if ($preset == 3) {
post_basic_auth("http://192.168.1.175: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://192.168.1.175: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://192.168.1.175: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://192.168.1.175: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://192.168.1.175: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 " . $_SERVER['REMOTE_ADDR']);
trigger_error("IPCamera automatisch uitgeschakeld vanaf " . $_SERVER['REMOTE_ADDR'], E_USER_NOTICE);
} else {
send_notification("Uitgeschakeld vanaf " . $_SERVER['REMOTE_ADDR']);
trigger_error("IPCamera uitgeschakeld vanaf " . $_SERVER['REMOTE_ADDR'], E_USER_NOTICE);
}
}
else {
post_basic_auth("http://192.168.1.175: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://192.168.1.175: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=110&-aemode=1&-image_type=1&-imgmode=1");
post_basic_auth("http://192.168.1.175: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://192.168.1.175: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://192.168.1.175: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://192.168.1.175: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 " . $_SERVER['REMOTE_ADDR']);
trigger_error("IPCamera automatisch ingeschakeld naar $preset vanaf " . $_SERVER['REMOTE_ADDR'], E_USER_NOTICE);
} else {
send_notification("Ingeschakeld naar $preset vanaf " . $_SERVER['REMOTE_ADDR']);
trigger_error("IPCamera ingeschakeld naar $preset vanaf " . $_SERVER['REMOTE_ADDR'], E_USER_NOTICE);
}
}
echo "Gelukt";
}
else {
http_response_code(503);
send_notification("Kon IPCamera niet naar $preset instellen");
trigger_error("Kon IPCamera niet naar $preset instellen", E_USER_WARNING);
echo "Mislukt";
}
function request_basic_auth($url) {
global $school;
$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;
else return false;
}
function post_basic_auth($url,$content) {
global $school;
$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;
else return false;
//return $result;
}
function send_notification($message) {
global $notification_api_key;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.wardpieters.nl/firebase/api.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query(
array(
"message" => $message,
"key" => $notification_api_key,
)
),
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded"
),
));
$response = json_decode(curl_exec($curl),true);
$err = curl_error($curl);
curl_close($curl);
if(!$err && $response['error'] == false) return true;
return false;
}
?>
@wardpieters

This comment has been minimized.

Copy link
Owner Author

commented Jun 2, 2018

@rinkp Vervang [API_KEY_NOTIFICATIONS] met de API key

@rinkp

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.