基于18B20+enc28j60+arduino+yeelink的远程温度监控
本帖最后由 zcbzjx 于 2013-7-17 11:35 编辑2013年7月17日,新版本库已经没有dhcpValid()方法,请各位留意。
2012.31 更新代码,改为自动获取ip地址,以及服务器用域名访问(dns)。
硬件看图。。。估计只有我看的清楚
代码
arduino1.01
请下载EtherCARD库/*
Yeelink sensor client
by zcb
2012.07.17
*/
#include <EtherCard.h>
#include <OneWire.h>
//
//18B20
#define ONEWIRE_PIN A0
OneWire ds(ONEWIRE_PIN);
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
char SensorID[] PROGMEM = "XXX"; // replace your sensor ID
// ethernet interface mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// remote website name
char website[] PROGMEM = "api.yeelink.net";
////////////////////////////////////////////////////////////
// for yeelink api
char APIKey[] PROGMEM = "XXXXXXXXXXXXXXXXX"; // replace your yeelink api key here
char DeviceID[] PROGMEM = "XXX"; // replace your device ID
// assign a MAC IP gateway dns for the ethernet controller.
const uint16_t PostingInterval = 15000;// delay between 2 datapoints, 30s
// Some global variables
char sensorData;
uint8_t dataLength;
uint8_t Ethernet::buffer;
uint32_t lastConnectionTime = 0; // last time you connected to the server, in milliseconds
Stash stash;
void setup() {
Serial.begin(57600);
Serial.println("\n");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("My IP: ", ether.myip);
// ether.printIp("Netmask: ", ether.mymask);
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);
}
void loop() {
if (!ether.dhcpValid() && !ether.dhcpSetup())
Serial.println("DHCP failed");
ether.packetLoop(ether.packetReceive());
if(millis() - lastConnectionTime > PostingInterval){
get_Send_String();
send_Data();
}
}
void send_Data() {
// Create a Post for yeelink server,
// and send request saving sessionID
Stash::prepare(PSTR("POST /v1.0/device/$F/sensor/$F/datapoints HTTP/1.1" "\r\n"
"Host: api.yeelink.net" "\r\n"
"U-ApiKey: $F" "\r\n"
"Content-Length: $D" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n"
"$S"),
DeviceID,SensorID,APIKey,dataLength,sensorData);
ether.tcpSend();
lastConnectionTime = millis();
}
///////////////////////////////////////////////////////////////////////////
// get data from temperature sensor
// you can replace this code for your sensor
void get_Send_String(){
uint16_t Tc_100 = get_Current_Temp();
uint8_t i,whole, fract;
whole = Tc_100/10 ;// separate off the whole and fractional portions
fract = Tc_100 % 10;
dataLength = sprintf(sensorData,"{\"value\":%d.%d}",whole,fract);
}
uint16_t get_Current_Temp(){ //0.1C
//returns the temperature from one DS18S20 in DEG Celsius
byte data;
byte addr;
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return 0;
}
if ( OneWire::crc8( addr, 7) != addr) {
return 1000;
}
if ( addr != 0x28) {
return 1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000);
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();
}
ds.reset_search();
uint16_t tempRead = (data << 8) | data; //using two's compliment
return tempRead*10/16;
}图片在最后,所以这儿对硬件简单说下,左边的2*5排针是接enc28j60的;
右边的8*4排针是atmega168pa 32脚转接的。第一脚在右下脚,逆时针数;
下边的5个排针(忘记画了,不过反正是5根线)是tjc3座,左边三个接18b20(Vcc,DQ,GND),右边2个接电源(5-12V直流)(-,+)。
那个贴片是asm1117-3.3没有画电容,输入输出都是接了100uf,0.1uf2个电容,mcu电源2端加接了个0.1uf电容。
还有4个圆的母杜邦座,哪个是铜柱,固定enc28J60的。
首先支持一下,然后麻烦一下楼主是否能把该写的东西写清楚点呢?特别是硬件连接图等等,给大家来养养眼!:) 简单解释了下,目前已经做了6套了,还要做2套,运行良好,大家可以看http://www.yeelink.net/devices/197 zcbzjx 发表于 2012-7-30 13:42 static/image/common/back.gif
简单解释了下,目前已经做了6套了,还要做2套,运行良好,大家可以看http://www.yeelink.net/devices/197
做产品??? 本帖最后由 zcbzjx 于 2012-7-30 14:08 编辑
也不算,就是单位自己用的。8个机房,一个机房一个 我看到可耻的关键时刻都开空调了。。 应该上原理图....洞洞板确实难看懂 监控机房的温度,这个还挺实用的,话说我一直没想清楚yeelink到底怎么应用到实际中,楼主提供了一个很好的例子 楼主你太牛逼了,一天的PV到200多,是不是全北京人都在点击你的设备看?
另外我给你发邮件没回应,能不能加我QQ?6032410,谢谢! 对了,有两个传感器的上下波动比较规律,锯齿型的,是不是有些传感器的问题? 教学楼空调开得太大了哈,22度要冻死人啦...低碳,低碳... 不是,是正常的,请看这个链接 erjiang 发表于 2012-7-31 16:48 static/image/common/back.gif
教学楼空调开得太大了哈,22度要冻死人啦...低碳,低碳...
你说的是综合楼的吧,空调调的是27度,由于传感器放得比较低,而空调的传感器位置可能比较高,所以。。 zcbzjx 发表于 2012-7-31 18:56 static/image/common/back.gif
你说的是综合楼的吧,空调调的是27度,由于传感器放得比较低,而空调的传感器位置可能比较高,所以。。
我突然觉得,通过你发布在yeelink的这个图表,能够最真实的表达变频空调对开关空调的明显优势,估计那这个图放给买空调的客户一看,他们买那个就会立即决定了。 楼主可不可以对代码进行下解释么?谢谢撒