Makerfabs-He
Published © GPL3+

Lora Gateway Based on MicroPython ESP32

Using two LoRa nodes to send the temperature and humidity data to the gateway, and then upload it to the Internet through the gateway.

IntermediateWork in progress3 hours3,415
Lora Gateway Based on MicroPython ESP32

Story

Read more

Code

TransmitterDHT11.ino

Arduino
TransmitterDHT11.ino
// LoRa 9x_TX
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messaging client (transmitter)
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example LoRa9x_RX

//Function:Transmit DHT11 to Lora
//Author: Charlin
//Date:2019/12/11

#include <SPI.h>
#include "RH_RF95.h"
#include "DHT.h"
#define DHTPIN 4     // Digital pin connected to the DHT sensor
#include "avr/boot.h"

// Uncomment whatever type you're using!m
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 2

// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 433.0

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);

void setup() 
{
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);

  
  while (!Serial);
  Serial.begin(9600);
  delay(100);

  Serial.println("Marduino LoRa TX Test!");

  // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);

  while (!rf95.init()) {
    Serial.println("LoRa radio init failed");
    while (1);
  }
  Serial.println("LoRa radio init OK!");

  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
  if (!rf95.setFrequency(RF95_FREQ)) {
    Serial.println("setFrequency failed");
    while (1);
  }
  Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
  
  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then 
  // you can set transmitter powers from 5 to 23 dBm:
  rf95.setTxPower(23, false);

  dht.begin();
}

int16_t packetnum = 0;  // packet counter, we increment per xmission
int16_t nodenum = 1;    //node number

void loop()
{

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float temperature = dht.readTemperature();

    // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    //return;
  }

  //Serial.print(F("Humidity: "));
  //Serial.print(humidity);
  //Serial.print(F("%  Temperature: "));
  //Serial.print(temperature);
  //Serial.println("Humidity is " + (String)humidity);
  //Serial.println("Temperature is " + (String)temperature);

  //String message = "#"+(String)packetnum+" Humidity:"+(String)humidity+"% Temperature:"+(String)temperature+"C";
  String message ="#"+(String)nodenum+" Humidity:"+(String)humidity+"% Temperature:"+(String)temperature+"C"+" num:"+(String)packetnum;
  Serial.println(message);
  packetnum++;
  Serial.println("Transmit: Sending to rf95_server");


  //Serial.println("UID Bytes:");
  //for (int i = 14; i < 14 + 10; i++)
  //{
  //  Serial.print(boot_signature_byte_get(i), HEX);
  //}
  //Serial.println("");
  
  // Send a message to rf95_server
  uint8_t radioPacket[message.length()+1];
  message.toCharArray(radioPacket, message.length()+1);
  radioPacket[message.length()+1]= '\0';
  Serial.println("Sending..."); delay(10);
  rf95.send((uint8_t *)radioPacket, message.length()+1); 
  
  Serial.println("Waiting for packet to complete..."); delay(10);
  rf95.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  Serial.println("Waiting for reply..."); delay(10);
  if (rf95.waitAvailableTimeout(2000))
  { 
    // Should be a reply message for us now   
    if (rf95.recv(buf, &len))
   {
      Serial.print("Got reply: ");
      Serial.println((char*)buf);
      Serial.print("RSSI: ");
      Serial.println(rf95.lastRssi(), DEC);    
    }
    else
    {
      Serial.println("Receive failed");
    }
  }
  else
  {
    Serial.println("No reply, is there a listener around?");
  }
  
  Serial.println("");
  
  //delay(1000);
}

Credits

Makerfabs-He

Makerfabs-He

9 projects • 9 followers
Makerfabs - IoT/ Open Source Hardware/ PCBA

Comments