cloud_message — Cloud Message

cloud_message The main functionality and functions of the module

Function

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

To turn on the cloud broadcast, you need to ensure the wi-fi connection, and this opening action will take effect, parameter:

  • topic_head - Prefix directory for cloud broadcast topics.
  • client_id - The unique client ID string used when connecting to the agent is randomly generated if client_id is zero length or none.In this case, the clean_session parameter of the connect function must be True.
  • server - The host name or IP address of the remote server.
  • port - The network port for the server host to connect to, by default, is 1883.Note: the default port for MQTT over SSL/TLS is 8883.(Optional)
  • user - User name registered on the server.(Optional)
  • password - The password registered on the server.(Optional)
  • keepalive - The client keepalive timeout value is 60 seconds by default.(Optional)
  • ssl - Whether SSL/TLS support can be enabled.(Optional)
cloud_message.get_info(message)

Get the attached parameter of broadcast information, return string type or numeric data, parameter:

  • message - The name of the information to broadcast.
cloud_message.broadcast(message, value = "")

Broadcast information with parameters, parameters:

  • message - The name of the information to broadcast.
  • value - Parameters attached to the broadcast message.

Sample Code 1:

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', '')

Sample Code 2:

# -*- 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")