arduino如何从功能返回

时间:2015-06-27 04:18:33

标签: arduino

我对arduino及其编程相对较新,但我一直在努力并取得成果。我添加了一个缩短版的草图,它连接到我的网络服务器并获取特定城市的当前温度。然后我将使用那个温度设置我的加热器也由我的arduino控制。我面临的问题是,在我从connectandread函数读取webserver上的数据后,我无法理解如何返回主循环。草图在接收数据(temp)后停止/停止。最终我将每隔2小时调用一次该功能。总而言之,我需要调用该函数然后运行后返回主循环。我希望我已经足够清楚地解释了。

byte server[] = { 192,168,0,2 }; //ip Address of the server you will connect to 

//The location to go to on the server 
//make sure to keep HTTP/1.0 at the end, this is telling it what type   of    file it is 
String location = "/index1.php HTTP/1.0"; 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
EthernetClient client; 
int convertedtemp; 
char inString[32]; // string for incoming serial data 
int stringPos = 0; // string index counter 
boolean startRead = false; // is reading? 
String responseString; 

void setup() { 
    Ethernet.begin(mac); 
    delay( 1000 ); 
    // Start the I2C interface 
    Wire.begin(); 
}

void ReadDS3231() { 
    int second,minute,hour,date,month,year,temperature;
    second=Clock.getSecond(); 
    minute=Clock.getMinute(); 
    hour=Clock.getHour(h12, PM); 
    date=Clock.getDate(); 
    month=Clock.getMonth(Century); 
    year=Clock.getYear(); 
    lcd.setCursor(0, 1);  
}

void loop() { 
    connectAndRead(); 

    // Reading temperature or humidity takes about 250 milliseconds! 
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow  sensor) 
    int h0 = dht0.readHumidity(); 
    int h1 = dht1.readHumidity(); 
    int t0 = dht0.readTemperature(); 
    int t1 = dht1.readTemperature(); 

    ReadDS3231(); delay(3000); 
} 

String connectAndRead() { 
    //connect to the server 

    Serial.println("connecting...");  

    if (client.connect(server, 80)) { 
        Serial.println("connected"); 
        client.print("GET "); 
        client.println(location); 
        client.println(); 

        //Connected - Read the page 
        return readPage(); //go and read the output 

    } else { 
        return "connection failed"; 
    } 

}

String readPage() { 
    //read the page, and capture & return everything between '<' and '>' 

    stringPos = 0; 
    memset( &inString, 0, 32 ); //clear inString memory 

    while (true) { 
        if (client.available()) { 
            char c = client.read(); 

            if (c == '<' ) { //'<' is our begining character 
                startRead = true; //Ready to start reading the part 
            } else if (startRead) { 
                if (c != '>') { //'>' is our ending character 
                    inString[stringPos] = c; 
                    stringPos ++; 
                } else { 
                    //got what we need here! We can disconnect now 
                    startRead = false; 
                    //Serial.print(inString); 
                    //convertedtemp = atoi(inString).c_str()); 
                    int val = atoi(inString); 
                    //int convertedhumid = atoi(getValuesFromKey(responseString,  "relative_humidity").c_str()); 
                    Serial.println(val); 
                    client.stop(); 
                    client.flush(); 

                    Serial.println("disconnecting."); 

                    //return inString; 
                    //return ; 
                } 

            } 
        } 

    } 

} 

1 个答案:

答案 0 :(得分:0)

好吧,至少您发布的草图版本中有一个无限循环...(while(true))。

尝试使用return inString;取消评论该行。

同样无关,因为您从外部来源读取数据,即使您没有收到“&gt;”,也需要在收到3个 1 字符后停止阅读。否则,服务器上的错误或恶意代码可能会损坏您的内存。