Google cloud functions 移动现有mqtt物联网设备以与Google云协同工作
我正在尝试安装两个不同的先前设计的物联网设备来与Google cloud配合使用,目前我的设备正在使用自定义mqtt主题发布其遥测,如: 云到设备:Google cloud functions 移动现有mqtt物联网设备以与Google云协同工作,google-cloud-functions,mqtt,google-cloud-pubsub,google-cloud-iot,Google Cloud Functions,Mqtt,Google Cloud Pubsub,Google Cloud Iot,我正在尝试安装两个不同的先前设计的物联网设备来与Google cloud配合使用,目前我的设备正在使用自定义mqtt主题发布其遥测,如: 云到设备: /devices/device1/battery/cmnd/stat (gets the status) /devices/device1/battery/cmnd/interval (value on payload) /devices/device1/fan/cmnd/turn (value on payload) 设备到云: /devic
/devices/device1/battery/cmnd/stat (gets the status)
/devices/device1/battery/cmnd/interval (value on payload)
/devices/device1/fan/cmnd/turn (value on payload)
设备到云:
/devices/device1/heartbeat/tele
/devices/device1/wifi/ip/tele
/devices/device1/wifi/rssi/tele
我是谷歌物联网云的新手,试图了解它是如何工作的,我应该如何设计后端来将我的设备连接到它
如果使用GCP,我将失去使用自定义mqtt主题的灵活性,并且我仅限于使用四个预定义主题:
- /设备/我的设备/事件/
- /设备/我的设备/命令/#
- /设备/我的设备/配置/
- /设备/我的设备/状态/
import base64
import sqlalchemy
from sqlalchemy import update
def device_telemetry_wifi_pubsub(event, context):
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
db_user = os.environ.get("DB_USER")
db_pass = os.environ.get("DB_PASS")
db_name = os.environ.get("DB_NAME")
cloud_sql_connection_name = os.environ.get("CLOUD_SQL_CONNECTION_NAME")
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername='mysql+pymysql',
username=db_user,
password=db_pass,
database=db_name,
query={
'unix_socket': '/cloudsql/{}'.format(cloud_sql_connection_name)
},
),
)
stmt = sqlalchemy.text('INSERT INTO wifistr(wifistr) VALUES (:data)')
try:
with db.connect() as conn:
conn.execute(stmt, data=pubsub_message)
message.ack()
except Exception as e:
print(e)
我正在使用“projects/{my project}/topics/wifi”主题触发器将接收到的json编码的wifi遥测数据插入数据库,我的计划是用我的设备发送的其余数据重复这个过程……它可以工作,但不确定这是否是我在GCP中尝试做的最好方法
我有几个问题/验证/假设:
请原谅,如果我的一些假设是错误的,或者我错过了一些东西,因为我对云事物非常陌生关于问题1和2,根据,为了分离来自同一设备的不同数据,您可以使用MQTT/HTTP子文件夹发布/订阅主题,以将遥测事件发布到不同的主题。此外,您还可以阅读更多。关于问题3和问题4,不可能为云功能创建模板,中没有描述此选项。因此,您必须为每个主题声明一个Clooud函数,但正如您所看到的,每个函数的实现可以委托给单个函数。最后,这显示了一个类似的场景,可以让您更好地理解。我分享的这些信息对你有帮助吗?