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
-
30
1
#define LARGE_JSON_BUFFERS 1
31
2
32
3
#include < Arduino.h>
33
4
#include < BME280.h>
34
5
#include < BME280I2C.h>
35
6
#include < SPI.h>
36
7
#include < Wire.h>
37
- #include < WiFi101 .h>
8
+ #include < WiFi .h>
38
9
#include < Thing.h>
39
10
#include < WebThingAdapter.h>
40
11
45
16
#define PIN_STATE_LOW LOW
46
17
#endif
47
18
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
+
48
31
WebThingAdapter *adapter;
49
32
50
33
const char *bme280Types[] = {" TemperatureSensor" , nullptr };
@@ -54,12 +37,16 @@ ThingProperty weatherTemp("temperature", "", NUMBER, "TemperatureProperty");
54
37
ThingProperty weatherHum (" humidity" , " " , NUMBER, " LevelProperty" );
55
38
ThingProperty weatherPres (" pressure" , " " , NUMBER, nullptr );
56
39
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
+ );
63
50
64
51
BME280I2C bme (settings);
65
52
@@ -79,46 +66,38 @@ void readBME280Data() {
79
66
}
80
67
81
68
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;
92
86
}
87
+ digitalWrite (ledPin, HIGH); // active low led
93
88
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 ());
96
94
97
95
Wire.begin ();
98
96
while (!bme.begin ()) {
99
- // Serial.println("Could not find BME280I2C sensor!");
97
+ Serial.println (" Could not find BME280I2C sensor!" );
100
98
delay (1000 );
101
99
}
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." );
122
101
123
102
// connected, make the LED stay on
124
103
digitalWrite (LED_BUILTIN, PIN_STATE_HIGH);
@@ -154,4 +133,4 @@ void setup() {
154
133
void loop () {
155
134
readBME280Data ();
156
135
adapter->update ();
157
- }
136
+ }
0 commit comments