如何将 Arduino MKR GSM 连接到 Arduino IoT 云服务?

物联网 MQTT 阿杜伊诺 云计算
2021-06-02 17:25:59

我在将 MKR GSM 连接到 Arduino Cloud 平台时遇到问题。我的代码如下所示:

.ino 文件。

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Bin One"
  https://create.arduino.cc/cloud/things/acdff5db-845e-42f4-851d-6b8367a3c258 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudTemperatureSensor temperature2;
  CloudTemperatureSensor temperature3;
  CloudTemperatureSensor temperature1;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_Sensor.h>

// everything to control the sensors
#define DHTPIN1            5
#define DHTPIN2            4
#define DHTPIN3            6

#define DHTTYPE           DHT22

DHT_Unified dht1(DHTPIN1, DHTTYPE);
DHT_Unified dht2(DHTPIN2, DHTTYPE);
DHT_Unified dht3(DHTPIN3, DHTTYPE);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
  
  dht1.begin();
  dht2.begin();
  dht3.begin();

  sensor_t sensor;
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  
  // Get temperature event
  sensors_event_t event1;
  dht1.temperature().getEvent(&event1);
  
  temperature1 = event1.temperature;
  
  // Get temperature event
  sensors_event_t event2;
  dht2.temperature().getEvent(&event2);
  
  temperature2 = event2.temperature;
  
  // Get temperature event
  sensors_event_t event3;
  dht3.temperature().getEvent(&event3);
  
  temperature3 = event3.temperature;
  
  delay(3600000);
}

.h 文件

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>


const char THING_ID[]      = "acdff5db-845e-42f4-851d-6b8367a3c258";

const char GPRS_APN[]      = SECRET_APN;
const char PINNUMBER[]     = SECRET_PIN;
const char GPRS_LOGIN[]    = SECRET_USERNAME;
const char GPRS_PASSWORD[] = SECRET_PASSWORD;


CloudTemperatureSensor temperature2;
CloudTemperatureSensor temperature3;
CloudTemperatureSensor temperature1;

void initProperties(){

  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addProperty(temperature2, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(temperature3, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(temperature1, READ, ON_CHANGE, NULL);

}

GSMConnectionHandler ArduinoIoTPreferredConnection(PINNUMBER, GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD);

我从串行得到的输出是

SIM card ok
GPRS.attachGPRS(): 4
Sending PING to outer space...
GPRS.ping(): -2
PING failed
Retrying in  "500" milliseconds

我已经在多个地方尝试过这个代码(我住在农村,所以我认为连接可能是问题),并且电池连接到 MKR GSM。

当它不能与 sim 卡一起使用时,我专门与 Arduino 一起使用,我在手机中使用了 sim。两者都还没有奏效。

我以前使用 MKR GSM 和为其购买的 sim 卡发送短信,同时监视粮仓几个月,所以我认为连接到 Arduino Cloud 一定是我做错了。

提前致谢!

0个回答
没有发现任何回复~