What is DeviceScript?If you are familiar with or have used MicroPython, you would know that MicroPython is a lightweight version of the Python programming language specifically designed for embedded systems and microcontrollers.DeviceScript, initiated by Microsoft, is a TypeScript-based language for hardware development. It is a scripting language suited for embedded devices and IoT applications, offering features such as simplicity, cross-platform support, device management, network communication, and scalability.DeviceScript brings a TypeScript developer experience to resource-constrained devices based on microcontrollers. DeviceScript code is compiled into custom VM bytecode that can run under highly constrained conditions.If you are a TypeScript developer looking to venture into hardware, you don't want to miss out on DeviceScript!
Project IntroductionPreviously, our projects focused on the utilization of input sensors such as buttons and knobs in DeviceScript, which are commonly encountered in keyboard applications. However, today we delve into a new sensor called EnvSensor, capable of detecting temperature and humidity in the environment. By harnessing real-world physical measurements, we can introduce intriguing forms of feedback.
Hardware ConnectionKB Nanoscript 2040 x 1KB EnvSensorx 1Jacdac cable x 1
Launch Visual Studio CodeChoose DeviceScript.
The software automatically detects two devices: the KB Brain RP2040 and one RotaryButton. (If you still can't establish a connection at this point, please refer to the configuration tutorial for DeviceScript provided in the initial article.)
ProgramingOpen the "main.ts" file in the DeviceScript environment.
First, import the Temperature and Humidity modules. Then, write a simple program to retrieve the temperature and humidity values and print them using console.log.
You can refer to this section for specific usage:
Temperature | DeviceScript (microsoft.github.io)
Humidity | DeviceScript (microsoft.github.io)
The specific code is as follows:
import { Temperature,Humidity} from "@devicescript/core"
const temperature = new Temperature()
const humidity = new Humidity()
temperature.reading.subscribe(async (value) => {
console.log("temperature=", value)
})
humidity.reading.subscribe(async (value) => {
console.log("humidity=", value)
})
Click to run
Experimental FindingsBy using the concise code provided above, one can obtain the numerical values of temperature and humidity in the environment. With the aid of some value-based judgments, one can create intriguing gameplay. We eagerly anticipate everyone's contributions and sharing!
Additional tutorials are available for further guidanceThis tutorial aims to swiftly familiarize you with DeviceScript.For further study materials, you can refer to the following sources:DeviceScript | DeviceScript (microsoft.github.ioThe TypeScript syntax documentation for Jacdac can be found at:Jacdac TypeScript - v1.33.1 (microsoft.github.io)eviceScript配置教程)The software automatically detects two devices: the KB Brain RP2040 and one RotaryButton. (If you still can't establish a connection at this point, please refer to the configuration tutorial for DeviceScript provided in the initial article.)
Comments
Please log in or sign up to comment.