代码拉取完成,页面将自动刷新
/*!
* @file pxt-microIoT/microIoT.ts
* @brief DFRobot's obloq makecode library.
* @n [Get the module here](http://www.dfrobot.com.cn/goods-1577.html)
* @n Obloq is a serial port of WIFI connection module, Obloq can connect
* to Microsoft Azure IoT and other standard MQTT protocol IoT.
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright MIT Lesser General Public License
*
* @author [email](jie.tang@dfrobot.com)
* @version V0.0.3
* @date 2019-12-31
*/
const OBLOQ_MQTT_EASY_IOT_SERVER_CHINA = "iot.dfrobot.com.cn"
const OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL = "api.beebotte.com"
const OBLOQ_MQTT_EASY_IOT_SERVER_EN = "iot.dfrobot.com"
const microIoT_WEBHOOKS_URL = "maker.ifttt.com"
const OBLOQ_MQTT_EASY_IOT_SERVER_TK = "api.thingspeak.com"
const microIoT_Weather_URL = "api.dfrobot.top"
enum NeoPixelColors {
//% block=red
Red = 0xFF0000,
//% block=orange
Orange = 0xFFA500,
//% block=yellow
Yellow = 0xFFFF00,
//% block=green
Green = 0x00FF00,
//% block=blue
Blue = 0x0000FF,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xFF00FF,
//% block=white
White = 0xFFFFFF,
//% block=black
Black = 0x000000
}
enum PIN {
P15,
P0,
P1,
P2,
P8,
P12,
P16
};
/**
*Obloq implementation method.
*/
//% weight=10 color=#008B00 icon="\uf1eb" block="microIoT"
namespace microIoT {
let BrightnessP15=255;
let BrightnessP1 = 255;
let BrightnessP0 = 255;
let BrightnessP2 = 255;
let BrightnessP8 = 255;
let BrightnessP12 = 255;
let BrightnessP16 = 255;
let LenP15 = 4;
let LenP1 = 4;
let LenP0 = 4;
let LenP2 = 4;
let LenP8 = 4;
let LenP12 = 4;
let LenP16 = 4;
let IIC_ADDRESS = 0x16
let Topic0CallBack: Action = null;
let Topic1CallBack: Action = null;
let Topic2CallBack: Action = null;
let Topic3CallBack: Action = null;
let Topic4CallBack: Action = null;
let Wifi_Status = 0x00
let microIoT_WEBHOOKS_KEY = ""
let microIoT_WEBHOOKS_EVENT = ""
let microIoT_BEEBOTTE_Token = ""
let microIoT_THINGSPEAK_KEY = ""
let READ_STATUS = 0x00
let SET_PARA = 0x01
let RUN_COMMAND = 0x02
/*set para*/
let SETWIFI_NAME = 0x01
let SETWIFI_PASSWORLD = 0x02
let SETMQTT_SERVER = 0x03
let SETMQTT_PORT = 0x04
let SETMQTT_ID = 0x05
let SETMQTT_PASSWORLD = 0x06
let SETHTTP_IP = 0x07
let SETHTTP_PORT = 0x08
/*run command*/
let SEND_PING = 0x01
let CONNECT_WIFI = 0x02
let RECONNECT_WIFI = 0x03
let DISCONECT_WIFI = 0x04
let CONNECT_MQTT = 0x05
let SUB_TOPIC0 = 0x06
let SUB_TOPIC1 = 0x07
let SUB_TOPIC2 = 0x08
let SUB_TOPIC3 = 0x09
let SUB_TOPIC4 = 0x0A
let PUB_TOPIC0 = 0x0B
let PUB_TOPIC1 = 0x0C
let PUB_TOPIC2 = 0x0D
let PUB_TOPIC3 = 0x0E
let PUB_TOPIC4 = 0x0F
let GET_URL = 0x10
let POST_URL = 0x11
let PUT_URL = 0x12
let GET_VERSION = 0x13
let mqttState = 0
let wifiConnected = 0
let READmode = 0x00
let versionState = 0
/*read para value*/
let READ_PING = 0x01
let READ_WIFISTATUS = 0x02
let READ_IP = 0x03
let READ_MQTTSTATUS = 0x04
let READ_SUBSTATUS = 0x05
let READ_TOPICDATA = 0x06
let HTTP_REQUEST = 0x10
let READ_VERSION = 0x12
/*para status */
let PING_OK = 0x01
let WIFI_DISCONNECT = 0x00
let WIFI_CONNECTING = 0x02
let WIFI_CONNECTED = 0x03
let MQTT_CONNECTED = 0x01
let MQTT_CONNECTERR = 0x02
let DISCONNECT_MQTT = 0x15
let SUB_TOPIC_OK = 0x01
let SUB_TOPIC_Ceiling = 0x02
let microIoTStatus = ""
let WIFI_NAME = ""
let WIFI_PASSWORLD = ""
let MQTT_SERVER = ""
let MQTT_PORT = ""
let MQTT_ID = ""
let MQTT_PASSWORLD = ""
let Topic_0 = ""
let Topic_1 = ""
let Topic_2 = ""
let Topic_3 = ""
let Topic_4 = ""
let RECDATA = ""
let HTTP_IP = ""
let HTTP_PORT = ""
let microIoT_IP = "0.0.0.0"
let G_city = 0;
export enum aMotors {
//% blockId="M1" block="M1"
M1 = 0,
//% blockId="M2" block="M2"
M2 = 1,
//% blockId="ALL" block="ALL"
ALL = 2
}
export enum aServos {
//% blockId="S1" block="S1"
S1 = 0,
//% blockId="S2" block="S2"
S2 = 1
}
export enum Dir {
//% blockId="CW" block="CW"
CW = 0x0,
//% blockId="CCW" block="CCW"
CCW = 0x1
}
export enum SERVERS {
//% blockId=SERVERS_China block="EasyIOT_CN"
China,
//% blockId=SERVERS_English block="EasyIOT_EN"
English,
}
export enum TOPIC {
topic_0 = 0,
topic_1 = 1,
topic_2 = 2,
topic_3 = 3,
topic_4 = 4
}
export class PacketMqtt {
public message: string;
}
/**
* Configure the micro:IoT servo.
*/
//% weight=50
//% blockId=microIoT_ServoRun block="Servo|%index|angle|%angle"
//% angle.min=0 angle.max=180
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
export function microIoT_ServoRun(index: aServos, angle: number): void {
let buf = pins.createBuffer(2);
if (index == 0) {
buf[0] = 0x14;
}
if (index == 1) {
buf[0] = 0x15;
}
buf[1] = angle;
pins.i2cWriteBuffer(0x16, buf);
}
/**
* Configure the direction and speed of the micro:IoT motor
*/
//% weight=49
//% blockId=microIoT_MotorRun block="Motor|%index|move|%Dir|at speed|%speed"
//% speed.min=0 speed.max=255
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
//% direction.fieldEditor="gridpicker" direction.fieldOptions.columns=2
export function microIoT_MotorRun(index: aMotors, direction: Dir, speed: number): void {
let buf = pins.createBuffer(3);
if (index == 0) {
buf[0] = 0x00;
if (direction == 0x00) {
buf[1] = 0x01;
} else {
buf[1] = 0x00;
}
} else if (index == 1) {
buf[0] = 0x02;
buf[1] = direction;
} else if (index == 2) {
buf[0] = 0x00;
if (direction == 0x00) {
buf[1] = 0x01;
} else {
buf[1] = 0x00;
}
buf[2] = speed;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
buf[0] = 0x02;
buf[1] = direction;
} else {
}
buf[2] = speed;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
/**
* Stop the motor
*/
//% weight=48
//% blockId=microIoT_motorStop block="Motor |%motors stop"
//% motors.fieldEditor="gridpicker" motors.fieldOptions.columns=2
export function microIoT_motorStop(motors: aMotors): void {
let buf = pins.createBuffer(3);
if (motors == 0) {
buf[0] = 0x00;
} else if (motors == 1) {
buf[0] = 0x02;
} else if (motors == 2) {
buf[0] = 0x00;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
buf[0] = 0x02;
}
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_setPara(cmd: number, para: string): void {
let buf = pins.createBuffer(para.length + 4);
buf[0] = 0x1E
buf[1] = SET_PARA
buf[2] = cmd
buf[3] = para.length
for (let i = 0; i < para.length; i++)
buf[i + 4] = para[i].charCodeAt(0)
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_runCommand(cmd: number): void {
let buf = pins.createBuffer(3);
buf[0] = 0x1E
buf[1] = RUN_COMMAND
buf[2] = cmd
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_readStatus(para: number): number {
let buf = pins.createBuffer(3);
buf[0] = 0x1E
buf[1] = READ_STATUS
buf[2] = para
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
let recbuf = pins.createBuffer(2)
recbuf = pins.i2cReadBuffer(IIC_ADDRESS, 2, false)
return recbuf[1]
}
function microIoT_readValue(para: number): string {
let buf = pins.createBuffer(3);
let paraValue = 0x00
let tempLen = 0x00
let dataValue = ""
buf[0] = 0x1E
buf[1] = READ_STATUS
buf[2] = para
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
microIoT_CheckStatus("READ_IP");
return RECDATA
}
function microIoT_ParaRunCommand(cmd: number, data: string): void {
let buf = pins.createBuffer(data.length + 4)
buf[0] = 0x1E
buf[1] = RUN_COMMAND
buf[2] = cmd
buf[3] = data.length
for (let i = 0; i < data.length; i++)
buf[i + 4] = data[i].charCodeAt(0)
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
}
function microIoT_CheckStatus(cmd: string): void {
let startTime = input.runningTime();
let currentTime = 0;
while (true) {
currentTime = input.runningTime();
if (microIoTStatus == cmd) {
serial.writeString("OKOK\r\n");
return;
}
basic.pause(50);
if (versionState == 1) {
if ((currentTime - startTime) > 20000)
return;
}
}
}
/**
* WiFi configuration
* @param SSID to SSID ,eg: "yourSSID"
* @param PASSWORD to PASSWORD ,eg: "yourPASSWORD"
*/
//% weight=100
//% blockId=microIoT_wifi block="Micro:IoT setup |Wi-Fi: |name: %SSID| password:%PASSWORD"
export function microIoT_WIFI(SSID: string, PASSWORD: string): void {
microIoT_setPara(SETWIFI_NAME, SSID)
microIoT_setPara(SETWIFI_PASSWORLD, PASSWORD)
microIoT_runCommand(CONNECT_WIFI)
microIoT_CheckStatus("WiFiConnected");
Wifi_Status = WIFI_CONNECTED
}
/**
* MQTT configuration
* @param SSID to SSID ,eg: "yourSSID"
* @param PASSWORD to PASSWORD ,eg: "yourPASSWORD"
* @param IOT_ID to IOT_ID ,eg: "yourIotId"
* @param IOT_PWD to IOT_PWD ,eg: "yourIotPwd"
* @param IOT_TOPIC to IOT_TOPIC ,eg: "yourIotTopic"
*/
//% weight=100
//% blockExternalInputs=1
//% blockId=microIoT_MQTT block="Micro:IoT setup mqtt|IOT_ID(user): %IOT_ID| IOT_PWD(password) :%IOT_PWD|(default topic_0) Topic: %IOT_TOPIC| server: %SERVERS"
export function microIoT_MQTT(/*SSID: string, PASSWORD: string,*/
IOT_ID: string, IOT_PWD: string,
IOT_TOPIC: string, servers: SERVERS):
void {
if (servers == SERVERS.China) {
microIoT_setPara(SETMQTT_SERVER, OBLOQ_MQTT_EASY_IOT_SERVER_CHINA)
} else if (servers == SERVERS.English) {
microIoT_setPara(SETMQTT_SERVER, OBLOQ_MQTT_EASY_IOT_SERVER_EN)
} //else { microIoT_setPara(SETMQTT_SERVER, OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL) }
microIoT_setPara(SETMQTT_PORT, "1883")
microIoT_setPara(SETMQTT_ID, IOT_ID)
microIoT_setPara(SETMQTT_PASSWORLD, IOT_PWD)
serial.writeString("wifi conneced ok\r\n");
microIoT_runCommand(CONNECT_MQTT);
microIoT_CheckStatus("MQTTConnected");
serial.writeString("mqtt connected\r\n");
basic.pause(100)
Topic_0 = IOT_TOPIC
microIoT_ParaRunCommand(SUB_TOPIC0, IOT_TOPIC);
microIoT_CheckStatus("SubTopicOK");
serial.writeString("sub topic ok\r\n");
}
/**
* Add an MQTT subscription
*/
//% weight=200
//% blockId=microIoT_add_topic
//% block="subscribe additional %top |: %IOT_TOPIC"
//% top.fieldEditor="gridpicker" top.fieldOptions.columns=2
//% advanced=true
export function microIoT_add_topic(top: TOPIC, IOT_TOPIC: string): void {
microIoT_ParaRunCommand((top + 0x06), IOT_TOPIC);
microIoT_CheckStatus("SubTopicOK");
}
/**
* MQTT sends information to the corresponding subscription
* @param Mess to Mess ,eg: "mess"
*/
//% weight=99
//% blockId=microIoT_SendMessage block="MQTT Send Message %string| to |%TOPIC"
export function microIoT_SendMessage(Mess: string, Topic: TOPIC): void {
let topic = 0
switch (Topic) {
case TOPIC.topic_0:
topic = PUB_TOPIC0
break;
case TOPIC.topic_1:
topic = PUB_TOPIC1
break;
case TOPIC.topic_2:
topic = PUB_TOPIC2
break;
case TOPIC.topic_3:
topic = PUB_TOPIC3
break;
case TOPIC.topic_4:
topic = PUB_TOPIC4
break;
default:
break;
}
microIoT_ParaRunCommand(topic, Mess)
}
function microIoT_callback(top: TOPIC, a: Action): void {
switch (top) {
case TOPIC.topic_0:
Topic0CallBack = a;
break;
case TOPIC.topic_1:
Topic1CallBack = a;
break;
case TOPIC.topic_2:
Topic2CallBack = a;
break;
case TOPIC.topic_3:
Topic3CallBack = a;
break;
case TOPIC.topic_4:
Topic4CallBack = a;
break;
default:
break;
}
}
/**
* MQTT processes the subscription when receiving message
*/
//% weight=98
//% blockGap=60
//% blockId=obloq_mqtt_callback_user_more block="MQTT on %top |received"
//% top.fieldEditor="gridpicker" top.fieldOptions.columns=2
export function microIoT_MQTT_Event(top: TOPIC, cb: (message: string) => void) {
microIoT_callback(top, () => {
const packet = new PacketMqtt()
packet.message = RECDATA
cb(packet.message)
});
}
/**
* IFTTT configuration
* @param EVENT to EVENT ,eg: "yourEvent"
* @param KEY to KEY ,eg: "yourKey"
*/
//% weight=80
//% receive.fieldEditor="gridpicker" receive.fieldOptions.columns=3
//% send.fieldEditor="gridpicker" send.fieldOptions.columns=3
//% blockId=microIoT_http_IFTTT
//% block="Webhooks config:|event: %EVENT|key: %KEY|"
export function microIoT_http_IFTTT(EVENT: string, KEY: string): void {
microIoT_WEBHOOKS_EVENT = EVENT
microIoT_WEBHOOKS_KEY = KEY
}
function microIoT_http_wait_request(time: number): string {
if (time < 100) {
time = 100
}
let timwout = time / 100
let _timeout = 0
while (true) {
basic.pause(100)
if (microIoTStatus == "HTTP_REQUEST") {
microIoTStatus = "";
return RECDATA
} else if (microIoTStatus == "HTTP_REQUESTFailed") {
microIoTStatus = "";
return "requestFailed"
}
_timeout += 1
if (_timeout > timwout) {
return "timeOut"
}
}
}
/**
* ThingSpeak configuration
* @param KEY to KEY ,eg: "your Key"
*/
//% weight=28
//% receive.fieldEditor="gridpicker" receive.fieldOptions.columns=3
//% send.fieldEditor="gridpicker" send.fieldOptions.columns=3
//% blockId=WiFi_IoT_I2C_ThingSpeak_configura
//% block="ThingSpeak configure key: %KEY"
export function ThingSpeakConfigure(KEY: string): void {
microIoT_THINGSPEAK_KEY = KEY
}
/**
* ThingSpeak configured and sent data
* @param field1 ,eg: 2020
*/
//% weight=27
//% blockId=WiFi_IoT_I2C_ThingSpeak_Configure
//% expandableArgumentMode="enabled"
//% inlineInputMode=inline
//% block="ThingSpeak send Field1: %field1||Field2: %field2|Field3: %field3|Field4: %field4|Field5: %field5|Field6: %field6|Field7: %field7 Field8: %field8"
export function ThingSpeakSend(field1: string, field2?: string, field3?: string, field4?: string, field5?: string, field6?: string, field7?: string, field8?: string): void {
microIoT_setPara(SETHTTP_IP, OBLOQ_MQTT_EASY_IOT_SERVER_TK)
let tempStr = ""
tempStr = "update?api_key=" + microIoT_THINGSPEAK_KEY + "&field1=" + field1
// if(field2 != undefined){
// tempStr += "&field2=" + field2
// }else if(field3 != undefined){
// tempStr += "&field3=" + field3
// }else if(field4 != undefined){
// tempStr += "&field4=" + field4
// }else if(field5 != undefined){
// tempStr += "&field5=" + field5
// }else if(field6 != undefined){
// tempStr += "&field6=" + field6
// }else if(field7 != undefined){
// tempStr += "&field7=" + field7
// }else if(field8 != undefined){
// tempStr += "&field8=" + field8
// }else{
// tempStr += "\r"
// }
if (field2 != undefined)
tempStr += "&field2=" + field2
if (field3 != undefined)
tempStr += "&field3=" + field3
if (field4 != undefined)
tempStr += "&field4=" + field4
if (field5 != undefined)
tempStr += "&field5=" + field5
if (field6 != undefined)
tempStr += "&field6=" + field6
if (field7 != undefined)
tempStr += "&field7=" + field7
if (field8 != undefined)
tempStr += "&field8=" + field8
microIoT_ParaRunCommand(GET_URL, tempStr);
}
/**
* IFTTT send data
* time(ms): private long maxWait
* @param time set timeout, eg: 10000
*/
//% weight=78
//% blockId=microIoT_http_post
//% block="IFTTT(post) | value1 %value1| value2 %value2| value3 %value3| timeout(ms) %time"
export function microIoT_http_post(value1: string, value2: string, value3: string, time: number): void {
microIoT_setPara(SETHTTP_IP, microIoT_WEBHOOKS_URL)
let tempStr = ""
tempStr = "trigger/" + microIoT_WEBHOOKS_EVENT + "/with/key/" + microIoT_WEBHOOKS_KEY + ",{\"value1\":\"" + value1 + "\",\"value2\":\"" + value2 + "\",\"value3\":\"" + value3 + "\" }" + "\r"
microIoT_ParaRunCommand(POST_URL, tempStr)
}
// /**Beebotte Configure
// * @param token ,eg: "Your Channel Token"
// */
// //%weight=30
// //%blockID=WiFi_IoT_I2C_BeeBotte_Configura block="BeeBotte configura key: %token "
// export function token(token:string):void{
// microIoT_BEEBOTTE_Token = token;
// }
// /**BeeBotte send data
// * @param channel ,eg: "Your Channel Name"
// * @param resource ,eg: "Your Resource Name"
// * @param data ,eg: "Send Message"
// */
// //%weight=29
// //%blockID=WiFi_IoT_I2C_BeeBotte_sendmessage block="BeeBotte Channel: %channel Resource: %resource send value %data "
// export function sendmessage(channel:string, resource:string, data:string){
// microIoT_setPara(SETHTTP_IP, OBLOQ_MQTT_EASY_IOT_SERVER_GLOBAL)
// let tempStr = ""
// tempStr = "v1/data/write/" + channel + "/" + resource + "?token=" + microIoT_BEEBOTTE_Token +",{\"data\":" + data + "}\r\n";
// microIoT_ParaRunCommand(POST_URL, tempStr);
// }
/**
* Get IP address.
*/
//% weight=51
//% blockId=microIoT_wifi_ipconfig
//% block="ipconfig"
//% advanced=true
export function microIoT_wifi_ipconfig(): string {
return microIoT_IP;
//microIoT_readValue(READ_IP)
}
/**
* Send the ping.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=49
//% blockId=Obloq_send_ping
//% block="sendPing"
//% advanced=true
export function microIoT_send_ping(): boolean {
let buf = pins.createBuffer(3);
buf[0] = 0x1E;
buf[1] = RUN_COMMAND;
buf[2] = SEND_PING;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
microIoT_CheckStatus("PingOK");
return true;
}
/**
* Get the software version.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=50
//% blockId=microIoT_get_version
//% block="get version"
//% advanced=true
export function microIoT_get_version(): string {
let buf = pins.createBuffer(3);
buf[0] = 0x1E;
buf[1] = RUN_COMMAND;
buf[2] = GET_VERSION;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
//microIoT_CheckStatus("READ_VERSION");
return RECDATA
}
/**
* Heartbeat request.time(ms): private long maxWait
* @param time to timeout, eg: 10000
*/
//% weight=48
//% blockId=microIoT_get_heartbeat
//% block="get heartbeat"
//% advanced=true
export function microIoT_get_heartbeat(): boolean {
return true
}
/**
* Stop the heartbeat request.
*/
//% weight=47
//% blockId=microIoT_stop_heartbeat
//% block="stop heartbeat"
//% advanced=true
export function microIoT_stop_heartbeat(): boolean {
return true
}
function microIoT_GetData(len: number): void {
RECDATA = ""
let tempbuf = pins.createBuffer(1)
tempbuf[0] = 0x22
pins.i2cWriteBuffer(IIC_ADDRESS, tempbuf);
let tempRecbuf = pins.createBuffer(len)
tempRecbuf = pins.i2cReadBuffer(IIC_ADDRESS, len, false)
for (let i = 0; i < len; i++) {
RECDATA += String.fromCharCode(tempRecbuf[i])
}
}
function microIoT_InquireStatus(): void {
let buf = pins.createBuffer(3)
let tempId = 0
let tempStatus = 0
buf[0] = 0x1E
buf[1] = READmode
buf[2] = 0x06
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
let recbuf = pins.createBuffer(2)
recbuf = pins.i2cReadBuffer(IIC_ADDRESS, 2, false)
tempId = recbuf[0]
tempStatus = recbuf[1]
switch (tempId) {
case READ_PING:
if (tempStatus == PING_OK) {
microIoTStatus = "PingOK"
} else {
microIoTStatus = "PingERR"
}
break;
case READ_WIFISTATUS:
if (tempStatus == WIFI_CONNECTING) {
microIoTStatus = "WiFiConnecting"
} else if (tempStatus == WIFI_CONNECTED) {
microIoTStatus = "WiFiConnected"
} else if (tempStatus == WIFI_DISCONNECT) {
microIoTStatus = "WiFiDisconnect"
wifiConnected++;
if (wifiConnected == 2) {
wifiConnected = 0;
microIoT_runCommand(WIFI_CONNECTED);
}
} else {
} break;
case READ_MQTTSTATUS:
if (tempStatus == MQTT_CONNECTED) {
microIoTStatus = "MQTTConnected"
mqttState = 1;
} else if (tempStatus == MQTT_CONNECTERR) {
microIoTStatus = "MQTTConnectERR"
} else if (tempStatus == 0) {//新版本修复重连
microIoT_runCommand(DISCONNECT_MQTT);
microIoT_runCommand(WIFI_CONNECTED);
}
break;
case READ_SUBSTATUS:
if (tempStatus == SUB_TOPIC_OK) {
microIoTStatus = "SubTopicOK"
} else if (tempStatus == SUB_TOPIC_Ceiling) {
microIoTStatus = "SubTopicCeiling"
} else {
microIoTStatus = "SubTopicERR"
}
break;
case READ_IP:
microIoTStatus = "READ_IP"
microIoT_GetData(tempStatus)
microIoT_IP = RECDATA
if (mqttState == 1) {
mqttState = 0;
microIoT_runCommand(DISCONNECT_MQTT);
basic.pause(200)
microIoT_runCommand(CONNECT_MQTT);
//microIoT_CheckStatus("MQTTConnected");
}
break;
case SUB_TOPIC0:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic0CallBack != null) {
Topic0CallBack();
}
break;
case SUB_TOPIC1:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic1CallBack != null) {
Topic1CallBack();
}
break;
case SUB_TOPIC2:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic2CallBack != null) {
Topic2CallBack();
}
break;
case SUB_TOPIC3:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic3CallBack != null) {
Topic3CallBack();
}
break;
case SUB_TOPIC4:
microIoTStatus = "READ_TOPICDATA"
microIoT_GetData(tempStatus)
if (Topic4CallBack != null) {
Topic4CallBack();
}
break;
case HTTP_REQUEST:
microIoTStatus = "HTTP_REQUEST"
microIoT_GetData(tempStatus)
break;
case READ_VERSION:
microIoTStatus = "READ_VERSION"
microIoT_GetData(tempStatus)
break;
default:
break;
}
basic.pause(50);
}
basic.forever(function () {
microIoT_InquireStatus();
})
/**
* Initialize OLED, just put the module in the module at the beginning of the code, no need to reuse
*/
//% weight=200
//% block="init device"
export function microIoT_initDisplay(): void {
microIoT_cmd(0xAE); // Set display OFF
microIoT_cmd(0xD5); // Set Display Clock Divide Ratio / OSC Frequency 0xD4
microIoT_cmd(0x80); // Display Clock Divide Ratio / OSC Frequency
microIoT_cmd(0xA8); // Set Multiplex Ratio
microIoT_cmd(0x3F); // Multiplex Ratio for 128x64 (64-1)
microIoT_cmd(0xD3); // Set Display Offset
microIoT_cmd(0x00); // Display Offset
microIoT_cmd(0x40); // Set Display Start Line
microIoT_cmd(0x8D); // Set Charge Pump
microIoT_cmd(0x14); // Charge Pump (0x10 External, 0x14 Internal DC/DC)
microIoT_cmd(0xA1); // Set Segment Re-Map
microIoT_cmd(0xC8); // Set Com Output Scan Direction
microIoT_cmd(0xDA); // Set COM Hardware Configuration
microIoT_cmd(0x12); // COM Hardware Configuration
microIoT_cmd(0x81); // Set Contrast
microIoT_cmd(0xCF); // Contrast
microIoT_cmd(0xD9); // Set Pre-Charge Period
microIoT_cmd(0xF1); // Set Pre-Charge Period (0x22 External, 0xF1 Internal)
microIoT_cmd(0xDB); // Set VCOMH Deselect Level
microIoT_cmd(0x40); // VCOMH Deselect Level
microIoT_cmd(0xA4); // Set all pixels OFF
microIoT_cmd(0xA6); // Set display not inverted
microIoT_cmd(0xAF); // Set display On
microIoT_clear();
let Version = microIoT.microIoT_get_version();
if (Version == "V4.1") {
versionState = 1
let buf = pins.createBuffer(3);
buf[0] = 0x1E;
buf[1] = 0x02;
buf[2] = 0x17;
pins.i2cWriteBuffer(IIC_ADDRESS, buf);
basic.pause(2000)
}
}
/**
* OLED clear
*/
//% weight=60
//% block="clear OLED"
export function microIoT_clear() {
for (let j = 0; j < 8; j++) {
microIoT_setText(j, 0);
{
for (let i = 0; i < 16; i++) //clear all columns
{
microIoT_putChar(' ');
}
}
}
microIoT_setText(0, 0);
}
function microIoT_setText(row: number, column: number) {
let r = row;
let c = column;
if (row < 0) { r = 0 }
if (column < 0) { c = 0 }
if (row > 7) { r = 7 }
if (column > 15) { c = 15 }
microIoT_cmd(0xB0 + r); //set page address
microIoT_cmd(0x00 + (8 * c & 0x0F)); //set column lower address
microIoT_cmd(0x10 + ((8 * c >> 4) & 0x0F)); //set column higher address
}
function microIoT_putChar(c: string) {
let c1 = c.charCodeAt(0);
microIoT_writeCustomChar(basicFont[c1 - 32]);
}
/**
* @param line line num (8 pixels per line), eg: 0
* @param text value , eg: DFRobot
* OLED display string
*/
//% weight=60
//% text.defl="DFRobot"
//% line.min=0 line.max=7
//% block="OLED show text %text|on line %line"
export function microIoT_showUserText(line: number, text: string): void {
microIoT_setText(line, 0);
for (let c of text) {
microIoT_putChar(c);
}
for (let i = text.length; i < 16; i++) {
microIoT_setText(line, i);
microIoT_putChar(" ");
}
}
/**
* @param line line num (8 pixels per line), eg: 0
* @param n value , eg: 2019
* OLED shows the number
*/
//% weight=60
//% line.min=0 line.max=7
//% block="OLED show number %n|on line %line"
export function microIoT_showUserNumber(line: number, n: number): void {
microIoT.microIoT_showUserText(line, "" + n)
}
function microIoT_writeCustomChar(c: string) {
for (let i = 0; i < 8; i++) {
microIoT_writeData(c.charCodeAt(i));
}
}
function microIoT_cmd(c: number) {
pins.i2cWriteNumber(0x3C, c, NumberFormat.UInt16BE);
}
function microIoT_writeData(n: number) {
let b = n;
if (n < 0) { n = 0 }
if (n > 255) { n = 255 }
pins.i2cWriteNumber(0x3C, 0x4000 + b, NumberFormat.UInt16BE);
}
const DISPLAY_OFF = 0xAE;
const DISPLAY_ON = 0xAF;
const basicFont: string[] = [
"\x00\x00\x00\x00\x00\x00\x00\x00", // " "
"\x00\x00\x5F\x00\x00\x00\x00\x00", // "!"
"\x00\x00\x07\x00\x07\x00\x00\x00", // """
"\x00\x14\x7F\x14\x7F\x14\x00\x00", // "#"
"\x00\x24\x2A\x7F\x2A\x12\x00\x00", // "$"
"\x00\x23\x13\x08\x64\x62\x00\x00", // "%"
"\x00\x36\x49\x55\x22\x50\x00\x00", // "&"
"\x00\x00\x05\x03\x00\x00\x00\x00", // "'"
"\x00\x1C\x22\x41\x00\x00\x00\x00", // "("
"\x00\x41\x22\x1C\x00\x00\x00\x00", // ")"
"\x00\x08\x2A\x1C\x2A\x08\x00\x00", // "*"
"\x00\x08\x08\x3E\x08\x08\x00\x00", // "+"
"\x00\xA0\x60\x00\x00\x00\x00\x00", // ","
"\x00\x08\x08\x08\x08\x08\x00\x00", // "-"
"\x00\x60\x60\x00\x00\x00\x00\x00", // "."
"\x00\x20\x10\x08\x04\x02\x00\x00", // "/"
"\x00\x3E\x51\x49\x45\x3E\x00\x00", // "0"
"\x00\x00\x42\x7F\x40\x00\x00\x00", // "1"
"\x00\x62\x51\x49\x49\x46\x00\x00", // "2"
"\x00\x22\x41\x49\x49\x36\x00\x00", // "3"
"\x00\x18\x14\x12\x7F\x10\x00\x00", // "4"
"\x00\x27\x45\x45\x45\x39\x00\x00", // "5"
"\x00\x3C\x4A\x49\x49\x30\x00\x00", // "6"
"\x00\x01\x71\x09\x05\x03\x00\x00", // "7"
"\x00\x36\x49\x49\x49\x36\x00\x00", // "8"
"\x00\x06\x49\x49\x29\x1E\x00\x00", // "9"
"\x00\x00\x36\x36\x00\x00\x00\x00", // ":"
"\x00\x00\xAC\x6C\x00\x00\x00\x00", // ";"
"\x00\x08\x14\x22\x41\x00\x00\x00", // "<"
"\x00\x14\x14\x14\x14\x14\x00\x00", // "="
"\x00\x41\x22\x14\x08\x00\x00\x00", // ">"
"\x00\x02\x01\x51\x09\x06\x00\x00", // "?"
"\x00\x32\x49\x79\x41\x3E\x00\x00", // "@"
"\x00\x7E\x09\x09\x09\x7E\x00\x00", // "A"
"\x00\x7F\x49\x49\x49\x36\x00\x00", // "B"
"\x00\x3E\x41\x41\x41\x22\x00\x00", // "C"
"\x00\x7F\x41\x41\x22\x1C\x00\x00", // "D"
"\x00\x7F\x49\x49\x49\x41\x00\x00", // "E"
"\x00\x7F\x09\x09\x09\x01\x00\x00", // "F"
"\x00\x3E\x41\x41\x51\x72\x00\x00", // "G"
"\x00\x7F\x08\x08\x08\x7F\x00\x00", // "H"
"\x00\x41\x7F\x41\x00\x00\x00\x00", // "I"
"\x00\x20\x40\x41\x3F\x01\x00\x00", // "J"
"\x00\x7F\x08\x14\x22\x41\x00\x00", // "K"
"\x00\x7F\x40\x40\x40\x40\x00\x00", // "L"
"\x00\x7F\x02\x0C\x02\x7F\x00\x00", // "M"
"\x00\x7F\x04\x08\x10\x7F\x00\x00", // "N"
"\x00\x3E\x41\x41\x41\x3E\x00\x00", // "O"
"\x00\x7F\x09\x09\x09\x06\x00\x00", // "P"
"\x00\x3E\x41\x51\x21\x5E\x00\x00", // "Q"
"\x00\x7F\x09\x19\x29\x46\x00\x00", // "R"
"\x00\x26\x49\x49\x49\x32\x00\x00", // "S"
"\x00\x01\x01\x7F\x01\x01\x00\x00", // "T"
"\x00\x3F\x40\x40\x40\x3F\x00\x00", // "U"
"\x00\x1F\x20\x40\x20\x1F\x00\x00", // "V"
"\x00\x3F\x40\x38\x40\x3F\x00\x00", // "W"
"\x00\x63\x14\x08\x14\x63\x00\x00", // "X"
"\x00\x03\x04\x78\x04\x03\x00\x00", // "Y"
"\x00\x61\x51\x49\x45\x43\x00\x00", // "Z"
"\x00\x7F\x41\x41\x00\x00\x00\x00", // """
"\x00\x02\x04\x08\x10\x20\x00\x00", // "\"
"\x00\x41\x41\x7F\x00\x00\x00\x00", // """
"\x00\x04\x02\x01\x02\x04\x00\x00", // "^"
"\x00\x80\x80\x80\x80\x80\x00\x00", // "_"
"\x00\x01\x02\x04\x00\x00\x00\x00", // "`"
"\x00\x20\x54\x54\x54\x78\x00\x00", // "a"
"\x00\x7F\x48\x44\x44\x38\x00\x00", // "b"
"\x00\x38\x44\x44\x28\x00\x00\x00", // "c"
"\x00\x38\x44\x44\x48\x7F\x00\x00", // "d"
"\x00\x38\x54\x54\x54\x18\x00\x00", // "e"
"\x00\x08\x7E\x09\x02\x00\x00\x00", // "f"
"\x00\x18\xA4\xA4\xA4\x7C\x00\x00", // "g"
"\x00\x7F\x08\x04\x04\x78\x00\x00", // "h"
"\x00\x00\x7D\x00\x00\x00\x00\x00", // "i"
"\x00\x80\x84\x7D\x00\x00\x00\x00", // "j"
"\x00\x7F\x10\x28\x44\x00\x00\x00", // "k"
"\x00\x41\x7F\x40\x00\x00\x00\x00", // "l"
"\x00\x7C\x04\x18\x04\x78\x00\x00", // "m"
"\x00\x7C\x08\x04\x7C\x00\x00\x00", // "n"
"\x00\x38\x44\x44\x38\x00\x00\x00", // "o"
"\x00\xFC\x24\x24\x18\x00\x00\x00", // "p"
"\x00\x18\x24\x24\xFC\x00\x00\x00", // "q"
"\x00\x00\x7C\x08\x04\x00\x00\x00", // "r"
"\x00\x48\x54\x54\x24\x00\x00\x00", // "s"
"\x00\x04\x7F\x44\x00\x00\x00\x00", // "t"
"\x00\x3C\x40\x40\x7C\x00\x00\x00", // "u"
"\x00\x1C\x20\x40\x20\x1C\x00\x00", // "v"
"\x00\x3C\x40\x30\x40\x3C\x00\x00", // "w"
"\x00\x44\x28\x10\x28\x44\x00\x00", // "x"
"\x00\x1C\xA0\xA0\x7C\x00\x00\x00", // "y"
"\x00\x44\x64\x54\x4C\x44\x00\x00", // "z"
"\x00\x08\x36\x41\x00\x00\x00\x00", // "{"
"\x00\x00\x7F\x00\x00\x00\x00\x00", // "|"
"\x00\x41\x36\x08\x00\x00\x00\x00", // "}"
"\x00\x02\x01\x01\x02\x01\x00\x00" // "~"
];
/**
* Set the three primary color:red, green, and blue
* @param r , eg: 255
* @param g , eg: 255
* @param b , eg: 255
*/
//% weight=60
//% r.min=0 r.max=255
//% g.min=0 g.max=255
//% b.min=0 b.max=255
//% block="red %r green %g blue %b"
export function microIoT_rgb(r: number, g: number, b: number): number {
return (r << 16) + (g << 8) + (b);
}
/**
* Setting the Number of lights
* @param len , eg: 7
*/
//% weight=60
//% len.min=0 len.max=40
//% block="pin %pin %len RGB LEDs"
export function microIoT_length(pin:PIN, len:number):void{
switch (pin) {
case PIN.P0:
LenP0 = len
break;
case PIN.P1:
LenP1 = len
break;
case PIN.P2:
LenP2 = len
break;
case PIN.P8:
LenP8 = len
break;
case PIN.P12:
LenP12 = len
break;
case PIN.P15:
LenP15 = len
break;
default:
LenP16 = len
break;
}
}
/**
* Set the color of the specified LEDs
* @param start , eg: 0
* @param stop , eg: 4
*/
//% weight=60
//% inlineInputMode=inline
//% start.min=0 start.max=40
//% stop.min=0 stop.max=40
//% rgb.shadow="colorNumberPicker"
//% block="pin %pin RGB %start to %stop show color %rgb"
export function microIoT_setIndexColor(pin: PIN, start: number, stop:number,rgb: number) {
let bufLength = 40 * 3;
let neopixel_buf = pins.createBuffer(bufLength);
for (let i = 0; i < bufLength; i++) {
neopixel_buf[i] = 0
}
let _pin;
let _brightness;
switch (pin) {
case PIN.P0:
_pin = DigitalPin.P0;
_brightness = BrightnessP0;
break;
case PIN.P1:
_pin = DigitalPin.P1;
_brightness = BrightnessP1;
break;
case PIN.P2:
_pin = DigitalPin.P2;
_brightness = BrightnessP2;
break;
case PIN.P8:
_pin = DigitalPin.P8;
_brightness = BrightnessP8;
break;
case PIN.P12:
_pin = DigitalPin.P12;
_brightness = BrightnessP12;
break;
case PIN.P15:
_pin = DigitalPin.P15;
_brightness = BrightnessP15;
break;
default:
_pin = DigitalPin.P16;
_brightness = BrightnessP16;
break;
}
let f = start;
let t = stop;
let r = (rgb >> 16) * (_brightness / 255);
let g = ((rgb >> 8) & 0xFF) * (_brightness / 255);
let b = ((rgb) & 0xFF) * (_brightness / 255);
for (let i = f; i <= t; i++) {
neopixel_buf[i * 3 + 0] = Math.round(g)
neopixel_buf[i * 3 + 1] = Math.round(r)
neopixel_buf[i * 3 + 2] = Math.round(b)
}
ws2812b.sendBuffer(neopixel_buf, _pin)
}
/**
* Set the brightness of RGB LED
* @param brightness , eg: 100
*/
//% weight=60
//% brightness.min=0 brightness.max=255
//% block="pin %pin LED brightness %brightness"
export function microIoT_setBrightness(pin: PIN,brightness: number) {
switch (pin) {
case PIN.P0:
BrightnessP0 = brightness;
break;
case PIN.P1:
BrightnessP1 = brightness;
break;
case PIN.P2:
BrightnessP2 = brightness;
break;
case PIN.P8:
BrightnessP8 = brightness;
break;
case PIN.P12:
BrightnessP12 = brightness;
break;
case PIN.P15:
BrightnessP15 = brightness;
break;
default:
BrightnessP16 = brightness;
break;
}
}
/**
* Turn off all RGB LEDs
*/
//% weight=60
//% block="pin %pin clear all LEDs"
export function microIoT_ledBlank(pin: PIN) {
let _len;
switch (pin) {
case PIN.P0:
_len = LenP0;
break;
case PIN.P1:
_len = LenP1
break;
case PIN.P2:
_len = LenP2
break;
case PIN.P8:
_len = LenP8
break;
case PIN.P12:
_len = LenP12
break;
case PIN.P15:
_len = LenP15
break;
default:
_len = LenP16
break;
}
microIoT_setIndexColor(pin, 0, _len,0);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。