Approved content

The content below is provided by a partner.

KittenBot/pxt-kittenwifi 0.2.7 GitHub

kittenwifi

Extension for Kittenbot Wifi module intro1 intro2

NOTICE:

Make sure you have upgrade firmware of your WiFi module to v2.82 or higher. image

Feature

<<<<<<< HEAD

  1. Control wifi access from microbit
  2. MQTT control, subscribe publish from microbit
  3. RESTFul PUT/GET request to online services, like IFTTT, thingspeak and so on
  4. UDP communication with other microbits in LAN

    a64a2a872ab456993aa3470864e4945b540a05d0


Basic usage

Connect wifi to the 2.4g band wireless network, connect to the MQTT server and subscribe to topics, try to send and receive messages. (KittenBot provides an MQTT server for debugging: https://iot.kittenbot.cn/)

    kittenwifi.on_wifi_connected(function () {
        basic.showIcon(IconNames.Yes)
    })
    input.onButtonPressed(Button.A, function () {
        kittenwifi.mqtt_sethost("iot.kittenbot.cn", "node01")
    })
    input.onButtonPressed(Button.AB, function () {
        kittenwifi.mqtt_publish_basic("/topicName", "message")
    })
    input.onButtonPressed(Button.B, function () {
        kittenwifi.mqtt_subscribe_basic("/topicName")
    })
    kittenwifi.on_mqtt_topic_data(function (topic, data) {
        basic.showString("" + topic + ":" + data)
    })
    kittenwifi.wifi_init(SerialPin.P2, SerialPin.P12)
    kittenwifi.wifi_join("rrouter", "password")

WIFI V2.8+ control protocol

Command definition

WF CMD Argc Callback Argv...

Recommand Callback Mapping

WIFI_STATUS_CHANGED = 1,
MQTT_CONN = 2,
MQTT_DISCON = 3,
MQTT_PUB = 4,
MQTT_DATA = 5,
UDP_SETUP = 6,
UDP_DATA = 7

SYNC command

Sync with wifi module with wifi status change callback

WF 1 0 1

return

WF 2 0 1 
WF 3 1 1 stat // wifistatus 1: other, 5: got ip
  WF 3 1 1 5 192.168.0.123

Wifi Join AP

Join dedicated access point with name and pass word

WF 52 2 52 ap pass

successful join would return wifi status 5, eg:

WF 3 1 1 5

change wifi module hostname

Hostname for wifi module discover or name connecte or ping, need lan support

WF 9 1 9 name

No return

Wifi Module IP get

Get Ip address of wifi module

WF 8 0 22

return

WF 3 1 22 192.168.0.111

Mqtt callback install

please refer callback mapping

WF 10 4 0 2 3 4 5

MQtt set host

WF 15 2 15 hostname clientid  

resend mqtt callback install after this command issued

MQtt subscribe

WF 12 2 0 topic qos

qos set to 0 or 1 for now, other value may not be implemented

expect callback 5 when someone pub on this topic

WF 3 2 5 /hello hello world 

MQtt pub

WF 11 4 11 qos retain topic data

MQtt last will message

WF 13 4 13 topic data 1 1

Note: this command will get mqtt offline, you should hard reset module to bring it alive

WF 3 0 5 /hello 1234567890

UDP comm start

Target ip and port (for both tx and rx)

WF 40 3 callback 192.168.0.2 5533 3

return

WF 40 clientNum 0

User should keep track of clientnum in the following process

UDP Data

WF 42 4 clientnum data

UDP send

WF 41 1 cliennum helloworld

Restful set host

return

WF 20 clientnum 0

Restful request

WF 21 2 cliennum method(GET or POST) /api/xxx? 

return, the maximum return limit to 256 bytes (64byte limit in microbit rx buffer). So, make sure you have a good api server :P

WF 23 2 clientnum code(20x or 40x) content

License

MIT

Supported targets

kittenwifi=github:Kittenbot/pxt-kittenwifi
kittenwifi=github:KittenBot/pxt-kittenwifi#v0.2.7