Luo Zeqing
Published © LGPL

LoRa->4G Gateway_Soil monitoring with Cloud Server via 4G

I have a LoRa Soil Moisture Sensor, but I want to use 4G to monitor soil remotely from the cloud server, so I made this LoRa to 4G device.

IntermediateProtip3 hours160
LoRa->4G Gateway_Soil monitoring with Cloud Server via 4G

Things used in this project

Software apps and online services

Arduino Web Editor
Arduino Web Editor

Story

Read more

Code

LoRa-to-4G.ino

Arduino
#include <RadioLib.h>
#include <SPI.h>

#include <Arduino.h>

HardwareSerial mySerial2(2);
#define DEBUG true
#define IO_RXD2 47
#define IO_TXD2 48

#define IO_GSM_PWRKEY 4
#define IO_GSM_RST    5

// LoRa
#define LORA_MOSI 16 // 23
#define LORA_MISO 18 // 19
#define LORA_SCK 8   // 18
#define LORA_CS 17   // 21

#define LORA_RST 15  // 22
#define LORA_DIO0 9  // 32
#define LORA_DIO1 14 // 35

#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

String Apikey = "ANPNRNZOVQO0UBRY";

SX1276 radio = new Module(LORA_CS, LORA_DIO0, LORA_RST, LORA_DIO1, SPI, SPISettings());

void setup()
{
    // put your setup code here, to run once:
    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);




    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);

    // 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 ... "));
    

    
}

void loop()
{
 //lora_send_task();
String str;
  int state = radio.receive(str);

  // you can also receive data as byte array
  /*
    size_t len = 8;
    byte byteArr[len];
    int state = radio.receive(byteArr, len);
  */

  if (state == ERR_NONE) {
    // packet was successfully received
    Serial.println(F("success!"));

    // print data of the packet
    Serial.print(F("Data:\t\t\t"));
    Serial.println(str);

    // print RSSI (Received Signal Strength Indicator)
    // of the last received packet
    Serial.print(F("RSSI:\t\t\t"));
    Serial.print(radio.getRSSI());
    Serial.println(F(" dBm"));

    // print SNR (Signal-to-Noise Ratio)
    // of the last received packet
    Serial.print(F("SNR:\t\t\t"));
    Serial.print(radio.getSNR());
    Serial.println(F(" dB"));

    // print frequency error
    // of the last received packet
    Serial.print(F("Frequency error:\t"));
    Serial.print(radio.getFrequencyError());
    Serial.println(F(" Hz"));

  } else if (state == ERR_RX_TIMEOUT) {
    // timeout occurred while waiting for a packet
    //Serial.println(F("timeout!"));

  } else if (state == ERR_CRC_MISMATCH) {
    // packet was received, but is malformed
    Serial.println(F("CRC error!"));

  }


  String numbers[10]; 
  int count = extractNumbers(str, numbers);
 
  for(int i = 0; i < count; i++) {
    Serial.println(numbers[i]);
  }
  
if (numbers[0] != "" )
{
   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);

}

}

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;
}


void lora_send_task()
{

    String msg = "IDXDEBUGACT001PARAM000000";
    radio.transmit(msg);
    delay(5000);
    msg = "IDXDEBUGACT000PARAM000000";
    radio.transmit(msg);
    delay(5000);
}

String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
    mySerial2.println(command);
    long int time = millis();
    while ((time + timeout) > millis())
    {
        while (mySerial2.available())
        {
            char c = mySerial2.read();
            response += c;
        }
    }
    if (debug)
    {
        Serial.print(response);
    }
    return response;
}

Credits

Luo Zeqing
46 projects • 14 followers
Makerfabs ESP32/ Lora/ Lorawan, IoT Hardware
Contact

Comments

Please log in or sign up to comment.