步驟:
1. 下載庫文件:
燒寫程序之前,我們需要用到ESP8266庫,下載地址為:
https://github.com/itead/ITEADLIB-ESP8266
溫馨提示:ESP8266 wifi庫文件是基于arduino IDE 1.0.5版本編譯的,如遇編譯不過,請嘗試其他版本。
打開庫中的uartWIFI.h文件,因為我們的mini RBoard使用的是跟UNO一樣的芯片,所以要在H文件里去掉定義UNO的注釋,注釋掉MEGA。
#define UNO
//#define MEGA
然后把_DBG_RXPIN_和_DBG_TXPIN_引腳定義改為D9和D10.
#define _DBG_RXPIN_ 9 //A0
#define _DBG_TXPIN_ 10 //A1
然后保存uartWIFI.h文件。
燒寫代碼:
插上FOCA,板子的類型選擇“Arduino UNO”,點擊燒寫程序。
代碼如下:
#define SSID "Itead_1(Public)"//type your own SSID name
#define PASSWORD "27955416"0//type your own WIFI passwor
#include "uartWIFI.h"
#include "SoftwareSerial.h"
WIFI wifi
extern int chlID;//client id(0-4)
void setup()
{
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
wifi.begin();
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if(!b)
{
DebugSerial.println("Init error");
}
delay(8000); //make sure the module can have enough time to get an IP address
String ipstring = wifi.showIP();
DebugSerial.println(ipstring);//show the ip address of module
delay(2000);
wifi.confMux(1);
delay(100);
if(wifi.confServer(1,8080))
DebugSerial.println("Server is set up");
}
void loop()
{
char buf[100];
int iLen = wifi.ReceiveMessage(buf);
if(iLen > 0)
{
//mini Rboard的繼電器控制腳是D4
if (strcmp(buf, "CH0ON") == 0)
{
digitalWrite(4,HIGH);
DebugSerial.println("CH0ON");
}
if (strcmp(buf, "CH0OFF") == 0)
{
digitalWrite(4,LOW);
DebugSerial.println("CH0OFF");
}
/* 以下多余的3個通道是4通道RBoard的繼電器引腳
if (strcmp(buf, "CH1ON") == 0)
{
digitalWrite(5,HIGH);
DebugSerial.println("CH1ON");
}
if (strcmp(buf, "CH1OFF") == 0)
{
digitalWrite(5,LOW);
DebugSerial.println("CH1OFF");
}
if (strcmp(buf, "CH2ON") == 0)
{
digitalWrite(6,HIGH);
DebugSerial.println("CH2ON");
}
if (strcmp(buf, "CH2OFF") == 0)
{
digitalWrite(6,LOW);
DebugSerial.println("CH2OFF");
}
if (strcmp(buf, "CH3ON") == 0)
{
digitalWrite(7,HIGH);
DebugSerial.println("CH3ON");
}
if (strcmp(buf, "CH3OFF") == 0)
{
digitalWrite(7,LOW);
DebugSerial.println("CH3OFF");
}
*/
}
}
黏貼代碼到arduino IDE中,在SSID和PASSWORD宏定義中修改成自己的路由器WIFI名稱和密碼。
#define SSID "Itead_1(Public)" //type your own SSID name
#define PASSWORD "27955416" //type your own WIFI password
編譯,燒寫,OK!
接下來我們還需要用到foca來讀取串口信息,查看wifi狀態(tài)。
按照以下連接方式連接FOCA和ESP8266到mini Rboard上。
|