Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Https MQTT客户机退出,然后调用HTTP通信_Https_Arduino_Mqtt - Fatal编程技术网

Https MQTT客户机退出,然后调用HTTP通信

Https MQTT客户机退出,然后调用HTTP通信,https,arduino,mqtt,Https,Arduino,Mqtt,我正在从事一个项目,目前我正在通过shiftr.io在3个MKR1000板之间进行通信 这种情况下,有两个现场传感器(发送器)测量湿度,并将其传回接收器集线器 我还使用一个推送框将数据从发送者发送到google sheets(GS)。此警报由接收者激活/禁用,但实际发送到GS的数据由发送者板处理 因此,问题是,当我切换GoogleSheet函数时,与代理(shiftr.io)的连接会断开。我通过让它在每次向GS发送数据时重新连接到MQTT代理来解决这个问题。但这是不正确的。。。现在无法关闭它 当

我正在从事一个项目,目前我正在通过shiftr.io在3个MKR1000板之间进行通信

这种情况下,有两个现场传感器(发送器)测量湿度,并将其传回接收器集线器

我还使用一个推送框将数据从发送者发送到google sheets(GS)。此警报由接收者激活/禁用,但实际发送到GS的数据由发送者板处理

因此,问题是,当我切换GoogleSheet函数时,与代理(shiftr.io)的连接会断开。我通过让它在每次向GS发送数据时重新连接到MQTT代理来解决这个问题。但这是不正确的。。。现在无法关闭它

当数据记录到GS时,wifi仍然连接

当我注释掉将数据发送到GS的函数时,它会很好地打开和关闭

向GS发送数据的代码是用HTTP编写的,这两个协议之间是否存在冲突

我不确定该发布什么代码

这是向GS发送数据的代码

API service using WiFi Client through PushingBox then relayed to Google @ https://docs.google.com/spreadsheets/d/18PuLae58fKfvN_ZnHHgYrBwy52JpvlNAgfhfxFYPM/edit#gid=0
  if (client2.connect(WEBSITE, 80))
  {
    client2.print("GET /pushingbox?devid=" + devid
                  + "&humidityData=" + (String) fHum
                  + "&celData="      + (String) fTemp

                 );

    // HTTP 1.1 provides a persistent connection, allowing batched requests
    // or pipelined to an output buffer
    client2.println(" HTTP/1.1");
    client2.print("Host: ");
    client2.println(WEBSITE);
    client2.println("User-Agent: MKR1000/1.0");
    client2.println();
    Serial.println("\nData Sent");
  }

  connect(); // this is to reconnect to MQTT client after it auto disconnects aftr each send.
}
除非两者之间存在已知问题,否则我不确定可以做什么,这一定与上述代码与MQTT客户端冲突有关

当我发表评论时,没有任何问题

任何想法

多谢各位

如果问题在本节中

    #include <SPI.h>
#include <WiFi101.h>
#include <MQTT.h>
#include <DHT.h>;
#include <DHT_U.h>

//////////Constants\\\\\\\

#define DHTPIN 6     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

///////Variables\\\\\\\\

int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value
bool SheetBegin = false;

String Temp_str;
String Hum_str;
String Payload;

char Atemp[50];
char AHum[50];

float fTemp;
float fHum;

unsigned long lastMillis = 0;

int  fehrData = 0;
int  hicData = 0;
int hifData = 0;


const char ssid[] = "*";
const char pass[] = "*";

const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "v7E593DA053CAC70"; //device ID on Pushingbox for our Scenario


WiFiClient client2;
MQTTClient client;





void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  client.begin("broker.shiftr.io", client2);
  client.onMessageAdvanced(messageReceived);
  dht.begin();


  connect();
}

void loop() {
  client.loop();

  if (!client.connected()) {
    connect();
  }
  ReadSensortoclient1();

  if (SheetBegin) {
    Serial.println("printing to sheets");
    //SendToSheets();
  }


}

void ReadSensortoclient1() {
  //Read data and store it to variables hum and temp
  fHum = dht.readHumidity();
  fTemp = dht.readTemperature();
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print(fHum);
  Serial.print(" %, Temp: ");
  Serial.print(fTemp);
  Serial.println(" Celsius");
  delay(2000); //Delay 2 sec.

  Temp_str = String(fTemp); //converting ftemp (the float variable above) to a string
  Temp_str.toCharArray(Atemp, Temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...

  Hum_str = String(fHum); //converting Humidity (the float variable above) to a string
  Hum_str.toCharArray(AHum, Hum_str.length() + 1); //packaging up the data to publish to mqtt whoa...


  // publish a message roughly 2 second.
  if (millis() - lastMillis > 2000) {
    lastMillis = millis();
    client.publish("TempTopic", Atemp);
    client.publish("HumidityTopic", AHum);

  }
}



void SendToSheets() {
  //API service using WiFi Client through PushingBox then relayed to Google @ https://docs.google.com/spreadsheets/d/18PuLae58fZ0KfvN_ZnHHgYrBwy52JpvlNAgfhfxFYPM/edit#gid=0
  if (client2.connect(WEBSITE, 80))
  {
    client2.print("GET /pushingbox?devid=" + devid
                  + "&humidityData=" + (String) fHum
                  + "&celData="      + (String) fTemp

                 );

    // HTTP 1.1 provides a persistent connection, allowing batched requests
    // or pipelined to an output buffer
    client2.println(" HTTP/1.1");
    client2.print("Host: ");
    client2.println(WEBSITE);
    client2.println("User-Agent: MKR1000/1.0");
    client2.println();
    Serial.println("\nData Sent");
  }

  connect();
}

void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.print("\nconnecting...");
  while (!client.connect("Sender", "6ddf11", "027c44da6fa7006")) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nconnected to sender!");
  client.subscribe("SheetTopic");


}
void messageReceived(MQTTClient * client, char topic[], char payload[], int payload_length) {

  if (strcmp(payload, "Start") == 0) {
    SheetBegin = true;
    Serial.println(SheetBegin);
  }

  else if (strcmp(payload, "End") == 0) {
    SheetBegin = false;
    Serial.println(SheetBegin);
  }
}
#包括
#包括
#包括
#包括,;
#包括
//////////常数\\\\\\\
#定义DHTPIN 6//我们连接到的引脚
#定义DHT类型DHT22//DHT 22(AM2302)
DHT DHT(DHTPIN,DHT类型);//为正常16mhz Arduino初始化DHT传感器
///////变数\\\\\\\\
int chk;
浮沉的嗡嗡声//存储湿度值
浮子温度//存储温度值
bool-SheetBegin=false;
字符串温度;
字符串Hum_str;
管柱有效载荷;
char-Atemp[50];
char AHum[50];
浮动fTemp;
浮动fHum;
无符号长lastMillis=0;
int-fehrData=0;
int hicData=0;
int-hifData=0;
常量字符ssid[]=“*”;
常量字符传递[]=“*”;
const char网站[]=“api.pushingbox.com”//pushingbox API服务器
常量字符串devid=“v7E593DA053CAC70”//我们场景的Pushingbox上的设备ID
WiFiClient客户端2;
MQTTClient;
无效设置(){
Serial.begin(9600);
WiFi.begin(ssid,pass);
client.begin(“broker.shiftr.io”,client2);
client.onMessageAdvanced(messageReceived);
dht.begin();
connect();
}
void循环(){
client.loop();
如果(!client.connected()){
connect();
}
ReadSensorToClient 1();
如果(开始){
Serial.println(“打印到纸张”);
//sendtoseets();
}
}
void ReadSensortoclient1(){
//读取数据并将其存储到变量hum和temp
fHum=dht.read湿度();
fTemp=dht.readTemperature();
//将温度和湿度值打印到串行监视器
连续打印(“湿度:”);
连续打印(fHum);
连续打印(“%,温度:”);
串行打印(fTemp);
串行打印项次(“摄氏度”);
延迟(2000);//延迟2秒。
Temp_str=String(fTemp);//将fTemp(上面的浮点变量)转换为字符串
Temp_str.toCharArray(Atemp,Temp_str.length()+1);//打包数据以发布到mqtt whoa。。。
Hum_str=String(fHum);//将湿度(上面的浮点变量)转换为字符串
Hum_str.toCharArray(AHum,Hum_str.length()+1);//打包数据以发布到mqtt whoa。。。
//发布消息大约2秒钟。
如果(毫秒()-lastMillis>2000){
lastMillis=millis();
客户。发布(“TENTOPIC”,Atemp);
客户。发布(“HumidityTopic”,AHum);
}
}
void SendToSheets(){
//通过PushingBox使用WiFi客户端的API服务,然后转发至Google@https://docs.google.com/spreadsheets/d/18PuLae58fZ0KfvN_ZnHHgYrBwy52JpvlNAgfhfxFYPM/edit#gid=0
if(客户端2.connect(网站,80))
{
客户端2.打印(“获取/推送框?设备=“+devid
+“&humidityData=“+(字符串)fHum”
+“&celData=“+(字符串)fTemp”
);
//HTTP 1.1提供了持久连接,允许批处理请求
//或通过管道连接到输出缓冲区
client2.println(“HTTP/1.1”);
客户2.打印(“主机:”);
client2.println(网站);
client2.println(“用户代理:MKR1000/1.0”);
client2.println();
Serial.println(“\n已发送数据”);
}
connect();
}
void connect(){
串行打印(“检查wifi…”);
while(WiFi.status()!=WL_已连接){
连续打印(“.”);
延迟(1000);
}
串行打印(“\n连接…”);
而(!client.connect(“发送方”、“6ddf11”、“027c44da6fa7006”)){
连续打印(“.”);
延迟(1000);
}
Serial.println(“\n已连接到发件人!”);
客户。订阅(“SheetTopic”);
}
接收到void message(MQTTClient*客户端,字符主题[],字符负载[],整数负载长度){
如果(strcmp(有效载荷,“启动”)==0){
SheetBegin=true;
Serial.println(SheetBegin);
}
否则如果(strcmp(有效载荷,“结束”)==0){
SheetBegin=false;
Serial.println(SheetBegin);
}
}

问题在于您没有发布的代码。为了清晰起见,我添加了te完整性。我将添加
client2.stop()(阅读回复后)您好,感谢您的回复!然而,在这种情况下,这无助于情况:(