Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 49 additions & 70 deletions examples/BME280/BME280.ino
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
/*
WiFi Web Server LED control via web of things (e.g., WebThings Gateway)
based on WiFi101.h example "Provisioning_WiFiWebServer.ino"

A simple web server that lets you control an LED via the web.
This sketch will print the IP address of your WiFi device (once connected)
to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the onboard LED.

You can also connect via the Things Gateway http-on-off-wifi-adapter.

If the IP address of your shield is yourAddress:
http://yourAddress/H turns the LED on
http://yourAddress/L turns it off

This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.

Circuit:
* WiFi using Microchip (Atmel) WINC1500
* LED attached to pin 1 (onboard LED) for SAMW25
* LED attached to pin 6 for MKR1000

created 25 Nov 2012
by Tom Igoe

updates: dh, kg 2018
*/

#define LARGE_JSON_BUFFERS 1

#include <Arduino.h>
#include <BME280.h>
#include <BME280I2C.h>
#include <SPI.h>
#include <Wire.h>
#include <WiFi101.h>
#include <WiFi.h>
#include <Thing.h>
#include <WebThingAdapter.h>

Expand All @@ -45,6 +16,18 @@
#define PIN_STATE_LOW LOW
#endif

#define ESP32

// TODO: Hardcode your wifi credentials here (and keep it private)
const char *ssid = "";
const char *password = "";

#if defined(LED_BUILTIN)
const int ledPin = LED_BUILTIN;
#else
const int ledPin = 13; // manually configure LED pin
#endif

WebThingAdapter *adapter;

const char *bme280Types[] = {"TemperatureSensor", nullptr};
Expand All @@ -54,12 +37,16 @@ ThingProperty weatherTemp("temperature", "", NUMBER, "TemperatureProperty");
ThingProperty weatherHum("humidity", "", NUMBER, "LevelProperty");
ThingProperty weatherPres("pressure", "", NUMBER, nullptr);

BME280I2C::Settings
settings(BME280::OSR_X1, BME280::OSR_X1, BME280::OSR_X1,
BME280::Mode_Forced, BME280::StandbyTime_1000ms,
BME280::Filter_Off, BME280::SpiEnable_False,
(BME280I2C::I2CAddr)0x76 // I2C address. I2C specific.
);
BME280I2C::Settings settings(
BME280::OSR_X1,
BME280::OSR_X1,
BME280::OSR_X1,
BME280::Mode_Forced,
BME280::StandbyTime_1000ms,
BME280::Filter_Off,
BME280::SpiEnable_False,
BME280I2C::I2CAddr_0x76 // I2C address. I2C specific.
);

BME280I2C bme(settings);

Expand All @@ -79,46 +66,38 @@ void readBME280Data() {
}

void setup() {
// Initialize serial:
// Serial.begin(9600);

// check for the presence of the shield:
// Serial.print("Configuring WiFi shield/module...\n");
if (WiFi.status() == WL_NO_SHIELD) {
// Serial.println("WiFi shield not present");
// don't continue:
while (true)
;
Serial.println("Initialize...");

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.begin(115200);
Serial.println("");
Serial.print("Connecting to \"");
Serial.print(ssid);
Serial.println("\"");
WiFi.begin(ssid, password);

bool blink = true;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
blink = !blink;
}
digitalWrite(ledPin, HIGH); // active low led

// configure the LED pin for output mode
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

Wire.begin();
while (!bme.begin()) {
// Serial.println("Could not find BME280I2C sensor!");
Serial.println("Could not find BME280I2C sensor!");
delay(1000);
}

// Serial.println("Starting in provisioning mode...");
// Start in provisioning mode:
// 1) This will try to connect to a previously associated access point.
// 2) If this fails, an access point named "wifi101-XXXX" will be created,
// where XXXX
// is the last 4 digits of the boards MAC address. Once you are connected
// to the access point, you can configure an SSID and password by
// visiting http://wifi101/
WiFi.beginProvision();

while (WiFi.status() != WL_CONNECTED) {
// wait while not connected

// blink the led to show an unconnected status
digitalWrite(LED_BUILTIN, PIN_STATE_HIGH);
delay(500);
digitalWrite(LED_BUILTIN, PIN_STATE_LOW);
delay(500);
}
Serial.println("BME280I2C connected and initialized.");

// connected, make the LED stay on
digitalWrite(LED_BUILTIN, PIN_STATE_HIGH);
Expand Down Expand Up @@ -154,4 +133,4 @@ void setup() {
void loop() {
readBME280Data();
adapter->update();
}
}
27 changes: 27 additions & 0 deletions examples/BME280/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## BME 280 WebThings example

In order to be able to use this example, you need to install the WebThings library.

Change the ssid and password to connect to your router

```c++
const char *ssid = "";
const char *password = "";
```

Make sure the BME/BMP 280 has the correct I2C address (0x76)
If it doesn't work or you know you use the other address, change it in

```c++
BME280I2C::Settings settings(
BME280::OSR_X1,
BME280::OSR_X1,
BME280::OSR_X1,
BME280::Mode_Forced,
BME280::StandbyTime_1000ms,
BME280::Filter_Off,
BME280::SpiEnable_False,
BME280I2C::I2CAddr_0x76 // I2C address. I2C specific.
);

```