“气象站”项目(一)逐步完善,拒绝懒癌

开始...
MQTT采集的数据如何持久化比较好,用什么库存储呢?
sudo apt-get intall sqlite

https://www.runoob.com/sqlite/sqlite-create-database.html
sqlite3 qxz.db

沙发:我两三个设备跑半个月就是10万条数据,数据库合适吗
我:感觉不太合适,心疼我的tf卡

http://yueqiumao.com/p/139.html
2G内存,才分100M会不会太小气了?
sudo mkdir /ramdisk
sudo mount -o size=100m -t tmpfs tmpfs /ramdisk
sudo umount /ramdisk

准备再10分钟就收工了,不能指望一天吃个胖子,今天收获已经不少了,手机控制8266,气象站也已经起步了,决定把数据放到内存中,以前温湿度存mysql,一年一张卡就废了

起来撸气象站啦,文件存储逻辑,收MQ,写内存文本文件,要素:时间、猪蹄、详情,每小时一个文件,移动到卡上(是否入库再说)
用啥写呢,试试python,其实还是喜欢用mono

http://mikuq.com/560.htm
尝试订阅yqm,结果提示ImportError: No module named paho.mqtt.client
sudo apt-get update
sudo apt-get install python-pip
pip install paho-mqtt
pip install --upgrade pip(非必需8.1.1->19.2.3)
球猫的地址变了mqtt.yqmiot.com:1883
用网页测试wss://yqmiot.com/_wss似乎也下线了
换mikuq.com:21883和ws://mikuq.com:21884/mqtt

python下要用到定时器
https://blog.csdn.net/cn_yaojin/article/details/82110845

消息发送测试

import paho.mqtt.client as mqtt
import threading
import time

client = mqtt.Client()
client.connect("mikuq.com", 21883, 50)

def heart_beat():
    mydt = time.strftime('%Y-%m-%d %H:%M:%S')
    print mydt

    global client
    client.publish("mqtt/timer", "{\"time\":\"" + mydt + "\"}")
    threading.Timer(10, heart_beat).start()

heart_beat()

消息接收测试

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import paho.mqtt.client as mqtt
import os.path as Path
import threading
import time
import shutil

full_fn = ''
fn=''

def on_connect(client, userdata, flags, rc):
  print 'on_connect'
  client.subscribe("mqtt/#")

def on_message(client, userdata, msg):
  global full_fn
  print msg.topic.decode("utf-8")
  print msg.payload.decode("utf-8")
  dt = time.strftime('%Y-%m-%d %H:%M:%S')
  fo = open(full_fn, "a+")
  fo.write(dt + '|' + msg.topic.decode("utf-8") + '|' + msg.payload.decode("utf-8") + '\n')
  fo.close()

def heart_beat():
  global full_fn
  global fn
  print 'heart_beat'
  if full_fn!='':
    print full_fn
    if Path.exists(full_fn):
      print 'move'
      shutil.move(full_fn, '/home/fa/data/' + fn)

  fn = time.strftime('%Y%m%d%H%M%S') + '.dat'
  full_fn = '/ramdisk/' + fn

  threading.Timer(3600, heart_beat).start()

heart_beat()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("mikuq.com", 21883, 50)
client.loop_forever()

标签: none

添加新评论