MQTT尝试将Arduino IOT板上的云平台连接失败

时间:2018-09-28 05:40:08

标签: c arduino cloud

我需要一些有关如何建立连接以及将一组数据发布到云平台的建议。我用于设备的库是PubSubClient。我使用的设备与cc3000(即DFR0321)兼容。我使用的云平台是Favoriot.com。有关如何建立MQTT的文档为https://platform.favoriot.com/tutorial/。尽管参数正确,但连接仍然失败。

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <cc3000_PubSubClient.h>
#include <SparkFun_ADXL345.h>
#include <string.h>
#include "utility/debug.h"

// Update these with values suitable for your network.
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   7  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

#define api "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IlJ5YW5MaW0iLCJyZWFkX3dyaXRlIjp0cnVlLCJpYXQiOjE1MzMwOTY0NDN9.uKySjJX3AqggeGS6Nl1gFnNHH9L_wb2spOKNvAM5KbE"
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
#define WLAN_SSID       "Biey Paradise"
#define WLAN_PASS       "biey8288744"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

ADXL345 adxl = ADXL345(); //variable adxl is an instance of the ADXL345 library
int x, y, z;

Adafruit_CC3000_Client client;

// We're going to set our broker IP and union it to something we can use
union ArrayToIp {
  byte array[4];
  uint32_t ip;
};

void callback(char* topic, byte* payload, unsigned int length) {

}

ArrayToIp server = { 182, 32, 245, 104 }; //favoriot
cc3000_PubSubClient mqttclient(server.ip, 1883, callback, client, cc3000);

void setup()
{
  Serial.begin(115200);

  // Allow the hardware to sort itself out

  Serial.begin(115200);
  Serial.println(F("Hello, CC3000!\n"));

  adxl.powerOn();
  adxl.setRangeSetting(16); // Range Setting
  // 2-16
  // Low val : Sensitive
  // High val: Wide measurement range

  //Set Activity/ Inactivity Thresholds (0-255)
  adxl.setActivityThreshold(75);    //62.5mg per increment
  adxl.setInactivityThreshold(75);  //62.5mg per increment
  adxl.setTimeInactivity(1);        // how many seconds of no activity is inactive?

  //Look of activity movement on this axes - 1 == on; 0 == off
  adxl.setActivityXYZ(1, 0, 0);

  //Look of inactivity movement on this axes - 1 == on; 0 == off
  adxl.setInactivityXYZ(1, 0, 0);

  //Set interrupt
  adxl.InactivityINT(1);
  adxl.ActivityINT(1);
  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);

  Serial.println(F("\nInitialising the CC3000 ..."));
  if (!cc3000.begin()) {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while (1);
  }

  /* Attempt to connect to an access point */
  char *ssid = WLAN_SSID;             /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);

  /* NOTE: Secure connections are not available in 'Tiny' mode! */
  if (!cc3000.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 (!cc3000.checkDHCP()) {
    delay(100); // ToDo: Insert a DHCP timeout!
    delay(1500);
  }

  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttclient.connect("ClientIDFavoriot", api, api)) {
      Serial.println("MQTT Success");
      mqttclient.subscribe("inTopic");
    } else {
      Serial.print("failed");
      Serial.println(mqttclient.connect("", api, api));
      Serial.println(" try again in 3 seconds");
      // Wait 5 seconds before retrying
      delay(3000);
    }
  }
}

void loop()
{
  mqttclient.publish("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IlJ5YW5MaW0iLCJyZWFkX3dyaXRlIjp0cnVlLCJpYXQiOjE1MzMwOTY0NDN9.uKySjJX3AqggeGS6Nl1gFnNHH9L_wb2spOKNvAM5KbE/hello", "Hello from Wildfire");
  mqttclient.loop();
}

0 个答案:

没有答案