- First, we need to prepare the environment for ESP32 development board programming in Arduino, and you can check this guide.
- RadioLib library is a wireless communication library. It supports a variety of wireless communication modules, including LoRa, NRF24L01, SX127x, and more. Using the RadioLib library, you can easily add wireless communication capabilities to your Arduino projects, whether it's for data transfer, remote control, or sensor networks.
#include <RadioLib.h>
- The SPI.h header file enables your Arduino project to use the functionality provided by the SPI library to communicate with external SPI devices.
#include <SPI.h>
- Define the pins used to connect to the LoRa module. Set the LoRa module frequency, bandwidth, spreading factor, coding rate, output power, and leading code length. Initialise "radio" to represent the LoRa module and pass the appropriate pins and parameters.
HardwareSerial mySerial2(2);
#define DEBUG true
#define IO_RXD2 47
#define IO_TXD2 48
#define IO_GSM_PWRKEY 4
#define IO_GSM_RST 5
#define LORA_MOSI 16
#define LORA_MISO 18
#define LORA_SCK 8
#define LORA_CS 17
#define LORA_RST 15
#define LORA_DIO0 9
#define LORA_DIO1 14
#define FREQUENCY 915.0
#define BANDWIDTH 125.0
#define SPREADING_FACTOR 9
#define CODING_RATE 7
#define OUTPUT_POWER 10
#define PREAMBLE_LEN 8
#define GAIN 0
SX1276 radio = new Module(LORA_CS, LORA_DIO0, LORA_RST, LORA_DIO1, SPI, SPISettings());
- Initialize your devices so that they work properly
Serial.begin(115200);
Serial.println(" Test Begin!");
mySerial2.begin(115200,SERIAL_8N1, IO_RXD2, IO_TXD2);
pinMode(IO_GSM_RST, OUTPUT);
digitalWrite(IO_GSM_RST, LOW);
pinMode(IO_GSM_PWRKEY, OUTPUT);
digitalWrite(IO_GSM_PWRKEY, HIGH);
delay(3000);
digitalWrite(IO_GSM_PWRKEY, LOW);
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
// initialize SX1276 with default settings
Serial.print(F("[SX1276] Initializing ... "));
int state = radio.begin(FREQUENCY, BANDWIDTH, SPREADING_FACTOR, CODING_RATE, SX127X_SYNC_WORD, OUTPUT_POWER, PREAMBLE_LEN, GAIN);
// int state = radio.begin();
if (state == ERR_NONE)
{
Serial.println(F("success!"));
}
else
{
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
Serial.print(F("Waiting for incoming transmission ... "));
String str;
int state = radio.receive(str);
if (state == ERR_NONE) {
Serial.println(F("success!"));
Serial.print(F("Data:\t\t\t"));
Serial.println(str);
Serial.print(F("RSSI:\t\t\t"));
Serial.print(radio.getRSSI());
Serial.println(F(" dBm"));
Serial.print(F("SNR:\t\t\t"));
Serial.print(radio.getSNR());
Serial.println(F(" dB"));
Serial.print(F("Frequency error:\t"));
Serial.print(radio.getFrequencyError());
Serial.println(F(" Hz"));
} else if (state == ERR_RX_TIMEOUT) {
} else if (state == ERR_CRC_MISMATCH) {
Serial.println(F("CRC error!"));
}
- Extract the number from the received message and store it in an array, modify the write channel URL given by ThingSpeak according to the array data
String numbers[10];
int extractNumbers(String input, String numbers[]) {
int count = 0;
String number = "";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (isDigit(c) || c == '.') {
number += c;
}
else if (c == ' ' && number != "") {
numbers[count++] = number;
number = "";
}
}
if (number != "") {
numbers[count++] = number;
}
return count;
}
int count = extractNumbers(str, numbers);
for(int i = 0; i < count; i++) {
Serial.println(numbers[i]);
}
String http_str = "AT+HTTPPARA=\"URL\",\"http://api.thingspeak.com/update?api_key=" + Apikey + "&field1=" + numbers[1] +"&field2=" + numbers[2] + "&field3="+ numbers[3] +"&field4="+ numbers[4]+"&field5="+ numbers[5] +"\"\r\n";
- Controlling LTE Access to Write Channel URLs Using AT Commands
String Apikey = "ANPNRNZOVQO0UBRY";
sendData("AT", 1000, DEBUG);
delay(3000);
sendData("AT+CICCID", 1000, DEBUG);
delay(3000);
sendData("AT+SIMCOMATI", 1000, DEBUG);
delay(3000);
sendData("AT+COPS?", 1000, DEBUG);
delay(3000);
sendData("AT+CPIN?", 3000, DEBUG);
delay(3000);
sendData("AT+CSQ", 1000, DEBUG);
delay(3000);
sendData("AT+CREG?", 1000, DEBUG);
delay(3000);
sendData("AT+CGREG?", 1000, DEBUG);
delay(3000);
sendData("AT+CPSI?", 1000, DEBUG);
delay(3000);
sendData("AT+CGDCONT=1,\"IP\",\"CMNET\"", 1000, DEBUG);
delay(3000);
sendData("AT+CGATT=1", 1000, DEBUG);
delay(3000);
String http_str = "AT+HTTPPARA=\"URL\",\"http://api.thingspeak.com/update?api_key=" + Apikey + "&field1=" + numbers[1] +"&field2=" + numbers[2] + "&field3="+ numbers[3] +"&field4="+ numbers[4]+"&field5="+ numbers[5] +"\"\r\n";
sendData("AT+HTTPINIT\r\n", 2000, DEBUG);
delay(2000);
sendData(http_str, 2000, DEBUG);
delay(2000);
sendData("AT+HTTPACTION=0\r\n", 3000, DEBUG);
delay(2000);
sendData("AT+HTTPTERM\r\n", 3000, DEBUG);
delay(2000);
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.