Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blynk IoT blocks are added #2

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ namespace WiFiBit {
*/
//% weight=95
//% blockId="wfb_blynk_write" block="Blynk: write %value to %pin, token is %auth_token"
export function writePinValue(value: string, pin: string, auth_token: string): void {
export function writeBlynkPinValue(value: string, pin: string, auth_token: string): void {
executeHttpMethod(
HttpMethod.GET,
"blynk-cloud.com",
Expand All @@ -159,7 +159,7 @@ namespace WiFiBit {
*/
//% weight=94
//% blockId="wfb_blynk_read" block="Blynk: read %pin, token is %auth_token"
export function readPinValue(pin: string, auth_token: string): string {
export function readBlynkPinValue(pin: string, auth_token: string): string {
executeAtCommand("ATE0", 1000)
let response: string
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
Expand All @@ -178,9 +178,51 @@ namespace WiFiBit {
}

/**
* Line separator. It's used when headers or body are multiline.
* Write Blynk IoT pin value.
* @param value Value, eg: "1"
* @param pin Pin, eg: "V1"
* @param auth_token Token, eg: "BzMEzpZ9Bud9ZUXZoJVEkbfneCavDVDx"
*/
//% weight=93
//% blockId="wfb_blynk_iot_write" block="Blynk IoT: write %value to %pin, token is %auth_token"
export function writeBlynkIoTPinValue(value: string, pin: string, auth_token: string): void {
executeHttpMethod(
HttpMethod.GET,
"blynk.cloud",
80,
"/external/api/update?token=" + auth_token + "&" + pin + "=" + value
)
}

/**
* Read Blynk IoT pin value.
* @param pin Pin, eg: "V1"
* @param auth_token Token, eg: "BzMEzpZ9Bud9ZUXZoJVEkbfneCavDVDx"
*/
//% weight=92
//% blockId="wfb_blynk_iot_read" block="Blynk IoT: read %pin, token is %auth_token"
export function readBlynkIoTPinValue(pin: string, auth_token: string): string {
executeAtCommand("ATE0", 1000)
let response: string
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
response += serial.readString()
})
executeHttpMethod(
HttpMethod.GET,
"blynk.cloud",
80,
"/external/api/get?token=" + auth_token + "&v-1&" + pin
)
let value: string = response.substr(response.indexOf("{\"" + pin + "\":") + 2 + pin.length + 2, response.indexOf("}") - response.indexOf("{\"" + pin + "\":") - 2 - pin.length - 2).replaceAll("\"", "")
response = null
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), () => { })
return value
}

/**
* Line separator. It's used when headers or body are multiline.
*/
//% weight=91
//% blockId="wfb_crlf" block="CRLF"
export function newline(): string {
return "\u000D" + "\u000A"
Expand Down
4 changes: 2 additions & 2 deletions pxt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pxt-wifi",
"version": "1.0.1",
"version": "1.1.0",
"description": "Connect micro:bit to the Internet with a WiFi:bit module.",
"dependencies": {
"core": "*"
Expand All @@ -13,4 +13,4 @@
"test.ts"
],
"public": true
}
}
15 changes: 13 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,27 @@
80,
"/14dabda3551b4dd5ab46464af582f7d2/update/A0?value=205"
)
WiFiBit.writePinValue(
WiFiBit.writeBlynkPinValue(
"510",
"A0",
"14dabda3551b4dd5ab46464af582f7d2"
)
let pinValue = "Pin A0:"
+ WiFiBit.newline()
+ WiFiBit.readPinValue(
+ WiFiBit.readBlynkPinValue(
"A0",
"14dabda3551b4dd5ab46464af582f7d2"
)
WiFiBit.writeBlynkIoTPinValue(
"1",
"V1",
"BzMEzpZ9Bud9ZUXZoJVEkbfneCavDVDx"
)
pinValue = "Pin V1:"
+ WiFiBit.newline()
+ WiFiBit.readBlynkIoTPinValue(
"V1",
"BzMEzpZ9Bud9ZUXZoJVEkbfneCavDVDx"
)
WiFiBit.disconnectFromWiFiNetwork()
}