forked from Xinyuan-LilyGO/LilyGo-LoRa-Series
/
LoRaReceiver.ino
115 lines (98 loc) · 3.18 KB
/
LoRaReceiver.ino
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
#include <HTTPClient.h>
#include <WiFi.h>
#include <LoRa.h>
#include "boards.h"
int counter = 0;
// WiFi Internet Router
const char* ssid = "WIFI_SSID";
const char* password = "WIFI_PASSWORD";
// LINE Messaging API
const char* line_channel_access_token = "LINE_CHANNEL_ACCESS_TOKEN";
const char* line_your_user_id = "LINE_YOUR_USER_ID";
void setup()
{
initBoard();
// When the power is turned on, a delay is required.
delay(1500);
Serial.println("LoRa Receiver");
LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DI0_PIN);
if (!LoRa.begin(LoRa_frequency)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setTxPower(13); // TX Power: 13dBm, 20mW, ARIB STD-T108
LoRa.setSignalBandwidth(125E3); // Signal Bandwidth: defaults to 125E3
LoRa.setSpreadingFactor(10); // Spreading Factor: defaults to 7, 6 to 12
LoRa.setCodingRate4(5); // Coding Rate: defaults to 5, 5 to 8
LoRa.enableCrc();
Serial.println("WiFi Begin");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("WiFi Connected");
}
void loop()
{
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
String recv = "";
// read packet
while (LoRa.available()) {
recv += (char)LoRa.read();
}
Serial.println(recv);
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
#ifdef HAS_DISPLAY
if (u8g2) {
digitalWrite(BOARD_LED, LED_ON);
u8g2->sleepOff();
u8g2->clearBuffer();
char buf[256];
u8g2->drawStr(0, 12, "Received OK!");
u8g2->drawStr(0, 26, recv.c_str());
snprintf(buf, sizeof(buf), "RSSI:%i, %i", LoRa.packetRssi(), counter);
u8g2->drawStr(0, 40, buf);
snprintf(buf, sizeof(buf), "SNR:%.1f", LoRa.packetSnr());
u8g2->drawStr(0, 56, buf);
u8g2->sendBuffer();
delay(500);
digitalWrite(BOARD_LED, LED_OFF);
u8g2->sleepOn();
++counter;
}
#endif
// LINE Messaging API
HTTPClient httpClient;
httpClient.begin("https://api.line.me/v2/bot/message/push");
httpClient.addHeader("Content-Type", "application/json");
char buf[256];
sprintf(buf, "Bearer %s", line_channel_access_token);
httpClient.addHeader("Authorization", buf);
sprintf(buf, "{"
"\"to\": \"%s\","
"\"messages\":["
"{"
"\"type\":\"text\","
"\"text\":\"%s%s\""
"}"
"]"
"}", line_your_user_id, "郵便来た: ", recv);
int status_code = httpClient.POST((uint8_t *)buf, strlen(buf));
if (status_code == 200)
{
Serial.print("OK");
}
else
{
Serial.printf("ERR %d", status_code);
}
httpClient.end();
}
}