关于Arduino+ESP8266上Yeelink使用问题
原来使用UNO+W5100(或ENC28J60)将DHT22的温度、湿度和BMP085气压和温度数据上传了Yeelink,由于使用网口受到网线的限制,使用不太方便,原来的WiFi板卡都较贵。串口转WiFi的ESP8266出现后引起注意,这模块有非常好的性能价格比,通常在15元左右,比网卡还便宜不少,上物联网小数据上传足足有余。最初基于以上想法,准备将原来UNO+W5100的方案想移植到UNO+ESP,一直不太稳定,本坛子里虽有方案介绍,但是做一般的无线串口较容易,上Yeelink一直不能通畅,有不少坛友也在询问,但是几乎都不太成功。经过多次反复我的结论是UNO的RAM相对较小,而网上提供的库优化还是有问题,因此不能满足这样的应用。我的最好的UNO+ESP的结果是,关掉所有的DebugSerial.print可以勉强完成将DHT数据上传至Yeelink。
最终还是将UNO换了Mege256RAM从2K扩大至8K使得数据上yeelink变得稳定了(使用ESP库),现在已经完成Mega+ESP+DTH+BMP的组合,稳定的将数据上传至Yeelink。虽然Mega板贵了些,但是和ESP的组合wifi上网不仅性价比好,而且使用也不受网线的限制了,值得一试。
还是希望ESP网卡公司能优化ESP的库,使得UNO+ESP的上网也能像UNO+W5100或UNO+ENC28j60一样自如,方便。
谢谢分享学习一下 楼主可否共享下Mega+ESP+DTH+BMP的组合的项目历程? ncyaoyi 发表于 2016-5-11 23:28 static/image/common/back.gif
楼主可否共享下Mega+ESP+DTH+BMP的组合的项目历程?
哈哈,有需要我可以整理下,只是时间很久了,花时间理一下吧。 同求Mega+ESP+DTH+BMP的组合的项目的例程?:):)感谢![email protected] 求分享,感谢~~ 本帖最后由 林定祥 于 2016-5-23 21:57 编辑
没有找到 ESP+DHT22+BMP085+MEGA,只找到一段较旧的程序,供参考
// 由UNO+ENC28J60+DHT22+BMP085
// 组成的双温度,单湿度,单气压传感器
// 通过网卡传输到http:www.yeelink.net,节点名HomeDog
//
//开发者:林定祥 邮箱:[email protected]
// 2013年5月26日
//
#include <Wire.h>
#include <DHT.h>
#include <EtherCard.h>
#define BMP085_ADDRESS 0x77// BMP085的I2C地址
#define OUT
#define REQUEST_RATE 15000 // (毫秒)yeelink要求数据传输间隔必须大于15秒
const unsigned char OSS = 0;// 采样设置
//定义使用函
float Temperature;
float Humidity;
int SendData;
int sensor=1;
// 气压校准值
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5用于计算bmp085GetTemperature(...),它也同时用于bmp085GetPressure(...)
// 所以Temperature(...)必须在Pressure(...)之前声明
long b5;
short Btemperature;
long Bpressure;
//DHT22初始化
#define DHTPIN 9 //DHT22采用1线制,数据线连Aduino D9
#define DHTTYPE 22
DHT dht(DHTPIN, DHTTYPE);
// DHT22 myDHT22(DHT22_PIN);
//定义网卡MAC地址及缓冲区
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer;
//上联网站设置
char website[] PROGMEM = "api.yeelink.net"; //站名
char urlBufT[] PROGMEM = "/v1.0/device/2512/sensor/3408/datapoints"; // 站点及温度传感器ID
char urlBufH[] PROGMEM = "/v1.0/device/2512/sensor/3563/datapoints"; // 站点及湿度传感器ID
char urlBufBP[] PROGMEM = "/v1.0/device/2512/sensor/4381/datapoints"; // 站点及压力传感器ID
char urlBufBT[] PROGMEM = "/v1.0/device/2512/sensor/4387/datapoints"; // 站点及第二温度传感器ID
char apiKey[] PROGMEM = "U-ApiKey: 34bde5ff710374a81034*****2981626"; // 用户KEY
static long timer;
// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer+off);
}
void setup () {
Serial.begin(57600);
Wire.begin();
bmp085Calibration();
Serial.println("\n");
Serial.println();
if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0)
Serial.println( "Failed to access Ethernet controller");
Serial.println("Setting up DHCP");
if (!ether.dhcpSetup())
Serial.println( "DHCP failed");
ether.printIp("My IP: ", ether.myip);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
timer = - REQUEST_RATE; // start timing out right away
}
void loop () {
Btemperature = bmp085GetTemperature(bmp085ReadUT());
Bpressure = bmp085GetPressure(bmp085ReadUP());
// DHT22_ERROR_t errorCode;
// delay(1000);
//errorCode = myDHT22.readData();
Temperature=dht.readTemperature();
Humidity=dht.readHumidity();
ether.packetLoop(ether.packetReceive());
if (millis() > timer + REQUEST_RATE) {
timer = millis();
Serial.println(">>> REQ");
static char buf;
switch(sensor){
case 1: //sensor 1 DHT22 Temperature
sensor=sensor+1;
get_send_string(buf,(Temperature-4)*10);
ether.httpPost (urlBufT, website, apiKey, buf, my_result_cb);
break;
case 2: //sensor 2 DHT22 Humidity
sensor=sensor+1;
get_send_string(buf,Humidity*10);
ether.httpPost (urlBufH, website, apiKey, buf, my_result_cb);
break;
case 3: //sensor 3 BMP085 Perature
sensor=sensor+1;
get_send_string(buf,Bpressure/10);
ether.httpPost (urlBufBP, website, apiKey, buf, my_result_cb);
break;
case 4: //sensor 4 BMP085 Temperature
sensor=1;
get_send_string(buf,(Btemperature-40));
ether.httpPost (urlBufBT, website, apiKey, buf, my_result_cb);
}
}
}
void get_send_string(OUT char *p,long SendData){
long Tc_100 = SendData;
Serial.println(Tc_100);
uint16_t whole,fract;
whole = Tc_100/10 ;// separate off the whole and fractional portions
fract = Tc_100 % 10;
sprintf(p,"{\"value\":%d.%d}",whole,fract);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
}
// 存储所有的BMP085的校准值到全局变量
// 校准值用来计算温度与气压
// 这个函数应该放在程序的开头
void bmp085Calibration()
{
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
}
// 计算温度赋值给变量ut
// 返回值的精度在0.1摄氏度
short bmp085GetTemperature(unsigned int ut)
{
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
return ((b5 + 8)>>4);
}
// 计算压力
// 校准值必须是已知的
// B5在bmp085GetTemperature(...)需要使用,所以必须先调用。
// 返回值以Pa为单位
long bmp085GetPressure(unsigned long up)
{
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// 计算B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
// 计算B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
return p;
}
// 在BMP085的'address'中读取一个字节
char bmp085Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 1);
while(!Wire.available());
return Wire.read();
}
// 从BMP085读取两个字节
// 第一个字节是从'address'
// 第二个字节是从'address'+1
int bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2);
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
// 读取未补偿的温度值
unsigned int bmp085ReadUT()
{
unsigned int ut;
// 在寄存器0xF4写入0x2E
// 这个用来请求进行温度读取
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// 至少等待4.5ms
delay(5);
// 从寄存器0xF6与0xF7读取两个字节
ut = bmp085ReadInt(0xF6);
return ut;
}
// 读取未补偿的压力值
unsigned long bmp085ReadUP()
{
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// 写入0x34+(OSS<<6)到寄存器0xF4
// 请求气压数据读取
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (OSS<<6));
Wire.endTransmission();
// 等待转换,延迟时间依赖于OSS
delay(2 + (3<<OSS));
// 读取寄存器0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF6);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 3);
// 等待数据变为可用
while(Wire.available() < 3);
msb = Wire.read();
lsb = Wire.read();
xlsb = Wire.read();
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
} 林定祥 发表于 2016-5-23 21:53 static/image/common/back.gif
没有找到 ESP+DHT22+BMP085+MEGA,只找到一段较旧的程序,供参考
// 由UNO+ENC28J60+DHT22+BMP085
感谢楼主分享。
页:
[1]