新人贴,dht11+enc28j60+yeelink
参考了很多谈内外的资料 本帖最后由 blueleafxp 于 2013-6-22 23:06 编辑//#include <OneWire.h>
#include <EtherCard.h>
//#include <DallasTemperature.h>
#define OUT
// Data wire is plugged into port A5 on the Arduino
#define ONE_WIRE_BUS A5
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
//OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
//DallasTemperature sensors(&oneWire);
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 3 //DHT11 PIN 3 连接UNO 3
#define REQUEST_RATE 15000 // milliseconds
// ethernet interface mac address
static byte mymac[] = {
0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = {
192,168,101,139 };
// gateway ip address
static byte gwip[] = {
192,168,101,1 };
// remote dns ip address
static byte dnsip[] = {
192,168,101,1 };
int mode_id=0;
// remote website name
char website[] PROGMEM = "api.yeelink.net";
char urlBuf[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/datapoints";// 用自己的id
char apiKey[] PROGMEM = "U-ApiKey: xxxxx";
char urlBuf1[] PROGMEM = "/v1.0/device/xxxx/sensor/xxxx/datapoints";//用自己的key
byte Ethernet::buffer; // a very small tcp/ip buffer is enough here
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 () {
///sensors.begin();
Serial.begin(57600);
Serial.println("\n");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
ether.staticSetup(myip, gwip, dnsip);
while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());
Serial.println("Gateway found");
timer = - REQUEST_RATE; // start timing out right away
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer + REQUEST_RATE) {
timer = millis();
Serial.println("\n>>> REQ");
static char buf,buf1;
get_send_string(buf,buf1);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
switch(mode_id)
{
case 1:
Serial.println("\n>>> temperature");
ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
mode_id--;
break;
case 0:
Serial.println("\n>>> humidity");
ether.httpPost (urlBuf1, website, apiKey, buf1, my_result_cb);
mode_id++;
break;
default:
mode_id=0;
break;
}
}
}
void get_send_string(OUT char *p ,OUT char *p1){
int chk = DHT11.read(DHT11PIN);
// sensors.requestTemperatures();
int Tc_100 =(float) DHT11.temperature*10;
int whole, fract;
whole = Tc_100/10 ;// separate off the whole and fractional portions
fract = Tc_100 % 10;
//whole=35;
//fract=3;
sprintf(p,"{\"value\":%d.%d}",whole,fract);
int Tc_200 =(float) DHT11.humidity*10;
whole = Tc_200/10 ;// separate off the whole and fractional portions
fract = Tc_200 % 10;
//whole=35;
//fract=3;
sprintf(p1,"{\"value\":%d.%d}",whole,fract);
Serial.println(p);
Serial.println(p1);
//sprintf(p,"{\"value\":%d}",Tc_100 );
}
// PIN Connections (Using Arduino UNO):
// VCC - 3.3V
// GND - GND
// SCK - Pin 13
// SO- Pin 12
// SI- Pin 11
// CS- Pin8
温控用 pin 3
代码里看的到,目前用的是 tp-link做无线客户端模式
这贴很好,很有借鉴性,谢谢你的分享,非常有用 谢谢分享,数据传输正常。 湿度数据如何上传呢? 感谢,enc28j60的库太诡异,看了你的例子好多了 enc28j60 ping也是通的,但yeelink没有接收到数据,求解
页:
[1]