简体   繁体   English

无法使用Arduino + wifi屏蔽连接到Xively,“ ret = -1无可用插座”

[英]Can't connect to Xively using Arduino + wifi shield, “ret = -1 No sockets available”

Thanks so much for looking guys. 非常感谢各位的光临。 I'm just getting started with Arduino wireless sensor projects and I've hit a clue wall. 我刚开始使用Arduino无线传感器项目,但遇到了麻烦。 I'd appreciate any insight or help troubleshooting my project. 如果您有任何见解或对我的项目进行故障排除,我将不胜感激。

I'm using an Arduino Mega, with the Wifi Shield to attempt to upload 4 channels (all floats) to my Xively feed. 我正在使用Arduino Mega和Wifi Shield,尝试将4个通道(所有浮动)上传到Xively Feed。 I'm using a modified form of the basic tutorial script (code at bottom). 我使用的是基本教程脚本的修改形式(底部代码)。 I verify and upload successfully. 我验证并成功上传。 When I run the board, the initial debugging info looks good. 当我运行电路板时,初始调试信息看起来不错。 When I attempt to do either a put or a get using the Xively client library I get an error (ret = -1, no sockets available, and http error on gets). 当我尝试使用Xively客户端库进行放置或获取操作时,出现错误(ret = -1,没有可用的套接字,并且获取时出现http错误)。 I've attached a serial log of the errors at the bottom as well. 我还在底部附加了错误的串行日志。

I've taken some troubleshooting steps already. 我已经采取了一些故障排除步骤。 I've re-downloaded all the xively libraries, and Wifi libraries to be sure. 我已经重新下载了所有xively库和Wifi库。 I thought manually setting a DNS server (8.8.8.8) might help. 我认为手动设置DNS服务器(8.8.8.8)可能会有所帮助。 I even imported the whole arduino library (although I don't think it should be needed) to get the DNS functionality. 我什至导入了整个arduino库(尽管我认为这不是必需的)以获取DNS功能。 It did not seem to make a difference, so I left it in as a troubleshooting breadcrumb. 它似乎没有什么区别,所以我把它留在了故障排除过程中。 Also as a troubleshooting step, I added a Xively client.get to see if I could pull but not push. 另外,作为故障排除步骤,我添加了一个Xively client.get,以查看是否可以拉出但不能推入。 That generated an HTTP error. 那产生了一个HTTP错误。 I also added a public wide open api key to my project and tried that (instead of the private one). 我还向我的项目添加了一个公共开放的api密钥,并对此进行了尝试(而不是私有的api密钥)。 Still no joy. 仍然没有喜悦。

It sorta feels like although I'm connecting successfully to my wifi, but can't make a real connection to any services. 虽然我可以成功连接到wifi,但是却无法与任何服务建立真正的连接。 I was hoping there was some super obvious step I missed. 我希望能错过一些非常明显的步骤。 I could really use a helpful clue to get me on the right track again. 我真的可以使用一条有用的线索使我再次走上正确的道路。 thanks! 谢谢!

Micah Wyenn 米卡·怀恩(Micah Wyenn)

#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);
}

-----------------------------Error Log Output------------------------------------------
    Hot Tub Monitor Debug Stream Starting
    -----------------------------------
    Attempting to connect to WPA SSID: LionsGate
    You're connected to the network
    3
    IP Address: 192.168.1.137
    192.168.1.137
    signal strength (RSSI):-27
    Sending RSSI to Xively:  -27
    Sending Temp to Xively:  227.32
    Sending Ph to Xively:  6.98
    Sending ORP to Xively:  0.49
    Uploading it to Xively
    xivelyclient.put returned -1
    HTTP Error
    Sending RSSI to Xively:  -27
    Sending Temp to Xively:  154.33
    Sending Ph to Xively:  6.94
    Sending ORP to Xively:  0.87
    Uploading it to Xively
    xivelyclient.put returned -1
    HTTP Error
    Sending RSSI to Xively:  -27
    Sending Temp to Xively:  147.25
    Sending Ph to Xively:  6.94
    Sending ORP to Xively:  0.83
    Uploading it to Xively
    No Socket available
    xivelyclient.put returned -1
    No Socket available
    HTTP Error
    Sending RSSI to Xively:  -27
    Sending Temp to Xively:  149.91
    Sending Ph to Xively:  6.94
    Sending ORP to Xively:  0.87
    Uploading it to Xively
    No Socket available
    xivelyclient.put returned -1
    No Socket available
    HTTP Error

Thanks for providing so much background on your question, it made it easy to test and figure out what your issue is. 感谢您为您的问题提供了如此多的背景知识,它使测试和弄清楚您的问题是很容易的。 As long as you are connecting to your wifi ok, it looks like your code should be good to go except for all the imports. 只要您可以正常连接到wifi,就可以看到您的代码应该不错,除了所有导入。 All I did was remove the extra imports you had at the beginning. 我要做的就是删除您一开始就有的多余导入。 Regarding setting your own DNS server, you can do this but this may have been what was causing your issues. 关于设置自己的DNS服务器,您可以执行此操作,但这可能是导致问题的原因。 If you are worried about DNS you are better of setting the IP manually and not relying on DNS. 如果您担心DNS,则最好手动设置IP,而不要依赖DNS。

Below is my reworked version of what I copied from your code. 以下是我从您的代码中复制的内容的改编版本。 Hopefully it will work with your network and feed. 希望它可以与您的网络和供稿一起使用。 I tested it on an UNO and Mega ADK (only Mega I had) with official Arduino WiFi shield and it worked on both. 我在带有官方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);
}

Hope this helps. 希望这可以帮助。

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

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