Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

测试 #8

Open
xiezuoru opened this issue May 22, 2019 · 2 comments
Open

测试 #8

xiezuoru opened this issue May 22, 2019 · 2 comments

Comments

@xiezuoru
Copy link
Collaborator

在这里提问

@408881465
Copy link

408881465 commented Jul 18, 2019

我尝试在ESP8266上用pubsubclient连接Siot,试了好几次都没有成功,一直返回**Attempting MQTT connection...failed, rc=-2 try again in 5 seconds** ,应该是连接失败。但是我用[pubsubclient](https://github.com/knolleary/pubsubclient)连接[mosquitto](https://mosquitto.org/download/)MQTT broker没有问题。不知什么原因?

pubsubclient连接SIoT测试代码如下:

`#include <ESP8266WiFi.h>
#include "PubSubClient.h"

const char* ssid = "mywifi";//WIFI名字
const char* password = "wifimima";//WIFI密码
const char* mqtt_server = "192.168.1.23";//MQTT服务器IP
const int mqtt_server_port = 1883;//MQTT服务器端口
const char* username = "siot";//MQTT用户名
const char* passward = "dfrobot";//MQTT用户密码
#define ID_MQTT "175867613117375488" //MQTT的Client ID

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

//灯光函数及引脚定义
void turnOnLed();
void turnOffLed();
const int B_led = 2;

void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();

// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '0') {//如果接收数字“1”,亮灯
turnOffLed();//开灯函数

}else if ((char)payload[0] == '1') {
turnOnLed();//关灯函数
}

}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(ID_MQTT,username,passward)) {
Serial.println("connected");

  client.subscribe("xzr/led");//主题
} else {
  Serial.print("failed, rc=");
  Serial.print(client.state());
  Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
  delay(5000);
}

}
}

void setup() {
pinMode(B_led,OUTPUT);

Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server,mqtt_server_port);
client.setCallback(callback);
digitalWrite(B_led,HIGH);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

}

//打开灯泡
void turnOnLed(){

digitalWrite(B_led,LOW);
}
//关闭灯泡
void turnOffLed(){
digitalWrite(B_led,HIGH);
}

`

@xiezuoru
Copy link
Collaborator Author

我可以确定的是,SIoT没有问题,ESP8266也是能够正常连SIoT的,因为df的obloq模块就是基于ESP8266。因为我手头没有8266模块,也没办法来帮助你。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants