本帖最后由 simonliu009 于 2017-3-18 12:24 编辑
ESP8266的固件,我使用了Arduino IDE来进行编程和烧写,在Arduino IDE安装ESP8266的库以后就能直接用Arduino IDE对ESP8266进行编程了,非常方便。
下面是ESP8266的源码,复制和传播请保留版权信息,谢谢。
/*21ic 第二届拆解+DIY大赛
作者:simonliu009@21ic
QQ:150739525
复制和传播请保留版权信息,谢谢。
*/
#include <FS.h>
#include <ESP8266WiFi.h>
#include <EthernetUdp.h>
#include <DNSServer.h>
#include <SPI.h>
// #include <Ethernet.h>
#define relay1 13
unsigned int localPort = 8267;
char packetBuffer[10];
const char *ssid = "Asus";//这里改为你要连接的wifi ssid
const char *password = "esp20170317";//你要连接的wifi密码
WiFiUDP Udp;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial1.println();
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial1.print(".");
}//如果没有连通向串口发送.....
Serial1.println("Wifi Connected :)");
Serial1.println("Local Ip Address");
Serial1.println(WiFi.localIP());
Udp.begin(localPort); //开始UDP监听
Serial1.print("UDP端口");
Serial1.print(localPort);
Serial1.println("监听开始......");
}
void loop()
{
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize)
{
for (int i = 0; i<=9; i++){
packetBuffer[i] = 'Z';
}
Udp.read(packetBuffer, 10); // 将UDP包读入缓冲区
if(packetBuffer[0]=='L'&&packetBuffer[1]=='A'&&packetBuffer[2]=='M'&&packetBuffer[3]=='P'&&packetBuffer[4]=='O'&&packetBuffer[5]=='N')
{
// delay(parseInt(packetBuffer[]) * 1000);
digitalWrite(relay1, HIGH);
}
if(packetBuffer[0]=='L'&&packetBuffer[1]=='A'&&packetBuffer[2]=='M'&&packetBuffer[3]=='P'&&packetBuffer[4]=='O'&&packetBuffer[5]=='F'&&packetBuffer[6]=='F')
{
digitalWrite(relay1, LOW);
}
}
}
|