Skip to content

Commit e0f43af

Browse files
authored
Deduplicate example code. (#102)
1 parent 0b10137 commit e0f43af

File tree

29 files changed

+370
-634
lines changed

29 files changed

+370
-634
lines changed

.github/workflows/pythonapp.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,4 @@ jobs:
3131
platformio update
3232
- name: Test examples
3333
run: |
34-
platformio run --project-dir examples/PlatformIO/BME280
35-
platformio run --project-dir examples/PlatformIO/LED
36-
platformio run --project-dir examples/PlatformIO/LEDLamp
37-
platformio run --project-dir examples/PlatformIO/TextDisplay
34+
for dir in examples/PlatformIO/*; do platformio run --project-dir "$dir"; done

EthernetWebThingAdapter.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@
1313
#if !defined(ESP32) && !defined(ESP8266)
1414

1515
#include <Arduino.h>
16+
1617
#if defined(STM32F7xx)
1718
#include <LwIP.h>
1819
#include <STM32Ethernet.h>
1920
#else
2021
#include <Ethernet.h>
2122
#include <EthernetClient.h>
2223
#include <EthernetServer.h>
23-
#include <EthernetUdp.h>
24-
#include <Dhcp.h>
25-
#include <Dns.h>
2624
#define CONFIG_MDNS 1
2725
#endif
2826

2927
#ifdef CONFIG_MDNS
28+
#include <EthernetUdp.h>
3029
#include <ArduinoMDNS.h>
31-
EthernetUDP udp;
32-
MDNS mdns(udp);
3330
#endif
3431

3532
#include <ArduinoJson.h>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Test of webthing-arduino with async behavior. Works with
3+
* thing-url-adapter.
4+
*/
5+
6+
#include <Arduino.h>
7+
#include <Thing.h>
8+
#include <WebThingAdapter.h>
9+
10+
const char *ssid = "XXXXXXX";
11+
const char *password = "XXXXXXX";
12+
13+
int loginCounter;
14+
WebThingAdapter *adapter;
15+
// Forward declaration
16+
void onOffChanged(ThingPropertyValue newValue);
17+
void textChanged(ThingPropertyValue newValue);
18+
void numberChanged(ThingPropertyValue newValue);
19+
20+
const char *asyncProperties[] = {"asyncProperty", nullptr};
21+
ThingDevice textDisplay("asyncProperty", "AsyncProperty Test",
22+
asyncProperties);
23+
ThingProperty text("text", "", STRING, nullptr, textChanged);
24+
ThingProperty onOff("bool", "", BOOLEAN, "OnOffProperty", onOffChanged);
25+
ThingProperty number("number", "", NUMBER, nullptr, numberChanged);
26+
27+
String message = "message";
28+
String lastMessage = message;
29+
boolean lastState = false;
30+
int lastNumber = 0;
31+
32+
// Callback functions
33+
//
34+
void onOffChanged(ThingPropertyValue newValue) {
35+
Serial.print("On/Off changed to : ");
36+
Serial.println(newValue.boolean);
37+
}
38+
39+
void textChanged(ThingPropertyValue newValue) {
40+
Serial.print("New message : ");
41+
Serial.println(*newValue.string);
42+
}
43+
44+
void numberChanged(ThingPropertyValue newValue) {
45+
Serial.print("New number : ");
46+
Serial.println(newValue.number);
47+
}
48+
49+
void setup() {
50+
Serial.begin(115200);
51+
52+
// counter used for login time out, function needed with ESP32
53+
loginCounter = 0;
54+
55+
#if defined(ESP8266) || defined(ESP32)
56+
WiFi.mode(WIFI_STA);
57+
#endif
58+
59+
Serial.print("Connecting : ");
60+
WiFi.begin(ssid, password);
61+
62+
// Wait for connection
63+
while (WiFi.status() != WL_CONNECTED) {
64+
65+
// more than 5 sec wait, try to login again.
66+
if (loginCounter == 10) {
67+
loginCounter = 0;
68+
WiFi.begin(ssid, password);
69+
} else {
70+
loginCounter++;
71+
}
72+
delay(500);
73+
Serial.print(".");
74+
}
75+
76+
Serial.println("");
77+
Serial.print("Connected to ");
78+
Serial.println(ssid);
79+
Serial.print("IP address: ");
80+
Serial.println(WiFi.localIP());
81+
82+
adapter = new WebThingAdapter("asyncProperty", WiFi.localIP());
83+
84+
ThingPropertyValue value;
85+
value.string = &message;
86+
text.setValue(value);
87+
88+
textDisplay.addProperty(&text);
89+
textDisplay.addProperty(&onOff);
90+
textDisplay.addProperty(&number);
91+
adapter->addDevice(&textDisplay);
92+
adapter->begin();
93+
}
94+
95+
void loop() {
96+
delay(500);
97+
adapter->update();
98+
}

examples/AsyncProperty/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/make -f
2+
# SPDX-License-Identifier: MPL-2.0
3+
#{
4+
# This Source Code Form is subject to the terms of the Mozilla Public
5+
# License, v. 2.0. If a copy of the MPL was not distributed with this
6+
# file, You can obtain one at http://mozilla.org/MPL/2.0/
7+
#}
8+
9+
default: all
10+
@echo "# $@: $^"
11+
12+
top_reldir?=../..
13+
topdir?=${CURDIR}/${top_reldir}
14+
15+
-include ${topdir}/Makefile
16+
17+
AVR_TOOLS_DIR?=${ARDUINO_DIR}/hardware/tools/avr
18+
ARDUINO_DIR?=/usr/share/arduino
19+
ARDMK_DIR?=${ARDUINO_DIR}
20+
arduino_mk?=${ARDMK_DIR}/Arduino.mk
21+
22+
#{ Config part
23+
VARIANT?=mega
24+
BOARD_TAG=mega2560
25+
BOARD_SUB=
26+
MCU?=at${BOARD_TAG}
27+
F_CPU?=16000000L
28+
MONITOR_PORT?=$(shell ls /dev/ttyUSB* 2> /dev/null \
29+
|| ls /dev/ttyACM* 2> /dev/null \
30+
|| echo "/dev/TODO/setup/monitor" \
31+
| sort | head -n1)
32+
#}
33+
34+
ARCHITECTURE=avr
35+
AVRDUDE_ARD_BAUDRATE?=115200
36+
MONITOR_BAUDRATE?=${AVRDUDE_ARD_BAUDRATE}
37+
AVRDUDE_ARD_PROGRAMMER?=wiring
38+
39+
CPPFLAGS+=-I${top_reldir}
40+
41+
ARDUINO_LIBS += Ethernet SPI
42+
ARDUINO_LIBS += ArduinoMDNS
43+
ARDUINO_LIBS += ArduinoJson
44+
ARDUINO_LIBS += WiFi101
45+
46+
-include ${arduino_mk}
47+
48+
rule/setup:
49+
@echo "Please setup your env to use ${arduino_mk}"
50+
51+
%: rule/setup
52+
@echo "Please run \"make $@\" again"
File renamed without changes.

examples/BME280/Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/make -f
2+
# SPDX-License-Identifier: MPL-2.0
3+
#{
4+
# This Source Code Form is subject to the terms of the Mozilla Public
5+
# License, v. 2.0. If a copy of the MPL was not distributed with this
6+
# file, You can obtain one at http://mozilla.org/MPL/2.0/
7+
#}
8+
9+
default: all
10+
@echo "# $@: $^"
11+
12+
top_reldir?=../..
13+
topdir?=${CURDIR}/${top_reldir}
14+
15+
-include ${topdir}/Makefile
16+
17+
AVR_TOOLS_DIR?=${ARDUINO_DIR}/hardware/tools/avr
18+
ARDUINO_DIR?=/usr/share/arduino
19+
ARDMK_DIR?=${ARDUINO_DIR}
20+
arduino_mk?=${ARDMK_DIR}/Arduino.mk
21+
22+
#{ Config part
23+
VARIANT?=mega
24+
BOARD_TAG=mega2560
25+
BOARD_SUB=
26+
MCU?=at${BOARD_TAG}
27+
F_CPU?=16000000L
28+
MONITOR_PORT?=$(shell ls /dev/ttyUSB* 2> /dev/null \
29+
|| ls /dev/ttyACM* 2> /dev/null \
30+
|| echo "/dev/TODO/setup/monitor" \
31+
| sort | head -n1)
32+
#}
33+
34+
ARCHITECTURE=avr
35+
AVRDUDE_ARD_BAUDRATE?=115200
36+
MONITOR_BAUDRATE?=${AVRDUDE_ARD_BAUDRATE}
37+
AVRDUDE_ARD_PROGRAMMER?=wiring
38+
39+
CPPFLAGS+=-I${top_reldir}
40+
41+
ARDUINO_LIBS += Ethernet SPI
42+
ARDUINO_LIBS += ArduinoMDNS
43+
ARDUINO_LIBS += ArduinoJson
44+
ARDUINO_LIBS += WiFi101
45+
ARDUINO_LIBS += BME280
46+
47+
-include ${arduino_mk}
48+
49+
rule/setup:
50+
@echo "Please setup your env to use ${arduino_mk}"
51+
52+
%: rule/setup
53+
@echo "Please run \"make $@\" again"

examples/LEDLamp/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ AVRDUDE_ARD_BAUDRATE?=115200
3636
MONITOR_BAUDRATE?=${AVRDUDE_ARD_BAUDRATE}
3737
AVRDUDE_ARD_PROGRAMMER?=wiring
3838

39-
CPPFLAGS+=-I${top_reldir}
39+
CPPFLAGS+=-I${top_reldir}
4040

4141
ARDUINO_LIBS += Ethernet SPI
4242
ARDUINO_LIBS += ArduinoMDNS
4343
ARDUINO_LIBS += ArduinoJson
4444
ARDUINO_LIBS += WiFi101
45+
ARDUINO_LIBS += ESP32_AnalogWrite
4546

4647
-include ${arduino_mk}
4748

examples/LevelSensor/LevelSensor.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
99
*/
1010

11-
#include <stdio.h>
11+
#include <Arduino.h>
1212
#include <Thing.h>
1313
#include <EthernetWebThingAdapter.h>
1414

@@ -28,7 +28,8 @@ int setupNetwork() {
2828
uint8_t ETHERNET_MAC[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
2929
uint8_t error = Ethernet.begin(ETHERNET_MAC);
3030
if (error == 0) {
31-
printf("error: %s\n", __FUNCTION__);
31+
Serial.print("error: ");
32+
Serial.println(__FUNCTION__);
3233
return -1;
3334
}
3435
return 0;

examples/LevelSensor/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ AVRDUDE_ARD_BAUDRATE?=115200
3636
MONITOR_BAUDRATE?=${AVRDUDE_ARD_BAUDRATE}
3737
AVRDUDE_ARD_PROGRAMMER?=wiring
3838

39-
CPPFLAGS+=-I${top_reldir}
39+
CPPFLAGS+=-I${top_reldir}
4040

4141
ARDUINO_LIBS += Ethernet SPI
4242
ARDUINO_LIBS += ArduinoMDNS

examples/PlatformIO/AsyncProperty/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
[global]
1919
lib_deps =
2020
https://github.com/mozilla-iot/webthing-arduino.git
21-
ArduinoJson
21+
ArduinoJson@6.13.0
2222
monitor_speed = 115200
2323

2424
[env:d1]

0 commit comments

Comments
 (0)