1
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: SIM でネットワークに接続

Last updated at Posted at 2022-11-19

こちらのページを参考にしました。
ESP32をLTEに接続する

SIM7600JCーH を使いました。

image.png

プログラム

sim_test.ino
// ---------------------------------------------------------------------
//	sim_test.ino
//
//					Jan/14/2023
// ---------------------------------------------------------------------
//通信モデムの設定
#define TINY_GSM_MODEM_SIM7600

//通信モデムとESP32がやりとりするためのSerialポートを設定
#define SerialAT Serial1

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

//通信先を設定。自分の通信会社のAPNとUser名、パスワードを設定
const char apn[] = "iijmobile.biz";
const char gprsUser[] = "mobile@iij";
const char gprsPass[] = "iij";

#define	PROGRAM	"sim_test.ino"
#define	VERSION	"2023-1-14 PM 17:48"

//httpsを使いたい時には、BasicHttpsClientのサンプルを見る
const char server[]	 = "httpbin.org";
const char resource[] = "/get";
const int port = 80;

TinyGsm modem(SerialAT);

#define uS_TO_S_FACTOR	        1000000ULL  //Conversion factor for micro seconds to seconds 
#define TIME_TO_SLEEP	         60          //Time ESP32 will go to sleep (in seconds) 
//ボーdの設定
#define PIN_TX	                27
#define PIN_RX	                26
#define UART_BAUD	             115200
#define PWR_PIN	               4
#define LED_PIN	               12
#define POWER_PIN	             25
#define IND_PIN	               36

// ---------------------------------------------------------------------
// [2]:
void sim_prepare_proc()
{
	// Onboard LED light, it can be used freely
	pinMode(LED_PIN, OUTPUT);
	digitalWrite(LED_PIN, LOW);

	// POWER_PIN : This pin controls the power supply of the SIM7600
	pinMode(POWER_PIN, OUTPUT);
	digitalWrite(POWER_PIN, HIGH);

	// PWR_PIN : This Pin is the PWR-KEY of the SIM7600
	// The time of active low level impulse of PWRKEY pin to power on module , type 500 ms
	pinMode(PWR_PIN, OUTPUT);
	digitalWrite(PWR_PIN, HIGH);
	delay(500);
	digitalWrite(PWR_PIN, LOW);

	// IND_PIN: It is connected to the SIM7600 status Pin,
	// through which you can know whether the module starts normally.
	pinMode(IND_PIN, INPUT);
}

// ---------------------------------------------------------------------
// [4]:
void modem_proc()
{
	Serial.println();
	Serial.println("*** modem_proc() start ***");

	SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);

	//モデムの初期化
	Serial.println("Initializing modem...");
	if (!modem.init()) {
		Serial.println("Failed to restart modem, delaying 10s and retrying");
		return;
	}
	Serial.println("enter setNetwork Mode");

//接続開始
	bool result;
	do {
		result = modem.setNetworkMode(38);//2 Automatic, 13 GSM only,	38 LTE only,	51 GSM and LTE only
		delay(500);
	} while (result != true);

	Serial.println("Waiting for network...");
	if (!modem.waitForNetwork()) {
		delay(10000);
		return;
	}

	if (modem.isNetworkConnected()) {
		Serial.println("Network connected");
	}

	Serial.print("Connecting to:");
	Serial.println(apn);
	if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
		delay(10000);
		return;
	}

	bool res = modem.isGprsConnected();
	Serial.print("GPRS status:");
	Serial.println(res);

	IPAddress local = modem.localIP();
	Serial.print("Local IP:");
	Serial.println(local);

	int csq = modem.getSignalQuality();
	Serial.print("Signal quality:");
	Serial.println(csq);
	Serial.println("*** modem_proc() end ***");
}

// ---------------------------------------------------------------------
// [6]:
void http_proc()
{
	//httpの接続開始
	TinyGsmClient client(modem);
	HttpClient http(client, server, port);

	Serial.println("HTTP GET request... ");
	int err = http.get(resource);
	if (err != 0) {
		Serial.println("failed to connect");
		delay(10000);
		return;
	}

//httpの通信結果を表示
	String body = http.responseBody();
	Serial.println("============Response===========");
	Serial.println(body);
	Serial.println("============Response end========");

	http.stop();
	Serial.println("Server disconnected");
}

// ---------------------------------------------------------------------
void setup()
{
	Serial.begin(115200);
	delay(1000);

	Serial.println("*** setup() start  ***");
	sim_prepare_proc();
	Serial.println("*** setup() bbb  ***");

	Serial.println("Wait...");

	delay(3000);
	Serial.println("*** setup ccc  ***");

	modem_proc();
	Serial.println("*** setup ddd  ***");

	http_proc();
	Serial.println("*** setup eee  ***");

//	modem.poweroff();//モデムオフ
//	Serial.println("Poweroff.");
	Serial.println("*** setup() end  ***");
}

// ---------------------------------------------------------------------
void loop()
{
	Serial.println("*** loop aaa  ***");
	delay(1000);
	Serial.println("*** loop bbb  ***");
	http_proc();
	delay(2000);

	bool res = modem.isGprsConnected();
	Serial.print("GPRS status:");
	Serial.println(res);
	Serial.println((res ? "OK: " : "FAILED: "));

	if (!modem.isGprsConnected())
		{
		Serial.println("*** not connected  ***");
		while (!modem.isGprsConnected())
			{
			modem.gprsConnect(apn, gprsUser, gprsPass);
			Serial.println("*** trying to connect  ***");
			delay(10000);
			}
		}
	else
		{
		Serial.println("*** connected  ***");
		}

	Serial.println("*** loop ccc  ***");
	Serial.println(PROGRAM);
	Serial.println(VERSION);
	
	delay(5000);
}

// ---------------------------------------------------------------------

image.png

1
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
1
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?