Skip to content

Commit

Permalink
Merge pull request #2 from bosnivan/blynk-iot-blocks
Browse files Browse the repository at this point in the history
Blynk IoT blocks are added
  • Loading branch information
davidzovko committed Sep 7, 2022
2 parents 0059ebc + 0f4a2a6 commit b2f719e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
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()
}

0 comments on commit b2f719e

Please sign in to comment.