【ESP8266系列】如何使用OCROBOT IDE用arduino语法来玩ESP8266
本帖最后由 迷你强 于 2016-8-31 15:48 编辑如何使用OCROBOT IDE用arduino语法来玩ESP8266ESP8266可是一个好玩的芯片,高性能,据说有160Mhz主频,wifi通讯,还有一定数量的IO口,当然传统的AT操作方式或者SDK的开发环境很多爱好者还是头疼的紧不过,好在开源界的大神多,ESP8266-Arduino 项目:https://github.com/esp8266/Arduino ,就将其兼容性做的出神入化,本系列教程就主要介绍如何用arduino的语法标准来玩ESP8266下面正式开始,我们将使用的硬件是OCROBOT ALPHA esp8266控制板,这个是我们的最新产品,暂时还买不到,您可以使用其他的ESP8266硬件,只要是标准硬件都没啥问题esp8266硬件区分下载模式和运行模式,一般硬件厂商使用跳线或者上下拉的方式实现,我们的硬件上采用拨动开关,如图拨上去就处于下载模式了然后我们来介绍下我们的IDE:IDE的下载:http://downloads.ocrobot.com/OCROBOT_0006_rc3_windows.zip前往下载测试版本的IDE即可,打开如下图首先我们要进行ESP8266的环境支持,你如果使用的是标准ide可以前往上面的github查阅安装方法。
[*]首先点击工具- 开发板:XXXXXX-开发板管理器
[*]开发板管理器里选择ESP8266选项卡,并点击安装
[*]然后就会自动进行环境的安装,等待一段时间,视网络及计算机情况,可能需要几分钟到十几分钟,耐心等待下面的进度条消失
[*]完成后点击关闭,然后再回到开发板菜单下,你会发现ESP8266板卡就出来了。
[*]我们选择 OCROBOT ESP8266 Module的板卡选项,介绍下子选项的含义其实很简单啦,需要调整的其实就是处理器的速度和端口号。。。。。
我们来测试下是否正常使用了,将模块设置为下载模式然后,尝试个闪灯
void setup() {
pinMode(D2, OUTPUT);
}
void loop() {
digitalWrite(D2, HIGH);
delay(1000);
digitalWrite(D2, LOW);
delay(1000);
}这样LED就闪起来了,熟悉的小伙伴应该会发现,和arduino的标准玩法没有任何不同嘛~,其实只有细微的差别,为了兼容管脚
所有的管脚前面要加上D字修饰一下,理论上绝大部分的使用都没有什么区别的。我们再来演示个串口通讯的程序试试(记得重新下载程序的时候按下res键让芯片复位,重新进入下载模式)void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("hello OCROBOT~!");
delay(1000);
}
下载完成打开串口监视器看下:对,这个开源解决方案能让ESP8266近乎于完美的兼容了arduino代码与IDE,我们将在以后的内容中介绍 I2C SPI总线与这个板子的核心功能WIFI,欢迎关注
本帖最后由 迷你强 于 2016-8-31 15:46 编辑
虽然我对这个系列有所保留,不过还是支持下
不知道wemos的代码能不能兼容呢
#include <ESP8266WiFi.h>
const char* ssid = "xxxxxxx";
const char* password = "yyyyyyyy";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
这段代码的功能是连上一个指定的wifi,当获取得到IP后从串口输出,然后开一个80端口的web服务
如果用浏览器访问http://server_ip/gpio/0 端口GPIO2就low电平
如果用浏览器访问http://server_ip/gpio/1 端口GPIO2就high电平
可以在GPIO2接个led测试效果
有机会的话,试一试哦 :lol 最近也有在做这个项目,arduino IDE调戏ESP8266兼容性蛮好,其实esp8266画龙点睛之一还有个SmartConfig功能,配合AirKiss可以无缝配置WIFI 如果结合微信公众平台的硬件API 可玩性蛮大 升级最新板固件即可支持 贴出这部分arduino代码供大家参考:
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
digitalWrite(LED, 0);
delay(500);
digitalWrite(LED, 1);
delay(500);
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.println(WiFi.SSID().c_str());
Serial.println(WiFi.psk().c_str());
break;
}
}
}
页:
[1]