Creating a Rockseven HTTP POST endpoint to receive Rockblock modem data
Rockseven has a nifty feature where data sent via the Rockblock modem can be sent to various endpoints. Besides having the data emailed, it is useful to have that data parsed by a website to display the data in a user friendly way (like on a tracking map!). Below is my implementation for LoCARB. Its pretty self explanatory and I’ve left the debugging portions of it for anyone to use. Just be sure to change the domain name to where your MySQL database resides and enter in your own username and password.
I also used this php endpoint to format any incoming data that needed it. In the previous post about packing a message to be sent via the satellite modem, I initially formatted the GPS coordinates to be sent with a decimal. In the end I decided to just leave all formatting functions to the endpoint so as to simplify the Arduino or Javascript code (for maps). In the code below, you’ll see expressions for adding decimal places, converting m/s to knots, km to miles, and miles to nautical miles.
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
<?php //UNCOMMENT BELOW FOR TESTING /* $imei = '123123'; $seqnum = '1'; $transmit_time = '2012-10-10 12:41:50'; $iridium_latitude = '36.2997759'; $iridium_longitude = '-122.2088373'; $iridium_cep = '14.4'; $data = '342c322c3135372c313130302c322c39392c312c322c322c312c34392c37322c36322c36372c36372c36372c302c3337313239373735392c2d31323231353838333733'; */ //UNCOMMENT BELOW FOR PRODUCTION USE // /* $imei = ''; $seqnum = ''; $transmit_time = ''; $iridium_latitude = ''; $iridium_longitude = ''; $iridium_cep = ''; $data = ''; // */ //Create Static Variables for Pixhawk GPS coords of LoCARB //Session ID is received IMEI packed with message $userName = 'LoCARB'; //Selection which will show in Tracking Map Routes $phoneNumber = '1'; $eventType = 'LoCARB'; $extraInfo = 'na'; //Create Static Variables for Iridium coords of LoCARB $IsessionID = 'LoCARB-Iridium'; $IuserName = 'LoCARB (using Iridium approximate GPS coords)'; //Selection which will show in Tracking Map Routes $IphoneNumber = '2'; $IeventType = 'LoCARB-Iridium'; //extraInfo is set to $extraInfo in message packing // Store received data in Variables if(isset($_POST["imei"])){$imei = $_POST["imei"];} if(isset($_POST["momsn"])){$seqnum = $_POST["momsn"];} if(isset($_POST["transmit_time"])){$transmit_time = $_POST["transmit_time"];} if(isset($_POST["iridium_latitude"])){$iridium_latitude = $_POST["iridium_latitude"];} if(isset($_POST["iridium_longitude"])){$iridium_longitude = $_POST["iridium_longitude"];} if(isset($_POST["iridium_cep"])){$iridium_cep = $_POST["iridium_cep"];} if(isset($_POST["data"])){$data = $_POST["data"];} // Exit if data is empty if ($data == 0) { exit('-1'); } //HANDLE DATA BELOW //Convert Received UTC time and date to Local Time (rockseven sends as UTC) $time = strtotime($transmit_time.' UTC'); $transmit_time = date("Y-m-d H:i:s", $time); //subtract time by 1 second so iridium-locarb route listing on gps map is not the default selection $Itransmit_time = date("Y-m-d H:i:s", strtotime('-1 seconds',strtotime($transmit_time))); //Decode RECEIVED DATA hex to string $decoded = pack('H*', $data); //Process decoded data from comma separated values into variables below $datastore = explode(',', $decoded); //Assign decoded data to variables $gps_fix = $datastore[0]; $gps_vel = $datastore[1] * 1.944; //Convert ground speed to knots $gps_head = $datastore[2]; $sys_bat = $datastore[3] / 100; //Add decimal point to voltage $sys_bcur = $datastore[4]; $sys_brem = $datastore[5]; $leak_value = $datastore[6]; $capsized_times = $datastore[7]; $px4_resetcounter = $datastore[8]; $motor_counter = $datastore[9]; $dht1_hum = $datastore[10]; $dht1_temp = $datastore[11]; $dht2_hum = $datastore[12]; $dht2_temp = $datastore[13]; $motor_temperature = $datastore[14]; $rudder_temperature = $datastore[15]; $rpm = $datastore[16]; $latitude = $datastore[17] / 10000000; //Received value has no decimal. Add decimal point $longitude = $datastore[18] / 10000000; //Received value has no decimal. Add decimal point //Convert iridium-cep to miles. Maintain precision, limit digits after decimal. $accuracy_precise = floor(($iridium_cep*100))/100; $accuracy = number_format(($accuracy_precise * .62137), 2); //SQL STUFF try { $conn = new PDO("mysql:host=MYSQL.ENTERYOURHOSTNAME.COM;dbname=DBNAME", USERNAME, PASSWORD); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //DISTANCE FROM LAUNCH TO CURRENT POSITION //Get LoCARB SQL values for calculating distance. $sqldist = $pdo->query("SELECT GPSLocationID, latitude, longitude FROM gpslocations WHERE latitude > 0 AND eventType = 'LoCARB' ORDER BY GPSLocationID ASC Limit 1"); $row = $sqldist->fetch(PDO::FETCH_ASSOC); $lat1 = $row['latitude']; $lon1 = $row['longitude']; $distance = distance($lat1, $lon1, $latitude, $longitude, "N"); ////Get Iridium-LoCARB SQL values for calculating distance. $sqlIdist = $pdo->query("SELECT GPSLocationID, latitude, longitude FROM gpslocations WHERE latitude > 0 AND eventType = 'LoCARB-Iridium' ORDER BY GPSLocationID ASC Limit 1"); $row = $sqlIdist->fetch(PDO::FETCH_ASSOC); $lat1 = $row['latitude']; $lon1 = $row['longitude']; $Idistance = distance($lat1, $lon1, $iridium_latitude, $iridium_longitude, "N"); //DISTANCE FROM LAST WAYPOINT - waypoint to waypoint distance //Get LoCARB SQL values for calculating distance. $sqltdist = $pdo->query("SELECT GPSLocationID, latitude, longitude FROM gpslocations WHERE latitude > 0 AND eventType = 'LoCARB' ORDER BY GPSLocationID DESC Limit 1"); $row = $sqltdist->fetch(PDO::FETCH_ASSOC); $lat1 = $row['latitude']; $lon1 = $row['longitude']; $tdistance = distance($lat1, $lon1, $latitude, $longitude, "N"); ////Get Iridium-LoCARB SQL values for calculating waypoint-waypoint distance. $sqlItdist = $pdo->query("SELECT GPSLocationID, latitude, longitude FROM gpslocations WHERE latitude > 0 AND eventType = 'LoCARB-Iridium' ORDER BY GPSLocationID DESC Limit 1"); $row = $sqlItdist->fetch(PDO::FETCH_ASSOC); $lat1 = $row['latitude']; $lon1 = $row['longitude']; $Itdistance = distance($lat1, $lon1, $iridium_latitude, $iridium_longitude, "N"); //CALCULATE TOTAL DISTANCE TRAVELED - Add up all waypoints in mysql tdistance column //Calculate total distance for locarb $sqlsumdist = $pdo->query("SELECT SUM(tdistance)AS totDistance FROM gpslocations WHERE eventType = 'LoCARB'"); $row = $sqlsumdist->fetch(PDO::FETCH_ASSOC); $totaldistance = $row['totDistance'] + $tdistance; //Calculate total distance for iridium-locarb $sqlIsumdist = $pdo->query("SELECT SUM(tdistance) AS totIDistance FROM gpslocations WHERE eventType = 'LoCARB-Iridium'"); $row = $sqlIsumdist->fetch(PDO::FETCH_ASSOC); $totalIdistance = $row['totIDistance'] + $Itdistance; //PACK LOCARB VALUES $sql = "INSERT INTO gpslocations ( sessionID, userName, gpsfix, speed, direction, heading, bvoltage, bcurrent, bremaining, leaksensor, capsized, px4reset, motorcount, dht1h, dht1t, dht2h, dht2t, motortemp, ruddertemp, rpm, latitude, longitude, gpsTime, phoneNumber, eventType, extraInfo, distance, tdistance, totaldistance) VALUES ('$imei', '$userName', '$gps_fix', '$gps_vel', '$gps_head', '$gps_head', '$sys_bat', '$sys_bcur', '$sys_brem', '$leak_value', '$capsized_times', '$px4_resetcounter', '$motor_counter', '$dht1_hum', '$dht1_temp', '$dht2_hum', '$dht2_temp', '$motor_temperature', '$rudder_temperature', '$rpm', '$latitude', '$longitude', '$transmit_time', '$phoneNumber', '$eventType', '$extraInfo', '$distance', '$tdistance', '$totaldistance')"; //PACK Iridium-LoCARB Values $sql1 = "INSERT INTO gpslocations ( sessionID, userName, gpsfix, speed, direction, heading, bvoltage, bcurrent, bremaining, leaksensor, capsized, px4reset, motorcount, dht1h, dht1t, dht2h, dht2t, motortemp, ruddertemp, rpm, latitude, longitude, gpsTime, phoneNumber, eventType, extraInfo, accuracy, distance, tdistance, totaldistance) VALUES ('$IsessionID', '$IuserName', '$gps_fix', '$gps_vel', '$gps_head', '$gps_head', '$sys_bat', '$sys_bcur', '$sys_brem', '$leak_value', '$capsized_times', '$px4_resetcounter', '$motor_counter', '$dht1_hum', '$dht1_temp', '$dht2_hum', '$dht2_temp', '$motor_temperature', '$rudder_temperature', '$rpm', '$iridium_latitude', '$iridium_longitude', '$Itransmit_time', '$IphoneNumber', '$IeventType', '$extraInfo', '$accuracy', '$Idistance', '$Itdistance', '$totalIdistance')"; //use exec() because no results are returned //Execute Distance from launch $conn->exec($sqldist); $conn->exec($sqlIdist); //Execute Way-point to way-point distance $conn->exec($sqltdist); $conn->exec($sqlItdist); //Execute total distance $conn->exec($sqlsumdist); $conn->exec($sqlIsumdist); //Execute received data from sat message and write to database $conn->exec($sql); $conn->exec($sql1); echo "New records created successfully"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; //Determine distance between two sets of GPS coordinates - Distance in Nautical Miles /* --------------------------------------------- Distance functions taken from https://www.geodatasource.com GeoDataSource.com (C) All Rights Reserved 2018 --------------------------------------------- */ function distance($lat1, $lon1, $latitude, $longitude, $unit) { if (($lat1 == $latitude) && ($lon1 == $longitude)) { return 0; } elseif ($lat1 == 0){ return 0; } else { $theta = $lon1 - $longitude; $dist = sin(deg2rad($lat1)) * sin(deg2rad($latitude)) + cos(deg2rad($lat1)) * cos(deg2rad($latitude)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $unit = strtoupper($unit); if ($unit == "K") { return ($miles * 1.609344); } else if ($unit == "N") { return ($miles * 0.8684); } else { return $miles; } } } ?> <html> <head> <title>LOCARB</title> </head> <body> <p> <p> <!--FOR TESTING - DISPLAY DIAGNOSTICS OUTPUT OF ABOVE--> IMEI: <?php echo "$imei";?><br> Received data: <?php echo "$data";?><br> Decoded data: <?php echo "$decoded"; ?><p> Transmit time converted to Local time#: <?php echo "$transmit_time";?><p> I-Transmit time converted to local -1 second#: <?php echo "$Itransmit_time";?><p> <p> Username: <?php echo "$userName";?><br> <p> GPS FIX: <?php echo "$gps_fix";?><br> GROUND SPEED: <?php echo "$gps_vel";?><br> DISTANCE: <?php echo "$distance";?><br> IRIDIUM DISTANCE: <?php echo "$Idistance";?><br> DISTANCE FROM LAST WAYPOINT: <?php echo "$tdistance";?><br> IRIDIUM DISTANCE FROM LAST WAYPOINT: <?php echo "$Itdistance";?><br> TOTAL DISTANCE: <?php echo "$totaldistance";?><br> TOTAL IRIDIUM DISTANCE: <?php echo "$totalIdistance";?><br> GPS HEADING: <?php echo "$gps_head";?><br> BATTERY VOLTAGE: <?php echo "$sys_bat";?><br> BATTERY CURRENT: <?php echo "$sys_bcur";?><br> BATTERY REMAINING: <?php echo "$sys_brem";?><br> LEAK SENSOR: <?php echo "$leak_value";?><br> CAPSIZED TIMES: <?php echo "$capsized_times";?><br> PIXHAWK RESET TIMES: <?php echo "$px4_resetcounter";?><br> MOTOR INITIALIZED TIMES: <?php echo "$motor_counter";?><br> DHT 1 HUMIDITY: <?php echo "$dht1_hum";?><br> DHT 1 TEMP: <?php echo "$dht1_temp";?><br> DHT 2 HUMIDITY: <?php echo "$dht2_hum";?><br> DHT 2 TEMP: <?php echo "$dht2_temp";?><br> MOTOR TEMP: <?php echo "$motor_temperature";?><br> RUDDER TEMP: <?php echo "$rudder_temperature";?><br> RPM: <?php echo "$rpm";?><br> LATITUDE: <?php echo "$latitude";?><br> LONGITUDE: <?php echo "$longitude";?><br> ACCURACY: <?php echo "$accuracy";?><br> </body> </html> |
The output result looks like so:
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 |
New records created successfully IMEI: 123123 Received data: 342c322c3135372c313130302c322c39392c312c322c322c312c34392c37322c36322c36372c36372c36372c302c3337313239373735392c2d31323231353838333733 Decoded data: 4,2,157,1100,2,99,1,2,2,1,49,72,62,67,67,67,0,371297759,-1221588373 Transmit time converted to Local time#: 2012-10-10 05:41:50 I-Transmit time converted to local -1 second#: 2012-10-10 05:41:49 Username: LoCARB GPS FIX: 4 GROUND SPEED: 3.888 DISTANCE: 35.676357570541 IRIDIUM DISTANCE: 83.347212156417 DISTANCE FROM LAST WAYPOINT: 249.82147935544 IRIDIUM DISTANCE FROM LAST WAYPOINT: 298.68198532013 TOTAL DISTANCE: 477.42147935544 TOTAL IRIDIUM DISTANCE: 528.88198532013 GPS HEADING: 157 BATTERY VOLTAGE: 11 BATTERY CURRENT: 2 BATTERY REMAINING: 99 LEAK SENSOR: 1 CAPSIZED TIMES: 2 PIXHAWK RESET TIMES: 2 MOTOR INITIALIZED TIMES: 1 DHT 1 HUMIDITY: 49 DHT 1 TEMP: 72 DHT 2 HUMIDITY: 62 DHT 2 TEMP: 67 MOTOR TEMP: 67 RUDDER TEMP: 67 RPM: 0 LATITUDE: 37.1297759 LONGITUDE: -122.1588373 ACCURACY: 8.95 |
An important lesson learned…
For directions on how to add an HTTP POST endpoint in your Rock7.com dashboard, download the RockSeven user guide and go to the very last page. After adding your endpoint, you’ll be able to test the endpoint by navigating to the ‘Test Delivery Groups’ selection on the bottom left menu (image below). Select your device group and click the ‘Send Test’ button to check and see if your endpoint is working as it should. If it is, there should be data in your database. Be aware that my code does not directly populate every field that is sent by the Rockseven POST submission and as such, you will just see your IMEI populate in the database.
One important thing to note is when testing your endpoint, do not repeatedly click the Send Test button as it will disable it after a few clicks. It did to me and I wasn’t able to add any URLs for a while.