2018年9月9日日曜日

STM32duino(Bluepill)とESP8266(NodeMCU)とInfluxdataとGrafana でワイヤレスロガー

STM32duino(Bluepill)のロガーデータをESP8266(NodeMCU)を使ってWifiワイヤレスでInfluxdataに飛ばし、Grafanaでグラフにしよう。
何とかいけそうな感じになってきた。

NodeMCUに書き込む init.lua のプログラム〜

-- send STM32duino sensor values to influxdb database
LOCATION = "4chADC-1"
-- #################################
INFLUXDB_HOST = "000.000.000.000"
INFLUXDB_DB = "influsdb_name"
-- set config about influxdata server
-- #################################
FLOAT_FIRMWARE = (1/3) > 0
CRLF = "\r\n"
wifi.setmode(wifi.STATION)
-- set wifi config via console
-- wifi.sta.config(WIFI_SSID,WIFI_PASSWORD)
wifi.sta.autoconnect(1)
-- #################################
function convertValue(val,val_dec)
  local result
  if FLOAT_FIRMWARE then
    -- Float firmware using this example
    result = string.format("%.1f",val)
  else
    -- Integer firmware using this example
    result = string.format("%d.%03d",val,val_dec)
  end
  return result
end
-- #################################
function buildDataLine(format,  a1, a2, a3, a4)
  return string.format(format, "a1data", a1)..
    "\n"..
    string.format(format, "a2data", a2)..
    "\n"..
    string.format(format, "a3data", a3)..
    "\n"..
    string.format(format, "a4data", a4)
end
-- #################################
function buildStatusLine(format, msg)
  return string.format(format, "status", msg)
end
-- #################################
function getDATAContent(location)
  local result
  local dataPattern = "%s"..
   " value=%s"
    local a1Value = convertValue(a1, a1_dec)
    local a2Value = convertValue(a2, a2_dec)
    local a3Value = convertValue(a3, a3_dec)
    local a4Value = convertValue(a4, a4_dec)
    result = buildDataLine(dataPattern,  a1, a2, a3, a4)
  return result
end
-- #################################
function buildPostRequest(host, db, data)
  return "POST /write?db="..db.." HTTP/1.1"..CRLF..
    "Host: "..host..CRLF..
    "Connection: close"..CRLF..
    "Content-Type: application/x-www-form-urlencoded"..CRLF..
    "Content-Length: "..string.len(data)..CRLF..
    CRLF..
    data
end
-- #################################
function sendData()
  local content = getDATAContent(LOCATION)
  socket = net.createConnection(net.TCP, 0)
  socket:on("receive",function(sck, c) print(c) end)
  socket:connect(8086, INFLUXDB_HOST)
  socket:on("connection", function(sck)
    local post_request = buildPostRequest(INFLUXDB_HOST, INFLUXDB_DB, content)
    sck:send(post_request)
  end)
end
-- ###### MAIN #########
 a1=10
 a2=10
 a3=10
 a4=10
tmr.alarm(0, 20 * 1000, tmr.ALARM_AUTO, sendData)

STM32duino Bluepillの方はこれから。
ロギングしたデータを使って
Serial1にa1=○○ a2=○○ a3=○○ a4=○○
てな具合に定期的に書き込めば、よろしいかと・・・・

0 件のコメント:

コメントを投稿