Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 411443f

Browse files
committedJun 15, 2018
Fix BME280 example
1 parent 1ff8db3 commit 411443f

File tree

2 files changed

+9
-133
lines changed

2 files changed

+9
-133
lines changed
 

‎examples/PlatformIO/BME280/platformio.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ env_default = samw25
1818
; https://platformio.org/lib/show/299/WiFi101
1919
; https://platformio.org/lib/show/2848/ArduinoMDNS
2020
lib_deps =
21+
https://github.com/mozilla-iot/webthing-esp8266.git
22+
ArduinoJson
2123
WiFi101
2224
ArduinoMDNS
23-
Adafruit BME280 Library
25+
BME280
2426

2527
[env:samw25]
2628
platform = atmelsam

‎examples/PlatformIO/BME280/src/BME280.cpp

Lines changed: 6 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,8 @@
3333
#include <SPI.h>
3434
#include <Wire.h>
3535
#include <WiFi101.h>
36-
#include "WiFi101WebThingAdapter.h"
37-
38-
#define USE_MDNS_RESPONDER 0
39-
40-
#if USE_MDNS_RESPONDER
41-
#include <WiFiMDNSResponder.h>
42-
#else
43-
#include <WiFiUdp.h>
44-
#include <ArduinoMDNS.h>
45-
#endif
36+
#include <Thing.h>
37+
#include <WebThingAdapter.h>
4638

4739
#ifndef PIN_STATE_HIGH
4840
#define PIN_STATE_HIGH HIGH
@@ -51,26 +43,7 @@
5143
#define PIN_STATE_LOW LOW
5244
#endif
5345

54-
55-
char mdnsName[] = "wifi101-XXXXXX"; // the MDNS name that the board will respond to
56-
// after WiFi settings have been provisioned.
57-
// The -XXXXXX will be replaced with the last 6 digits of the MAC address.
58-
// The actual MDNS name will have '.local' after the name above, so
59-
// "wifi101-123456" will be accessible on the MDNS name "wifi101-123456.local".
60-
61-
byte mac[6];
62-
63-
WiFi101AsyncServer server;
64-
65-
#if USE_MDNS_RESPONDER
66-
// Create a MDNS responder to listen and respond to MDNS name requests.
67-
WiFiMDNSResponder mdnsResponder;
68-
#else
69-
WiFiUDP udp;
70-
MDNS mdns(udp);
71-
#endif
72-
73-
// void printWiFiStatus();
46+
WebThingAdapter adapter("weathersensor");
7447

7548
ThingDevice weather("bme280", "BME280 Weather Sensor", "thing");
7649
ThingProperty weatherTemp("temperature", "", NUMBER);
@@ -147,113 +120,14 @@ void setup() {
147120
// connected, make the LED stay on
148121
digitalWrite(LED_BUILTIN, PIN_STATE_HIGH);
149122

150-
WiFi.macAddress(mac);
151-
// Replace the XXXXXX in wifi101-XXXXXX with the last 6 digits of the MAC address.
152-
if (strcmp(&mdnsName[7], "-XXXXXX") == 0) {
153-
sprintf(&mdnsName[7], "-%.2X%.2X%.2X", mac[2], mac[1], mac[0]);
154-
}
155-
156123
weather.addProperty(&weatherTemp);
157124
weather.addProperty(&weatherPres);
158125
weather.addProperty(&weatherHum);
159-
server.addDevice(&weather);
160-
server.begin();
161-
162-
#if USE_MDNS_RESPONDER
163-
// Setup the MDNS responder to listen to the configured name.
164-
// NOTE: You _must_ call this _after_ connecting to the WiFi network and
165-
// being assigned an IP address.
166-
if (!mdnsResponder.begin(mdnsName)) {
167-
// Serial.println("Failed to start MDNS responder!");
168-
while(1);
169-
}
170-
#else
171-
// Initialize the mDNS library. You can now reach or ping this
172-
// Arduino via the host name "arduino.local", provided that your operating
173-
// system is mDNS/Bonjour-enabled (such as MacOS X).
174-
// Always call this before any other method!
175-
mdns.begin(WiFi.localIP(), mdnsName);
176-
177-
// Now let's register the service we're offering (a web service) via Bonjour!
178-
// To do so, we call the addServiceRecord() method. The first argument is the
179-
// name of our service instance and its type, separated by a dot. In this
180-
// case, the service type is _http. There are many other service types, use
181-
// google to look up some common ones, but you can also invent your own
182-
// service type, like _mycoolservice - As long as your clients know what to
183-
// look for, you're good to go.
184-
// The second argument is the port on which the service is running. This is
185-
// port 80 here, the standard HTTP port.
186-
// The last argument is the protocol type of the service, either TCP or UDP.
187-
// Of course, our service is a TCP service.
188-
// With the service registered, it will show up in a mDNS/Bonjour-enabled web
189-
// browser. As an example, if you are using Apple's Safari, you will now see
190-
// the service under Bookmarks -> Bonjour (Provided that you have enabled
191-
// Bonjour in the "Bookmarks" preferences in Safari).
192-
mdns.addServiceRecord("http-on-off._http",
193-
80,
194-
MDNSServiceTCP);
195-
#endif
196-
197-
// Serial.print("Server listening at http://");
198-
// Serial.print(mdnsName);
199-
// Serial.println(".local/");
200-
201-
// you're connected now, so print out the status:
202-
// printWiFiStatus();
126+
adapter.addDevice(&weather);
127+
adapter.begin();
203128
}
204129

205-
unsigned long lastPrint = 0;
206-
207130
void loop() {
208-
#if USE_MDNS_RESPONDER
209-
// Call the update() function on the MDNS responder every loop iteration to
210-
// make sure it can detect and respond to name requests.
211-
mdnsResponder.poll();
212-
#else
213-
mdns.run();
214-
#endif
215-
216-
// print wifi status every 30 seconds
217-
unsigned long now = millis();
218-
if ((now - lastPrint) > 30000) {
219-
lastPrint = now;
220-
// Serial.println("");
221-
// printWiFiStatus();
222-
}
223-
224131
readBME280Data();
225-
server.update();
132+
adapter.update();
226133
}
227-
228-
// void printWiFiStatus() {
229-
// // print the SSID of the network you're attached to:
230-
// Serial.print("SSID: ");
231-
// Serial.println(WiFi.SSID());
232-
//
233-
// // print your WiFi shield's IP address:
234-
// IPAddress ip = WiFi.localIP();
235-
// Serial.print("IP Address: ");
236-
// Serial.println(ip);
237-
//
238-
// Serial.print("MDNS Name: ");
239-
// Serial.println(mdnsName);
240-
//
241-
// Serial.print("Mac: ");
242-
// Serial.print(mac[5], HEX);
243-
// Serial.print(":");
244-
// Serial.print(mac[4], HEX);
245-
// Serial.print(":");
246-
// Serial.print(mac[3], HEX);
247-
// Serial.print(":");
248-
// Serial.print(mac[2], HEX);
249-
// Serial.print(":");
250-
// Serial.print(mac[1], HEX);
251-
// Serial.print(":");
252-
// Serial.println(mac[0], HEX);
253-
//
254-
// // print the received signal strength:
255-
// long rssi = WiFi.RSSI();
256-
// Serial.print("signal strength (RSSI):");
257-
// Serial.print(rssi);
258-
// Serial.println(" dBm");
259-
// }

0 commit comments

Comments
 (0)
Please sign in to comment.