-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrab-cli.php
More file actions
164 lines (133 loc) · 5.28 KB
/
crab-cli.php
File metadata and controls
164 lines (133 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
CRAB CLI script by Ole-Henrik Jakobsen
This script will limit number of inputs to 2 sensors, which is the maximum for the website it will be posted to.
One sensor should be placed in the water, and the other (which is optional) should be placed on land in the shadow.
It's a good idea to mark them properly with eg. colored tie wrap.
Accepted measurment units is limited to C (Celcius) and F (Farenheit).
You will need a key to be able to upload your data, please register an account at https://crab.today
Requirements: php-cli, php-curl
Last updated: 2020-04-15
*/
// get variables from the configuration file and parse it
// put array data into variables
$host = "send.crab.today"; // Set the hostname where to send the data to
$port = 443; // Set the port for the hostname
$connwait = 10; // Wait [num] minutes until connection failure
// print help text if nothing or "help" is issued.
if($argc === 1 || preg_match("/^(.*)help(.*)$/i", $argv[1])) {
die("_______________________________________________________________________________
CRAB help
Usage:
php crab.php [options]
Options:
API key: (required)
key Set the API key for CRAB
Temperature sensor(s): (required)
temp Send the temperature measured in format:
[-]XX.XXX[,[-]XX.XXX] (second sensor is optional).
serial Set a unique serial name per temp sensor. If none is available,
make up one like 'water' and 'air' etc. Allowed characters:
upto 32 characters of: a-z A-Z 0-9 - _ .
The serial name should not be changed after first executed report,
as you will need to reconfigure the sensors in CRAB and
earlier reported data will be unaccessible (but not deleted).
Create a new API key with new sensors instead.
unit Specify if the temperature is in
[C]elcius or [F]arenheit.
GPS Location sensor: (required)
loc Send the GPS latitude and longitude in decimal format:
[lat,long]: [-]XX.XXXXX,[-]XX.XXXXX
Custom data: (optional)
custom Send custom data from FILE to store on CRAB logs page.
No formatting available, only pure text with line breaks.
Max 256 characters long.
Image capture: (optional)
image Send an image to the server, specfy the file with full path.
Supports only JPEG and PNG images, max. 10MB (1200x800).
Examples:
One temperature sensor and custom:
php crab.php \
key=FOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFO \
temp=22.545 \
serial=53n50r-1337 \
unit=C \
loc=-10.00000,50.00000 \
custom=/home/crab/customdata.txt
Two temperature sensors and an image:
php crab.php \
key=FOOFOOFOOFOOFOOFOOFOOFOOFOOFOOFO \
temp=22.545,31.337 \
serial=53n50r-1337,s3n50r-31337 \
unit=C \
loc=-10.00000,50.00000
image=/home/crab/image.png
Mark that first sensor will match the first serial number,
and second will match the second.
Use commas [,] for separation.
_______________________________________________________________________________
https://crab.today/ support@crab.today
");
}
else {
// create an array with our data
for($i=1;$i<$argc;$i++) { //skip first argv as it just contains this filename $i=1
list($arr_key, $arr_val) = explode('=', $argv[$i]);
$arg_array[$arr_key] = $arr_val;
}
// defining and adjusting variables
$key = (isset($arg_array["key"]) ? $arg_array["key"] : null);
$temp = (str_replace(",", ";", isset($arg_array["temp"])) ? $arg_array["temp"] : null);
$serial = (str_replace(",", ";", isset($arg_array["serial"])) ? $arg_array["serial"] : null);
$unit = (isset($arg_array["unit"]) ? $arg_array["unit"] : null);
$loc = (isset($arg_array["loc"]) ? $arg_array["loc"] : null);
$custom = (isset($arg_array["custom"]) ? $arg_array["custom"] : null);
$imgfile = (isset($arg_array["image"]) ? $arg_array["image"] : null);
$lat = "";
$long = "";
// split location data
if($loc) {
list($lat, $long) = explode(",", $loc);
}
// check if we can connect before we continue
$timeout = $connwait*60; // $min*60s
$fp = fsockopen('ssl://'.$host,$port,$errno,$errstr,$timeout);
if(!$fp) {
die("Could not open a connection to $host\n");
}
fclose($fp);
// setup POST URI
$url = "https://" . $host . "";
// setup POST array
$post = array('key' => $key,
'lat' => $lat,
'long' => $long,
'temp' => $temp,
'serial' => $serial,
'unit' => $unit
);
if($custom) {
$post['custom'] = file_get_contents($custom, false, NULL, 0, 384);
}
// if an image is supplied, add this into the array as well
if($imgfile) {
// very basic checking for mimetype after extension, the real deal happens serverside anyway.
if(stristr($imgfile, 'png')) {
$mimetype = "image/png";
}
else { $mimetype = "image/jpeg"; }
// create the file for POST
$post['image'] = curl_file_create($imgfile, $mimetype, $key);
}
// create a new cURL resource
$ch = curl_init();
// set URL and POST options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
}
?>