显示标签为“ESP8266”的博文。显示所有博文
显示标签为“ESP8266”的博文。显示所有博文

2017年5月27日星期六

使用arduino开发esp8266的初始配置

一. 下载arduino安装包
https://downloads.arduino.cc/arduino-1.8.2-linux64.tar.xz, 下载后一定用sha512sum校验软件包是否完整, 建议用下载软件下载, 在web中下载太痛苦了.
二. 安装
将软件包解压,并拷贝至相应目录
sudo mv  arduino-1.8.2 /opt/
我的环境是mint 18.1 kde, 安装有点问题, 需要修改install.sh
将install.sh内的下列语句修改为
原:RESOURCE_NAME=cc.arduion.arduinoide
改:RESOURCE_NAME=arduion-arduinoide
运行即可生成菜单
三. 配置
打开File-Preferences, 这里可以配置代理, 修改语言等, 在板管理URL中加入下列地址, 点击OK
http://arduino.esp8266.com/stable/package_esp8266com_index.json
打开Tools-Board-Board manager, 拉至最后, 安装esp8266
四. 端口配置
检测是否发现设备
lsmod | grep usbserial, lsusb
我的环境自动安装了驱动, 比较省事, 端口是ttyUSB0
查看ttyUSB0的权限, 属于dialout, 将用户加入此组
sudo minicom -s
配置好端口与速率






2017年3月16日星期四

ESP8266 arduino库的一些学习

http://esp8266.github.io/Arduino/versions/2.3.0/doc/reference.html

  1. Digital IO 兼容arduino pinMode, digitalRead, digitalWrite. 状态可为INPUT, OUTPUT, INPUT_PULLUP. 16脚可以设置为为INPUT, OUTPUT, INPUT_PULLDOWN. 启动时所有的引脚默认配置是INPUT.
  2.  GPIO6-11脚不能使用, 原因是因为模块内部, 引脚连接到了SPI Flash芯片上,  如果是QIO模式,会使用6-11脚, DIO模式, 可以使用9和11. 
  3. 支持attachInterrupt, detachInterrupt引脚中断,除了16脚, 中断可以使用所有的其它引脚,中断类型支持CHANGE, RISING, FALLING.
  4. Analog input, 可读入模块电源电压, 或ADC电压, 电压范围是0-1.0V, 如果使用ESP.getVcc()读取VCC电压,则ADC脚不能有接线, 另外ADC_MODE(ADC_VCC);要写在程序中, 最好是在函数外, #include后边.
  5. Analog output analogWrite(pin, value), 用来将引脚做为PWM输出, 数值可为0-PWMRANGE, 0可关断PWM, 默认PWMRANGE=1023, 可以使用analogWriteRange(new_range)改变.
  6. Timing and delays millis(), micros(), 分别显示reset后的毫秒和微秒. delay(ms)暂停程序但wifi和tcpip仍然工作, delayMicroseconds(us)暂停程序微秒. 当wifi连接后, 有大量的代码需要在程序后台运行, wifi和tcp/ip库在每次loop()完成后, 有机会控制一些挂起的事件或运行delay(),  如果你的loop()程序没有调用delay()而使用的时间超过了50ms, 你可能需要增加调用delay()来保证wifi堆栈工作正常. 这里有个yield()等于delay(0), delayMicroseconds(),如果超过20毫秒不推荐使用.
  7. Serial 使用与arduino相同, 使用gpio1(tx), gpio3(rx). 也可以调用Serial.begin()后使用Serial.swap重新映射到gpio15(tx), gpio13(rx),再次调用Serial.swap后恢复gpio1与gpio3. Serial1 使用gpio(tx), 但是无法接收数据, 因为RX脚被用于闪存芯片连接,可能使用用Serial1.begin()调用. 如果serial1没有使用并且serial没有交换, 在调用Serial.begin后,调用Serial.set_tx(2)可以将gpio映射为UART0的tx, 或直接调用Serial.begin(baud, config, mode,2). 默认当你调用Serial.begin后, wifi库的诊断输出是关闭的. 为了重新打开debug输出, 调用Serial.setDebugOutput(true), 如果用serial1输出debug, 调用Serial1.setDebugOut(true). 你仍然需要使用Serial.setDebugOutput(true)来打开printf()功能. Serial与Serial1支持5,6,7,8数据位, odd(o), even(E), no parity(N), 1或2停止位, 你可以使用Serial.begin(baudrate, SERIAL_8N1), Serial.begin(baudrate, Serial_6E2)等来设置你所希望的串口方式. Serial[1].baudRate(), 可以获取端口速率
  8. Progmem 程序中的字符都会占用内存空间, 最好的办法是使用宏与FPSTR(),这里有一个举例
String response1; 
response1 += F("http:"); 
... 
String response2; 
response2 += F("http:");

const char HTTP[] PROGMEM = "http:"; 
... 
{ String response1; 
 response1 += FPSTR(HTTP); 
 ... String response2; 
 response2 += FPSTR(HTTP); 
}

http://esp8266.github.io/Arduino/versions/2.3.0/doc/libraries.html


2017年3月15日星期三

ESP8266读取多个传感器

https://arduinodiy.wordpress.com/2016/10/11/using-the-4-pins-of-the-esp8266-01/
这是一个使用受限的引脚数来接入多个器件的例子,主人使用的是ESP8266-01,
1 URXD GPIO3 UART_RXD
2 CH_PD
3 RESET GPIO16*
4 VCC
5 GND
6 GPIO2*
7 GPIO0* wifi状态工作模式
8 UTXD GPIO1 UART_TXD

有GPIO2,GPIO15,GPIO0(Wifi状态灯及工作模式),GPIO16(REST),GPIO1(UART_TXD),GPIO3(UART_RXD)
使用2个引脚来做I2C,用于BMP180和OLED
使用1个引脚通过OneWire协议读取2个DS18B20,
使用1个引脚使用DHT11

引脚定义如下:
GPIO0->I2C SDA
GPIO2->I2C SCL
GPIO1->DHT11
GPIO3->OneWire Bus

主人使用单总线读取DS18B20,需要知道器件的地址,这是读取地址的链接:
https://arduino-info.wikispaces.com/Brick-Temperature-DS18B20#Test%20Sketch%20to%20read%20DS18B20%20addresses

文章中提到如果DHT11读取数据出现"failed to read",可能是DHT.cpp 156-158
// End the start signal by setting data line high for 40 microseconds.
    digitalWrite(_pin, HIGH);
    delayMicroseconds(40);
要把它注释掉
------------------------------------------------
/*
   Field 1 temp roost (DHT11)
   Field 2 humidity roost (DHT11)
   field 3 Coop temperature (DS18B20)
   field 4 soil temperature (DS18B20)
   field 5 Airpressure (bmp180)
   field 6 Outside temperature (bmp180)
 * */
#include <DHT.h>
#include  <OneWire.h>//  http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <DallasTemperature.h> //   http://milesburton.com/Main_Page?title=Dallas_Tem...
#include <Adafruit_BMP085.h>
#include <ESP8266WiFi.h>
#include "SSD1306.h"
SSD1306  display(0x3c, 0, 2);

#define DHTPIN 1  //GPIO1 (Tx)
#define DHTTYPE  DHT11
#define ONE_WIRE_BUS 3 // GPIO3=Rx

const char* ssid     = "YourSSID";
const char* password = "YourPassword";
const char* host = "api.thingspeak.com";
const char* writeAPIKey = "W367812985"; //use YOUR writeApi

//DHT11 stuff
float temperature_buiten;
float temperature_buiten2;
DHT dht(DHTPIN, DHTTYPE, 15);

//DS18b20 stuff
OneWire oneWire(ONE_WIRE_BUS); //oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire);// Pass address of our oneWire instance to Dallas Temperature.
DeviceAddress Probe01 = { 0x28, 0x0F, 0x2A, 0x28, 0x00, 0x00, 0x80, 0x9F};
DeviceAddress Probe02 = { 0x28, 0x10, 0xA4, 0x57, 0x04, 0x00, 0x00, 0xA9};

// bmp180 stuff
Adafruit_BMP085 bmp;

void setup() {
  //I2C stuff
  Wire.pins(0, 2);
  Wire.begin(0, 2);
  // Initialize sensors
  //dht 11 stuff
  dht.begin();
  //ds18b20 stuff
  sensors.begin();//ds18b20
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  //bmp180 stuff
  if (!bmp.begin()) {
    //   Serial.println("No BMP180 / BMP085");
    //   while (1) {}
  }

  //OLED stuff
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  delay(1000);

  //  Connect to WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {
  //ds18b20stuff-------------------
  sensors.requestTemperatures(); // Send the command to get temperatures
  temperature_buiten = sensors.getTempC(Probe01);//
  temperature_buiten2 = sensors.getTempC(Probe02);//
  //dht11 stuff--------------------
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  if (isnan(humidity) || isnan(temperature)) {
    return;
  }
  //bmp stuff-------------------------
    String t= String(bmp.readTemperature());
    String p=String(bmp.readPressure());
  //OLED stuff--------------------------
  display.clear();
  display.drawString(0,10,p);//bmp pressure
  display.drawString(0,24,String(temperature_buiten));//ds18b20
  display.drawString(0,38,String(humidity));//dht11
  display.display();

  // make TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    return;
  }

  String url = "/update?key=";
  url += writeAPIKey;
  url += "&field1=";
  url += String(temperature);// roost (DHT1)
  url += "&field2=";
  url += String(humidity);// roost (DHT11)
  url += "&field3=";
  url += String(temperature_buiten);//coop temperature (DS18B20 nr 1)
  url += "&field4=";
  url += String(temperature_buiten2); //soil temperature (DS18B29 nr 2)
  url +="&field5=";
  url +=String(bmp.readTemperature());Outside temperature (BMP180)
  url +="&field6=";
  url +=String(bmp.readPressure());// Airpressure (BMP180)
  url += "\r\n";

  // Send request to the server
  client.print(String("GET url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

  delay(1000);

}

2017年2月16日星期四

ESP8266学习

        ESP8266是我最近在网上看到的一块芯片,是中国上海乐鑫生产的,集wifi, Mcu,于一身,小芯片,大用处.安信可将乐鑫的芯片进行了二次封装,集成了flash,晶振,天线等重新销售.因此互联上的资料很乱,有乐鑫的,安信可的.
        乐鑫的网址http://www.espressif.com/zh-hans
        安信可的网址http://www.ai-thinker.com/

        ESP8266的使用以我的了解,大约有以下几种途径:

  • 一种是AT指令,不管是乐鑫还是安信可的模块,出厂都刷了AT固件,可以通过AT指令控制芯片工作,AT手册两个官网都有相应的资料.

  • 一种是刷入nodemcu的固件,通过lua脚本语言编程,控制芯片工作,项目托管在github,https://github.com/nodemcu/nodemcu-firmware/releases,编译好的固件只有20150704的,分为两个,一个是整数,一个是浮点,其它的版本需要自己编译.
  • 这里有个网址,http://tinylab.org/nodemcu-kickstart/,可以看看,参考如何使用nodemcu来开发esp8266
  • ESPlorer是一个基于java编写的开发开具,https://esp8266.ru/esplorer/,大家可以看看,在这个软件界面,可以直接编写lua脚本并上传调试.

  • 一种是使用官方的SDK或其它的SDK,使用IDE环境进行编程,编译开发



  • 一种是使用用arduino IDE环境,使用esp8266的相关插件,进行开发,在arduino IDE附加开发板管理器网址输入https://github.com/esp8266/Arduino/releases/download/2.3.0/package_esp8266com_index.json