本篇分享一个编写简单的完整鸿蒙小项目的流程。
以本文制作一个智能水表的demo为例:
你需要先学会:
- 点亮一个Led灯(没错只需要会点灯的北向代码即可,非常简单,本文也以此为模板制作)
声明:本文编译sdk版本为api7,IDE环境 3.0.0.800(3.0Beta2),语言基于为 JSUI
成果展示
小项目全部部署在基于openharmony的小熊派上,效果如图:

图片仅供演示,系统在设备中实际运行良好
一、确定功能
确定实现功能如下:
- 获取传感器数据(水流量传感器)并实时渲染在屏幕上
- 可以对水阀进行控制(发送控制指令)
- 自动(手动)上报数据,发送给服务器/上位机以供后续处理
- 其他(如统计数据,计算费用等…)
所有功能实现的代码都将贴在文章末尾,以供编译参考
二、添加南向接口并调用
注意:南向部分需要自己实现,本项目内容可以参考 基于 OpenHarmony 的水流量监测系统
其本质依然是gpio操作和一些基础知识组合,会点led后就可以放心大胆的上啦~
依照物联网项目的基本架构:端管云用,我们北向也可以类比依照这四点实现功能设计(不涉及后端)
- 端:即感知识别层,用于信息生成
- 管:信息的传输,用于信息传输,具体为调用通信接口与上位机通信
- 云:信息处理
- 用:信息应用:如微信小程序设计
我们参考Led点灯的接口,在 @system.app.d.ts 末尾添加如下接口声明:
没错,就是led灯的接口连名字都不带改的,我们利用回调函数返回的对象,在res对象里添加传感器数据:值 作为data内容,发送的命令码,即为对水阀的控制开关指令
在处理页面逻辑的文件上,我们也要添加主动调用接口的方法:
三、添加通信接口(可选)
如有需要可以添加网络请求接粗体口,在api7(及以前)可以用@system.fetch,aip6后推荐使用@ohos.net.http (fetch不在维护,建议弃用)
如果报错,尝试在配置文件config.json里修改网络权限
默认在module模块下:
轻量级穿戴设备似乎不支持网络通信接口,最后弃用改为南向上传(此处代码仅供参考)
提醒:由于需要传输的是南向部分传输过来的传感器数据,所以建议也在南向部分处理数据上传,以减小时延和精度误差等
四、其他
上位机(这里以微信小程序示例)主要负责远程监控与管理,设计如下:
控制面板页面设计 |
统计页面设计 |
 |
 |
页面仅代表功能演示,不代表实际数据
项目源代码:
var led = {open:1,close:0,change:2}
import app from '@system.app';
import router from "@system.router";
export default {
data: {
title: '当日累计:',
statu:'0',
rate: 0,
rate_L:0,
sec:0,
min:0,
info:"正常",
tmp_rate:-1,
curr_rate:0,
code1:0,
code2:"关闭",
code3:"未连接",
timer:0,
time:0,
},
onInit(){
this.startTimer()
this.info="初始化.."
},
exit(e){
console.log("terminate!")
app.terminate()
},
startTimer() {
this.time= setInterval(()=>{
this.runtime()
},1000);
},
runtime(){
this.sec++
if(this.sec===60){
this.sec=0
this.min++
}
},
getRate(){
let that=this
try{
app.ledcontrol({
code:led.open,
success(res){
that.tmp_rate=that.rate
that.rate = (Number(JSON.stringify(res.led_status)))
that.curr_rate=that.rate-that.tmp_rate
that.code1=that.curr_rate
},
fail(res,code){
console.log("get fail")
},
complete(){
}
})
}catch(e){
console.log(err)
this.device_id="err"
}
},
openDoor(e){
console.log("open")
this.info="运行中"
this.code2="开启"
function closeapp()
{
console.log("close")
}
const ShowRate = ()=>
{
this.time++
this.device_id++;
let that=this
app.ledcontrol({
code:led.open,
success(res){
that.rate = (Number(JSON.stringify(res.led_status)))
},
fail(res,code){
},
complete(){
}
})
}
this.rate=-1;
try{
this.timer= setInterval(function(){ShowRate()},100);
}catch(e){
console.error("err"+e)
this.info="计时器故障"
}
},
close(e){
console.log("close timer")
this.info="关闭"
this.code2="关闭"
try{
clearInterval(this.timer)
}catch{
this.info="故障"
}
let that = this
app.ledcontrol({
code:led.close,
success(res){
that.statu = res.led_status
},
fail(res,code){
},
complete(){
}
})
},
datalist(){
},
senddata(){
try{
fetch.fetch({
url: 'http://127.0.0.1'+'?data='+this.rate,
responseType: 'json',
success: res => {
this.code3="已连接"
let data = JSON.parse(res.data);
console.log(res.data)
}
});
console.log("手动上报数据")
}
catch(e){
console.log(e);
this.code3="连接失败"
}
},
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
实现智能家居的一小步
不错不错,学有所成了
打通南北不错的项目
节约用水是需要一直做下去的事业
从点灯开始 —> 学会亿点知识 —> 成为大佬
设计开关水阀的目的是啥呢?
问的很好,这属于一开始画的大饼(bushi)但是没完全实现的内容:水阀可以做到精准控水,和远程控水两个功能;
对应实际生活中,水表可以检测某水龙头/水管漏水的情况,然后自动/远程关闭。
另外就是在储水时可以不必等待,比如我习惯打水的时候顺便上个洗手间,但又怕水打满溢出了,有了这个我可以控制“装五百毫升”就关水 这一操作,