布列松 发表于 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 static/image/common/back.gif
arduino程序的结构是,定义,setup,loop三部分,将不同程序安三部分不同的位置放入就可以了。

有无相关的教程,最好有例子介绍怎样合并的

林定祥 发表于 2014-12-15 22:55:37

布列松 发表于 2014-12-15 18:27 static/image/common/back.gif
有无相关的教程,最好有例子介绍怎样合并的

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

布列松 发表于 2014-12-18 14:01:24

林定祥 发表于 2014-12-15 22:55 static/image/common/back.gif
通产单个程序先跑一下,确认单个程序没有问题,然后再上面介绍各个部分就为,再调试,动手实践下吧。估计 ...

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

#include <Ethernet.h>
#include <WiFi.h>
#include <SPI.h>
#include <yl_data_point.h>
#include <yl_device.h>
#include <yl_w5100_client.h>
#include <yl_wifi_client.h>
#include <yl_messenger.h>
#include <yl_sensor.h>
#include <yl_value_data_point.h>
#include <yl_sensor.h>

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

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

const int THERM_PIN= A5;

float lm35_convertor(int analog_num)
{
return analog_num * (5.0 / 1024.0 * 100);
}

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

void loop()
{
int v = analogRead(THERM_PIN);
Serial.println(lm35_convertor(v));
yl_value_data_point dp(lm35_convertor(v));
therm.single_post(messenger, dp);
delay(1000);
}

布列松 发表于 2014-12-18 14:02:10

/*
Yeelink sensor client power switch example
*/

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>

byte buff;

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


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

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

void setup() {
pinMode(5, OUTPUT);
Wire.begin();
// start serial port:
Serial.begin(57600);

// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for(;;)
      ;
}
else {
    Serial.println("Ethernet configuration OK");
}
}

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

if (client.available()) {
    char c = client.read();
   // Serial.print(c);
      if (c == '{')
      ResponseBegin = true;
      else if (c == '}')
      ResponseBegin = false;

      if (ResponseBegin)
      returnValue += c;   
}
if (returnValue.length() !=0 && (ResponseBegin == false))
{
    Serial.println(returnValue);
   
    if (returnValue.charAt(returnValue.length() - 1) == '1') {
      Serial.println("turn on the LED");
      digitalWrite(5, HIGH);
    }
      else if(returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the LED");
      digitalWrite(5, LOW);
    }
   returnValue = "";
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
}

// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    // read sensor data, replace with your code
    //int sensorReading = readLightSensor();
    Serial.print("yeelink:");
    //get data from server
    getData();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}



// this method makes a HTTP connection to the server and get data back
void getData(void) {
// if there's a successful connection:
if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
   
    client.print("GET /v1.0/device/");
    client.print(DEVICEID);
    client.print("/sensor/");
    client.print(SENSORID1);
    client.print("/datapoints");
    client.println(" HTTP/1.1");
    client.println("Host: api.yeelink.net");
    client.print("Accept: *");
    client.print("/");
    client.println("*");
    client.print("U-ApiKey: ");
    client.println(APIKEY);
    client.println("Content-Length: 0");
    client.println("Connection: close");
    client.println();
    Serial.println("print get done.");
   
}
else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
}
   // note the time that the connection was made or attempted:
lastConnectionTime = millis();
}


林定祥 发表于 2014-12-18 21:44:25

布列松 发表于 2014-12-18 14:02 static/image/common/back.gif
/*
Yeelink sensor client power switch example
*/


拼过了吗?

布列松 发表于 2014-12-18 22:48:29

林定祥 发表于 2014-12-18 21:44 static/image/common/back.gif
拼过了吗?

都不知从那里开始拼,好像两段差很远
页: [1]
查看完整版本: 请问怎样才可以把两个文件不同的代码合在一起的