#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0xFF, 0x1D }; / UNIQUE TO BOARD
IPAddress ip(192, 168, 1, 30); /Manufacturer recommended address
IPAddress myDns(1, 1, 1, 1); /Random, not sure if I need to do anything specific here
IPAddress gateway(192, 168, 1, 1); /Random, not sure if I need to do anything specific here
IPAddress subnet(255, 255, 255, 255);
/ initialize the library instance:
EthernetClient client;
/Camera
IPAddress server(192, 168, 1, 115); / (Camera) IPAddress
unsigned long lastConnectionTime = 0; / last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; / delay between updates, in milliseconds
/ the "L" is needed to use long type numbers
String big_string = "";
void setup() {
/ start serial port:
Serial.begin(115200);
while (!Serial) {
; / wait for serial port to connect. Needed for native USB port only
}
delay(1000);
/ start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac, ip, myDns, gateway, subnet);
/ print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
/ if there's incoming data from the net connection.
/ send it out the serial port. This is for debugging
/ purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
/ if ten seconds have passed since your last connection,
/ then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequestHigh();
delay(900000ul);
httpRequestLow();
}
}
void httpRequestHigh() {
/ close any connection before send a new request.
/ This will free the socket on the WiFi shield
client.stop();
/ if there's a successful connection:
if (client.connect(server, 80)) {
/Prepare big_string send the posted data.
big_string += "<?xml version=\"1.0\"encoding=\"UTF-8\"?>\n\r";
big_string += "<IOInputPort version=\"2.0\" xmlns=\"http://www.hikvision.com/ver20/XMLSchema\">\n\r";
big_string += "<id>1</id>\n\r";
big_string += "<enabled>true</enabled>\n\r";
big_string += "<triggering>high</triggering>\n\r"; / open
/ big_string += "<name/>\n\r";
big_string += "</IOInputPort>\n\r";
Serial.println("connecting...");
/ send the HTTP GET request:
client.println("PUT /IO/inputs/1 HTTP/1.1");
client.println("Host: 192.168.1.115");
client.println("Authorization: Basic ###############="); / amend authenticatrion
client.println("User-Agent: arduino-ethernet");
client.print("Content-length: ");
client.println(big_string.length());
/ tell the server you are done with the header.
client.println();
/ send the posted data.
client.print(big_string);
Serial.println("----------------------------------");
Serial.println(big_string);
Serial.println(big_string.length());
Serial.println("----------------------------------");
big_string = "";
/ finish up
client.println("Connection: close");
client.println();
/ note the time that the connection was made:
lastConnectionTime = millis();
} else {
/ if you couldn't make a connection:
Serial.println("connection failed");
}
}
void httpRequestLow() {
/ close any connection before send a new request.
/ This will free the socket on the WiFi shield
client.stop();
/ if there's a successful connection:
if (client.connect(server, 80)) {
/Prepare big_string send the posted data.
big_string += "<?xml version=\"1.0\"encoding=\"UTF-8\"?>\n\r";
big_string += "<IOInputPort version=\"2.0\" xmlns=\"http://www.hikvision.com/ver20/XMLSchema\">\n\r";
big_string += "<id>1</id>\n\r";
big_string += "<enabled>true</enabled>\n\r";
big_string += "<triggering>low</triggering>\n\r"; / colsed
/ big_string += "<name/>\n\r";
big_string += "</IOInputPort>\n\r";
Serial.println("connecting...");
/ send the HTTP GET request:
client.println("PUT /IO/inputs/1 HTTP/1.1");
client.println("Host: 192.168.1.115");
client.println("Authorization: Basic YWRtaW46TW9sbHlNNSE=");
client.println("User-Agent: arduino-ethernet");
client.print("Content-length: ");
client.println(big_string.length());
/ tell the server you are done with the header.
client.println();
/ send the posted data.
client.print(big_string);
Serial.println("----------------------------------");
Serial.println(big_string);
Serial.println(big_string.length());
Serial.println("----------------------------------");
big_string = "";
/ finish up
client.println("Connection: close");
client.println();
/ note the time that the connection was made:
lastConnectionTime = millis();
} else {
/ if you couldn't make a connection:
Serial.println("connection failed");
}
}