极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16042|回复: 7

请问怎样才可以把两个文件不同的代码合在一起的

[复制链接]
发表于 2014-12-14 00:19:16 | 显示全部楼层 |阅读模式
就是用 arduino的IDE,怎样才能把两个文件不同的代码合在一起的
回复

使用道具 举报

发表于 2014-12-14 01:36:14 | 显示全部楼层
arduino程序的结构是,定义,setup,loop三部分,将不同程序安三部分不同的位置放入就可以了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-15 18:27:32 | 显示全部楼层
林定祥 发表于 2014-12-14 01:36
arduino程序的结构是,定义,setup,loop三部分,将不同程序安三部分不同的位置放入就可以了。

有无相关的教程,最好有例子介绍怎样合并的
回复 支持 反对

使用道具 举报

发表于 2014-12-15 22:55:37 | 显示全部楼层
布列松 发表于 2014-12-15 18:27
有无相关的教程,最好有例子介绍怎样合并的

通产单个程序先跑一下,确认单个程序没有问题,然后再上面介绍各个部分就为,再调试,动手实践下吧。估计教材上也是这两句话。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-18 14:01:24 | 显示全部楼层
林定祥 发表于 2014-12-15 22:55
通产单个程序先跑一下,确认单个程序没有问题,然后再上面介绍各个部分就为,再调试,动手实践下吧。估计 ...

好像很难 那以下这两段程序怎样结合在一起呢,

  1. #include <Ethernet.h>
  2. #include <WiFi.h>
  3. #include <SPI.h>
  4. #include <yl_data_point.h>
  5. #include <yl_device.h>
  6. #include <yl_w5100_client.h>
  7. #include <yl_wifi_client.h>
  8. #include <yl_messenger.h>
  9. #include <yl_sensor.h>
  10. #include <yl_value_data_point.h>
  11. #include <yl_sensor.h>

  12. //this example reads data from a lm35dz sensor, convert value to degree Celsius
  13. //and then post it to yeelink.net

  14. //replace 2633 3539 with ur device id and sensor id
  15. yl_device ardu(xxxxx);  //此处替换为你的设备编号
  16. yl_sensor therm(xxxxx, &ardu);//此处替换为你的传感器编号
  17. //replace first param value with ur u-apikey
  18. yl_w5100_client client;
  19. yl_messenger messenger(&client, "xxxxx", "api.yeelink.net");   //此处替换为你自己的API KEY

  20. const int THERM_PIN  = A5;

  21. float lm35_convertor(int analog_num)
  22. {
  23.   return analog_num * (5.0 / 1024.0 * 100);
  24. }

  25. void setup()
  26. {
  27.   Serial.begin(9600);        //for output information
  28.   byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA};
  29.   Ethernet.begin(mac);
  30. }

  31. void loop()
  32. {
  33.   int v = analogRead(THERM_PIN);
  34.   Serial.println(lm35_convertor(v));
  35.   yl_value_data_point dp(lm35_convertor(v));
  36.   therm.single_post(messenger, dp);
  37.   delay(1000);
  38. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-18 14:02:10 | 显示全部楼层
  1. /*
  2. Yeelink sensor client power switch example
  3. */

  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6. #include <Wire.h>
  7. #include <math.h>

  8. byte buff[2];

  9. // for yeelink api
  10. #define APIKEY         "xxxxx" // 此处替换为你自己的API KEY
  11. #define DEVICEID       xxxxx// 此处替换为你的设备编号
  12. #define SENSORID1       xxxxx// 此处替换为你的传感器编号


  13. // assign a MAC address for the ethernet controller.
  14. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  15. // initialize the library instance:
  16. EthernetClient client ;
  17. char server[] = "api.yeelink.net";   // name address for yeelink API

  18. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  19. boolean lastConnected = false;                 // state of the connection last time through the main loop
  20. const unsigned long postingInterval = 3*1000; // delay between 2 datapoints, 30s
  21. String returnValue = "";
  22. boolean ResponseBegin = false;

  23. void setup() {
  24.   pinMode(5, OUTPUT);
  25.   Wire.begin();
  26.   // start serial port:
  27. Serial.begin(57600);
  28.   
  29.   // start the Ethernet connection with DHCP:
  30.   if (Ethernet.begin(mac) == 0) {
  31.     Serial.println("Failed to configure Ethernet using DHCP");
  32.     for(;;)
  33.       ;
  34.   }
  35.   else {
  36.     Serial.println("Ethernet configuration OK");
  37.   }
  38. }

  39. void loop() {
  40.   // if there's incoming data from the net connection.
  41.   // send it out the serial port.  This is for debugging
  42.   // purposes only:

  43.   if (client.available()) {
  44.     char c = client.read();
  45.    // Serial.print(c);
  46.       if (c == '{')
  47.         ResponseBegin = true;
  48.       else if (c == '}')
  49.         ResponseBegin = false;

  50.       if (ResponseBegin)
  51.         returnValue += c;   
  52.   }
  53.   if (returnValue.length() !=0 && (ResponseBegin == false))
  54.   {
  55.     Serial.println(returnValue);
  56.    
  57.     if (returnValue.charAt(returnValue.length() - 1) == '1') {
  58.       Serial.println("turn on the LED");
  59.       digitalWrite(5, HIGH);
  60.     }
  61.       else if(returnValue.charAt(returnValue.length() - 1) == '0') {
  62.       Serial.println("turn off the LED");
  63.       digitalWrite(5, LOW);
  64.     }
  65.      returnValue = "";
  66.   }
  67.   // if there's no net connection, but there was one last time
  68.   // through the loop, then stop the client:
  69.   if (!client.connected() && lastConnected) {
  70.     Serial.println();
  71.     Serial.println("disconnecting.");
  72.     client.stop();
  73.   }
  74.   
  75.   // if you're not connected, and ten seconds have passed since
  76.   // your last connection, then connect again and send data:
  77.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  78.     // read sensor data, replace with your code
  79.     //int sensorReading = readLightSensor();
  80.     Serial.print("yeelink:");
  81.     //get data from server  
  82.     getData();
  83.   }
  84.   // store the state of the connection for next time through
  85.   // the loop:
  86.   lastConnected = client.connected();
  87. }



  88. // this method makes a HTTP connection to the server and get data back
  89. void getData(void) {
  90.   // if there's a successful connection:
  91.   if (client.connect(server, 80)) {
  92.     Serial.println("connecting...");
  93.     // send the HTTP GET request:
  94.    
  95.     client.print("GET /v1.0/device/");
  96.     client.print(DEVICEID);
  97.     client.print("/sensor/");
  98.     client.print(SENSORID1);
  99.     client.print("/datapoints");
  100.     client.println(" HTTP/1.1");
  101.     client.println("Host: api.yeelink.net");
  102.     client.print("Accept: *");
  103.     client.print("/");
  104.     client.println("*");
  105.     client.print("U-ApiKey: ");
  106.     client.println(APIKEY);
  107.     client.println("Content-Length: 0");
  108.     client.println("Connection: close");
  109.     client.println();
  110.     Serial.println("print get done.");
  111.    
  112.   }
  113.   else {
  114.     // if you couldn't make a connection:
  115.     Serial.println("connection failed");
  116.     Serial.println();
  117.     Serial.println("disconnecting.");
  118.     client.stop();
  119.   }
  120.    // note the time that the connection was made or attempted:
  121.   lastConnectionTime = millis();
  122. }


复制代码
回复 支持 反对

使用道具 举报

发表于 2014-12-18 21:44:25 | 显示全部楼层
布列松 发表于 2014-12-18 14:02
/*
Yeelink sensor client power switch example
*/

拼过了吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-18 22:48:29 | 显示全部楼层
林定祥 发表于 2014-12-18 21:44
拼过了吗?

都不知从那里开始拼,好像两段差很远
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 19:53 , Processed in 0.122112 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表