前言:本文主要介紹如何在機(jī)智云平臺(tái)創(chuàng)建一個(gè)智能產(chǎn)品、定義功能數(shù)據(jù)點(diǎn)、生成MCU代碼包并附上修改好的代碼、生成APP代碼編譯附上參考代碼。
需準(zhǔn)備的硬件主要有Arduino uno R3、ESP8266
連線方式:A0讀取模擬電壓輸入;Key1接到D6;Key2接到D7;LED接到D4;
一、創(chuàng)建項(xiàng)目
1.1 創(chuàng)建新產(chǎn)品
登陸機(jī)智云AIoT開發(fā)平臺(tái) dev.gizwits.com 創(chuàng)建智能產(chǎn)品
1.2 進(jìn)入項(xiàng)目可查看信息
什么是Product Key、Product Secret
Product Key又稱產(chǎn)品標(biāo)識(shí)碼,開發(fā)者通過機(jī)智云后臺(tái)創(chuàng)建新產(chǎn)品后,自動(dòng)生成的一個(gè)32位字符串。在機(jī)智云的數(shù)據(jù)庫(kù)中是一個(gè)唯一的號(hào)碼,開發(fā)者將ProductKey寫入設(shè)備主控MCU后,機(jī)智云通過此標(biāo)識(shí)碼對(duì)設(shè)備進(jìn)行識(shí)別并自動(dòng)完成注冊(cè)。
設(shè)備接入機(jī)智云的前提是,需要機(jī)智云認(rèn)同這個(gè)設(shè)備。Productkey是設(shè)備接入機(jī)智云的一個(gè)重要參數(shù),該參數(shù)的基本含義是:一款設(shè)備識(shí)別碼。例如某公司生產(chǎn)空調(diào)、熱水器,空調(diào)和熱水器是不同款設(shè)備,該公司設(shè)備接入機(jī)智云,至少需要兩個(gè)Productkey參數(shù)。在開發(fā)MCU過程中,需要使用到該參數(shù)。
http://docs.gizwits.com/zh-cn/quickstart/noun.html
二、添加數(shù)據(jù)點(diǎn)
根據(jù)系統(tǒng)的需求添加數(shù)據(jù)點(diǎn),可參考本系統(tǒng)數(shù)據(jù)點(diǎn)
什么是數(shù)據(jù)點(diǎn)?
數(shù)據(jù)點(diǎn)即設(shè)備產(chǎn)品的功能的抽象,用于描述產(chǎn)品功能及其參數(shù)。創(chuàng)建數(shù)據(jù)點(diǎn)后,設(shè)備與云端通訊的數(shù)據(jù)格式即可確定,設(shè)備、機(jī)智云可以相互識(shí)別設(shè)備與機(jī)智云互聯(lián)互通的數(shù)據(jù)。
參考:http://docs.gizwits.com/zh-cn/quickstart/UseMCU.html
三、進(jìn)行虛擬設(shè)備調(diào)試
3.1 創(chuàng)建好數(shù)據(jù)點(diǎn)后,進(jìn)行虛擬設(shè)備調(diào)試
什么是虛擬設(shè)備?類似一款仿真器,能夠模擬硬件設(shè)備。
虛擬設(shè)備是機(jī)智云云端可自動(dòng)生成的一個(gè)仿真智能硬件,可模擬要開發(fā)或正在開發(fā)的智能硬件,來進(jìn)行云端設(shè)備控制、手機(jī)APP控制、上報(bào)數(shù)據(jù)等需求。
開發(fā)者或企業(yè)研發(fā)人員在機(jī)智云平臺(tái)開發(fā)時(shí),如果正式的智能硬件產(chǎn)品產(chǎn)品還未完全開發(fā)完成,就可以很方便地使用該“虛擬設(shè)備”與正式產(chǎn)品的控制APP進(jìn)行調(diào)試和驗(yàn)證,加快研發(fā)周期,降低研發(fā)成本。
https://baijiahao.baidu.com/s?id=1725343397578131479&wfr=spider&for=pc
3.2 在機(jī)智云APP能夠觀察機(jī)智云平臺(tái)模擬的數(shù)據(jù)
根據(jù)系統(tǒng)的需要添加或修改數(shù)據(jù)點(diǎn)信息
四、下載MCU代碼包
4.1 在確定數(shù)據(jù)點(diǎn)無誤后,點(diǎn)擊MCU開發(fā)
4.2 生成代碼包后,把代碼包導(dǎo)入Arduino IDE開發(fā)環(huán)境的libraries文件中,libraries是Arduino的庫(kù)文件,包含自帶的庫(kù)文件和用戶添加的外部庫(kù)文件
把networkConfig.ino的配網(wǎng)代碼移植到simpleTry.ino,把串口打印的代碼注釋起來,可參考資料分享:史上最詳細(xì)的Arduino uno R3接入機(jī)智云教程
https://club.gizwits.com/thread-6728-1-1.html
然后根據(jù)要求修改代碼,以下僅供參考:
#include
#include
#include
#include
int con_ledPin = 13; //成功連接網(wǎng)絡(luò),13引腳低電平
int smokePin = 2;
int movePin = 3;
int alarmPin = 4;
int ledPin = 5;
int relayPin1 = 8;
int relayPin2 = 9;
int firePin = A0;
int lightPin = A1;
int beepPin = 10;
int temhumPin = 11;
bool alarmflag = 0;
dht myDHT_11;
SoftwareSerial mySerial(A2, A3); // A2 -> RX, A3 -> TXGizwits myGizwits;
#define KEY1 6
#define KEY2 7
#define KEY1_SHORT_PRESS 1
#define KEY1_LONG_PRESS 2
#define KEY2_SHORT_PRESS 4
#define KEY2_LONG_PRESS 8
#define NO_KEY 0
#define KEY_LONG_TIMER 3
#define ALARM_SHORT_PRESS 16
#define ALARM_LONG_PRESS 32
unsigned long Last_KeyTime = 0;
unsigned long gokit_time_s(void)
{
return millis() / 1000;
}
char gokit_key1down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY1) == LOW)
{
delay(100);
if (digitalRead(KEY1) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(KEY1) == LOW)
{
if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
{
Last_KeyTime = gokit_time_s();
return KEY1_LONG_PRESS;
}
} //until open the key
if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
{
return KEY1_SHORT_PRESS;
}
return 0;
}
return 0;
}
return 0;
}
char gokit_key2down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY2) == LOW)
{
delay(100);
if (digitalRead(KEY2) == LOW)
{
delay(100);
if (digitalRead(KEY2) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(KEY2) == LOW) //until open the key
{
if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
{
Last_KeyTime = gokit_time_s();
return KEY2_LONG_PRESS;
}
}
if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
{
return KEY2_SHORT_PRESS;
}
return 0;
}
return 0;
}
return 0;
}
//室內(nèi)報(bào)警監(jiān)測(cè)
char gokit_alarmdown(void)
{
unsigned long keep_time = 0;
if (digitalRead(alarmPin) == LOW)
{
delay(100);
if (digitalRead(alarmPin) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(alarmPin) == LOW) //until open the key
{
if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
{
Last_KeyTime = gokit_time_s();
return ALARM_LONG_PRESS;
}
}
if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
{
return ALARM_SHORT_PRESS;
}
return 0;
}
return 0;
}
return 0;
}
char gokit_keydown(void)
{
char ret = 0;
ret |= gokit_alarmdown();
ret |= gokit_key2down();
ret |= gokit_key1down();
return ret;
}
void KEY_Handle(void)
{
/* Press for over than 3 second is Long Press */
switch (gokit_keydown())
{
case KEY1_SHORT_PRESS:
myGizwits.setBindMode(WIFI_PRODUCTION_TEST);
break;
case KEY1_LONG_PRESS:
myGizwits.setBindMode(WIFI_RESET_MODE);
break;
case KEY2_SHORT_PRESS:
myGizwits.setBindMode(WIFI_SOFTAP_MODE);
//Soft AP mode
break;
case KEY2_LONG_PRESS:
myGizwits.setBindMode(WIFI_AIRLINK_MODE);
//AirLink mode
break;
case ALARM_SHORT_PRESS:
alarmflag = ((alarmflag == 0) ? 1 : 0);
if (alarmflag)
{
digitalWrite(beepPin, LOW);
}
else
{
digitalWrite(beepPin, HIGH);
}
break;
case ALARM_LONG_PRESS:
digitalWrite(ledPin, HIGH); //設(shè)置為高電平
digitalWrite(relayPin1, HIGH); //設(shè)置為高電平
digitalWrite(relayPin2, HIGH); //設(shè)置為高電平
break;
default:
break;
}
}
//獲取溫度數(shù)據(jù)int dht_11_gettemperature() {
int chk = myDHT_11.read11(temhumPin);
int value = myDHT_11.temperature;
return value;
}
//獲取濕度數(shù)據(jù)int dht_11_gethumidity() {
int chk = myDHT_11.read11(temhumPin);
int value = myDHT_11.humidity;
return value;
}
/**
Serial Init , Gizwits Init
@param none
@return none
*/
void setup() {
// put your setup code here, to run once:
mySerial.begin(115200);
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
pinMode(smokePin, INPUT); //設(shè)置為數(shù)字輸入模式
pinMode(movePin, INPUT);
pinMode(alarmPin, INPUT_PULLUP);
pinMode(firePin, INPUT);
pinMode(lightPin, INPUT); //模擬量為輸入模式
pinMode(beepPin, OUTPUT); //設(shè)置為數(shù)字輸出模式
pinMode(ledPin, OUTPUT);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(con_ledPin, OUTPUT);
digitalWrite(beepPin, HIGH); //設(shè)置為高電平
myGizwits.begin();
}
void wifiStatusHandle()
{
if (myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER))
{
digitalWrite(con_ledPin, HIGH); //設(shè)置為低電平
digitalWrite(ledPin, HIGH); //設(shè)置為高電平
digitalWrite(relayPin1, HIGH); //設(shè)置為高電平
digitalWrite(relayPin2, HIGH); //設(shè)置為高電平
}
}
/**
Arduino loop
@param none
@return none
*/
void loop() {
//Configure network
//if(XXX) //Trigger Condition
//myGizwits.setBindMode(0x02); //0x01:Enter AP Mode;0x02:Enter Airlink Mode
bool varW_Alarm = alarmflag;//Add Sensor Data Collection
myGizwits.write(VALUE_Alarm, varW_Alarm);
unsigned long varW_fire = analogRead(firePin);//Add Sensor Data Collection
myGizwits.write(VALUE_fire, varW_fire);
unsigned long varW_Move_Test = digitalRead(movePin);//Add Sensor Data Collection
myGizwits.write(VALUE_Move_Test, varW_Move_Test);
unsigned long varW_Tem = dht_11_gettemperature();//Add Sensor Data Collection
myGizwits.write(VALUE_Tem, varW_Tem);
unsigned long varW_Hum = dht_11_gethumidity();//Add Sensor Data Collection
myGizwits.write(VALUE_Hum, varW_Hum);
unsigned long varW_Light = analogRead(lightPin);//Add Sensor Data Collection
myGizwits.write(VALUE_Light, varW_Light);
bool varR_LED_OnOff = 0;
if (myGizwits.hasBeenSet(EVENT_LED_OnOff))
{
myGizwits.read(EVENT_LED_OnOff, &varR_LED_OnOff); //Address for storing data
if (varR_LED_OnOff == 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
bool varR_Relay_1 = 0;
if (myGizwits.hasBeenSet(EVENT_Relay_1))
{
myGizwits.read(EVENT_Relay_1, &varR_Relay_1); //Address for storing data
if (varR_Relay_1 == 0)
{
digitalWrite(relayPin1, HIGH);
}
else
{
digitalWrite(relayPin1, LOW);
}
}
bool varR_Relay_2 = 0;
if (myGizwits.hasBeenSet(EVENT_Relay_2))
{
myGizwits.read(EVENT_Relay_2, &varR_Relay_2); //Address for storing data
if (varR_Relay_2 == 0)
{
digitalWrite(relayPin2, HIGH);
}
else
{
digitalWrite(relayPin2, LOW);
}
}
bool varR_beep = 0;
if (myGizwits.hasBeenSet(EVENT_beep))
{
myGizwits.read(EVENT_beep, &varR_beep); //Address for storing data
if (varR_beep == 0 )
{
digitalWrite(beepPin, HIGH);
}
else
{
digitalWrite(beepPin, LOW);
}
}
KEY_Handle();
wifiStatusHandle();
myGizwits.process();
}
五、下載Android開發(fā)代碼包
5.1 在應(yīng)用開發(fā)生成APP代碼包
通過Android Studio軟件導(dǎo)入項(xiàng)目進(jìn)行編譯,因版本問題需要修改關(guān)鍵詞,compile 均替換為implementation,testcompile均替換為testimplementation,可參考以下代碼:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '28.0.2'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.gizwits.opensource.appkit"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "2.5.1.072715"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint1.1.0'
testImplementation 'junit4.12'
implementation 'com.android.support24.2.1'
implementation files('libs/BLEasyConfig.jar')
implementation files('libs/easylink2.0.jar')
implementation files('libs/GizWifiSDK.jar')
implementation files('libs/jpush-android-2.1.5.jar')
implementation files('libs/Lark7618SDK_noudp1606031910_0101.jar')
implementation files('libs/libammsdk.jar')
implementation files('libs/libForEElink.jar')
implementation files('libs/LSFSK_1.0.jar')
implementation files('libs/ltlink2.jar')
implementation files('libs/mta-sdk-1.6.2.jar')
implementation files('libs/OneShotConfig.jar')
implementation files('libs/open_sdk_r5756.jar')
implementation files('libs/pushservice-5.6.0.30.jar')
implementation files('libs/simpleconfigwizardlib.jar')
implementation files('libs/xUtils-2.6.14.jar')
implementation files('libs/zxing.jar')
implementation 'com.android.support25.3.0'
implementation 'com.yanzhenjie1.0.5'
}
同時(shí)可參考基于機(jī)智云AIoT平臺(tái)的Android開源app修改教程
https://www.sohu.com/a/476083681_486728
使項(xiàng)目成功編譯
原文轉(zhuǎn)自:https://blog.csdn.net/a6662580/article/details/122558080
-
機(jī)智云
+關(guān)注
關(guān)注
2文章
582瀏覽量
26369
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論