diff --git a/ArduinoProps.vcxitems b/ArduinoProps.vcxitems index a3e6a9f..c56d5e0 100644 --- a/ArduinoProps.vcxitems +++ b/ArduinoProps.vcxitems @@ -42,6 +42,8 @@ + + @@ -51,6 +53,8 @@ + + diff --git a/ArduinoProps.vcxitems.filters b/ArduinoProps.vcxitems.filters index c2f8559..d70493f 100644 --- a/ArduinoProps.vcxitems.filters +++ b/ArduinoProps.vcxitems.filters @@ -80,6 +80,12 @@ Source Files + + Source Files + + + Source Files + @@ -103,6 +109,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/EXAMPLES.md b/EXAMPLES.md index 5e95534..2dbfd45 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -5,6 +5,7 @@ An adaptation of the internal led Blink example (https://www.arduino.cc/en/tutor 2. [**BlinkOnEthernetProp**](#2-blinkonethernetprop-the-blink-example-on-an-ethernet-prop-with-arduinoprops-library): the Blink example on an Ethernet prop with *ArduinoProps library* 3. [**BlinkOnWifiProp**](#3-blinkonwifiprop-the-blink-example-on-a-wifi-prop-with-arduinoprops-library): the Blink example on a Wifi prop with *ArduinoProps library* 4. [**BlinkOnBridgePubSub**](#4-blinkonbridgepubsub-the-blink-example-on-prop-using-pubsubclient-directly): the Blink example on prop using *PubSubClient* directly +5. [**BlinkOnStm32Nucleo144Prop**](#5-blinkonstm32nucelo144prop-the-blink-example-on-an-stm32-nucelo-144-prop-with-arduinoprops-library): the Blink example on an STM32 Nucleo-144 prop with *ArduinoProps library* MQTT messages are received asynchronously therefore to keep the sketch responsive to MQTT commands, calls to delay() should be avoided (except short ones, say < 100 milliseconds). @@ -456,9 +457,55 @@ Global variables use 1013 bytes (39%) of dynamic memory, which leaves 1547 bytes ``` +## 5. *BlinkOnStm32Nucleo144Prop*: the Blink example on an STM32 Nucleo-144 prop with *ArduinoProps library* + +Sketch with *BlinkOnStm32Nucleo144Prop* has been derived *EthernetProp*. + +Includes for STM32 are different: +```c +#include +#include +#include "Stm32Nucleo144Prop.h" +#include "ArduinoProps.h" +#include "Stm32Millis.h" +extern Stm32MillisClass Stm32Millis; +``` + +Ethernet connection start is slightly different: +```c + if (ip == IPAddress(0,0,0,0)) { + if (!Ethernet.begin(mac)) { + // if DHCP fails, start with a hard-coded address: + Ethernet.begin(mac, IPAddress(10, 90, 90, 239)); + } + } + else { + Ethernet.begin(mac, ip); + } +``` + +`millis()` is missing in STM323duino library, you must start **Stm32Millis** in `setup()`: +```c + // millis() missing in STM32duino + Stm32Millis.begin(); +``` + +#### Pay atention to the board MAC address: +MAC adresses are hardware identifiers on the network so they must be unique. + +A good practice is to increment only the byte at the very right (`0x03`) when adding a new Arduino Ethernet on your network. + +```csharp +byte mac[] = { 0x46, 0x4F, 0xEA, 0x10, 0x20, 0x03 }; //<<< MAKE SURE IT'S UNIQUE IN YOUR NETWORK!!! and not a reserved MAC + +``` + + + + ## Author -**Faure Systems** (Nov 15th, 2019) +**Faure Systems** (Jun 25th, 2019) * company: FAURE SYSTEMS SAS * mail: *dev at faure dot systems* * github: fauresystems diff --git a/README.md b/README.md index 141f130..74efb2c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ArduinoProps library +# ArduinoProps library 1. [Installation and usage](#1-installation-and-usage) 2. [Escape room 2.0 prop with Arduino](#2-escape-room-20-prop-with-arduino) 3. [*ArduinoProps library* value for escape room 2.0](#3-arduinoprops-library-value-for-escape-room-20) @@ -49,6 +49,28 @@ Learn more at fauresystems diff --git a/examples/BlinkOnStm32Nucleo144Prop/BlinkOnStm32Nucleo144Prop.ino b/examples/BlinkOnStm32Nucleo144Prop/BlinkOnStm32Nucleo144Prop.ino new file mode 100644 index 0000000..398765d --- /dev/null +++ b/examples/BlinkOnStm32Nucleo144Prop/BlinkOnStm32Nucleo144Prop.ino @@ -0,0 +1,121 @@ +/* Stm32Nucleo144Prop.ino + MIT License (c) Faure Systems + + Adapt the Blink example (https://www.arduino.cc/en/tutorial/blink) as a + simple MQTT prop. Avoid delay() calls (except short ones) in loop() to + ensure CPU for MQTT protocol. Use PropAction checks instead. + + Copy and change it to build your first Arduino connected prop, you will + only be limited by your imagination. + + Requirements: + - install ArduinoProps.zip library and dependencies (https://github.com/fauresystems/ArduinoProps) + - help: https://github.com/xcape-io/ArduinoProps/blob/master/help/ArduinoProps_sketch.md + - STM32 Nucleo-144 borads require: STM32duino LwIP and STM32duino STM32Ethernet libraries + +*/ +#include +#include +#include "Stm32Nucleo144Prop.h" +#include "ArduinoProps.h" +#include "Stm32Millis.h" +extern Stm32MillisClass Stm32Millis; + +// If you're running xcape.io Room software you have to respect prop inbox/outbox +// topicw syntax: Room/[escape room name]/Props/[propsname]/inbox|outbox +// https://xcape.io/go/room + +Stm32Nucleo144Prop prop(u8"Nucleo Blink", // as MQTT client id, should be unique per client for given broker + u8"Room/My room/Props/Nucleo Blink/inbox", + u8"Room/My room/Props/Nucleo Blink/outbox", + "192.168.1.42", // your MQTT server IP address + 1883); // your MQTT server port; + +byte mac[] = { 0x46, 0x4F, 0xEA, 0x10, 0x20, 0x04 }; //<<< MAKE SURE IT'S UNIQUE IN YOUR NETWORK!!! and not a reserved MAC +IPAddress ip(192, 168, 1, 177); //<<< Set to (0,0,0,0) to get an address from DHCP +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +IPAddress server(192, 168, 1, 42); // numeric IP for Google (no DNS) +//char server[] = "broker.mqtt-dashboard.com"; // name address for mqtt broker (using DNS) + +PropDataLogical blinking(u8"blink", u8"yes", u8"no", true); +PropDataLogical led(u8"led"); + +void blink(); // forward +PropAction blinkingAction = PropAction(1000, blink); + +void setup() +{ + if (ip == IPAddress(0,0,0,0)) { + if (!Ethernet.begin(mac)) { + // if DHCP fails, start with a hard-coded address: + Ethernet.begin(mac, IPAddress(10, 90, 90, 239)); + } + } + else { + Ethernet.begin(mac, ip); + } + + delay(1500); // allow the hardware to sort itself out + + // millis() missing in STM32duino + Stm32Millis.begin(); + + prop.addData(&blinking); + prop.addData(&led); + + prop.begin(InboxMessage::run); + + pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output + + // At this point, the broker is not connected yet +} + +void loop() +{ + prop.loop(); + + led.setValue(digitalRead(LED_BUILTIN)); // read I/O + + blinkingAction.check(); // do your stuff, don't freeze the loop with delay() calls +} + +void blink() +{ + if (blinking.value()) { + led.setValue(!led.value()); + digitalWrite(LED_BUILTIN, led.value() ? HIGH : LOW); + } +} + +void InboxMessage::run(String a) { + + if (a == u8"app:startup") + { + prop.sendAllData(); + prop.sendDone(a); + } + else if (a == u8"reset-mcu") + { + prop.resetMcu(); + } + else if (a == "blink:1") + { + blinking.setValue(true); + + prop.sendAllData(); // all data change, we don't have to be selctive then + prop.sendDone(a); // acknowledge prop command action + } + else if (a == "blink:0") + { + blinking.setValue(false); + + prop.sendAllData(); // all data change, we don't have to be selctive then + prop.sendDone(a); // acknowledge prop command action + } + else + { + // acknowledge omition of the prop command + prop.sendOmit(a); + } +} diff --git a/help/Prop-Protocol.png b/help/Prop-Protocol.png index 16724c3..30ae313 100644 Binary files a/help/Prop-Protocol.png and b/help/Prop-Protocol.png differ diff --git a/help/board-manager-1.png b/help/board-manager-1.png new file mode 100644 index 0000000..6c3a5ad Binary files /dev/null and b/help/board-manager-1.png differ diff --git a/help/open-board-manager.png b/help/open-board-manager.png new file mode 100644 index 0000000..90481d1 Binary files /dev/null and b/help/open-board-manager.png differ diff --git a/help/preferences-board-manager.png b/help/preferences-board-manager.png new file mode 100644 index 0000000..9dda5fa Binary files /dev/null and b/help/preferences-board-manager.png differ diff --git a/keywords.txt b/keywords.txt index 05c62ae..8e6aea2 100644 --- a/keywords.txt +++ b/keywords.txt @@ -10,6 +10,8 @@ Prop KEYWORD1 BridgeProp KEYWORD1 EthernetProp KEYWORD1 WifiProp KEYWORD1 +Stm32Nucleo144Prop KEYWORD1 +Stm32Millis KEYWORD1 PropAction KEYWORD1 PropData KEYWORD1 PropDataDecimal KEYWORD1 @@ -56,6 +58,7 @@ setBrokerIpAddress KEYWORD2 checkDataChanges KEYWORD2 resetIntervals KEYWORD2 send KEYWORD2 +update KEYWORD2 ####################################### diff --git a/library.properties b/library.properties index 02ca7b3..4b16782 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ArduinoProps -version=1.0.0 +version=1.1.0 author=Faure Systems maintainer=Faure Systems sentence=ArduinoProps Library diff --git a/src/ArduinoProps.h b/src/ArduinoProps.h index e23ba4e..3746a9a 100644 --- a/src/ArduinoProps.h +++ b/src/ArduinoProps.h @@ -10,6 +10,9 @@ #ifndef _ArduinoProps_h #define _ArduinoProps_h +#if !defined(STM32F4xx) && !defined(STM32F7xx) // tested with STM32F767 + + #if defined(ARDUINO) && ARDUINO >= 100 #include "arduino.h" #else @@ -19,6 +22,11 @@ #include "BridgeProp.h" #include "EthernetProp.h" #include "WifiProp.h" + +#else // !STM32F4xx && !STM32F7xx +#include +#endif // STM32F4 + #include "PropAction.h" #include "PropData.h" diff --git a/src/Prop.cpp b/src/Prop.cpp index e8e8913..5b4c75f 100644 --- a/src/Prop.cpp +++ b/src/Prop.cpp @@ -8,7 +8,7 @@ */ #include "Prop.h" -#include +//#include #if defined(__AVR__) #include #endif @@ -99,7 +99,7 @@ void Prop::checkDataChanges() void Prop::resetMcu() { -#if defined(ARDUINO_ARCH_SAMD) +#if defined(ARDUINO_ARCH_SAMD) || defined(STM32F4xx) || defined(STM32F7xx) NVIC_SystemReset(); #elif defined(ARDUINO_ARCH_AVR) || defined(__AVR__) wdt_enable(WDTO_15MS); diff --git a/src/Prop.h b/src/Prop.h index d134bfd..d8ce097 100644 --- a/src/Prop.h +++ b/src/Prop.h @@ -9,6 +9,8 @@ #ifndef PROP_H #define PROP_H +#if !defined(STM32F4xx) && !defined(STM32F7xx) // tested with STM32F767 + #if defined(ARDUINO) && ARDUINO >= 100 #include "arduino.h" #else @@ -16,6 +18,9 @@ #endif #include + +#endif // !STM32F4xx && !STM32F7xx + #include #include #include "PropAction.h" diff --git a/src/PropAction.cpp b/src/PropAction.cpp index be1ba1e..90dc22c 100644 --- a/src/PropAction.cpp +++ b/src/PropAction.cpp @@ -1,5 +1,5 @@ /* - Name: PropAction.h + Name: PropAction.cpp Author: Faure Systems Editor: https://github.com/fauresystems License: MIT License (c) Faure Systems @@ -9,6 +9,10 @@ #include "PropAction.h" +#if defined(STM32F4xx) || defined(STM32F7xx) +#include "Stm32Millis.h" +#endif + PropAction::PropAction() { _active = true; diff --git a/src/PropAction.h b/src/PropAction.h index 502bd5c..f9ee58f 100644 --- a/src/PropAction.h +++ b/src/PropAction.h @@ -10,12 +10,6 @@ #ifndef PROPACTION_H #define PROPACTION_H -#if defined(ARDUINO) && ARDUINO >= 100 -#include "arduino.h" -#else -#include "WProgram.h" -#endif - class PropAction { public: diff --git a/src/PropData.h b/src/PropData.h index 34f8f3d..c5059a4 100644 --- a/src/PropData.h +++ b/src/PropData.h @@ -9,12 +9,6 @@ #ifndef PROPDATA_H #define PROPDATA_H -#if defined(ARDUINO) && ARDUINO >= 100 -#include "arduino.h" -#else -#include "WProgram.h" -#endif - #include #include @@ -59,7 +53,7 @@ class PropDataInteger : public PropData class PropDataLogical : public PropData { public: - PropDataLogical(const char *, const char *trueval = NULL, const char *falseval = NULL, bool initial = false); + PropDataLogical(const char *, const char *trueval = 0, const char *falseval = 0, bool initial = false); String fetch(); String fetchChange(); void setValue(const bool); diff --git a/src/Stm32Millis.cpp b/src/Stm32Millis.cpp new file mode 100644 index 0000000..9599586 --- /dev/null +++ b/src/Stm32Millis.cpp @@ -0,0 +1,16 @@ +/* + Name: Stm32Millis.cpp + Author: Faure Systems + Editor: https://github.com/fauresystems + License: MIT License (c) Faure Systems + + Implement millis() missing in STM32duino. +*/ +#if defined(STM32F4xx) || defined(STM32F7xx) + +#include "Stm32Millis.h" + +unsigned long Stm32MillisClass::milliseconds = 0; +Stm32MillisClass Stm32Millis; + +#endif // STM32F4xx|STM32F7xx diff --git a/src/Stm32Millis.h b/src/Stm32Millis.h new file mode 100644 index 0000000..f259c73 --- /dev/null +++ b/src/Stm32Millis.h @@ -0,0 +1,48 @@ +/* + Name: Stm32Millis.h + Author: Faure Systems + Editor: https://github.com/fauresystems + License: MIT License (c) Faure Systems + + Implement millis() missing in STM32duino. + https://github.com/stm32duino/wiki/wiki/HardwareTimer-library +*/ + +#ifndef STM32MILLIS_H +#define STM32MILLIS_H + +#if defined(STM32F4xx) || defined(STM32F7xx) + +#include + +class Stm32MillisClass { + + public: + HardwareTimer *_timer; + static unsigned long milliseconds; + + static void update(void) { + ++Stm32MillisClass::milliseconds; + } + + void begin() { + +#if defined(TIM1) + TIM_TypeDef *Instance = TIM1; +#else + TIM_TypeDef *Instance = TIM2; +#endif + + // Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished. + _timer = new HardwareTimer(Instance); + + _timer->setOverflow(1000, MICROSEC_FORMAT); + _timer->attachInterrupt(update); + _timer->resume(); + } +}; + +extern Stm32MillisClass Stm32Millis; + +#endif // STM32F4xx|STM32F7xx +#endif diff --git a/src/Stm32Nucleo144Prop.cpp b/src/Stm32Nucleo144Prop.cpp new file mode 100644 index 0000000..c588522 --- /dev/null +++ b/src/Stm32Nucleo144Prop.cpp @@ -0,0 +1,21 @@ +/* + Name: Stm32Nucleo144Prop.cpp + Author: Faure Systems + Editor: https://github.com/fauresystems + License: MIT License (c) Faure Systems + + Prop for Arduino with Ethernet shield. +*/ + +#include "Stm32Nucleo144Prop.h" + +Stm32Nucleo144Prop::Stm32Nucleo144Prop(const char* client_id, const char* in_box, const char* out_box, const char* broker, const int port) + : Prop(client_id, in_box, out_box, broker, port) +{ + _client.setClient(_ethernetClient); +} + +void Stm32Nucleo144Prop::begin(void(*on_message)(String)) +{ + if (on_message) onInboxMessageReceived = on_message; +} diff --git a/src/Stm32Nucleo144Prop.h b/src/Stm32Nucleo144Prop.h new file mode 100644 index 0000000..b01a584 --- /dev/null +++ b/src/Stm32Nucleo144Prop.h @@ -0,0 +1,27 @@ +/* + Name: Stm32Nucleo144Prop.h + Author: Faure Systems + Editor: https://github.com/fauresystems + License: MIT License (c) Faure Systems + + Prop for Arduino with Ethernet shield. +*/ +#ifndef STM32NUCLEO144PROP_H +#define STM32NUCLEO144PROP_H + + +#include +#include +#include "Prop.h" + +class Stm32Nucleo144Prop : public Prop +{ + public: + Stm32Nucleo144Prop(const char*, const char*, const char*, const char*, const int); + void begin(void(*)(String) = NULL); + + private: + EthernetClient _ethernetClient; +}; + +#endif