ESP32→LTE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//通信モデムの設定
#define TINY_GSM_MODEM_SIM7600
 
//シリアルモニタに出力するためのserialを設定
#define SerialMon Serial
 
//通信モデムとESP32がやりとりするためのSerialポートを設定
#define SerialAT Serial1
 
#include <TinyGsmClient.h>
#include <Ticker.h>
#include <ArduinoHttpClient.h>
 
//通信先を設定。自分の通信会社のAPNとUser名、パスワードを設定
const char apn[] = "lte.ocn.ne.jp";
const char gprsUser[] = "mobileid@ocn";
const char gprsPass[] = "mobile";
 
//アクセス先の設定 http://mkbtm.net//SIM7600/test.txt
//httpsを使いたい時には、BasicHttpsClientのサンプルを見る
const char server[] = "mkbtm.net";
const char resource[] = "/SIM7600/test.txt";
const int port = 80;
 
TinyGsm modem(SerialAT);
 
Ticker tick;//LEDの点滅用
 
#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
 
void setup()
{
SerialMon.begin(115200);
delay(10);
 
// 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);
 
attachInterrupt(IND_PIN, []() {
detachInterrupt(IND_PIN);
// If SIM7600 starts normally, then set the onboard LED to flash once every 1 second
tick.attach_ms(1000, []() {
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
});
}, CHANGE);
 
SerialMon.println("Wait...");
 
delay(3000);
 
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
 
//モデムの初期化
SerialMon.println("Initializing modem...");
if (!modem.init()) {
SerialMon.println("Failed to restart modem, delaying 10s and retrying");
return;
}
SerialMon.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);
 
SerialMon.println("Waiting for network...");
if (!modem.waitForNetwork()) {
delay(10000);
return;
}
 
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
 
SerialMon.print("Connecting to:");
SerialMon.println(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
delay(10000);
return;
}
 
bool res = modem.isGprsConnected();
SerialMon.print("GPRS status:");
SerialMon.println(res);
 
IPAddress local = modem.localIP();
SerialMon.print("Local IP:");
SerialMon.println(local);
 
int csq = modem.getSignalQuality();
SerialMon.print("Signal quality:");
SerialMon.println(csq);
 
//httpの接続開始
TinyGsmClient client(modem);
HttpClient http(client, server, port);
 
SerialMon.println("HTTP GET request... ");
int err = http.get(resource);
if (err != 0) {
SerialMon.println("failed to connect");
delay(10000);
return;
}
 
//httpの通信結果を表示
String body = http.responseBody();
SerialMon.println("============Response===========");
SerialMon.println(body);
SerialMon.println("============Response end========");
 
http.stop();
SerialMon.println("Server disconnected");
 
modem.poweroff();//モデムオフ
SerialMon.println("Poweroff.");
}
 
void loop()
{
//スリープへ
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
delay(200);
esp_deep_sleep_start();
}
  • follow us in feedly

この記事の著者

momo

1966年訓子府町生まれの訓子府育ち。玉葱や米、メロンを栽培する農家です。一眼レフを本格的に始めたのは2005年。仕事の時でもいつでもカメラを持ち歩く自称農場カメラマン。普段の生活を撮るのが主で、その他ストロボを使っての商品撮影、スタジオ撮影も。愛好家グループで年1回写真展を行っている。農機具の改造や作製、電子工作など、モノづくりが大好きです。

この著者の最新の記事

関連記事

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

2025年5月
 1234
567891011
12131415161718
19202122232425
262728293031  

カテゴリー