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 mysql = require('mysql').createConnection({ |
||||
host : 'db.yimian.xyz', |
||||
user : 'home', |
||||
password : 'KJ4j2JL6s^k@jsa2', |
||||
database : 'home' |
||||
}); |
||||
|
||||
|
||||
|
||||
mysql.connect(); |
||||
const sql = 'INSERT INTO sola_mqtt(timestamp,topic,value) VALUES(?,?,?)'; |
||||
|
||||
|
||||
|
||||
mqtt.on('connect', function () { |
||||
mqtt.subscribe('hass/#', function (err) { |
||||
if (!err) { |
||||
console.log('Connected...'); |
||||
} |
||||
}) |
||||
}) |
||||
|
||||
mqtt.on('message', function (topic, message) { |
||||
// message is Buffer
|
||||
console.log(topic.toString(), message.toString()); |
||||
let params = [(new Date()).valueOf(), topic.toString(), message.toString()]; |
||||
|
||||
mysql.query(sql,params,function (err, result) { |
||||
if(err){ |
||||
console.log('[INSERT ERROR] - ',err.message); |
||||
return; |
||||
}
|
||||
}); |
||||
|
||||
|
||||
const mqtt = require('mqtt').connect('mqtt://192.168.3.4'); |
||||
const mysql = require('mysql').createConnection({ |
||||
host : 'db.yimian.xyz', |
||||
user : 'home', |
||||
password : 'KJ4j2JL6s^k@jsa2', |
||||
database : 'home' |
||||
}); |
||||
|
||||
|
||||
|
||||
mysql.connect(); |
||||
const sql = 'INSERT INTO sola_mqtt(timestamp,topic,value) VALUES(?,?,?)'; |
||||
|
||||
|
||||
|
||||
mqtt.on('connect', function () { |
||||
mqtt.subscribe('hass/#', function (err) { |
||||
if (!err) { |
||||
console.log('Connected...'); |
||||
} |
||||
}) |
||||
}) |
||||
|
||||
mqtt.on('message', function (topic, message) { |
||||
// message is Buffer
|
||||
console.log(topic.toString(), message.toString()); |
||||
let params = [(new Date()).valueOf(), topic.toString(), message.toString()]; |
||||
|
||||
mysql.query(sql,params,function (err, result) { |
||||
if(err){ |
||||
console.log('[INSERT ERROR] - ',err.message); |
||||
return; |
||||
}
|
||||
}); |
||||
|
||||
|
||||
}) |
@ -1,11 +1,11 @@ |
||||
{ |
||||
"name": "sola_mqtt", |
||||
"version": "1.0.0", |
||||
"main": "index.js", |
||||
"author": "iotcat <i@iotcat.me>", |
||||
"license": "MIT", |
||||
"dependencies": { |
||||
"mqtt": "^3.0.0", |
||||
"mysql": "^2.18.1" |
||||
} |
||||
} |
||||
{ |
||||
"name": "sola_mqtt", |
||||
"version": "1.0.0", |
||||
"main": "index.js", |
||||
"author": "iotcat <i@iotcat.me>", |
||||
"license": "MIT", |
||||
"dependencies": { |
||||
"mqtt": "^3.0.0", |
||||
"mysql": "^2.18.1" |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue