Last active
July 10, 2016 06:45
-
-
Save gucchan22/49da3627f52eb05dcd93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("date-utils"); | |
var exec = require("child_process").exec; | |
var process = require("process"); | |
setInterval(function() { | |
exec("fswebcam /tmp/captured_"+cap_timestamp+".jpg", function(err, stdout) { | |
//var data = fs.readFileSync("/tmp/captured_"+cap_timestamp"+.jpg").toString("base64"); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("date-utils"); | |
var mqtt = require("mqtt"), | |
os = require("os"), | |
fs = require("fs"), | |
serial = require("serialport"), | |
process = require("process"), | |
exec = require("child_process").exec; | |
// Is it debugging? | |
var debug = true; | |
// Network Interface | |
var netif = os.networkInterfaces().eth0[0]; | |
// Serial-port for GPS sensor(GT-730F/L(Skytraq)). | |
var gps_sensor = new serial.SerialPort("/dev/ttyUSB0", { | |
baudRate : 38400, | |
dataBits : 8, | |
stopBits : 1, | |
flowControl : false, | |
parser : serial.parsers.readline("\n") | |
}); | |
var recv_pos = {}; | |
gps_sensor.on("data", function(recv) { | |
var gprmc = null; | |
switch(recv.slice(0,6)) { | |
case "$GPRMC" : | |
gprmc = recv.split(","); | |
recv_pos.lat = gprmc[3]; // N | |
recv_pos.lon = gprmc[5]; // E | |
console.log("[Got GPS]: lat:"+recv_pos.lat+", lon:"+recv_pos.lon); | |
recv_cnt = 0; | |
break; | |
} | |
}); | |
require("getmac").getMac(function(err, hw_addr) { | |
var broker = mqtt.createClient(1883,"quickstart.messaging.internetofthings.ibmcloud.com", { | |
"clientId" : "d:quickstart:MyDevice:" + hw_addr.split(":").join("") | |
}); | |
setInterval(function() { | |
var cap_timestamp = (new Date()).toFormat("YYYYMMDDHH24MISS"); | |
exec("fswebcam /tmp/captured_"+cap_timestamp+".jpg", function(err, stdout) { | |
var data = fs.readFileSync("/tmp/captured_"+cap_timestamp"+.jpg").toString("base64"); | |
var lat = recv_pos.lat, lon = recv_pos.lon; | |
broker.publish("iot-2/evt/myeventtype/fmt/json", JSON.stringify({ | |
d : { | |
v_ip : netif.address, | |
v_streaming_port : 8081, | |
v_address_family : netif.family, | |
v_loc_lat : recv_pos.lat, | |
v_loc_lon : recv_pos.lon, | |
v_signaled : cap_timestamp, | |
data_img : data | |
} | |
})); | |
if(debug) { | |
console.log("[Published]: "+netif.address+":8081, (GPS: lat:"+lat+", lon:"+lon+")"); | |
if(err != null) console.log("[Error]: couldn't upload a picture that you've captured."); | |
} | |
}); | |
}, 1000 * 2); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("date-utils"); | |
var mqtt = require("mqtt"), | |
os = require("os"), | |
fs = require("fs"), | |
serial = require("serialport"), | |
process = require("process"), | |
exec = require("child_process").exec; | |
// Is it debugging? | |
var debug = true; | |
// Network Interface | |
var netif = os.getNetworkInterfaces().eth0[0]; | |
var streaming_port = 8081; | |
// Serial-port for GPS sensor(GT-730F/L). | |
var gps_sensor = new serial.SerialPort("/dev/ttyUSB0", { | |
baudRate : 38400, | |
dataBits : 8, | |
stopBits : 1, | |
flowControl : false, | |
parser : serial.parsers.readline("\n") | |
}); | |
var recv_pos = null; | |
// MQTT Community-name for the IBM IoT Foundation | |
var communities = { | |
event : "iot-2/evt/myeventtype/fmt/json" | |
}; | |
gps_sensor.on("data", function(recv) { | |
recv_pos = recv; | |
}); | |
require("getmac").getMac(function(err, hw_addr) { | |
var broker = mqtt.createClient(1883, "quickstart.messaging.internetofthings.ibmcloud.com", { | |
"clientId" : "d:quickstart:MyDevice:" + hw_addr.split(":").join("") | |
}); | |
setInterval(function() { | |
exec("fswebcam /tmp/captured"+(new Date()).toFormat("YYYYMMDDHH24MISS"), function(err, stdout) { | |
broker.publish(communities.event, JSON.stringify({ | |
d : { | |
vehicle_ip : netif.address, | |
vehicle_streaming_port : streaming_port, | |
vehicle_address_family : netif.family, | |
vehicle_loc_lat :, | |
vehicle_loc_lon :, | |
vehicle_timestamp : (new Date()).toFormat("YYYYMMDDHH24MISS") | |
} | |
})); | |
if(debug) { | |
console.log("[Published] "+netif.address+":"+streaming_port+" in lat:"+vehicle_loc_lat+", lon:"+vehicle_loc_lon); | |
err != null ? console.log("[Error] : couldn't upload image that you've captured") : console.log("[Success]: upload completed."); | |
} | |
}); | |
}, 1000 * 2); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment