HJ212噪音上报
概述
本项目接了一个箱体噪音传感器,采集噪音分贝上传到HJ212服务器,包括实时数据,分钟数据,小时数据和日数据。
代码
var server1 = "iot.easelive.cn";
var port1 = 3344;
var curDev = 0;
var readCount = 2;
var pw = "123456";
var mn = Box.sn();
var report_period_heartbeat = 30 * 1000;
var report_period_uart = 30 * 1000;
var modbus_fail = 0;
var result = {
noise :0.0,
};
function nextMinute()
{
var t = Box.getTime();
Timer.start(1, (60 - t.second) * 1000);
}
function timeString()
{
var t = Box.getTime();
return t.format("%Y%0M%0D%0h%0m%0s");
}
function reportMinute()
{
var cp="DataTime=" + timeString() + ";B03-Avg=" + result.noise.toFixed(1);
var data_HJ212 ="QN=" + timeString() + "000;"+"ST=36;CN=2051;PW=" + pw + ";MN=" + mn + ";Flag=5;CP=&&" + cp + "&&";
print("report is",data_HJ212);
Net.send(0, Packet.HJ212.packet(data_HJ212));
}
function reportHour()
{
var cp="DataTime=" + timeString() + ";B03-Avg=" + result.noise.toFixed(1);
var data_HJ212 ="QN=" + timeString() + "000;"+"ST=36;CN=2061;PW=" + pw + ";MN=" + mn + ";Flag=5;CP=&&" + cp + "&&";
print("report is",data_HJ212);
Net.send(0, Packet.HJ212.packet(data_HJ212));
}
function reportDay()
{
var cp="DataTime=" + timeString() + ";B03-Avg=" + result.noise.toFixed(1);
var data_HJ212 ="QN=" + timeString() + "000;"+"ST=36;CN=2031;PW=" + pw + ";MN=" + mn + ";Flag=5;CP=&&" + cp + "&&";
print("report is",data_HJ212);
Net.send(0, Packet.HJ212.packet(data_HJ212));
}
function heartbeat()
{
var cp="DataTime=" + timeString() + ";B03-Rtd=" + result.noise.toFixed(1);
var data_HJ212 ="QN=" + timeString() + "000;"+"ST=36;CN=2011;PW=" + pw + ";MN=" + mn + ";Flag=5;CP=&&" + cp + "&&";
print("heartbeat is",data_HJ212);
Net.send(0, Packet.HJ212.packet(data_HJ212));
}
function hj212CB(data)
{
print("receive", data);
}
var parser = Packet.HJ212.Parser.create(hj212CB);
function netCB(m)
{
switch(m.event)
{
case Net.READY:
print("ready");
Net.syncTime();
Net.tcp(0, server1, port1);
break;
case Net.CONN_OK:
print("conn ok");
heartbeat();
Timer.start(3, report_period_heartbeat);
nextMinute();
break;
case Net.CONN_FAIL:
break;
case Net.CONN_CLOSE:
break;
case Net.RECV:
//print(m.data);
if(m.id ==0 )
{
parser.parse(m.data);
}
break;
}
}
function modbusCB(data)
{
Timer.stop(4);
print(data.addr, data.cmd, data.data.hex());
result.noise = data.data.geti(1, "2b") / 10;
modbus_fail = 0;
}
function timerCB(id)
{
switch(id)
{
case 1:
var t = Box.getTime();
if(t.minute % 5 == 0)
{
reportMinute();
if(t.minute == 0)
{
reportHour();
if(t.hour == 0)
{
reportDay();
Net.syncTime();
}
}
}
nextMinute();
break;
case 2:
UART.send(2, Packet.Modbus.packet(1, 3, 1, 1));
Timer.start(4, 2000);
Timer.start(2, report_period_uart);
break;
case 3:
if(Net.isConnected(0))
{
heartbeat();
}
Timer.start(3, report_period_heartbeat);
break;
case 4:
modbus_fail++;
if(modbus_fail > 4)
result.noise = 0.0;
break;
}
}
function uartCB(port, data)
{
if(port == 1)
{
var cmd = data.gets();
if(cmd == "read")
{
UART.send(2, Packet.Modbus.packet(1, 3, 1, 1));
}
else
{
print("unknown cmd");
}
}
if(port == 2)
{
pm.parse(data);
//print(data);
}
}
UART.open(2, 9600);
var pm = Packet.Modbus.Parser.create(modbusCB);
GPIO.set(GPIO.POWER, 1);
Net.init();
Timer.start(2, 5000);
while(true)
{
var m = Event.get();
switch(m.msg)
{
case Event.NET_EVENT:
netCB(m);
break;
case Event.TIMER_OUT:
timerCB(m.id);
break;
case Event.UART_RECV:
uartCB(m.port, m.data);
break;
}
}
下载代码(右键另存为):noise.js