2

非常感谢你们找人。我刚刚开始使用 Arduino 无线传感器项目,但我遇到了一个线索。我将不胜感激任何见解或帮助解决我的项目。

我正在使用带有 Wifi Shield 的 Arduino Mega 尝试将 4 个频道(所有浮动)上传到我的 Xively 提要。我正在使用基本教程脚本的修改形式(底部的代码)。我验证并成功上传。当我运行电路板时,初始调试信息看起来不错。当我尝试使用 Xively 客户端库执行 put 或 get 时,我收到一个错误(ret = -1,没有可用的套接字,并且 get 时出现 http 错误)。我还在底部附上了错误的串行日志。

我已经采取了一些故障排除步骤。我已经重新下载了所有 xively 库和 Wifi 库。我认为手动设置 DNS 服务器(8.8.8.8)可能会有所帮助。我什至导入了整个 arduino 库(尽管我认为不需要它)来获取 DNS 功能。它似乎没有什么区别,所以我把它作为故障排除面包屑留下了。同样作为故障排除步骤,我添加了一个 Xively client.get 以查看我是否可以拉而不是推。这产生了一个 HTTP 错误。我还在我的项目中添加了一个公开的公开 api 密钥并尝试了它(而不是私有密钥)。仍然没有喜悦。

感觉有点像虽然我成功连接到我的 wifi,但无法真正连接到任何服务。我希望我错过了一些非常明显的步骤。我真的可以用一个有用的线索让我再次走上正轨。谢谢!

米卡·维恩

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>

/*
     Hot Tub Monitor 
         --- If you are only calibrating your sensor, use the calibrate sensor script.  This is for submerged operation.
     This script will allow you to test to ensure the temp, Ph, and ORD sensors are operating and are calibrated correctly.----

     This script was written for the MEGA 2560 with the wireless shield, connecting in to Xively's cloud graph service

     The Mega 2560 with the Wireless shield and Probes uses the following pins:
     *  SPI bus interface 
     *  Onboard MicroSD card reader (uses SD Lib) on digital pin 4
     *  Digital Pins 7, 50, 51, 52, and 53 are reserved and shouldn't be used
     *  Digital Pin 18 is set to OUTPUT to power the temp probe on/off 
     *  Atlas Scientific Temp Sensor input on Analog Uno pin A4
     *  Phidgets Ph Sensor input on Analog Uno pin A2
     *  Phidgets ORD Sensor input on Analog Uno pin A0
*/
// Libraries in use
#include <SPI.h>
#include <WiFi.h>
#include <b64.h>
#include <HttpClient.h>
#include <CountingStream.h>
#include <Xively.h>
#include <XivelyClient.h>
#include <XivelyDatastream.h>
#include <XivelyFeed.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>

// setting up some of the globals
const int TEMPPIN = 4;
const int PHPIN = 1;
const int ORPPIN = 0;
boolean isDebugEnabled = true;   //setting up the debug state
int status = WL_IDLE_STATUS;
char ssid[] = "<security snip>"; //  your network SSID (name) 
char pass[] = "<security snip>";    // your network password (use for WPA, or use as key for WEP)

// My Xively key to let you upload data
char xivelyKey[] = "<security snip>";
// My xively feed ID
#define xivelyFeed <security snip>
// My datastreams
char myWirelessStrStream[] = "MonitorWirelessStrength";
char myORPStream[] = "ORP";
char myPhStream[] = "Ph";
char myTempStream[] = "Temp";

// Creating the datastreams
XivelyDatastream datastreams[] = {
    XivelyDatastream(myWirelessStrStream, strlen(myWirelessStrStream), DATASTREAM_FLOAT),
    XivelyDatastream(myORPStream, strlen(myORPStream), DATASTREAM_FLOAT),
    XivelyDatastream(myPhStream, strlen(myPhStream), DATASTREAM_FLOAT), 
    XivelyDatastream(myTempStream, strlen(myTempStream), DATASTREAM_FLOAT)
    };
XivelyFeed feed(xivelyFeed, datastreams, 4);

//starting the Xively client
WiFiClient client;
XivelyClient xivelyclient(client);


void setup() {
  if (isDebugEnabled) {   //setting up the debug stream
    Serial.begin(9600);
    Serial.println("Hot Tub Monitor Debug Stream Starting");
    Serial.println("-----------------------------------");
  }
  pinMode(12,OUTPUT);   //this pin turns on the temp sensor - battery saver to have this on/off switchable

  // connect to the wifi
  // check for presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 

  //Connect to the wifi network
    while ( status != WL_CONNECTED) { 
      Serial.print("Attempting to connect to WPA SSID: ");
      Serial.println(ssid);
      // Connect to WPA/WPA2 network:    
      status = WiFi.begin(ssid, pass);

      // wait 10 seconds for connection:
      delay(10000);
  }
  Serial.println("You're connected to the network");
  Serial.println(status);
  //printCurrentNet();
  printWifiData();

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

}

void loop() {
// report on the WiFi Signal strength  
   long rssi = WiFi.RSSI();
  if (isDebugEnabled) {   //send the signal str to Xively
    Serial.print("Sending RSSI to Xively:  ");
    Serial.println(rssi);
  }
    // print the received signal strength:
  datastreams[0].setFloat(rssi);

//  get the temp from our Atlas Sci probe
  float tempC=get_temp();
  float tempF = (tempC*1.8)+32;
  datastreams[1].setFloat(tempF);

// debugging info for temp
  if (isDebugEnabled) {   //send the temp to Xively
    Serial.print("Sending Temp to Xively:  ");
    Serial.println(tempF);
  }

// get the Ph from our phidget's monitor  
  float Ph=get_Ph(tempC);
  datastreams[2].setFloat(Ph);

// debugging info for Ph
  if (isDebugEnabled) {   //send the Ph to Xively
    Serial.print("Sending Ph to Xively:  ");
    Serial.println(Ph);
  } 

// get the Ph from our phidget's monitor  
  float ORP=get_ORP();
  datastreams[3].setFloat(ORP);

// debugging info for Ph
  if (isDebugEnabled) {   //send the Ph to Xively
    Serial.print("Sending ORP to Xively:  ");
    Serial.println(ORP);
  } 


  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  Serial.print("xivelyclient.put returned ");
  Serial.println(ret);


   delay(10000);
}  
float get_Ph(float tempC) {  
  float Ph = analogRead(PHPIN);
  Ph = 7 -((2.5 - (Ph/200))/((0.257179 + 0.0000941468)*tempC));  // convert to the ph
  return Ph;
  }

float get_ORP() {
  float ORP = analogRead(ORPPIN);
  ORP = (2.5 - (ORP/200))/1.037;  // convert to proper ORP
  return ORP;
  } 

 float get_temp(){      
  float v_out;
  float Temp;
  digitalWrite(A4, LOW);   //wtf is this for?
  digitalWrite(12, HIGH);
  delay(2);
  v_out = analogRead(4);
  digitalWrite(12, LOW);
  v_out*=.0048;
  v_out*=1000;
  Temp=0.0512 * v_out -20.5128;
  return Temp;
}

void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);
}

 
    ------------------错误日志输出------ ----------------------
    Hot Tub Monitor 调试流开始
    ----------------------------------
    尝试连接到 WPA SSID:LionsGate
    您已连接到网络
    3
    IP地址:192.168.1.137
    192.168.1.137
    信号强度(RSSI):-27
    向 Xively 发送 RSSI:-27
    向 Xively 发送温度:227.32
    发送Ph到Xively:6.98
    向 Xively 发送 ORP:0.49
    上传到 Xively
    xivelyclient.put 返回 -1
    HTTP 错误
    向 Xively 发送 RSSI:-27
    向 Xively 发送温度:154.33
    发送Ph到Xively:6.94
    向 Xively 发送 ORP:0.87
    上传到 Xively
    xivelyclient.put 返回 -1
    HTTP 错误
    向 Xively 发送 RSSI:-27
    向 Xively 发送温度:147.25
    发送Ph到Xively:6.94
    向 Xively 发送 ORP:0.83
    上传到 Xively
    没有可用的插座
    xivelyclient.put 返回 -1
    没有可用的插座
    HTTP 错误
    向 Xively 发送 RSSI:-27
    向 Xively 发送温度:149.91
    发送Ph到Xively:6.94
    向 Xively 发送 ORP:0.87
    上传到 Xively
    没有可用的插座
    xivelyclient.put 返回 -1
    没有可用的插座
    HTTP 错误

4

1 回答 1

1

感谢您为您的问题提供了如此多的背景知识,它使测试和找出您的问题变得容易。只要你连接到你的 wifi 就可以了,看起来你的代码应该很好,除了所有的导入。我所做的只是删除你一开始的额外进口。关于设置您自己的 DNS 服务器,您可以这样做,但这可能是导致您的问题的原因。如果您担心 DNS,最好手动设置 IP 而不要依赖 DNS。

以下是我从您的代码中复制的内容的修改版本。希望它适用于您的网络和提要。我在带有官方 Arduino WiFi 扩展板的 UNO 和 Mega ADK(我只有 Mega)上对其进行了测试,并且两者都适用。

// Libraries in use
#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>
/*
     Hot Tub Monitor 
         --- If you are only calibrating your sensor, use the calibrate sensor script.  This is for submerged operation.
     This script will allow you to test to ensure the temp, Ph, and ORD sensors are operating and are calibrated correctly.----

     This script was written for the MEGA 2560 with the wireless shield, connecting in to Xively's cloud graph service

     The Mega 2560 with the Wireless shield and Probes uses the following pins:
     *  SPI bus interface 
     *  Onboard MicroSD card reader (uses SD Lib) on digital pin 4
     *  Digital Pins 7, 50, 51, 52, and 53 are reserved and shouldn't be used
     *  Digital Pin 18 is set to OUTPUT to power the temp probe on/off 
     *  Atlas Scientific Temp Sensor input on Analog Uno pin A4
     *  Phidgets Ph Sensor input on Analog Uno pin A2
     *  Phidgets ORD Sensor input on Analog Uno pin A0
*/


// setting up some of the globals
const int TEMPPIN = 4;
const int PHPIN = 1;
const int ORPPIN = 0;
boolean isDebugEnabled = true;   //setting up the debug state
int status = WL_IDLE_STATUS;
char ssid[] = "LMI-GUEST"; //  your network SSID (name) 
char pass[] = "2009LMIGuest!";    // your network password (use for WPA, or use as key for WEP)

// My Xively key to let you upload data
char xivelyKey[] = "<snip for security>";
// My xively feed ID
#define xivelyFeed 121601
// My datastreams
char myWirelessStrStream[] = "MonitorWirelessStrength";
char myORPStream[] = "ORP";
char myPhStream[] = "Ph";
char myTempStream[] = "Temp";

// Creating the datastreams
XivelyDatastream datastreams[] = {
    XivelyDatastream(myWirelessStrStream, strlen(myWirelessStrStream), DATASTREAM_FLOAT),
    XivelyDatastream(myORPStream, strlen(myORPStream), DATASTREAM_FLOAT),
    XivelyDatastream(myPhStream, strlen(myPhStream), DATASTREAM_FLOAT), 
    XivelyDatastream(myTempStream, strlen(myTempStream), DATASTREAM_FLOAT)
    };
XivelyFeed feed(xivelyFeed, datastreams, 4);

//starting the Xively client
WiFiClient client;
XivelyClient xivelyclient(client);


void setup() {
  if (isDebugEnabled) {   //setting up the debug stream
    Serial.begin(9600);
    Serial.println("Hot Tub Monitor Debug Stream Starting");
    Serial.println("-----------------------------------");
  }
  pinMode(12,OUTPUT);   //this pin turns on the temp sensor - battery saver to have this on/off switchable

  // connect to the wifi
  // check for presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 

  //Connect to the wifi network
    while ( status != WL_CONNECTED) { 
      Serial.print("Attempting to connect to WPA SSID: ");
      Serial.println(ssid);
      // Connect to WPA/WPA2 network:    
      status = WiFi.begin(ssid, pass);

      // wait 10 seconds for connection:
      delay(10000);
  }
  Serial.println("You're connected to the network");
  Serial.println(status);
  //printCurrentNet();
  printWifiData();

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);

}

void loop() {
// report on the WiFi Signal strength  
   long rssi = WiFi.RSSI();
  if (isDebugEnabled) {   //send the signal str to Xively
    Serial.print("Sending RSSI to Xively:  ");
    Serial.println(rssi);
  }
    // print the received signal strength:
  datastreams[0].setFloat(rssi);

//  get the temp from our Atlas Sci probe
  float tempC=get_temp();
  float tempF = (tempC*1.8)+32;
  datastreams[1].setFloat(tempF);

// debugging info for temp
  if (isDebugEnabled) {   //send the temp to Xively
    Serial.print("Sending Temp to Xively:  ");
    Serial.println(tempF);
  }

// get the Ph from our phidget's monitor  
  float Ph=get_Ph(tempC);
  datastreams[2].setFloat(Ph);

// debugging info for Ph
  if (isDebugEnabled) {   //send the Ph to Xively
    Serial.print("Sending Ph to Xively:  ");
    Serial.println(Ph);
  } 

// get the Ph from our phidget's monitor  
  float ORP=get_ORP();
  datastreams[3].setFloat(ORP);

// debugging info for Ph
  if (isDebugEnabled) {   //send the Ph to Xively
    Serial.print("Sending ORP to Xively:  ");
    Serial.println(ORP);
  } 


  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  Serial.print("xivelyclient.put returned ");
  Serial.println(ret);


   delay(10000);
}  
float get_Ph(float tempC) {  
  float Ph = analogRead(PHPIN);
  Ph = 7 -((2.5 - (Ph/200))/((0.257179 + 0.0000941468)*tempC));  // convert to the ph
  return Ph;
  }

float get_ORP() {
  float ORP = analogRead(ORPPIN);
  ORP = (2.5 - (ORP/200))/1.037;  // convert to proper ORP
  return ORP;
  } 

 float get_temp(){      
  float v_out;
  float Temp;
  digitalWrite(A4, LOW);   //wtf is this for?
  digitalWrite(12, HIGH);
  delay(2);
  v_out = analogRead(4);
  digitalWrite(12, LOW);
  v_out*=.0048;
  v_out*=1000;
  Temp=0.0512 * v_out -20.5128;
  return Temp;
}

void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);
}

希望这可以帮助。

于 2013-07-01T16:26:30.463 回答