本文實(shí)現(xiàn)Arduino UNO R3通過(guò)ESP8266-01模塊連接機(jī)智云平臺(tái),從而實(shí)現(xiàn)用手機(jī)APP遠(yuǎn)程點(diǎn)亮LED燈。
ESP8266模塊本地調(diào)試
1
當(dāng)我們參考機(jī)智云官網(wǎng)給的教程時(shí),關(guān)于Arduino平臺(tái)主要都是關(guān)于機(jī)智云官方自己的開(kāi)發(fā)板Gokit的資料。但當(dāng)我們只使用Arduino UNO R3接入機(jī)智云平臺(tái),有很多教程通過(guò)添加了兩個(gè)按鍵來(lái)選擇airlink和softap模式,其實(shí)我們只要在本地給ESP8266配好了網(wǎng)絡(luò)連接,就不需要每次通過(guò)按鍵切換配網(wǎng)模式。
第一步:首先在官網(wǎng)下載機(jī)智云固件GAgent,
第二步:使用ESP8266下載工具ESPFlashDownloadTool,將固件下載至ESP8266中。
第三步:在機(jī)智云官網(wǎng)下載機(jī)智云串口調(diào)試助手,
將ESP8266通過(guò)串口轉(zhuǎn)USB工具插在電腦上,安裝好驅(qū)動(dòng),選擇中確的驅(qū)動(dòng)后,打開(kāi)串口,填入在官網(wǎng)申請(qǐng)的product_key和Product Secret后,就能在右邊看見(jiàn)數(shù)據(jù)點(diǎn)了,此時(shí)再打開(kāi)指令界面,點(diǎn)擊AirLink命令,ESP8266就進(jìn)入了自動(dòng)配網(wǎng)模式;
第四步:下載機(jī)智云移動(dòng)端APP
打開(kāi)APP,點(diǎn)擊一鍵配置,輸入家里的WiFi賬號(hào)及密碼,選擇樂(lè)鑫,因?yàn)閯偛盼覀円呀?jīng)將ESP8266設(shè)置成AirLink透?jìng)髂J?,現(xiàn)在通過(guò)APP就可以將家里的WiFi信息發(fā)送給ESP8266,此時(shí)APP發(fā)現(xiàn)新設(shè)備下會(huì)出現(xiàn)你的WiFi模組,點(diǎn)擊后就會(huì)看到你定義的數(shù)據(jù)點(diǎn)了。
硬件搭建
2
2.1硬件連接圖
LED接上220歐姆電阻,連在Arduino的D4上,ESP8266CH-PD和vcc都接高電平。
注意:
在給Arduino燒錄程序的時(shí)候要斷開(kāi)ESP8266與Arduino的串口連接,否則會(huì)有干擾導(dǎo)致代碼燒錄不成功;
ESP8266-01只能供給3.3v電壓,5v電壓可能會(huì)燒壞WiFi模組
2.2項(xiàng)目源碼
#include
#include
#include
int ledPin = 13; // LED connected to digital pin 13,it's used for status
int myledPin = 4; // my LED connected to digital pin 4
int sensorValue = 0; // value read from the pot
bool varR_LED_OnOff = 0;
Gizwits 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
unsignedlongLast_KeyTime= 0;
unsignedlong gokit_time_s(void)
{
return millis() / 1000;
}
char gokit_key1down(void)
{
unsignedlong 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;
}
return0;
}
return0;
}
return0;
}
char gokit_key2down(void)
{
intunsignedlong keep_time = 0;
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;
}
return0;
}
return0;
}
return0;
}
/*******************************************************
* function : gokit_keydown
* Description : check the gokit key1 or key2 event
* return : KEY1_LONG_PRESS KEY1_SHORT_PRESS
* KEY2_LONG_PRESS KEY2_SHORT_PRESS
* 0-no keydown event.
* Add by Alex.lin --2014-12-25
******************************************************/
char gokit_keydown(void)
{
char ret = 0;
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:
// Serial.println(F("KEY1_SHORT_PRESS , Production Test Mode "));
myGizwits.setBindMode(WIFI_PRODUCTION_TEST);
break;
case KEY1_LONG_PRESS:
// Serial.println(F("KEY1_LONG_PRESS ,Wifi Reset"));
myGizwits.setBindMode(WIFI_RESET_MODE);
break;
case KEY2_SHORT_PRESS:
// Serial.println(F("KEY2_SHORT_PRESS Soft AP mode"));
myGizwits.setBindMode(WIFI_SOFTAP_MODE);
//Soft AP mode
break;
case KEY2_LONG_PRESS:
// Serial.println(F("KEY2_LONG_PRESS ,AirLink mode"));
digitalWrite(ledPin, HIGH); // sets the LED on
myGizwits.setBindMode(WIFI_AIRLINK_MODE);
//AirLink mode
break;
default:
break;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(myledPin, OUTPUT); // sets the digital pin as output
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
myGizwits.begin();
}
void wifiStatusHandle()
{
// if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP))
// Serial.println(F("WIFI_SOFTAP!"));
//
// if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK))
// Serial.println(F("WIFI_AIRLINK!"));
//
// if(myGizwits.wifiHasBeenSet(WIFI_STATION))
// Serial.println(F("WIFI_STATION!"));
//
if(myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER))
digitalWrite(ledPin, LOW); // sets the LED off
// Serial.println(F("WIFI_CON_ROUTER!"));
//
// if(myGizwits.wifiHasBeenSet(WIFI_DISCON_ROUTER))
// Serial.println(F("WIFI_DISCON_ROUTER!"));
//
// if(myGizwits.wifiHasBeenSet(WIFI_CON_M2M))
// Serial.println(F("WIFI_CON_M2M!"));
//
// if(myGizwits.wifiHasBeenSet(WIFI_DISCON_M2M))
// Serial.println(F("WIFI_DISCON_M2M!"));
}
void loop() {
//Configure network
//if(XXX) //Trigger Condition
//myGizwits.setBindMode(0x02); //0x01:Enter AP Mode;0x02:Enter Airlink Mode
/*
unsigned long varW_ADCValue = 0;//Add Sensor Data Collection
myGizwits.write(VALUE_ADCVALUE, varW_ADCValue);
bool varR_LED_OnOff = 0;
if(myGizwits.hasBeenSet(EVENT_LED_ONOFF))
{
myGizwits.read(EVENT_LED_ONOFF,&varR_LED_OnOff);//Address for storing data
}
//binary datapoint handle
*/
sensorValue = analogRead(A0);
unsignedlong varW_ADCValue = sensorValue;//Add Sensor Data Collection
myGizwits.write(VALUE_ADCVALUE, varW_ADCValue);
if(myGizwits.hasBeenSet(EVENT_led_onoff))
{
myGizwits.read(EVENT_led_onoff,&varR_LED_OnOff);//Address for storing data
}
if(varR_LED_OnOff==1)
{
digitalWrite(myledPin, HIGH); // sets the user LED on
}
else
{
digitalWrite(myledPin, LOW); // sets the user LED off
}
KEY_Handle();//key handle , network configure
wifiStatusHandle();//WIFI Status Handle
myGizwits.process();
}
至此,你的ESP8266模組就成功連接上機(jī)智云服務(wù)平臺(tái)了,無(wú)論你在任何地方,無(wú)論用WiFi和4G都能通過(guò)APP來(lái)控制LED等的亮滅了,機(jī)智云還提供了APP的開(kāi)源框架,我們可以在此基礎(chǔ)上高度自由地定制我們自己的APP接入機(jī)智云物聯(lián)網(wǎng)平臺(tái)。
原文標(biāo)題:機(jī)友分享 | Arduino使用ESP8266連接機(jī)智云物聯(lián)網(wǎng)平臺(tái)
文章出處:【微信公眾號(hào):機(jī)智云開(kāi)發(fā)者】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
責(zé)任編輯:haq
-
物聯(lián)網(wǎng)
+關(guān)注
關(guān)注
2902文章
44115瀏覽量
370413 -
機(jī)智云
+關(guān)注
關(guān)注
2文章
573瀏覽量
26362
原文標(biāo)題:機(jī)友分享 | Arduino使用ESP8266連接機(jī)智云物聯(lián)網(wǎng)平臺(tái)
文章出處:【微信號(hào):IoTMaker,微信公眾號(hào):機(jī)智云開(kāi)發(fā)者】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論