mirror of https://github.com/IoTcat/sola-mqtt.git
master
parent
6e1328d2e5
commit
dce786a404
4 changed files with 644 additions and 644 deletions
@ -1 +1 @@ |
|||||||
node_modules/ |
node_modules/ |
||||||
|
@ -1,37 +1,37 @@ |
|||||||
const mqtt = require('mqtt').connect('mqtt://192.168.3.4'); |
const mqtt = require('mqtt').connect('mqtt://192.168.3.4'); |
||||||
const mysql = require('mysql').createConnection({ |
const mysql = require('mysql').createConnection({ |
||||||
host : 'db.yimian.xyz', |
host : 'db.yimian.xyz', |
||||||
user : 'home', |
user : 'home', |
||||||
password : 'KJ4j2JL6s^k@jsa2', |
password : 'KJ4j2JL6s^k@jsa2', |
||||||
database : 'home' |
database : 'home' |
||||||
}); |
}); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mysql.connect(); |
mysql.connect(); |
||||||
const sql = 'INSERT INTO sola_mqtt(timestamp,topic,value) VALUES(?,?,?)'; |
const sql = 'INSERT INTO sola_mqtt(timestamp,topic,value) VALUES(?,?,?)'; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mqtt.on('connect', function () { |
mqtt.on('connect', function () { |
||||||
mqtt.subscribe('hass/#', function (err) { |
mqtt.subscribe('hass/#', function (err) { |
||||||
if (!err) { |
if (!err) { |
||||||
console.log('Connected...'); |
console.log('Connected...'); |
||||||
} |
} |
||||||
}) |
}) |
||||||
}) |
}) |
||||||
|
|
||||||
mqtt.on('message', function (topic, message) { |
mqtt.on('message', function (topic, message) { |
||||||
// message is Buffer
|
// message is Buffer
|
||||||
console.log(topic.toString(), message.toString()); |
console.log(topic.toString(), message.toString()); |
||||||
let params = [(new Date()).valueOf(), topic.toString(), message.toString()]; |
let params = [(new Date()).valueOf(), topic.toString(), message.toString()]; |
||||||
|
|
||||||
mysql.query(sql,params,function (err, result) { |
mysql.query(sql,params,function (err, result) { |
||||||
if(err){ |
if(err){ |
||||||
console.log('[INSERT ERROR] - ',err.message); |
console.log('[INSERT ERROR] - ',err.message); |
||||||
return; |
return; |
||||||
}
|
}
|
||||||
}); |
}); |
||||||
|
|
||||||
|
|
||||||
}) |
}) |
@ -1,11 +1,11 @@ |
|||||||
{ |
{ |
||||||
"name": "sola_mqtt", |
"name": "sola_mqtt", |
||||||
"version": "1.0.0", |
"version": "1.0.0", |
||||||
"main": "index.js", |
"main": "index.js", |
||||||
"author": "iotcat <i@iotcat.me>", |
"author": "iotcat <i@iotcat.me>", |
||||||
"license": "MIT", |
"license": "MIT", |
||||||
"dependencies": { |
"dependencies": { |
||||||
"mqtt": "^3.0.0", |
"mqtt": "^3.0.0", |
||||||
"mysql": "^2.18.1" |
"mysql": "^2.18.1" |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue