查看: 1894|回复: 0

Gokit申请

[复制链接]
  • TA的每日心情
    开心
    2020-2-14 12:16
  • 签到天数: 827 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-3-26 15:52:15 | 显示全部楼层 |阅读模式
    分享到:
    arduino+W5100+yelink平台实现温度上传,代码分享

    #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();
    }

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-11-15 06:30 , Processed in 0.105513 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.