0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ESP32-DevKitC-32E + SIM7080G CAT-M/NB-IoT Unit で HTTP通信

Posted at

概要

ESP32開発ボードと通信モジュールを使って通信する。

環境

ESP32開発ボード ESP32-DevKitC-32E (技適◎)
https://www.espressif.com/en/products/devkits/esp32-devkitc
通信モジュール SIM7080G CAT-M/NB-IoT Unit (技適◎)
https://shop.m5stack.com/products/sim7080g-cat-m-nb-iot-unit
SIM SORACOM IoT SIM (plan-D)
https://soracom.jp/store/13380/
Editor Visual Studio Code
https://code.visualstudio.com/
IDE PlatformIO
https://platformio.org/
Library TinyGSM@^0.11.5
https://github.com/vshymanskyy/TinyGSM
ArduinoHttpClient@^0.4.0
https://github.com/arduino-libraries/ArduinoHttpClient

配線

ESP32-DevKitC-32E SIM7080G CAT-M/NB-IoT Unit
5V 5V
GND GND
Serial2 RX:16 TXD
Serial2 TX:17 RXD

実装

TinyGSM のサンプルを削りながら実装。
SORACOMのAPNについては、以下を参照。
https://soracom.jp/start/

#define TINY_GSM_MODEM_SIM7080

#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200

const char apn[] = "soracom.io";
const char gprsUser[] = "sora";
const char gprsPass[] = "sora";

const char server[] = "vsh.pp.ua";
const char resource[] = "/TinyGSM/logo.txt";
const int port = 80;

#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>

TinyGsm modem(Serial2);
TinyGsmClient client(modem);
HttpClient http(client, server, port);

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

  TinyGsmAutoBaud(Serial2, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);

  modem.restart();

  Serial.println("@ Wait For Network @");
  if (!modem.waitForNetwork())
  {
    // TODO:エラー処理
    return;
  }

  Serial.println("@ Wait For Network Connected @");
  if (!modem.isNetworkConnected())
  {
    // TODO:エラー処理
    return;
  }

  Serial.println("@ Connect GPRS: " + String(apn) + " @");
  if (!modem.gprsConnect(apn, gprsUser, gprsPass))
  {
    // TODO:エラー処理
    return;
  }

  Serial.println("@ Connected GPRS @");
  if (!modem.isGprsConnected())
  {
    // TODO:エラー処理
    return;
  }

  Serial.println("@ HTTP Get @");
  int err = http.get(resource);
  if (err != 0)
  {
    // TODO:エラー処理
    return;
  }

  int status = http.responseStatusCode();
  Serial.println("@ Status Code: " + String(status) + " @");
  if (!status)
  {
    // TODO:エラー処理
    return;
  }

  Serial.print("@ Header @");
  while (http.headerAvailable())
  {
    String headerName = http.readHeaderName();
    String headerValue = http.readHeaderValue();
    Serial.println("    " + headerName + " : " + headerValue);
  }

  int length = http.contentLength();
  if (length >= 0)
  {
    Serial.println("@ Content length: " + String(length) + " @");
  }

  Serial.println("@ Body @");
  String body = http.responseBody();
  Serial.println(body);

  http.stop();
  Serial.println("@ Stop HTTP @");

  modem.gprsDisconnect();
  Serial.println("@ Disconnect GPRS @");
}

void loop()
{
}

検証

アップロードしシリアルモニターで取得できることを確認。

@ Wait For Network @
@ Wait For Network Connected @
@ Connect GPRS: soracom.io @
@ Connected GPRS @
@ HTTP Get @
@ Status Code: 200 @
@ Header @    Server : nginx/1.10.3 (Ubuntu)
    Date : Mon, 13 Jun 2022 08:31:46 GMT
    Content-Type : text/plain; charset=UTF-8
    Content-Length : 121
    Connection : close
    X-Powered-By : Express
    Access-Control-Allow-Origin : *
    Access-Control-Allow-Headers : X-Requested-With
    Cross-Origin-Resource-Policy : cross-origin
    Accept-Ranges : bytes
    Cache-Control : public, max-age=86400
    Last-Modified : Wed, 27 Sep 2017 09:03:12 GMT
    ETag : W/"79-15ec2936080"
@ Content length: 121 @
@ Body @

  _____            _____  _____  _____
    |  | |\ | \_/ |  ___ |_____ |  |  |
    |  | | \|  |  |_____| _____||  |  |


@ Stop HTTP @
@ Disconnect GPRS @

所感

### Unhandled:〜といったログが何度も表示され原因が判らなかったが、配線とコーディングをやり直したら発生しなくなった。配線が間違っていたのではないかと思われる。

PWRKEYの設定が必要かと考えたが、CAT-Mからは端子が出ておらず何も設定できなかった。CAT-M側で設定してくれているのかもしれない。

技適対応済の構成で簡単に試すことが出来ました。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?