使用SIM 800L将数据发送到Thingspeak时如何处理错误

时间:2019-08-06 11:05:30

标签: arduino gps gsm

我刚开始使用此Thingspeak服务器 这次我想使用SIM 800L将gps纬度和较长时间发送到我的Thingspeak服务器 我已经成功连接到我的APN,但是当它尝试连接到Thingspeak时,串行监视器返回断开连接 这是我的arduino代码,就像

// Select your modem:
#define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM900
// #define TINY_GSM_MODEM_A6
// #define TINY_GSM_MODEM_A7
// #define TINY_GSM_MODEM_M590
// #define TINY_GSM_MODEM_ESP8266

// Increase RX buffer if needed
//#define TINY_GSM_RX_BUFFER 512

#include <TinyGsmClient.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>
// Uncomment this if you want to see all AT commands
//#define DUMP_AT_COMMANDS

// Uncomment this if you want to use SSL
//#define USE_SSL

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial

// Use Hardware Serial on Mega, Leonardo, Micro
#define gsm Serial1

TinyGPS gps;
//SoftwareSerial gsm(2, 3); // RX, TX
#define gsm Serial1
//SoftwareSerial ss(6, 7);
#define ss Serial2

// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn\[\]  = "telkomsel";
const char user\[\] = "";
const char pass\[\] = "";

// Server details
const char server\[\] = "api.thingspeak.com";
const char resource\[\] = "https://api.thingspeak.com/update?api_key=OJDV7CZKI82QQD4D&field1=0";
//buffers"https://api.thingspeak.com/update?api_key=OJDV7CZKI82QQD4D&field1=%s&field2=%s";
char buffer\[100\];
char latbuffer\[20\];
char lonbuffer\[20\];

#ifdef DUMP_AT_COMMANDS
  #include <StreamDebugger.h>
  StreamDebugger debugger(gsm, SerialMon);
  TinyGsm modem(debugger);
#else
  TinyGsm modem(gsm);
#endif

#ifdef USE_SSL
  TinyGsmClientSecure client(modem);
  const int  port = 443;
#else
  TinyGsmClient client(modem);
  const int  port = 80;
#endif

float flat, flon;
unsigned long age;

void setup() {
  // Set console baud rate (
  SerialMon.begin(115200);
  delay(10);

  // Set GSM module baud rate
  gsm.begin(57600);
  delay(2000);

  // Set GPS module baud rate
  ss.begin(9600);
  delay(2000);

  SerialMon.println(F("Initializing modem..."));
  modem.restart();
  // Unlock your SIM card with a PIN
  //modem.simUnlock("1234");
}

void loop() {

  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      //Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  if (newData)
  {
    gps.f_get_position(&flat, &flon, &age);
    SerialMon.print("LAT=");
    SerialMon.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
    SerialMon.print(" LON=");
    SerialMon.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
    delay(10);

    //_______
    SerialMon.print(F("Waiting for network..."));
    if (!modem.waitForNetwork()) {
      SerialMon.println(" fail...");
      delay(5000);
      return;     
    }

    SerialMon.println(" OK");

    SerialMon.print(F("Connecting to "));
    SerialMon.print(apn);

    if (!modem.gprsConnect(apn, user, pass)) {
      SerialMon.println(" fail...");
      delay(5000);
      return;
    }
    SerialMon.println(" OK");

    SerialMon.print(F("Connecting to "));
    SerialMon.print(server);
    if (!client.connect(server, port)) {
      SerialMon.println(" fail");
      delay(5000);
    }

    SerialMon.println(" OK");


    sprintf(buffer, resource,dtostrf(flat, 6,4,latbuffer),  dtostrf(flon ,6,4,lonbuffer) );
    // Make a HTTP GET request:
    client.print(String("GET ") + buffer + " HTTP/1.0\r\n");
    client.print(String("Host: ") + server + "\r\n");
    client.print("Connection: close\r\n\r\n");


    unsigned long timeout = millis();

    // Shutdown

    client.stop();
    SerialMon.println(F("Server disconnected"));
  //_______

    }
    gps.stats(&chars, &sentences, &failed);
      SerialMon.print(" CHARS=");
      SerialMon.print(chars);
      SerialMon.print(" SENTENCES=");
      SerialMon.print(sentences);
      SerialMon.print(" CSUM ERR=");
      SerialMon.println(failed);
    if (chars == 0){
      SerialMon.println("** No characters received from GPS: check wiring **");
    }

    delay(5000);
}][1]

希望你们能帮助我 我在上面的图片上附加了我的错误

0 个答案:

没有答案