Skip to content

Commit

Permalink
fixes for makecode V6
Browse files Browse the repository at this point in the history
  • Loading branch information
MKleinSB committed Jan 14, 2024
1 parent 2fd053d commit 753f922
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 66 deletions.
151 changes: 86 additions & 65 deletions dht11.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,105 @@
// Originalpaket von
// https://github.com/lioujj/pxt-DHT11
// angepasst an Calliope mini M. Klein 13.09.2020
// MIT License
/**

Check failure on line 1 in dht11.ts

View workflow job for this annotation

GitHub Actions / build (8.x)

Property 'C16' does not exist on type 'typeof DigitalPin'.
* MakeCode editor extension for DHT11 humidity/temperature sensor
* by Alan Wang https://github.com/alankrantas/pxt-dht11_dht22
* Changes by M. Klein 13 & 14.1.2024
*/


//% color=#2159b2 icon="\uf2c9" block="DHT11"
namespace DHT11 {
let pin = 9; // C16
let init = false;
function dht11Request(): void {
pins.digitalWritePin(pin, 0)
basic.pause(18)
let i = pins.digitalReadPin(pin)
pins.setPull(pin, PinPullMode.PullUp);

}

let _temperature: number = -999.0
let _humidity: number = -999.0
let _readSuccessful: boolean = false
let _sensorresponding: boolean = false
let dht11Pin: DigitalPin = DigitalPin.C16
let dht11startTime: number = 0
let dht11WaitTime: number = 2000

//% blockId=setPin block="DHT11 at pin %myPin"
//% myPin.defl=DigitalPin.C16
//% myPin.defl=DigitalPin.C16 weight=100
//% myPin.fieldEditor="gridpicker" myPin.fieldOptions.columns=4
//% myPin.fieldOptions.tooltips="false" myPin.fieldOptions.width="300"
export function setPin(myPin: DigitalPin): void {
pin = myPin;
init = true;
dht11Pin = myPin;
}

/**
* Query data from DHT11 sensor.
*/
export function queryData(dataPin: DigitalPin) {

//% blockId=temperature block="temperature in ˚C"
export function temperature(): number {
if (init) {
dht11Request();
while (pins.digitalReadPin(pin) == 1);
while (pins.digitalReadPin(pin) == 0);
while (pins.digitalReadPin(pin) == 1);
let value = 0;
let counter = 0;

for (let i = 0; i <= 32 - 1; i++) {
while (pins.digitalReadPin(pin) == 0);
counter = 0
while (pins.digitalReadPin(pin) == 1) {
counter += 1;
}
if (i > 15) {
if (counter > 2) {
value = value + (1 << (31 - i));
}
}
//initialize
let checksum: number = 0
let checksumTmp: number = 0
let dataArray: boolean[] = []
let resultArray: number[] = []

for (let index = 0; index < 40; index++) dataArray.push(false)
for (let index = 0; index < 5; index++) resultArray.push(0)

_humidity = -999.0
_temperature = -999.0

//request data
pins.digitalWritePin(dataPin, 0) //begin protocol, pull down pin
basic.pause(18)

pins.setPull(dataPin, PinPullMode.PullUp) //pull up data pin
pins.digitalReadPin(dataPin) //pull up pin
control.waitMicros(40)

if (pins.digitalReadPin(dataPin) == 1) {
serial.writeLine("dht11 not responding")
} else {
_sensorresponding = true

while (pins.digitalReadPin(dataPin) == 0); //sensor response
while (pins.digitalReadPin(dataPin) == 1); //sensor response

//read data (5 bytes)
for (let index = 0; index < 40; index++) {
while (pins.digitalReadPin(dataPin) == 1);
while (pins.digitalReadPin(dataPin) == 0);
control.waitMicros(28)
//if sensor still pull up data pin after 28 us it means 1, otherwise 0
if (pins.digitalReadPin(dataPin) == 1) dataArray[index] = true
}
return ((value & 0x0000ff00) >> 8);

//convert byte number array to integer
for (let index = 0; index < 5; index++)
for (let index2 = 0; index2 < 8; index2++)
if (dataArray[8 * index + index2]) resultArray[index] += 2 ** (7 - index2)

//verify checksum
checksumTmp = resultArray[0] + resultArray[1] + resultArray[2] + resultArray[3]
checksum = resultArray[4]
if (checksumTmp >= 512) checksumTmp -= 512
if (checksumTmp >= 256) checksumTmp -= 256
if (checksum == checksumTmp) _readSuccessful = true

//read data if checksum ok
if (_readSuccessful) {

_humidity = resultArray[0] + resultArray[1] / 100
_temperature = Math.round(resultArray[2] + resultArray[3] / 100)
} else
serial.writeLine("checksumerror")
}
else
return 0;
dht11startTime = input.runningTime()
}

//% blockId=humidity block="humidity in percent"
export function humidity(): number {
if (init) {
dht11Request();

while (pins.digitalReadPin(pin) == 1);
while (pins.digitalReadPin(pin) == 0);
while (pins.digitalReadPin(pin) == 1);

let value = 0;
let counter = 0;

for (let i = 0; i <= 8 - 1; i++) {
while (pins.digitalReadPin(pin) == 0);
counter = 0
while (pins.digitalReadPin(pin) == 1) {
counter += 1;
}
if (counter > 3) {
value = value + (1 << (7 - i));
}
}
return value;
}
else
return 0;
if (dht11WaitTime > (input.runningTime() - dht11startTime)) { basic.pause(dht11WaitTime - (input.runningTime() - dht11startTime)) }
queryData(dht11Pin);
return _humidity
}

//% blockId=temperature block="temperature in ˚C"
export function temperature(): number {
if (dht11WaitTime > (input.runningTime() - dht11startTime)) { basic.pause(dht11WaitTime - (input.runningTime() - dht11startTime)) }
queryData(dht11Pin);
return _temperature
}
} // Gib deinen Code hier ein
}
7 changes: 6 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
],
"testFiles": [],
"public": true,
"targetVersions": {
"target": "6.0.27",
"targetId": "calliopemini"
},
"supportedTargets": [
"calliopemini",
"microbit"
],
"preferredEditor": "tsprj"
}
}

0 comments on commit 753f922

Please sign in to comment.