步骤:
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状态。
按照以下连接方式连接FOCA和ESP8266到mini Rboard上。
|