简体   繁体   English

如何从Arduino库中读取数组?

[英]How to read an array from Arduino library?

I am using ethernet module to upload data to a server using Cayenne-Arduino-Library and arduino_uip . 我正在使用以太网模块使用Cayenne-Arduino-Libraryarduino_uip将数据上传到服务器。 I want to read the myip[] from CayenneEthernet.h 我想从CayenneEthernet.h中读取myip[]

Original: 原版的:

// DHCP with domain
void begin(const char* auth,
    const char* domain = BLYNK_DEFAULT_DOMAIN,
    uint16_t port = BLYNK_DEFAULT_PORT,
    const byte mac[] = _blynkEthernetMac)
{
    BLYNK_LOG("Here we are");// I added this to find this function.
    ...
    IPAddress myip = Ethernet.localIP();
    BLYNK_LOG("My IP: %d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]);
}

Edited: 编辑:

// DHCP with domain
int* begin(const char* auth,
    const char* domain = BLYNK_DEFAULT_DOMAIN,
    uint16_t port = BLYNK_DEFAULT_PORT,
    const byte mac[] = _blynkEthernetMac)
{   
    ...
    IPAddress myip = Ethernet.localIP();
    BLYNK_LOG("My IP: %d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]);
    return myip;
}

Arduino code: Arduino代码:

#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneDefines.h>
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
#include <CayenneEthernetClient.h>
#define VIRTUAL_PIN V1
#define VIRTUAL_PIN V0
...
void setup(){
...
  int *A=Cayenne.begin(token);
...
}

void loop(){
...
}

I am getting this error: 我收到此错误:

error: void value not ignored as it ought to be 错误:空值不应该被忽略

Question: 题:

How can I return the array correctly? 如何正确返回数组?

UPDATE: After searching Cayenne.begin() is defined on CayenneEthernetClient.h 更新:搜索后,在Cayenne.begin()上定义了Cayenne.begin Cayenne.begin()

class CayenneEthernetClient : public CayenneClient
{
public:
    void begin(const char* token, const byte mac[] = NULL)
    {
        BLYNK_LOG("HERE WE ARE 3"); 
        Blynk.begin(token, CAYENNE_DOMAIN, CAYENNE_PORT, GetMACAddress(token, mac));

    }

private:

    const byte* GetMACAddress(const char* token, const byte mac[])
    {
        ...
        return _mac;
    }

    byte _mac[6];
};

CayenneEthernetClient Cayenne;

After, this begin function is called on BlynkProtocol.h 之后,在BlynkProtocol.h上调用开始函数

private:
    int readHeader(BlynkHeader& hdr);
    uint16_t getNextMsgId();

protected:
    void begin(const char* auth) {
        BLYNK_LOG("HERE WE ARE 2");
        this->authkey = auth;
    }
    bool processInput(void);

After, this begin() is called on BlynkEthernet.h 之后,在BlynkEthernet.h上调用begin()

// DHCP with domain
void begin( const char* auth,
            const char* domain = BLYNK_DEFAULT_DOMAIN,
            uint16_t port      = BLYNK_DEFAULT_PORT,
            const byte mac[]   = _blynkEthernetMac)
{
    Base::begin(auth);
    BLYNK_LOG("Getting IP...");
    BLYNK_LOG("HERE WE ARE 1");
    if (!Ethernet.begin((byte*)mac)) {
        BLYNK_FATAL("DHCP Failed!");
    }
    // give the Ethernet shield a second to initialize:
    ::delay(1000);
    this->conn.begin(domain, port);
    IPAddress myip = Ethernet.localIP();
    BLYNK_LOG("My IP: %d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]);

}

And this begin() is called finally on BlynkArduinoClient.h 最后在BlynkArduinoClient.h上调用了begin()

void begin(const char* d, uint16_t p) {
    BLYNK_LOG("HERE WE ARE 9");
    domain = d;
    port = p;
}

Output on serial monitor: 在串行监视器上输出:

 [75] HERE WE ARE 3 [76] MAC: FE-9D-D2-DD-A3-A0 [76] HERE WE ARE 2 [77] Getting IP... [80] HERE WE ARE 1 [5385] HERE WE ARE 9 [5386] My IP: 10.42.0.162 

I think It's not a good idea to modify the code or even the API of a library. 我认为修改代码或库的API并不是一个好主意。 My approach here would be to get the IP address the same way the Cayenne library does it: A call to Ethernet.localIP() : 我在这里的方法是使用Cayenne库的相同方式获取IP地址:调用Ethernet.localIP()

void setup() {
    Cayenne.begin(token);
    IPAddress myip = Ethernet.localIP();
}

PS: The error PS:错误

void value not ignored as it ought to be 无效值不应该被忽略

probably occurs because you have only modified one of the three overridden begin() methods in the Cayenne class. 发生这种情况的原因可能是,您只修改了Cayenne类中三个覆盖的begin()方法之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM