Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Andrea ZGuzFelipe LeonardoJazmín HernándezAndres SabasCarlos Alatorre
Published

Getting started: BastWAN

Let’s send data to The Things Network using a BMP280 sensor and our BastWAN board.

IntermediateProtip2 hours441
Getting started: BastWAN

Things used in this project

Hardware components

Electronic Cats BastWAN
×1
BMP280
×1

Software apps and online services

Arduino IDE
Arduino IDE
The Things Stack
The Things Industries The Things Stack

Hand tools and fabrication machines

Breadboard, 400 Pin
Breadboard, 400 Pin
10 Pc. Jumper Wire Kit, 20 cm Long
10 Pc. Jumper Wire Kit, 20 cm Long

Story

Read more

Schematics

BastWAN and BMP280 module wirings

Code

BMP_Practice_LoRaWAN.zip

Arduino
Sketch to share BMP280's data to TTN, using BastWAN as the end device.
/**
 * Example of ABP device
 * Authors: 
 *        Ivan Moreno
 *        Eduardo Contreras
 *  June 2019
 * 
 * This code is beerware; if you see me (or any other collaborator 
 * member) at the local, and you've found our code helpful, 
 * please buy us a round!
 * Distributed as-is; no warranty is given.
 */
#include <lorawan.h> // Library Beelan http://librarymanager/All#Beelan
#include <CayenneLPP.h> //https://github.com/ElectronicCats/CayenneLPP
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

CayenneLPP lpp(51);
Adafruit_BME280 bme; // I2C communication

unsigned int delayTime;

//ABP Credentials 
const char *devAddr = "00000000"; //Here your Device Address
const char *nwkSKey = "00000000000000000000000000000000"; // Here your Network Session Key
const char *appSKey = "00000000000000000000000000000000"; //Here your App Session Key


const unsigned long interval = 10000;    // 10 s interval to send message
unsigned long previousMillis = 0;  // will store last time message sent

char outStr[255]; //Array for incoming data from the server
byte recvStatus = 0; //Data read from LoRa server


//Enabling pins for RAK module
const sRFM_pins RFM_pins = {
  .CS = SS,
  .RST = RFM_RST,
  .DIO0 = RFM_DIO0,
  .DIO1 = RFM_DIO1,
  .DIO2 = RFM_DIO2,
  .DIO5 = RFM_DIO5,
};

void setup() {
  // Setup loraID access
  delayTime = 10000;
  Serial.begin(115200);
  while(!Serial);
  if(!lora.init()){
    Serial.println("RFM95 not detected");
    delay(5000);
    return;
  }
  
  pinMode(RFM_TCX_ON,OUTPUT);
  pinMode(RFM_SWITCH,OUTPUT);
  pinMode(LED_BUILTIN,OUTPUT);
  
  // Set LoRaWAN Class change CLASS_A or CLASS_C
  lora.setDeviceClass(CLASS_A);

  // Set Data Rate
  lora.setDataRate(SF8BW125);

  // set channel to random
  lora.setChannel(MULTI);
  
  // Put ABP Key and DevAddress here
  lora.setNwkSKey(nwkSKey);
  lora.setAppSKey(appSKey);
  lora.setDevAddr(devAddr);

 unsigned int status; //Status of BME Sensor
    
 status = bme.begin();  
  if (!status) {
      Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
      Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
      Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
      Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
      Serial.print("        ID of 0x60 represents a BME 280.\n");
      Serial.print("        ID of 0x61 represents a BME 680.\n");
      while (1) delay(10);
  }
  Serial.println("-- BMP Practice --");
}

void loop() {
  // Check interval overflow
  if(millis() - previousMillis > interval) {
    previousMillis = millis(); 
    Serial.println("Sending: ");
    printvariables(); //Getting data from BME-280 and printing it on the Serial Monitor
    lora.sendUplink((char *)lpp.getBuffer(), lpp.getSize(), 0, 1); //Send an Uplink to the LoRa Server (printing char type to the buffer for Cayenne LPP, get size of the buffer, unconfirmed, Port 1)
  }

  recvStatus = lora.readData(outStr);
  if(recvStatus) {
    Serial.println(outStr); //If data received from the server, print it on the Serial Monitor
  }
  // Check Lora RX
  lora.update();
}

//Getting data from BME-280 and printing it on the Serial Monitor
void printvariables() {
  lpp.reset(); //Reset Cayenne LPP buffer 

  int temper = bme.readTemperature();
  Serial.print("Temperature = ");
  Serial.print(temper);
  Serial.println(" C");
  lpp.addTemperature(1, temper); //Print this variable first position in the queue

  int pressPA = bme.readPressure() / 100.0;
  Serial.print("Pressure = ");
  Serial.print(pressPA);
  Serial.println(" hPa");
  lpp.addBarometricPressure(2, pressPA); //Print this variable second position in the queue

  int humid = bme.readHumidity();
  Serial.print("Humidity = ");
  Serial.print(humid);
  Serial.println(" %");
  lpp.addRelativeHumidity(3, humid); //Print this variable third position in the queue

  Serial.println();
}

Credits

Andrea ZGuz
11 projects • 2 followers
Electronic Engineer. Support Engineer at Electronic Cats.
Contact
Felipe Leonardo
1 project • 0 followers
Contact
Jazmín Hernández
14 projects • 5 followers
Electronics Engineer. Passionate for tech and languages.
Contact
Andres Sabas
50 projects • 50 followers
Co-Founder of The Inventor's House Hackerspace, DIY, Workaholic
Contact
Carlos Alatorre
19 projects • 6 followers
Mechatronic Engineer working on supporting you! An open source user. I'm very curious in almost most of my life areas. Born and raised in MX
Contact

Comments

Please log in or sign up to comment.