买了块WiDo想与自建的WebAPI通信失败,求大神指点

2015-06-111.2万
AI 快速预览详细 收起
本文讨论了使用DFRobot WiDo与自建WebAPI通信失败的问题,并提供了详细的解决方案。通过配置Adafruit_CC3000库和正确的连接参数(如WLAN_SSID、WLAN_PASS和WLAN_SECURITY),可以解决通信失败的问题。文章还提供了具体的代码示例和调试步骤,帮助用户快速实现设备间的稳定通信。
买了块WiDo想与自建的WebAPI通信失败,求大神指点
/***************************************************
* This is an example for the DFRobot WiDo - Wifi Integrated IoT lite sensor and control node
*
* Designed specifically to work with the DFRobot WiDo products:
*
*
* The main library is forked from Adafruit
*
* Written by Lauren
* BSD license, all text above must be included in any redistribution
*
****************************************************/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"

#define WiDo_IRQ   7
#define WiDo_VBAT  5
#define WiDo_CS    10

Adafruit_CC3000 WiDo = Adafruit_CC3000(WiDo_CS, WiDo_IRQ, WiDo_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "ChinaNet-Y2AT"           // cannot be longer than 32 characters!
#define WLAN_PASS       "lhzmkgww"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define TOKEN           "7daf"  //attach your own token generated from the DFRobot community website

// To get the full feature of CC3000 library from Adafruit, please comment the define command below
// #define CC3000_TINY_DRIVER    // saving the flash memory space for leonardo

#define TIMEOUT_MS      2000

void setup(){
  Serial.begin(115200);
  Serial.println(F("Hello, Wido!\n"));

  /* Initialise the module and test the hardware connection */
  Serial.println(F("\nInitialising the CC3000 ..."));
  if (!WiDo.begin())
  {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while(1);
  }

  /* NOTE: Secure connections are not available in 'Tiny' mode!
   By default connectToAP will retry indefinitely, however you can pass an
   optional maximum number of retries (greater than zero) as the fourth parameter.
   */
  if (!WiDo.connectToAP(WLAN_SSID,WLAN_PASS,WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.println(F("Connected!"));

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!WiDo.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  
}


void loop(){
  static Adafruit_CC3000_Client IoTclient;

  if(IoTclient.connected()){
    //Convert LM35 sensor reading to degrees
    int sensValue = analogRead(0) *0.0048828125 * 100;

    char clientString[50];
    sprintf(clientString,"%s%s%s%s","GET  ","/Data?Value=",TOKEN," HTTP/1.1");
    Serial.println(clientString);

    // attach the token to the IOT Server and Upload the sensor dataIoTclient
    IoTclient.fastrprintln(clientString);

    IoTclient.fastrprint(F("\r\n"));
    IoTclient.fastrprint(F("\r\n"));

    Serial.println();
//    Serial.println("Upload data to the IoT Server");

    /* Read data until either the connection is closed, or the idle timeout is reached. */
    unsigned long lastRead = millis();
    while (IoTclient.connected() && (millis() - lastRead < TIMEOUT_MS)) {
      while (IoTclient.available()) {
        char c = IoTclient.read();
        Serial.print(c);
        lastRead = millis();
      }
    }
    IoTclient.close();
  }
  else{
    // Config the Host IP and DFRobot community IoT service port
    // Data Upload service PORT:  8124
    // Real time controling service PORT: 9120
    uint32_t ip = WiDo.IP2U32(211,149,219,61);
    IoTclient = WiDo.connectTCP(ip,8010);
    Serial.println("Connecting IoT Server...");
  }

  delay(5000);

}
买了块WiDo想与自建的WebAPI通信失败,求大神指点图1

请求的地址在浏览器中可以用 211.149.219.61:8010/api/data?value=eeeeeeuuuio


请大神们指点迷津。谢谢


创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(11)
yijian3014
yijian3014
停止工作是不是防止内存溢出?
yijian3014
yijian3014
停止工作是不是防止内存溢出?
yijian3014
yijian3014
停止工作是不是防止内存溢出?
yijian3014
yijian3014
说一句外行话,是不是他的自我工作机制?应该分辩一下是停止工作还是死机!
visionsl
visionsl
我测试了一下, 其实 " HTTP/1.1"可以完全去掉, 就可以正常工作了

visionsl
visionsl
LZ的服务器段用的是WebAPI,是怎么看出来的?
Cain
Cain
可以试试降低波特率,加大通讯间隔等方式降低wido负载、避免通信口堵塞
maxfeel
maxfeel
作者
感谢二位的答复,问题已经解决,问题出在get 传参数最后一个参数,实例代码给的默认是 HTTP/1.1,实际我服务器段用的是WebAPI,所以改成HTTPAPI/2.0 功能就实现了。但是目前测试感觉WidDo并不稳定,在单独加电(5V 1A)运行数个小时以后就停止工作。用USB接电脑串口监视,监视了一段时间串口也会停止接受数据。我个人感觉是硬件问题,想问问各位WIDO的稳定情况如何?????
Cain
Cain
恩,说得对,“error400是请求出错
由于语法格式有误,服务器无法理解此请求。 不作修改,客户程序就无法重复此请求”
R2D2
R2D2
看反馈是你的服务器已经链接,有反馈数据,但是你的service并没有正确响应wido的request。建议自己看看webservice里的调用格式。
Cain
Cain
通信格式是什么?看反馈是完全没连上啊。先试试能否连上wifi,再试通信协议是否正确
- 没有更多了 -