|
33 | 33 | #include <SPI.h>
|
34 | 34 | #include <Wire.h>
|
35 | 35 | #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> |
46 | 38 |
|
47 | 39 | #ifndef PIN_STATE_HIGH
|
48 | 40 | #define PIN_STATE_HIGH HIGH
|
|
51 | 43 | #define PIN_STATE_LOW LOW
|
52 | 44 | #endif
|
53 | 45 |
|
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"); |
74 | 47 |
|
75 | 48 | ThingDevice weather("bme280", "BME280 Weather Sensor", "thing");
|
76 | 49 | ThingProperty weatherTemp("temperature", "", NUMBER);
|
@@ -147,113 +120,14 @@ void setup() {
|
147 | 120 | // connected, make the LED stay on
|
148 | 121 | digitalWrite(LED_BUILTIN, PIN_STATE_HIGH);
|
149 | 122 |
|
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 |
| - |
156 | 123 | weather.addProperty(&weatherTemp);
|
157 | 124 | weather.addProperty(&weatherPres);
|
158 | 125 | 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(); |
203 | 128 | }
|
204 | 129 |
|
205 |
| -unsigned long lastPrint = 0; |
206 |
| - |
207 | 130 | 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 |
| - |
224 | 131 | readBME280Data();
|
225 |
| - server.update(); |
| 132 | + adapter.update(); |
226 | 133 | }
|
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