Skip to content

Commit bc9565d

Browse files
committed
Add LED example for @platformio
1 parent fa9577d commit bc9565d

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
This directory is intended for the project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link to executable file.
4+
5+
The source code of each library should be placed in separate directory, like
6+
"lib/private_lib/[here are source files]".
7+
8+
For example, see how can be organized `Foo` and `Bar` libraries:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) http://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- readme.txt --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
Then in `src/main.c` you should use:
31+
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
// rest H/C/CPP code
36+
37+
PlatformIO will find your libraries automatically, configure preprocessor's
38+
include paths and build them.
39+
40+
More information about PlatformIO Library Dependency Finder
41+
- http://docs.platformio.org/page/librarymanager/ldf.html
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; http://docs.platformio.org/page/projectconf.html
10+
11+
[platformio]
12+
; uncomment below to build a one env
13+
; env_default= nodemcuv2
14+
; env_default= esp32dev
15+
; env_default= samd21g18a
16+
17+
[global]
18+
lib_deps =
19+
https://github.com/mozilla-iot/webthing-esp8266.git
20+
ArduinoJson
21+
22+
[env:nodemcuv2]
23+
platform = espressif8266
24+
board = nodemcuv2
25+
framework = arduino
26+
lib_deps = ${global.lib_deps}
27+
lib_ignore =
28+
ArduinoMDNS
29+
WiFi101
30+
lib_ldf_mode = deep+
31+
32+
[env:esp32dev]
33+
platform = espressif32
34+
board = esp32dev
35+
framework = arduino
36+
lib_deps =
37+
${global.lib_deps}
38+
ESP Async WebServer
39+
lib_ignore = WiFi101
40+
lib_ldf_mode = deep+
41+
42+
[env:samd21g18a]
43+
platform = atmelsam
44+
board = samd21g18a
45+
framework = arduino
46+
lib_deps =
47+
${global.lib_deps}
48+
WiFi101
49+
ArduinoMDNS
50+
lib_ldf_mode = deep+
51+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Simple ESP8266 server compliant with Mozilla's proposed WoT API
3+
* Based on the HelloServer example
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
10+
#include <Arduino.h>
11+
#include "Thing.h"
12+
#include "WebThingAdapter.h"
13+
14+
//TODO: Hardcode your wifi credentials here (and keep it private)
15+
const char* ssid = "public";
16+
const char* password = "";
17+
18+
#if defined(LED_BUILTIN)
19+
const int ledPin = LED_BUILTIN;
20+
#else
21+
const int ledPin = 13; // manully configure LED pin
22+
#endif
23+
24+
WebThingAdapter adapter("w25");
25+
26+
ThingDevice led("led", "Built-in LED", "onOffSwitch");
27+
28+
ThingProperty ledOn("on", "", BOOLEAN);
29+
30+
bool lastOn = false;
31+
32+
void setup(void){
33+
pinMode(ledPin, OUTPUT);
34+
digitalWrite(ledPin, HIGH);
35+
Serial.begin(115200);
36+
Serial.println("");
37+
Serial.print("Connecting to \"");
38+
Serial.print(ssid);
39+
Serial.println("\"");
40+
#if defined(ESP8266) || defined(ESP32)
41+
WiFi.mode(WIFI_STA);
42+
#endif
43+
WiFi.begin(ssid, password);
44+
Serial.println("");
45+
46+
// Wait for connection
47+
bool blink = true;
48+
while (WiFi.status() != WL_CONNECTED) {
49+
delay(500);
50+
Serial.print(".");
51+
digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
52+
blink = !blink;
53+
}
54+
digitalWrite(ledPin, HIGH); // active low led
55+
56+
Serial.println("");
57+
Serial.print("Connected to ");
58+
Serial.println(ssid);
59+
Serial.print("IP address: ");
60+
Serial.println(WiFi.localIP());
61+
62+
led.addProperty(&ledOn);
63+
adapter.addDevice(&led);
64+
adapter.begin();
65+
Serial.println("HTTP server started");
66+
Serial.print("http://");
67+
Serial.print(WiFi.localIP());
68+
Serial.print("/things/");
69+
Serial.println(led.id);
70+
}
71+
72+
void loop(void){
73+
adapter.update();
74+
bool on = ledOn.getValue().boolean;
75+
digitalWrite(ledPin, on ? LOW : HIGH); // active low led
76+
if (on != lastOn) {
77+
Serial.print(led.id);
78+
Serial.print(": ");
79+
Serial.println(on);
80+
}
81+
lastOn = on;
82+
}
83+

0 commit comments

Comments
 (0)