最近研究物聯網相關的東西,做了個小原型,稱它為智能管家,能用語音控制設備以及手機APP控制設備。
先看看結構圖:
功能描述:
- 通過語音控制器,說話,比如說出“幫我開燈”,led燈就量,或者說出“把這個燈給關了”,led燈就滅了
- 手機APP上有手動開關來控制led燈的亮和滅
- 支持全國范圍覆蓋,既不能局限於家里的LAN
目前為止,用到的設備主要就是ESPDuino。
ESPDuino:WIFI+Arduino
相關:
- ESP8266-01/ESP-01雖然很便宜但是用起來很麻煩,所以棄用
- Arduino Yun/Tian雖然功能強大,但是太貴700、800的節奏,棄用(不過這2個是帶操作系統的,linino,基於openwrt)
- ESPDuino: 不帶OS,集成WIFI功能,方便還便宜,大概40左右
分別來介紹下,分為:
- OneNet部分
- 手機APP部分
- 語音控制器部分
- ESPDuino執行部分
OneNet部分:
這里我們的主要工作是建立設備,記錄下api_key,產品Id這些,並且我們使用的是MQTT協議進行通信(在產品定義中有)(需要新建產品以及新建設備2個步驟)
手機APP部分
由於不會app開發,並且onenet提供了簡單的app平台,因此就用onenet的來做,如下,新增一個應用后進入設計app界面:
上圖中代表,此時點擊 ON button就會下發指令到設備espduino,並且指令為 開燈 字符串, OFF button則為指令 關燈 字符串。
然后保存,需要在手機上看的話,需要安裝onenet的app,需要自己找找,最終效果截圖:
手機app部分也算完工了,很簡陋,但是能控制就好。
語音控制器部分
這個部分分成了2個部分:語音解析文本及命令解析、命令下發;
語音解析文本及命令解析程序:
用的是訊飛語音api,c代碼,我是用MFC做了個GUI程序,把demo代碼嵌進去,把文本解析成相應的命令,如“開燈”、“關燈”
命令下發程序:
接收MFC發來的命令,比如“開燈”、“關燈”,然后調用onenet的api來下發指令到設備
這部分是用C#做的,因為有很方便的現成的庫,很容易的幾行代碼就搞定
由於MFC比較復雜,這部分的我就截幾段我認為關鍵的代碼吧:
MFC代碼:
static void show_result(char *string, char is_over)
{
COORD orig, current;
CONSOLE_SCREEN_BUFFER_INFO info;
HANDLE w = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(w, &info);
current = info.dwCursorPosition;
if (current.X == last_pos.X && current.Y == last_pos.Y) {
SetConsoleCursorPosition(w, begin_pos);
}
else {
/* changed by other routines, use the new pos as start */
begin_pos = current;
}
if (is_over)
SetConsoleTextAttribute(w, FOREGROUND_GREEN);
//printf("Result: [ %s ]\n", string);
printf(string);
if (is_over)
{
CString s = CString(string);
//AfxMessageBox(LPCTSTR(s));
HWND m_hWnd = pFrame->m_hWnd;//->GetMainWnd()->m_hWnd;
SetDlgItemText(m_hWnd, IDC_STATIC, LPCTSTR(s));
SetDlgItemText(m_hWnd, IDC_STATIC_ACTION, LPCTSTR(s));
CString cmd = CString("C:\\Users\\Danny\\Desktop\\research\\Windows_iat1220_5c82a0db\\samples\\mqtt-publish\\bin\\Debug\\mqtt-publish.exe ");
if(s.Find(_T("開"))>=0&& s.Find(_T("燈")) >= 0)
cmd += "開燈";
else if (s.Find(_T("關")) >= 0 && s.Find(_T("燈")) >= 0)
cmd += "關燈";
else
cmd += "UNKNOWN";
USES_CONVERSION;
LPCSTR lpcstr = (LPCSTR)T2A(cmd);
WinExec(lpcstr, SW_HIDE);
//PostMessage(pFrame->GetMainWnd()->m_hWnd, WM_MY_MESSAGE, NULL, NULL);
}
if (is_over)
SetConsoleTextAttribute(w, info.wAttributes);
GetConsoleScreenBufferInfo(w, &info);
last_pos = info.dwCursorPosition;
}
上面的mqtt-publish.exe就是C#寫的下發指令到設備的程序,核心代碼:
static void Main(string[] args) { if (args.Length == 0) return; string cmd = args[0]; SendCmdRequest request = new SendCmdRequest(); request.CmdContent = cmd; request.DeviceID = 520355898; request.IsByte = true; request.Protocol = Scheme.HTTP; DefaultOneNETClient client = new DefaultOneNETClient("api.heclouds.com", "vYKWEtx7jELTP2V4o=s1NgE1EdA="); var response=client.Execute(request); Console.WriteLine(response.Body); }
C#很簡單,紅色部分是需要根據自己的onenet來改的,需要對應上,用到的nuget庫:
ESPDuino部分
花費的時間最多的地方是這個部分,其次是MFC部分
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <PubSubClient.h> const char *ssid = "你的wifi name"; const char *password = "你的wifi密碼"; const char* mqtt_server = "183.230.40.39"; const char* mqtt_device_id="520355898"; const char* mqtt_product_id="223168"; const char* mqtt_api_key="vYKWEtx7jELTP2V4o=s1NgE1EdA="; WiFiClient client; PubSubClient mqttClient(client); long lastMsg = 0; char msg_buf[200]; char dataTemplete[]="{\"kq\":%d}"; char msgJson[75]; char debug_buf[200]; int i; unsigned short json_len=0; uint8_t* packet_p; uint8_t debug_buffer_start_index = 0; void setup() { Serial.begin(115200); pinMode(BUILTIN_LED, OUTPUT); delay(10); WifiConnected(); initMQTT(); } void initMQTT() { mqttClient.setServer(mqtt_server, 6002); mqttClient.connect(mqtt_device_id,mqtt_product_id,mqtt_api_key); mqttClient.setCallback(callback); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); String s_payload=String((char *)payload).substring(0, length); Serial.println(s_payload); //開燈命令? if (s_payload.equals("關燈")||s_payload.equals("\"關燈\"")) { digitalWrite(BUILTIN_LED, LOW); } else if (s_payload.equals("開燈")||s_payload.equals("\"開燈\"")){ digitalWrite(BUILTIN_LED, HIGH); } } void reconnect() { // Loop until we're reconnected while (!mqttClient.connected()) { Serial.print("Attempting MQTT connection...");
if (mqttClient.connect(mqtt_device_id,mqtt_product_id,mqtt_api_key)) { //One net user name as product ID , and password as APIKey Serial.println("connected"); // Once connected, publish an announcement... mqttClient.publish("outTopic", "hello world"); // ... and resubscribe mqttClient.subscribe("inTopic"); } else { Serial.print("failed, rc="); Serial.print(mqttClient.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void loop() { if (!mqttClient.connected()) { reconnect(); } mqttClient.loop(); long now = millis(); if (now - lastMsg > 2000) { lastMsg = now; int kqValue=analogRead(A0); snprintf(msgJson,sizeof(msgJson),dataTemplete,kqValue); json_len=strlen(msgJson); //packet length count the end char '\0' msg_buf[0]=char(0x03); //palyLoad packet byte 1, one_net mqtt Publish packet payload byte 1, type3 , json type2 msg_buf[1]=char(json_len>>8); //high 8 bits of json_len (16bits as short int type) msg_buf[2]=char(json_len&0xff); //low 8 bits of json_len (16bits as short int type) // snprintf(msg_buf+3,40,dataTemplete,value); memcpy(msg_buf+3,msgJson,strlen(msgJson)); msg_buf[3+strlen(msgJson)] = 0; Serial.print("Publish message: "); Serial.println(msgJson); mqttClient.publish("$dp",(uint8_t*)msg_buf,3+strlen(msgJson),false); } } void WifiConnected() { WiFi.disconnect(); WiFi.mode(WIFI_STA); Serial.println(); Serial.print("Connecting to"); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_DISCONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WIFI connected"); Serial.println(WiFi.localIP()); }
至此,可以語音控制和手機控制了。
后續擴展命令會較容易,可以move到C#部分來做,如果覺得語音轉文本有不准的地方,目前的想法是通過神經網絡來做識別,而不是通過目前的這種很挫的判斷辦法
OK,完工。
有要代碼的就找我發給你。