Compare commits

..

11 Commits

Author SHA1 Message Date
445e3e2103 Serveradresse auf iot.rlp.schule setzen. 2023-07-18 11:10:46 +02:00
Baozhu Zuo
e1e2634801
Create LICENSE 2021-06-24 14:26:48 +08:00
Baozhu Zuo
c585249c82
update to v0.5.0 2021-03-03 08:41:54 +08:00
Baozhu Zuo
6f841cdabc
Merge pull request #23 from matsujirushi/master
Add GroveAHT20 blocks
2021-03-03 08:40:54 +08:00
Takashi Matsuoka
1548cd4b9c Add GroveAHT20 2021-03-03 09:31:50 +09:00
MiroChao
f55133bc27
Merge pull request #22 from IsQianGe/patch-2
Correct description
2020-12-19 12:27:32 +08:00
IsQianGe
717b846846
Correct description 2020-12-17 17:33:09 +08:00
Baozhu Zuo
96df6b18b2 update to v0.4.0 2020-12-17 15:26:56 +08:00
Baozhu Zuo
c64162453a
Merge pull request #20 from IsQianGe/patch-1
Added V2 version of ultrasonic sensor module
2020-12-15 08:18:04 +08:00
IsQianGe
fa4ff3da22
Added V2 version of ultrasonic sensor module 2020-12-14 20:03:00 +08:00
jerryyip
854654aae4 bump to 0.3.0; support Grove UART WiFi V2 2020-10-12 20:55:42 +08:00
8 changed files with 450 additions and 6 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Seeed Studio
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -120,6 +120,31 @@ Use ``||point||`` to open or close point dispay.
Use ``||clear||`` to clean display.
### Grove - UART WiFi V2
Connect to a WiFi and send data to ThinkSpeak or IFTTT, specify the UART tx and rx pin.
```blocks
grove.setupWifi(
SerialPin.P15,
SerialPin.P1,
BaudRate.BaudRate115200,
"test-ssid",
"test-passwd"
)
basic.forever(() => {
if (grove.wifiOK()) {
basic.showIcon(IconNames.Yes)
} else {
basic.showIcon(IconNames.No)
}
grove.sendToThinkSpeak("write_api_key", 1, 2, 3, 4, 5, 6, 7, 8)
grove.sendToIFTTT("ifttt_event", "ifttt_key", "hello", 'micro', 'bit')
basic.pause(60000)
})
```
## License
MIT

View File

@ -20,5 +20,8 @@
"grove.TM1637.set|block": "%strip|[Grove - 4桁ディスプレイ]|明るさを|%level|に変更する",
"grove.TM1637.bit|block": "%strip|[Grove - 4桁ディスプレイ]|%dispData|を|%bitAddr|桁目に表示する",
"grove.TM1637.point|block": "%strip|[Grove - 4桁ディスプレイ]|コロンの表示を|%point|に変更する",
"grove.TM1637.clear|block": "%strip|[Grove - 4桁ディスプレイ]|表示を消す"
}
"grove.TM1637.clear|block": "%strip|[Grove - 4桁ディスプレイ]|表示を消す",
"grove.aht20ReadTemperatureC|block": "[Grove - 温湿度センサー]|温度(℃)を読み取る",
"grove.aht20ReadTemperatureF|block": "[Grove - 温湿度センサー]|温度(℉)を読み取る",
"grove.aht20ReadHumidity|block": "[Grove - 温湿度センサー]|湿度を読み取る"
}

71
blocks/GroveAHT20.ts Normal file
View File

@ -0,0 +1,71 @@
/**
* Grove - AHT20 Custom Block
*/
//% groups=['AHT20']
namespace grove
{
function Read(aht20: grove.sensors.AHT20): { Humidity: number, Temperature: number }
{
if (!aht20.GetState().Calibrated)
{
aht20.Initialization();
if (!aht20.GetState().Calibrated) return null;
}
aht20.TriggerMeasurement();
for (let i = 0; ; ++i)
{
if (!aht20.GetState().Busy) break;
if (i >= 500) return null;
basic.pause(10);
}
return aht20.Read();
}
/**
* Read the temperature(°C) from Grove-AHT20(SKU#101990644)
*/
//% group="AHT20"
//% block="[Grove - Temp&Humi Sensor]|Read the temperature(°C))"
//% weight=3
export function aht20ReadTemperatureC(): number
{
const aht20 = new grove.sensors.AHT20();
const val = Read(aht20);
if (val == null) return null;
return val.Temperature;
}
/**
* Read the temperature(°F) from Grove-AHT20(SKU#101990644)
*/
//% group="AHT20"
//% block="[Grove - Temp&Humi Sensor]|Read the temperature(°F))"
//% weight=2
export function aht20ReadTemperatureF(): number
{
const aht20 = new grove.sensors.AHT20();
const val = Read(aht20);
if (val == null) return null;
return val.Temperature * 9 / 5 + 32;
}
/**
* Read the humidity from Grove-AHT20(SKU#101990644)
*/
//% group="AHT20"
//% block="[Grove - Temp&Humi Sensor]|Read the humidity"
//% weight=1
export function aht20ReadHumidity(): number
{
const aht20 = new grove.sensors.AHT20();
const val = Read(aht20);
if (val == null) return null;
return val.Humidity;
}
}

231
main.ts
View File

@ -88,7 +88,7 @@ enum GroveJoystickKey {
* Functions to operate Grove module.
*/
//% weight=10 color=#9F79EE icon="\uf1b3" block="Grove"
//% groups='["4-Digit","Ultrasonic","Gesture","Thumbjoystick"]'
//% groups='["4-Digit","Ultrasonic","Gesture","Thumbjoystick","UartWiFi"]'
namespace grove {
/**
*
@ -452,7 +452,68 @@ namespace grove {
let joystick = new GroveJoystick();
let paj7620 = new PAJ7620();
// adapted to Calliope mini V2 Core by M.Klein 17.09.2020
/**
* Create a new driver of Grove - Ultrasonic Sensor to measure distances in cm
* @param pin signal pin of ultrasonic ranger module
*/
//% blockId=grove_ultrasonic_centimeters_v2 block="(V2)Ultrasonic Sensor (in cm) at|%pin"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% group="Ultrasonic" pin.defl=DigitalPin.C16
export function measureInCentimetersV2(pin: DigitalPin): number
{
let duration = 0;
let RangeInCentimeters = 0;
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(20);
pins.digitalWritePin(pin, 0);
duration = pins.pulseIn(pin, PulseValue.High, 50000); // Max duration 50 ms
RangeInCentimeters = duration * 153 / 44 / 2 / 100 ;
if(RangeInCentimeters > 0) distanceBackup = RangeInCentimeters;
else RangeInCentimeters = distanceBackup;
basic.pause(50);
return RangeInCentimeters;
}
/**
* Create a new driver Grove - Ultrasonic Sensor to measure distances in inch
* @param pin signal pin of ultrasonic ranger module
*/
//% blockId=grove_ultrasonic_inches_v2 block="(V2)Ultrasonic Sensor (in inch) at|%pin"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false" pin.fieldOptions.width="250"
//% group="Ultrasonic" pin.defl=DigitalPin.C16
export function measureInInchesV2(pin: DigitalPin): number
{
let duration = 0;
let RangeInInches = 0;
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(20);
pins.digitalWritePin(pin, 0);
duration = pins.pulseIn(pin, PulseValue.High, 100000); // Max duration 100 ms
RangeInInches = duration * 153 / 113 / 2 / 100;
if(RangeInInches > 0) distanceBackup = RangeInInches;
else RangeInInches = distanceBackup;
basic.pause(50);
return RangeInInches;
}
/**
* Create a new driver of Grove - Ultrasonic Sensor to measure distances in cm
* @param pin signal pin of ultrasonic ranger module
@ -638,4 +699,170 @@ namespace grove {
})
}
}
let isWifiConnected = false;
/**
* Setup Grove - Uart WiFi V2 to connect to Wi-Fi
*/
//% block="Setup Wifi|TX %txPin|RX %rxPin|Baud rate %baudrate|SSID = %ssid|Password = %passwd"
//% group="UartWiFi"
//% txPin.defl=SerialPin.P15
//% rxPin.defl=SerialPin.P1
//% baudRate.defl=BaudRate.BaudRate115200
export function setupWifi(txPin: SerialPin, rxPin: SerialPin, baudRate: BaudRate, ssid: string, passwd: string) {
let result = 0
isWifiConnected = false
serial.redirect(
txPin,
rxPin,
baudRate
)
sendAtCmd("AT")
result = waitAtResponse("OK", "ERROR", "None", 1000)
sendAtCmd("AT+CWMODE=1")
result = waitAtResponse("OK", "ERROR", "None", 1000)
sendAtCmd(`AT+CWJAP="${ssid}","${passwd}"`)
result = waitAtResponse("WIFI GOT IP", "ERROR", "None", 20000)
if (result == 1) {
isWifiConnected = true
}
}
/**
* Check if Grove - Uart WiFi V2 is connected to Wifi
*/
//% block="Wifi OK?"
//% group="UartWiFi"
export function wifiOK() {
return isWifiConnected
}
/**
* Send data to ThinkSpeak
*/
//% block="Send Data to your ThingSpeak Channel|Write API Key %apiKey|Field1 %field1|Field2 %field2|Field3 %field3|Field4 %field4|Field5 %field5|Field6 %field6|Field7 %field7|Field8 %field8"
//% group="UartWiFi"
//% apiKey.defl="your Write API Key"
export function sendToThinkSpeak(apiKey: string, field1: number, field2: number, field3: number, field4: number, field5: number, field6: number, field7: number, field8: number) {
let result = 0
let retry = 2
// close the previous TCP connection
if (isWifiConnected) {
sendAtCmd("AT+CIPCLOSE")
waitAtResponse("OK", "ERROR", "None", 2000)
}
while (isWifiConnected && retry > 0) {
retry = retry - 1;
// establish TCP connection
sendAtCmd("AT+CIPSTART=\"TCP\",\"iot.rlp.schule\",80")
result = waitAtResponse("OK", "ALREADY CONNECTED", "ERROR", 2000)
if (result == 3) continue
let data = "GET /update?api_key=" + apiKey
if (!isNaN(field1)) data = data + "&field1=" + field1
if (!isNaN(field2)) data = data + "&field2=" + field2
if (!isNaN(field3)) data = data + "&field3=" + field3
if (!isNaN(field4)) data = data + "&field4=" + field4
if (!isNaN(field5)) data = data + "&field5=" + field5
if (!isNaN(field6)) data = data + "&field6=" + field6
if (!isNaN(field7)) data = data + "&field7=" + field7
if (!isNaN(field8)) data = data + "&field8=" + field8
sendAtCmd("AT+CIPSEND=" + (data.length + 2))
result = waitAtResponse(">", "OK", "ERROR", 2000)
if (result == 3) continue
sendAtCmd(data)
result = waitAtResponse("SEND OK", "SEND FAIL", "ERROR", 5000)
// // close the TCP connection
// sendAtCmd("AT+CIPCLOSE")
// waitAtResponse("OK", "ERROR", "None", 2000)
if (result == 1) break
}
}
/**
* Send data to IFTTT
*/
//% block="Send Data to your IFTTT Event|Event %event|Key %key|value1 %value1|value2 %value2|value3 %value3"
//% group="UartWiFi"
//% event.defl="your Event"
//% key.defl="your Key"
//% value1.defl="hello"
//% value2.defl="micro"
//% value3.defl="bit"
export function sendToIFTTT(event: string, key: string, value1: string, value2: string, value3: string) {
let result = 0
let retry = 2
// close the previous TCP connection
if (isWifiConnected) {
sendAtCmd("AT+CIPCLOSE")
waitAtResponse("OK", "ERROR", "None", 2000)
}
while (isWifiConnected && retry > 0) {
retry = retry - 1;
// establish TCP connection
sendAtCmd("AT+CIPSTART=\"TCP\",\"maker.ifttt.com\",80")
result = waitAtResponse("OK", "ALREADY CONNECTED", "ERROR", 2000)
if (result == 3) continue
let data = "GET /trigger/" + event + "/with/key/" + key
data = data + "?value1=" + value1
data = data + "&value2=" + value2
data = data + "&value3=" + value3
data = data + " HTTP/1.1"
data = data + "\u000D\u000A"
data = data + "User-Agent: curl/7.58.0"
data = data + "\u000D\u000A"
data = data + "Host: maker.ifttt.com"
data = data + "\u000D\u000A"
data = data + "Accept: */*"
data = data + "\u000D\u000A"
sendAtCmd("AT+CIPSEND=" + (data.length + 2))
result = waitAtResponse(">", "OK", "ERROR", 2000)
if (result == 3) continue
sendAtCmd(data)
result = waitAtResponse("SEND OK", "SEND FAIL", "ERROR", 5000)
// // close the TCP connection
// sendAtCmd("AT+CIPCLOSE")
// waitAtResponse("OK", "ERROR", "None", 2000)
if (result == 1) break
}
}
function waitAtResponse(target1: string, target2: string, target3: string, timeout: number) {
let buffer = ""
let start = input.runningTime()
while ((input.runningTime() - start) < timeout) {
buffer += serial.readString()
if (buffer.includes(target1)) return 1
if (buffer.includes(target2)) return 2
if (buffer.includes(target3)) return 3
basic.pause(100)
}
return 0
}
function sendAtCmd(cmd: string) {
serial.writeString(cmd + "\u000D\u000A")
}
}

View File

@ -1,6 +1,6 @@
{
"name": "Grove",
"version": "0.2.7",
"version": "0.5.0",
"description": "A Microsoft MakeCode package for Seeed Studio Grove module",
"license": "MIT",
"dependencies": {
@ -9,8 +9,10 @@
"files": [
"README.md",
"main.ts",
"blocks/GroveAHT20.ts",
"_locales/ja/Grove-strings.json",
"_locales/de/Grove-strings.json"
"_locales/de/Grove-strings.json",
"sensors/AHT20.ts"
],
"testFiles": [
"test.ts"

87
sensors/AHT20.ts Normal file
View File

@ -0,0 +1,87 @@
namespace grove
{
export namespace sensors
{
export class AHT20
{
public constructor(address: number = 0x38)
{
this._Address = address;
}
public Initialization(): AHT20
{
const buf = pins.createBuffer(3);
buf[0] = 0xbe;
buf[1] = 0x08;
buf[2] = 0x00;
pins.i2cWriteBuffer(this._Address, buf, false);
basic.pause(10);
return this;
}
public TriggerMeasurement(): AHT20
{
const buf = pins.createBuffer(3);
buf[0] = 0xac;
buf[1] = 0x33;
buf[2] = 0x00;
pins.i2cWriteBuffer(this._Address, buf, false);
basic.pause(80);
return this;
}
public GetState(): { Busy: boolean, Calibrated: boolean }
{
const buf = pins.i2cReadBuffer(this._Address, 1, false);
const busy = buf[0] & 0x80 ? true : false;
const calibrated = buf[0] & 0x08 ? true : false;
return { Busy: busy, Calibrated: calibrated };
}
public Read(): { Humidity: number, Temperature: number }
{
const buf = pins.i2cReadBuffer(this._Address, 7, false);
const crc8 = AHT20.CalcCRC8(buf, 0, 6);
if (buf[6] != crc8) return null;
const humidity = ((buf[1] << 12) + (buf[2] << 4) + (buf[3] >> 4)) * 100 / 1048576;
const temperature = (((buf[3] & 0x0f) << 16) + (buf[4] << 8) + buf[5]) * 200 / 1048576 - 50;
return { Humidity: humidity, Temperature: temperature };
}
private _Address: number;
private static CalcCRC8(buf: Buffer, offset: number, size: number): number
{
let crc8 = 0xff;
for (let i = 0; i < size; ++i)
{
crc8 ^= buf[offset + i];
for (let j = 0; j < 8; ++j)
{
if (crc8 & 0x80)
{
crc8 <<= 1;
crc8 ^= 0x31;
}
else
{
crc8 <<= 1;
}
crc8 &= 0xff;
}
}
return crc8;
}
}
}
}

View File

@ -1,4 +1,12 @@
let _4Digit = grove.createDisplay(DigitalPin.C16, DigitalPin.C17)
grove.setupWifi(
SerialPin.P15,
SerialPin.P1,
BaudRate.BaudRate115200,
"test-ssid",
"test-passwd"
)
basic.forever(function () {
_4Digit.bit(6, 1)
})