0

我正在尝试通过 将来自 Analog Sensor 1 的数据发送arduino yun到谷歌文档中的电子表格pushingbox.com。所以我得到了pushingbox和谷歌文档之间的联系,但它是我正在努力解决的Yun和之间的联系。pushingbox我已Yun通过以太网将网络连接到网络,并且该连接工作正常。

MAC地址和IP已经从我原来的地方改变了。似乎无法找到此代码不起作用的原因。它只是在串行监视器上显示“正在连接...”。下面我粘贴了我的代码。

有人请帮助我。

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

//-------------------------------------------------------------------------------
byte mac[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; //Setting MAC Address
char server[] = "api.pushingbox.com"; //pushingbox API server
IPAddress ip(000,000,000,00); //Arduino IP address. Only used when DHCP is turned off.
EthernetClient client; //define 'client' as object
String data; //GET query with data
float suhu; //suhu (bahasa Indonesia) means temperature
boolean koneksi = false;
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  if (Ethernet.begin(mac) == 0) {
  Serial.println("Failed to configure Ethernet using DHCP");
  Ethernet.begin(mac, ip);
  }
  delay(1000);
}
//------------------------------------------------------------------------------
void loop(){
   int baca_ADC = analogRead(1); //read analog input on pin A1
   suhu = baca_ADC * 5.0 * 100.0/1024.0; // ADC to celcius conversion
   kemasData(); //packing GET query with data
   Serial.println("connecting...");
   if (client.connect(server, 80)) {
     sendData();  
     koneksi = true; //connected = true
   }
   else{
     Serial.println("connection failed");
   }
  // loop
  while(koneksi){
    if (client.available()) {
    char c = client.read(); //save http header to c
    Serial.print(c); //print http header to serial monitor
    }
    if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
          Serial.print("Temperature Sent :");
          Serial.println(suhu); //print sent value to serial monitor
    client.stop(); 
          koneksi = false; 
          data = ""; //data reset
    }
  }
  delay(5000); // interval
}

void kemasData(){
  data+="";
  data+="GET /pushingbox?devid=xxxxxxxxxxxxxxxx&tempData="; //GET request query to pushingbox API
  data+=suhu;
  data+=" HTTP/1.1";
}
void sendData(){
  Serial.println("connected");
  client.println(data);
  client.println("Host: api.pushingbox.com");
  client.println("Connection: close");
  client.println();
}
4

1 回答 1

0

我认为您的代码卡在 while() 循环中,您是否正在连接......每 5 秒连续编写一次还是只编写一次?if once then 我认为这绝对是 while loop()

于 2015-09-11T09:20:58.113 回答