|
本帖最后由 zcbzjx 于 2013-5-29 00:39 编辑
2013年5月29日更新:由于库文件更新比较多,以前的帖子有些不适用了,重新开贴又没有必要,所以就直接修改这个帖子,并争取把这个帖子修改成精华帖。
最近研究ethercard库,发觉有个httpPost函数,用这个库函数发送post数据比较省事,而且可以方便获得服务器返回数据。
实验硬件器材和这个帖子的实验器材一样,不再赘述。
ArduinoIDE版本:1.0.4
库文件为最新的ethercard库文件
Yeelink代码 - #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);
- #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[] = {
- 10,21,0,193 };
- // gateway ip address
- static byte gwip[] = {
- 10,21,0,1 };
- // remote dns ip address
- static byte dnsip[] = {
- 10,11,5,25 };
- // remote website name
- char website[] PROGMEM = "api.yeelink.net";
- char urlBuf[] PROGMEM = "/v1.0/device/xxx/sensor/xxx/datapoints";
- char apiKey[] PROGMEM = "U-ApiKey: xxxxxxxxxxxxxx";
- byte Ethernet::buffer[400]; // 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[getStaticIP]");
- 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[20];
- get_send_string(buf);
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- ether.printIp("Server: ", ether.hisip);
- ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
- }
- }
- void get_send_string(OUT char *p){
- sensors.requestTemperatures();
- uint16_t Tc_100 = sensors.getTempCByIndex(0)*10;
- uint8_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);
- }
复制代码
串口图如下:
乐联网代码如下: - #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 7
- // 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);
-
- #define REQUEST_RATE 15000 // milliseconds
- // ethernet interface mac address 需要根据你的实际网络环境进行更改,我这儿用的是固定ip
- static byte mymac[] = {
- 0x74,0x69,0x69,0x2D,0x30,0x31 };
- // ethernet interface ip address
- static byte myip[] = {
- 10,21,0,193 };
- // gateway ip address
- static byte gwip[] = {
- 10,21,0,1 };
- // remote dns ip address
- static byte dnsip[] = {
- 10,21,0,1 };
-
- // remote website name
- char website[] PROGMEM = "www.lewei50.com";
- char urlBuf[] PROGMEM = "/api/V1/gateway/UpdateSensors/01"; // 01应该更改为你对应的网关标识
- char apiKey[] PROGMEM = "userkey:xxxxxxxxxxxxxxxxxxxxxxxxx"; //需要更改为你的userkey
- byte Ethernet::buffer[700]; // 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[getStaticIP]");
-
- 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[20];
- get_send_string(buf);
- if (!ether.dnsLookup(website))
- Serial.println("DNS failed");
- ether.printIp("Server: ", ether.hisip);
- ether.httpPost (urlBuf, website, apiKey, buf, my_result_cb);
- }
- }
-
- void get_send_string(OUT char *p){
- sensors.requestTemperatures();
- uint16_t Tc_100 = sensors.getTempCByIndex(0)*10;
- uint8_t whole, fract;
- whole = Tc_100/10 ; // separate off the whole and fractional portions
- fract = Tc_100 % 10;
- sprintf(p,"[{"Name":"T2","Value":"%d.%d"}]",whole,fract); //T2应该更改为你对应的设备标识
- }
复制代码
以下为串口切图:
相信大家已经看出来来了,代码没什么大的变化。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|