Added two examples UniversalSensorMaster.ino and BME280_Client.ino

This commit is contained in:
Marcel Schulz 2017-11-20 22:14:52 +01:00
parent 28f7ce0fc4
commit 1128d8c3c4
2 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,112 @@
/*
* Author - Marcel Schulz (alias Schullebernd)
* *************************************************************
* See further library details on https://github.com/Schullebernd/SBNetwork
* *************************************************************
* This example is a client with a BME280 temperatur, humidity and barometer sensor on I2C.
* It sends the measured values every 20 seconds to the master.
* To setup the master device for this example, open the UniversalSensorMaster example and run it on a second device.
* **************************************************************
* Step 1 - Prepare your device like in the example sketch GettingStartedClient
*
* Step 2 - Connect the BME280 sensor to your arduino/wemos device
* Connect Arduino --> BME280
* A4 --> SDA
* A5 --> SCL
* 3V3 --> VCC
* GND --> GND
* empty --> CSB
* empty --> SD0
* The I2C Address of BME280 usually is 0x76 --> Put this into bme.begin(0x76) at setup function. If this is not working try 0x77.
*
* Step 3 - Download and include the Sparkfun BME280 library.
* https://github.com/sparkfun/SparkFun_BME280_Arduino_Library
*
* Step 4 - Run the project
* Open the file SBNetwork_config.h in the library folder ../libraries/SBNetwork/src/SBNetwork_config.h and comment out (put two // at the line start) the line 6.
* Line 6 should now look like this //#define RUN_AS_MASTER
* Connect the Arduino via USB to the PC, select the right board and COM interface in the tools menu and run the project.
*/
#include <SparkFunBME280.h>
#include <SBNetwork_config.h>
#include <SBNetwork.h>
// Type in here the mac address of the device.
// This must be unique within your complete network otherwise the network will not work.
//SBMacAddress deviceMac(0x01, 0x02, 0x03, 0x04, 0x05);
SBMacAddress deviceMac(0x05, 0x04, 0x03, 0x02, 0x01);
// Create a new network device with Wemos D1 mini and set the _ce and _cs pin
//SBNetwork networkDevice(D2, D8);
SBNetwork networkDevice(6, 7);
// time variables
uint32_t wait = 20000;
uint32_t lastWait = wait;
// Define the BME280 sensor
BME280 bme;
void setup() {
Serial.begin(19200);
Serial.println(F("*** PRESS 'N' to reset the device"));
// Initialize the network device
networkDevice.initialize(deviceMac);
// Initialize the BME280 sensor
bme.settings.commInterface = I2C_MODE;
bme.settings.I2CAddress = 0x76;
bme.settings.runMode = 3;
bme.settings.tStandby = 0;
bme.settings.filter = 0;
bme.settings.tempOverSample = 1;
bme.settings.pressOverSample = 1;
bme.settings.humidOverSample = 1;
Serial.print("Starting BME280... result of .begin(): 0x");
//Calling .begin() causes the settings to be loaded
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
Serial.println(bme.begin(), HEX);
}
void loop() {
if (Serial.available())
{
char c = toupper(Serial.read());
if (c == 'N') {
networkDevice.resetData();
}
}
// Call this in the loop() function to maintain the network device
networkDevice.update();
// Send a message to the master every 4 seconds
if (lastWait < millis()) {
float fTemp = bme.readTempC();
Serial.print("Temperature: ");
Serial.print(fTemp, 2);
Serial.println(" degrees C");
float fPressure = bme.readFloatPressure();
Serial.print("Pressure: ");
Serial.print(fPressure, 2);
Serial.println(" Pa");
float fHumidity = bme.readFloatHumidity();
Serial.print("%RH: ");
Serial.print(fHumidity, 2);
Serial.println(" %");
byte message[10 + (3*4)]; // the first 10 bytes containing the type of the sensor. The 3*4 bytes are needed for the values. float has a length of 32 bits = 4 bytes and we need 3 of them.
strcpy((char*)message, "BME280");
memcpy((void*)(message + 10), &fTemp, sizeof(float));
memcpy((void*)(message + 10 + 4), &fPressure, sizeof(float));
memcpy((void*)(message + 10 + 4 + 4), &fHumidity, sizeof(float));
networkDevice.sendToDevice(networkDevice.NetworkDevice.MasterMAC, message, 10 + (3*4));
lastWait += wait;
}
} // Loop

View File

@ -0,0 +1,90 @@
/*
* Author - Marcel Schulz (alias Schullebernd)
* *************************************************************
* See further library details on https://github.com/Schullebernd/SBNetwork
* *************************************************************
* This Getting started is prepared for using it with a Wemos D1 mini as a master device.
* If you want to use an Arduino, then change the line 39 "SBNetwork networkDevice(D2, D8);" to the correct pinout for an Arduino (6,7).
* To get shure, that the SBNetwork library is build for a Master-Device open the SBNetwork_config.h in the libraries folder and get shure that line 6 is not commented out.
* #define RUN_AS_MASTER
* **************************************************************
* Step 1 - Prepare your device
* Connect a nRF24L01 transmitter to a Wemos D1 mini or an Arduino Device
* WEMOS > RF24 ARDUINO > RF24 ---------------------------------------
* ------------ -------------- | GND # # VCC TOP VIEW |
* 3V3 > VCC VCC > VCC | CE # # CSN OF nRF24L01 |
* GND > GND GND > GND | SCK # # MOSI |
* D2 > CE 6 > CE | MISO # # IRQ |
* D8 > CSN 7 > CSN | |
* D7 > MOSI 11 > MOSI ---------------------------------------
* D6 > MISO 12 > MISO
* D5 > SCK 13 > SCK
*
* Step 2 - Build the sketch for the master device
* Open the file SBNetwork_config.h in the library folder ../libraries/SBNetwork/src/SBNetwork_config.h and comment out (put two // at the line start) the line 6.
* Line 6 should now look like this //#define RUN_AS_MASTER
* Connect the Wemos via USB to the PC, select the right board and COM interface in the tools menu and run the project.
* After building and loading up to the Wemos, the serial monitor should show some log data.
*/
#include <SBNetwork_config.h>
#include <SBNetwork.h>
// Type in here the mac address of the device.
// This must be unique within your complete network otherwise the network will not work.
SBMacAddress deviceMac(0x01, 0x02, 0x03, 0x04, 0x05);
//SBMacAddress deviceMac(0x05, 0x04, 0x03, 0x02, 0x01);
// Create a new network device with Wemos D1 mini and set the _ce and _cs pin.
SBNetwork networkDevice(D2, D8);
//SBNetwork networkDevice(6, 7);
void setup() {
// Init serial connection
Serial.begin(19200);
// Initialize the network device
networkDevice.initialize(deviceMac);
Serial.println(F("*** PRESS 'N' to reset the device"));
}
void loop() {
// This routine is for resetting the device
// All flash data will be deleted
if (Serial.available())
{
char c = toupper(Serial.read());
if (c == 'N') {
networkDevice.resetData();
}
}
// Call this in the loop() function to maintain the network device
networkDevice.update();
// Check, if there are messages available
uint8_t messageSize = networkDevice.available();
if (messageSize > 0) {
byte* message = (byte*)networkDevice.getMessage();
Serial.print(F("Received Content from sensor: "));
Serial.println((char*)message);
if (strcmp((char*)message, "BME280") == 0) {
// We have received a BME280 transmission
float fTemp, fPressure, fHumidity;
memcpy(&fTemp, (void*)(message + 10), sizeof(float));
memcpy(&fPressure, (void*)(message + 10 + 4), sizeof(float));
memcpy(&fHumidity, (void*)(message + 10 + 4 + 4), sizeof(float));
Serial.print("Temperature: ");
Serial.print(fTemp, 2);
Serial.println(" degrees C");
Serial.print("Pressure: ");
Serial.print(fPressure, 2);
Serial.println(" Pa");
Serial.print("%RH: ");
Serial.print(fHumidity, 2);
Serial.println(" %");
}
}
} // Loop