Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Cayenne-MQTT-mbed Servo nfc X_NUCLEO_IDW01M1v2 NetworkSocketAPI 13
Revision 12:32dbf8ff8d80, committed 2019-09-05
- Comitter:
- kapitaninternet
- Date:
- Thu Sep 05 20:00:08 2019 +0000
- Parent:
- 11:60c50eae8b81
- Child:
- 13:7b3b429e9731
- Commit message:
- 1
Changed in this revision
| Servo.h | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.h Thu Sep 05 20:00:08 2019 +0000
@@ -0,0 +1,98 @@
+/* mbed R/C Servo Library
+ * Copyright (c) 2007-2010 sford, cstyles
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_SERVO_H
+#define MBED_SERVO_H
+
+#include "mbed.h"
+
+/** Servo control class, based on a PwmOut
+ *
+ * Example:
+ * @code
+ * // Continuously sweep the servo through it's full range
+ * #include "mbed.h"
+ * #include "Servo.h"
+ *
+ * Servo myservo(p21);
+ *
+ * int main() {
+ * while(1) {
+ * for(int i=0; i<100; i++) {
+ * myservo = i/100.0;
+ * wait(0.01);
+ * }
+ * for(int i=100; i>0; i--) {
+ * myservo = i/100.0;
+ * wait(0.01);
+ * }
+ * }
+ * }
+ * @endcode
+ */
+class Servo {
+
+public:
+ /** Create a servo object connected to the specified PwmOut pin
+ *
+ * @param pin PwmOut pin to connect to
+ */
+ Servo(PinName pin);
+
+ /** Set the servo position, normalised to it's full range
+ *
+ * @param percent A normalised number 0.0-1.0 to represent the full range.
+ */
+ void write(float percent);
+
+ /** Read the servo motors current position
+ *
+ * @param returns A normalised number 0.0-1.0 representing the full range.
+ */
+ float read();
+
+ /** Set the servo position
+ *
+ * @param degrees Servo position in degrees
+ */
+ void position(float degrees);
+
+ /** Allows calibration of the range and angles for a particular servo
+ *
+ * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
+ * @param degrees Angle from centre to maximum/minimum position in degrees
+ */
+ void calibrate(float range = 0.0005, float degrees = 45.0);
+
+ /** Shorthand for the write and read functions */
+ Servo& operator= (float percent);
+ Servo& operator= (Servo& rhs);
+ operator float();
+
+protected:
+ PwmOut _pwm;
+ float _range;
+ float _degrees;
+ float _p;
+};
+
+#endif
--- a/main.cpp Sun Sep 01 15:50:42 2019 +0000
+++ b/main.cpp Thu Sep 05 20:00:08 2019 +0000
@@ -7,10 +7,8 @@
#include "XNucleoNFC01A1.h" // modul nfc
#include "NDefLib/NDefNfcTag.h"
#include "NDefLib/RecordType/RecordURI.h"
-
-AnalogIn ain(A0);
-DigitalOut led1(LED1);
-DigitalOut wifiLed(LED3);
+#include "Servo.h"
+
/* Instantiate the expansion board */
static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
@@ -24,45 +22,52 @@
char* password = "19f07b4d8806fe42bdda724980634f39d8e639ba";
char* clientID = "bb8e7cc0-74b9-11e9-94e9-493d67fd755e";
+AnalogIn ain(A0);
+DigitalOut myLed(LED2);
+Servo myservo(PA_6);
+DigitalOut actuatorPin2(PA_7);
+float voltageMultiplier = 5.0;
+int publishInterval = 1000;
+
// WiFi network info.
char* ssid = "Interneto";
char* wifiPassword = "matu1234";
-/* Helper function for printing floats & doubles */
-static char *print_double(char* str, double v, int decimalDigits=2)
-{
- int i = 1;
- int intPart, fractPart;
- int len;
- char *ptr;
+// /* Helper function for printing floats & doubles */
+// static char *print_double(char* str, double v, int decimalDigits=2)
+// {
+// int i = 1;
+// int intPart, fractPart;
+// int len;
+// char *ptr;
- /* prepare decimal digits multiplicator */
- for (;decimalDigits!=0; i*=10, decimalDigits--);
+// /* prepare decimal digits multiplicator */
+// for (;decimalDigits!=0; i*=10, decimalDigits--);
- /* calculate integer & fractinal parts */
- intPart = (int)v;
- fractPart = (int)((v-(double)(int)v)*i);
+// /* calculate integer & fractinal parts */
+// intPart = (int)v;
+// fractPart = (int)((v-(double)(int)v)*i);
- /* fill in integer part */
- sprintf(str, "%i.", intPart);
+// /* fill in integer part */
+// sprintf(str, "%i.", intPart);
- /* prepare fill in of fractional part */
- len = strlen(str);
- ptr = &str[len];
+// /* prepare fill in of fractional part */
+// len = strlen(str);
+// ptr = &str[len];
- /* fill in leading fractional zeros */
- for (i/=10;i>1; i/=10, ptr++) {
- if (fractPart >= i) {
- break;
- }
- *ptr = '0';
- }
+// /* fill in leading fractional zeros */
+// for (i/=10;i>1; i/=10, ptr++) {
+// if (fractPart >= i) {
+// break;
+// }
+// *ptr = '0';
+// }
- /* fill in (rest of) fractional part */
- sprintf(ptr, "%i", fractPart);
+// /* fill in (rest of) fractional part */
+// sprintf(ptr, "%i", fractPart);
- return str;
-}
+// return str;
+// }
/**
* Write a Ndef URI message linking to st.com site.
@@ -124,9 +129,11 @@
if (message.topic == COMMAND_TOPIC) {
switch(message.channel) {
- case 0:
- // Set the onboard LED state
- led1 = atoi(message.getValue());
+ case 4:
+ // Set the onboard LED state & actuator PIN
+ myLed = atoi(message.getValue());
+
+ actuatorPin2 = atoi(message.getValue());
// Publish the updated LED state
if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
printf("Publish LED state failure, error: %d\n", error);
@@ -173,8 +180,6 @@
// Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed.
mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
- //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
- //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
return CAYENNE_SUCCESS;
}
@@ -184,16 +189,8 @@
*/
void loop(void)
{
- if(network.connected())
- {
- wifiLed = 1;
- } else
- {
- wifiLed = 0;
- }
-
// Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
- MQTTTimer timer(2500);
+ MQTTTimer timer(publishInterval);
while (true) {
// Yield to allow MQTT message processing.
@@ -203,41 +200,30 @@
if (!network.connected() || !mqttClient.connected()) {
network.disconnect();
mqttClient.disconnect();
- printf("Reconnecting\n");
while (connectClient() != CAYENNE_SUCCESS) {
- wait(2);
- printf("Reconnect failed, retrying\n");
+ wait(3);
}
}
- // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
+ // Publish data every few seconds. This should be changed to send your actual data to Cayenne.
if (timer.expired()) {
int error = 0;
uint8_t id;
float value1, value2;
- char buffer1[32], buffer2[32];
+ // char buffer1[32], buffer2[32];
/* Enable all sensors */
hum_temp->enable();
press_temp->enable();
-
+ hum_temp->read_id(&id);
+ press_temp->read_id(&id);
- hum_temp->read_id(&id);
- printf("HTS221 humidity & temperature = 0x%X\r\n", id);
- press_temp->read_id(&id);
- printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
-
- printf("\r\n");
hum_temp->get_temperature(&value1);
- hum_temp->get_humidity(&value2);
- printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
-
- press_temp->get_temperature(&value1);
+ // press_temp->get_temperature(&value1);
press_temp->get_pressure(&value2);
- printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
- float voltage_read = ain.read();
- printf("---\r\n");
+ // printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
+ float voltage_read = ain.read() * voltageMultiplier;
if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, value1)) != CAYENNE_SUCCESS) {
printf("Publish temperature failed, error: %d\n", error);
@@ -251,7 +237,7 @@
printf("Publish barometric pressure failed, error: %d\n", error);
}
// Restart the countdown timer for publishing data every 2 seconds. Change the timeout parameter to publish at a different interval.
- timer.countdown_ms(2000);
+ timer.countdown_ms(publishInterval);
}
}
}
@@ -261,25 +247,29 @@
*/
int main()
{
- printf("Initializing interface\n");
+ myLed = 0;
+
+ actuatorPin2 = 0;
interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
// Set the default function that receives Cayenne messages.
mqttClient.setDefaultMessageHandler(messageArrived);
+
+ for(float p=0; p<1.0; p += 0.2) {
+ myservo = p;
+ wait(1.5);
+}
// Connect to Cayenne.
if (connectClient() == CAYENNE_SUCCESS) {
// Run main loop.
loop();
}
- else {
- printf("Connection failed, exiting\n");
- }
+
if (mqttClient.connected())
mqttClient.disconnect();
if (network.connected())
network.disconnect();
-return 0;
-
+ return 0;
}
\ No newline at end of file