17611538698
webmaster@21cto.com

一百元的智能家居——Asp.Net Mvc Api+讯飞语音+Android+Arduino

资讯 0 16497 2017-04-27 12:01:23
大半夜的,先说些废话提提神

     如今智能家居已经不再停留在概念阶段,高大上的科技公司都已经推出了自己的部分或全套的智能家居解决方案,不过就目前的现状而言,大多还停留在展厅阶段,还没有广泛的推广起来,有人说最大的问题是标准不统一云云,但在我看来,最大的问题在于两个方面,一个是价格,一个是操作的简便性,技术上的问题并不是阻碍智能家居推广的核心因素。

     再来说说最近很火很惹人爱的微软小娜,Cortana。本人作为微软的死忠,作为一名靠.Net混饭的屌丝程序男,自然是有一部撸妹的,并且在小娜推送当天更新了手机,迫不及待地体验了小娜的功能,小娜会记事、会提醒、会唱歌、会说笑话、会学林志玲等等着实让我惊叹了一把,而且语气非常接近自然人的语气,不是那种生涩的机器语调。作为平时生活中不使用Android,Iphone的我,那晚想当然的觉得小娜应该就是当今世界上最好的语音助手了吧,怀着这样的心情,让小娜给我定了个闹钟,第二天早早起床迫不及待要向同事得瑟一把了。

    第二天给同事小马看后,小马淡然一笑,拿出他的Android手机,默默的打开手机上的一个叫“灵犀”的软件,递给我说:“试试这个”。不试不知道,一试,心情还有点小低落,优越感顿无,怎么说呢,应该说除了在语气上小娜占上风外,在识准率,识别速度,各类功能应用上,小娜和“灵犀”已然不是一个档次,可能是小娜刚起步吧,希望以后可以不断完善。

    看了一下“灵犀”官网,恍然大悟,这就是科大讯飞的产品啊,再次把玩灵犀,就只想真心点赞了,当时脑子里还飘过一个画面,未来能够对百度造成威胁的,很有可能就是科大讯飞,科大讯飞将把持移动互联网的使用入口!哦,那画面太美,不知李彦宏怎么想这个问题。

    语音识别不仅仅会影响到人们使用手机的方式,在智能家居方面也大有可为,最直接的就是带来操作上的便捷性,试想,如果你在家时,你慢悠悠的拿出你的手机,慢悠悠的打开你的操作应用,慢悠悠的点击一个控制按钮,然后你的电视或者调动窗帘才执行你的命令,这图的个什么啊,还不如直接走过去手动暴力解决,而如果你在家的时候,只需要说“小文,换到体育频道”,或者“小文,打开窗帘”,然后安放在屋子里的中控盒子二十四小时随时听候理解你的命令,出门在外的时候,也只需对着手机说一声就行,这才叫易用的智能家居啊!

    今天我们就采用Arduino开发板+Asp.Net MVC Api+Android版的讯飞语言SDK,实现用语音来控制一个小Led的实验。

关于Arduino   

     我的硬件准备

     


     左边是Arduino主板,右边是网络拓展板,下边的是什么?哈哈,实验室小孟同学亲手给我焊接的小小Led灯,用来模仿开关性质的设备,这么小,看着都是泪水啊。组合后就是这么个样子了

   


   以上总造价不超过100元。

    Ardunio是一个开源的开发板,据说国外都是小朋友和艺术家都可以玩的开发板,简单易学,功能完善,目前也是非常火爆。事实也确实如此,看了几个示例工程后,有一定开发基础的人一看就知道怎么玩了。大部分web开发者基本上可以直接上手Arduino的网络部分的开发,看一遍示例保证你明白怎么个搞法,不信?那上个截图吧,这是Ardunio提供的IDE,内置了很多常用的开发场景示例,基本上在示例的基础上简单修改就可以完成一些简单的任务。




 如果你是一个web开发者,好吧,看到菜单你也应该联想到你熟悉的网站开发模式了吧,是的,Arduino为你提供了客户端模式,服务端模式,还有些不常用的其他模式,让我们来看看Ardunio的WebClientRepeatping,这是一个客户端轮询服务端的方式,简单粗暴,通信稳定,容易理解和上手,下面是Ardunio提供的该模式示例代码





1 /*
2 Repeating Web client
3
4 This sketch connects to a a web server and makes a request
5 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6 the Adafruit Ethernet shield, either one will work, as long as it's got
7 a Wiznet Ethernet module on board.
8
9 This example uses DNS, by assigning the Ethernet client with a MAC address,
10 IP address, and DNS address.
11
12 Circuit:
13 * Ethernet shield attached to pins 10, 11, 12, 13
14
15 created 19 Apr 2012
16 by Tom Igoe
17 modified 21 Jan 2014
18 by Federico Vanzati
19
20 http://arduino.cc/en/Tutorial/WebClientRepeating
21 This code is in the public domain.
22
23 */
24
25 #include <SPI.h>
26 #include <Ethernet.h>
27
28 // assign a MAC address for the ethernet controller.
29 // fill in your address here:
30 byte mac[] = {
31 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
32 };
33 // fill in an available IP address on your network here,
34 // for manual configuration:
35 IPAddress ip(192, 168, 1, 177);
36
37 // fill in your Domain Name Server address here:
38 IPAddress myDns(1, 1, 1, 1);
39
40 // initialize the library instance:
41 EthernetClient client;
42
43 char server[] = "www.arduino.cc";
44 //IPAddress server(64,131,82,241);
45
46 unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
47 const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
48 // the "L" is needed to use long type numbers
49
50 void setup() {
51 // start serial port:
52 Serial.begin(9600);
53 while (!Serial) {
54 ; // wait for serial port to connect. Needed for Leonardo only
55 }
56
57 // give the ethernet module time to boot up:
58 delay(1000);
59 // start the Ethernet connection using a fixed IP address and DNS server:
60 Ethernet.begin(mac, ip, myDns);
61 // print the Ethernet board/shield's IP address:
62 Serial.print("My IP address: ");
63 Serial.println(Ethernet.localIP());
64 }
65
66 void loop() {
67 // if there's incoming data from the net connection.
68 // send it out the serial port. This is for debugging
69 // purposes only:
70 if (client.available()) {
71 char c = client.read();
72 Serial.write(c);
73 }
74
75 // if ten seconds have passed since your last connection,
76 // then connect again and send data:
77 if (millis() - lastConnectionTime > postingInterval) {
78 httpRequest();
79 }
80
81 }
82
83 // this method makes a HTTP connection to the server:
84 void httpRequest() {
85 // close any connection before send a new request.
86 // This will free the socket on the WiFi shield
87 client.stop();
88
89 // if there's a successful connection:
90 if (client.connect(server, 80)) {
91 Serial.println("connecting...");
92 // send the HTTP PUT request:
93 client.println("GET /latest.txt HTTP/1.1");
94 client.println("Host: www.arduino.cc");
95 client.println("User-Agent: arduino-ethernet");
96 client.println("Connection: close");
97 client.println();
98
99 // note the time that the connection was made:
100 lastConnectionTime = millis();
101 }
102 else {
103 // if you couldn't make a connection:
104 Serial.println("connection failed");
105 }
106 }

View Code
请从头到尾看一变,我保证如果你有编程基础看一遍也就知道的差不多该怎么玩了。无非就是定时轮询,请求服务器,对返回的字符串进行处理。下面的代码就是我在上面代码的基础上做的修改,实现了定时从服务器获取命令,如果是1则打开Led,如果是2则关闭Led。





1 /*
2 Repeating Web client
3
4 This sketch connects to a a web server and makes a request
5 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6 the Adafruit Ethernet shield, either one will work, as long as it's got
7 a Wiznet Ethernet module on board.
8
9 This example uses DNS, by assigning the Ethernet client with a MAC address,
10 IP address, and DNS address.
11
12 Circuit:
13 * Ethernet shield attached to pins 10, 11, 12, 13
14
15 created 19 Apr 2012
16 by Tom Igoe
17 modified 21 Jan 2014
18 by Federico Vanzati
19
20 http://arduino.cc/en/Tutorial/WebClientRepeating
21 This code is in the public domain.
22
23 */
24
25 #include <SPI.h>
26 #include <Ethernet.h>
27 const int ledPin = 2;
28 // assign a MAC address for the ethernet controller.
29 // fill in your address here:
30 byte mac[] = {
31 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF
32 };
33 // fill in an available IP address on your network here,
34 // for manual configuration:
35 IPAddress ip(192, 168, 1, 177);
36
37 EthernetClient client;
38
39 IPAddress server(你的服务器地址);
40
41 unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
42 const unsigned long postingInterval = 2L * 1000L; // delay between updates, in milliseconds
43 // the "L" is needed to use long type numbers
44
45 void setup() {
46 // start serial port:
47 Serial.begin(9600);
48 while (!Serial) {
49 ; // wait for serial port to connect. Needed for Leonardo only
50 }
51 // give the ethernet module time to boot up:
52 delay(1000);
53 pinMode(ledPin, OUTPUT);
54 // start the Ethernet connection using a fixed IP address and DNS server:
55 Ethernet.begin(mac);
56 // print the Ethernet board/shield's IP address:
57 Serial.print("My IP address: ");
58 Serial.println(Ethernet.localIP());
59
60 }
61 int iscoming=0;
62
63 void loop() {
64 // if there's incoming data from the net connection.
65 // send it out the serial port. This is for debugging
66 // purposes only:
67 if (client.available()) {
68 char c = client.read();
69 if(iscoming==1){
70 handle(c);
71 iscoming=0;
72 }
73 if(c=='@'){
74 iscoming=1;
75 }
76 Serial.write(c);
77 }
78
79 // if ten seconds have passed since your last connection,
80 // then connect again and send data:
81 if (millis() - lastConnectionTime > postingInterval) {
82 httpRequest();
83 }
84
85 }
86
87 void handle(char value){
88 if(value=='1'){
89 Serial.print("dakai");
90 digitalWrite(ledPin, LOW);
91 }
92 if(value=='2'){
93 Serial.print("guanbi");
94 digitalWrite(ledPin, HIGH);
95 }
96 }
97 // this method makes a HTTP connection to the server:
98 void httpRequest() {
99 // close any connection before send a new request.
100 // This will free the socket on the WiFi shield
101 client.stop();
102 // if there's a successful connection:
103 delay(1000);
104 if (client.connect(server, 8009)) {
105 Serial.println("connecting...");
106 // send the HTTP PUT request:
107 //api/getcmd是请求的网站资源
108 client.println("GET /api/getcmd HTTP/1.1");
109 client.println("Host: www.arduino.cc");
110 client.println("User-Agent: arduino-ethernet");
111 client.println("Connection: close");
112 client.println();
113 // note the time that the connection was made:
114 lastConnectionTime = millis();
115 }
116 else {
117 // if you couldn't make a connection:
118 Serial.println("connection failed");
119
120 }
121 }

View Code
Android客户端

关于科大讯飞的介绍,自己去官网看吧,深耕语音领域二十年,不是一般企业可以超越的,很好很强大。申请成为开发者,创建应用,开通服务,下载SDK(我下的是带UI的SDK),参考SDK的代码就可以开始自己的开发了。话不多说,直接上核心代码

 





1 /**
2 * 听写UI监听器
3 */
4 private RecognizerDialogListener recognizerDialogListener=new RecognizerDialogListener(){
5 public void onResult(RecognizerResult results, boolean isLast) {
6 String text = JsonParser.parseIatResult(results.getResultString());
7 mResultText.append(text);
8 mResultText.setSelection(mResultText.length());
9 sendmeg(text);
10 }
11
12 private void sendmeg(String msg){
13 AsyncHttpClient client = new AsyncHttpClient();
14 try {
15 String url = "http://服务器地址/api/values/"+msg;
16 client.get(url, new AsyncHttpResponseHandler() {
17 @Override
18 public void onSuccess(int statusCode, Header[] headers,
19 byte[] responseBody) {
20 super.onSuccess(statusCode, headers, responseBody);
21 Toast.makeText(CmdActivity.this,
22 "请求成功" + new String(responseBody), Toast.LENGTH_SHORT).show();
23 }
24
25 @Override
26 public void onFailure(int statusCode, Header[] headers,
27 byte[] responseBody, Throwable error) {
28 // TODO Auto-generated method stub
29 super.onFailure(statusCode, headers, responseBody, error);
30 }
31 });
32 } catch (Exception e) {
33 }
34
35 } /**

View Code
 

Asp.Net Mvc Api

服务端的代码相对就简单了,我这里创建了两个控制器,一个负责接收手机端的命令,存入数据库,一个负责返回Arduino的网络请求,将命令代码返回。虽然觉得没必要,还是上一下代码吧

接收手机端传过来的命令





1 // GET api/values/5
2 public string Get(string id)
3 {
4 ardunioEntities a = new ardunioEntities();
5 string tempid = "123456789";
6 cmd c = a.cmd.Where<cmd>(n => n.ardunioid ==tempid).SingleOrDefault();
7 if (c == null)
8 {
9 c = new cmd();
10 c.ardunioid = tempid;
11 c.command = "0";
12 c.status = "未读";
13 a.cmd.Add(c);
14 }
15 if (id.Contains("打开"))
16 {
17 {
18 c.command = "1";
19 c.status = "未读";
20
21 }
22 }
23 if (id.Contains("关闭"))
24 {
25 {
26 c.command = "2";
27 c.status = "未读";
28 }
29 }
30 a.SaveChanges();
31 return "接收成功!";
32
33 }

View Code
返回命令给Arduino的请求





1 public string Get()
2 {
3
4 ardunioEntities a = new ardunioEntities();
5 string tempid = "123456789";
6 cmd c = a.cmd.Where<cmd>(n => n.ardunioid == tempid & n.status == "未读").SingleOrDefault();
7 if (c == null)
8 {
9 return "@0";
10 }
11 c.status = "已读";
12 a.Entry(c).State = EntityState.Modified;
13 a.SaveChanges();
14 return "@"+c.command;
15 }

View Code
效果实现与总结

    对着手机说“打开灯泡”,然后灯就亮了!话说程序员看到这一幕,兴奋之情溢于言表啊。(请忽略旁边的美桃,女友由于对我崇拜的五体投地给我洗的,嘻嘻)。

    这篇文章只是一个小实验而已,后续还可以做很多有趣的玩法,比如在这个程序的基础上开发语音控制电动窗帘的的工作(目前正在玩耍...),比如直接弄一个Android开发板或者使用语音模块做一个中控,二十四小时伺服,回到家直接说话“小文,换台到体育频道”等等等等,现有的智能家居产品都可以把语音技术集成过来,相信会大大增加智能家居的易用性。

    本月科大讯飞即将召开智能家庭产品发布会,我们实验室也申请了票,期待看到高大上的东东开开眼。。。。一场革命或许已经来临。

    看完了就默默点个赞吧:)





 

评论