cloud_message --- 云广播模块

cloud_message 模块的主要功能与函数

功能相关函数

cloud_message.start(topic_head, cliend_id=None, server="mq.makeblock.com", port=1883, user=None, password=None, keepalive=60, ssl=False)

开启云广播,需保证Wi-Fi连接,这个开启动作才生效,参数:

  • topic_head 云广播主题的前缀目录。
  • client_id 连接到代理时使用的唯一客户端ID字符串,如果client_id为零长度或无,则将随机生成一个。在这种情况下,connect 函数的clean_session参数必须为True。
  • server 远程服务器的主机名或IP地址。
  • port (可选)要连接的服务器主机的网络端口,默认为1883。请注意:MQTT over SSL/TLS的默认端口为8883。
  • user (可选)在服务器上注册的用户名。
  • password (可选)在服务器上注册的密码。
  • keepalive (可选)客户端的keepalive超时值,默认为60秒。
  • ssl (可选)是否能使SSL/TLS支持。
cloud_message.get_info(message)

获取广播信息的附带参数,返回字符串类型或者数值数据,参数:

  • message 字符串数据,广播的信息名称。
cloud_message.broadcast(message, value = "")

广播信息并附带参数,参数:

  • message 字符串数据,广播的信息名称。
  • value 广播信息附带的参数。

程序示例一:

import haloboard
import event
haloboard.cloud_message.start('/USER/1014148/MESSAGE')

@event.start
def on_start():
    haloboard.wifi.start(ssid = 'Maker-guest', password = 'makeblock', mode = haloboard.wifi.WLAN_MODE_STA)
    while not haloboard.wifi.is_connected():
        pass
    haloboard.led.show_all(126, 211, 33)
    time.sleep(2)
    haloboard.led.off_all()
    haloboard.cloud_message.broadcast('hello', '')

程序示例二:

# -*- coding: utf-8 -*-
# generated by mBlock5 for <product>
# codes make you happy
import time
import math
import random
import haloboard, event
# from micropython import mem_info

@event.start
def on_start():
    haloboard.led.show_all(50, 50, 50)
    haloboard.wifi.start(ssid = 'Maker-guest', password = 'makeblock', mode = haloboard.wifi.WLAN_MODE_STA)
    haloboard.cloud_message.start('/USER/11/MESSAGE')
    while not haloboard.wifi.is_connected():
        pass
    haloboard.led.show_all(0, 50, 0)

@event.cloud_message('b')
def on_cloud_message1():
    haloboard.led.show_all(50, 0, 0)
    print(haloboard.cloud_message.get_info("b"))

@event.cloud_message('a')
def on_cloud_message2():
    haloboard.led.show_all(0, 0, 50)
    print(haloboard.cloud_message.get_info("a"))
    # mem_info()

@event.button_pressed
def on_button_pressed():
    haloboard.cloud_message.broadcast('b', "test1")
    haloboard.cloud_message.broadcast('a', "test2")