— HaeramKim, BuildYourOwnIoTPlatform — 1 min read
NOTE: this notes are from “Build Your Own IoT Platform” by Anand Tamboli. And I can’t speak english very well, so some sentences or word might be inappropriate and might have some misunderstandings.
DatabasesCreatePrivilegesAdd user accountlogin information (Generate password is optionary)Global privilegesGo btn.Create tableNumber of data as 51NAME TYPE LEN DEFAULT INDEX A_I2ID INT 11 NONE PRIMARY_KEY ENABLE3TOPIC VARCHAR 1024 NONE4PAYLOAD VARCHAR 2028 NONE5TIMESTAMP VARCHAR 15 NONE6DELETED BINARY 1 AS DEFINED:0saveManage Palette in NodeRED menuInstall Menu -> Install node-red-node-MySQL
inject node: Make sure that msg.payload be “timestamp” and set repeatto interval with every 15 seconds.debug node: There’s no required configuration.MQTT out node: Server: Connection:localhost with port 1883MQTT V3.1.1node-redSecurity: Enter your MQTT username and password.Topic: timestamp
HTTP in node: Set method to POST and URL to /pub/:topic/:payloadfunction node:msq.req stores all the parsed data from HTTP requestmsg.req.paramsfunction node is msg. Thus, you have to store return value to msg.1msg.topic = msg.req.params.topic.toString();2msg.payload = msg.req.params.payload.toString();3msg.qos = 2;4msg.retain = false;5
6return msg;MQTT out node: Basic server configurations are already setted. So, just fill out the topic to “mqtt”.function node: 1msg.payload = {2 success: true,3 message:"published: " +4 msg.req.params.topic + 5 "/" + 6 msg.req.params.payload7};8
9return msg;HTTP response node: No configurations are neccesary.
MQTT in node: Set topic to # to subscribe all kind of topic and set QoS to 2 to filtering important data.function node: This node makes SQL query for DB.1// Create query2// get microtime3var timestamp = new Date().getTime()/1000;4// pad it with trailing zeroes5timestamp = timestamp.toString() + "000";6// trim to exact length 10 + 1 + 37timestamp = timestamp.substring(0, 14);8
9var strQuery = "INSERT INTO thingsData (topic, payload, timestamp, deleted)" +10 "VALUES ('" + escape(msg.topic) + "','" + escape(msg.payload) + "','" + timestamp + "', 0);";11msg.topic = strQuery;12
13return msg;mysql node: Set database property to:127.0.0.13306debug node: No configurations required.
HTTP in node: Set method to GET and set URL to /record/:topicHTTP in node: Set method to GET and set URL to /records/:topic/last/:countfunction node: 1// Create query2
3// if required record count is not specified4// set default to 15if(!msg.req.params.count) {6 msg.req.params.count = 1;7}8
9// build the sql query10msg.topic =11 "SELECT id,topic,payload,timestamp " +12 "FROM thingsData " +13 "WHERE topic='" + escape(msg.req.params.topic) + "' " +14 "AND deleted=0 " +15 "ORDER BY id DESC " +16 "LIMIT " + msg.req.params.count + ";";17
18return msg;mysql, function, http response node: No configuration required.forever package. It’s available in NPM.1npm install -g forever1forever start -l node-red.log --append /usr/local/bin/node-red