|
本帖最后由 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[20];
- uint8_t dataLength;
- uint8_t Ethernet::buffer[300];
- uint32_t lastConnectionTime = 0; // last time you connected to the server, in milliseconds
-
- Stash stash;
-
-
-
- void setup() {
- Serial.begin(57600);
- Serial.println("\n[yeelink client]");
-
- 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[12];
- byte addr[8];
- if ( !ds.search(addr)) {
- //no more sensors on chain, reset search
- ds.reset_search();
- return 0;
- }
- if ( OneWire::crc8( addr, 7) != addr[7]) {
- return 1000;
- }
- if ( addr[0] != 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[i] = ds.read();
- }
- ds.reset_search();
- uint16_t tempRead = (data[1] << 8) | data[0]; //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的。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|