Skip to content

Commit 2646d73

Browse files
authored
Merge pull request #6 from Citrullin/bmp280_example
Improve bmp280 example
2 parents 1e1d8d7 + 7d15182 commit 2646d73

File tree

1 file changed

+49
-70
lines changed

1 file changed

+49
-70
lines changed

examples/BME280/BME280.ino

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
1-
/*
2-
WiFi Web Server LED control via web of things (e.g., WebThings Gateway)
3-
based on WiFi101.h example "Provisioning_WiFiWebServer.ino"
4-
5-
A simple web server that lets you control an LED via the web.
6-
This sketch will print the IP address of your WiFi device (once connected)
7-
to the Serial monitor. From there, you can open that address in a web browser
8-
to turn on and off the onboard LED.
9-
10-
You can also connect via the Things Gateway http-on-off-wifi-adapter.
11-
12-
If the IP address of your shield is yourAddress:
13-
http://yourAddress/H turns the LED on
14-
http://yourAddress/L turns it off
15-
16-
This example is written for a network using WPA encryption. For
17-
WEP or WPA, change the Wifi.begin() call accordingly.
18-
19-
Circuit:
20-
* WiFi using Microchip (Atmel) WINC1500
21-
* LED attached to pin 1 (onboard LED) for SAMW25
22-
* LED attached to pin 6 for MKR1000
23-
24-
created 25 Nov 2012
25-
by Tom Igoe
26-
27-
updates: dh, kg 2018
28-
*/
29-
301
#define LARGE_JSON_BUFFERS 1
312

323
#include <Arduino.h>
334
#include <BME280.h>
345
#include <BME280I2C.h>
356
#include <SPI.h>
367
#include <Wire.h>
37-
#include <WiFi101.h>
8+
#include <WiFi.h>
389
#include <Thing.h>
3910
#include <WebThingAdapter.h>
4011

@@ -45,6 +16,18 @@
4516
#define PIN_STATE_LOW LOW
4617
#endif
4718

19+
#define ESP32
20+
21+
// TODO: Hardcode your wifi credentials here (and keep it private)
22+
const char *ssid = "";
23+
const char *password = "";
24+
25+
#if defined(LED_BUILTIN)
26+
const int ledPin = LED_BUILTIN;
27+
#else
28+
const int ledPin = 13; // manually configure LED pin
29+
#endif
30+
4831
WebThingAdapter *adapter;
4932

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

57-
BME280I2C::Settings
58-
settings(BME280::OSR_X1, BME280::OSR_X1, BME280::OSR_X1,
59-
BME280::Mode_Forced, BME280::StandbyTime_1000ms,
60-
BME280::Filter_Off, BME280::SpiEnable_False,
61-
(BME280I2C::I2CAddr)0x76 // I2C address. I2C specific.
62-
);
40+
BME280I2C::Settings settings(
41+
BME280::OSR_X1,
42+
BME280::OSR_X1,
43+
BME280::OSR_X1,
44+
BME280::Mode_Forced,
45+
BME280::StandbyTime_1000ms,
46+
BME280::Filter_Off,
47+
BME280::SpiEnable_False,
48+
BME280I2C::I2CAddr_0x76 // I2C address. I2C specific.
49+
);
6350

6451
BME280I2C bme(settings);
6552

@@ -79,46 +66,38 @@ void readBME280Data() {
7966
}
8067

8168
void setup() {
82-
// Initialize serial:
83-
// Serial.begin(9600);
84-
85-
// check for the presence of the shield:
86-
// Serial.print("Configuring WiFi shield/module...\n");
87-
if (WiFi.status() == WL_NO_SHIELD) {
88-
// Serial.println("WiFi shield not present");
89-
// don't continue:
90-
while (true)
91-
;
69+
Serial.println("Initialize...");
70+
71+
pinMode(ledPin, OUTPUT);
72+
digitalWrite(ledPin, HIGH);
73+
Serial.begin(115200);
74+
Serial.println("");
75+
Serial.print("Connecting to \"");
76+
Serial.print(ssid);
77+
Serial.println("\"");
78+
WiFi.begin(ssid, password);
79+
80+
bool blink = true;
81+
while (WiFi.status() != WL_CONNECTED) {
82+
delay(500);
83+
Serial.print(".");
84+
digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
85+
blink = !blink;
9286
}
87+
digitalWrite(ledPin, HIGH); // active low led
9388

94-
// configure the LED pin for output mode
95-
pinMode(LED_BUILTIN, OUTPUT);
89+
Serial.println("");
90+
Serial.print("Connected to ");
91+
Serial.println(ssid);
92+
Serial.print("IP address: ");
93+
Serial.println(WiFi.localIP());
9694

9795
Wire.begin();
9896
while (!bme.begin()) {
99-
// Serial.println("Could not find BME280I2C sensor!");
97+
Serial.println("Could not find BME280I2C sensor!");
10098
delay(1000);
10199
}
102-
103-
// Serial.println("Starting in provisioning mode...");
104-
// Start in provisioning mode:
105-
// 1) This will try to connect to a previously associated access point.
106-
// 2) If this fails, an access point named "wifi101-XXXX" will be created,
107-
// where XXXX
108-
// is the last 4 digits of the boards MAC address. Once you are connected
109-
// to the access point, you can configure an SSID and password by
110-
// visiting http://wifi101/
111-
WiFi.beginProvision();
112-
113-
while (WiFi.status() != WL_CONNECTED) {
114-
// wait while not connected
115-
116-
// blink the led to show an unconnected status
117-
digitalWrite(LED_BUILTIN, PIN_STATE_HIGH);
118-
delay(500);
119-
digitalWrite(LED_BUILTIN, PIN_STATE_LOW);
120-
delay(500);
121-
}
100+
Serial.println("BME280I2C connected and initialized.");
122101

123102
// connected, make the LED stay on
124103
digitalWrite(LED_BUILTIN, PIN_STATE_HIGH);
@@ -154,4 +133,4 @@ void setup() {
154133
void loop() {
155134
readBME280Data();
156135
adapter->update();
157-
}
136+
}

0 commit comments

Comments
 (0)