Olja Jakovljevic
/
mc_proximity_servo_V1
Student project, Faculty of Electrical Engineering, University of Belgrade
Revision 0:0093b6d26596, committed 2022-07-13
- Comitter:
- oljakovljevic
- Date:
- Wed Jul 13 15:08:13 2022 +0000
- Commit message:
- Student project, Faculty of Electrical Engineering, University of Belgrade
Changed in this revision
diff -r 000000000000 -r 0093b6d26596 .gitignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
diff -r 000000000000 -r 0093b6d26596 CONTRIBUTING.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CONTRIBUTING.md Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,5 @@ +# Contributing to Mbed OS + +Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor. + +To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html).
diff -r 000000000000 -r 0093b6d26596 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,64 @@ +![](./resources/official_armmbed_example_badge.png) +# Blinky Mbed OS example + +The example project is part of the [Arm Mbed OS Official Examples](https://os.mbed.com/code/) and is the [getting started example for Mbed OS](https://os.mbed.com/docs/mbed-os/v5.14/quick-start/index.html). It contains an application that repeatedly blinks an LED on supported [Mbed boards](https://os.mbed.com/platforms/). + +You can build the project with all supported [Mbed OS build tools](https://os.mbed.com/docs/mbed-os/latest/tools/index.html). However, this example project specifically refers to the command-line interface tool [Arm Mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli). +(Note: To see a rendered example you can import into the Arm Online Compiler, please see our [import quick start](https://os.mbed.com/docs/mbed-os/latest/quick-start/online-with-the-online-compiler.html#importing-the-code).) + +1. [Install Mbed CLI](https://os.mbed.com/docs/mbed-os/latest/quick-start/offline-with-mbed-cli.html). + +1. Clone this repository on your system, and change the current directory to where the project was cloned: + + ```bash + $ git clone git@github.com:armmbed/mbed-os-example-blinky && cd mbed-os-example-blinky + ``` + + Alternatively, you can download the example project with Arm Mbed CLI using the `import` subcommand: + + ```bash + $ mbed import mbed-os-example-blinky && cd mbed-os-example-blinky + ``` + + +## Application functionality + +The `main()` function is the single thread in the application. It toggles the state of a digital output connected to an LED on the board. + +## Building and running + +1. Connect a USB cable between the USB port on the board and the host computer. +2. <a name="build_cmd"></a> Run the following command to build the example project and program the microcontroller flash memory: + ```bash + $ mbed compile -m <TARGET> -t <TOOLCHAIN> --flash + ``` +The binary is located at `./BUILD/<TARGET>/<TOOLCHAIN>/mbed-os-example-blinky.bin`. + +Alternatively, you can manually copy the binary to the board, which you mount on the host computer over USB. + +Depending on the target, you can build the example project with the `GCC_ARM`, `ARM` or `IAR` toolchain. After installing Arm Mbed CLI, run the command below to determine which toolchain supports your target: + +```bash +$ mbed compile -S +``` + +## Expected output +The LED on your target turns on and off every 500 milliseconds. + + +## Troubleshooting +If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it. + +## Related Links + +* [Mbed OS Stats API](https://os.mbed.com/docs/latest/apis/mbed-statistics.html). +* [Mbed OS Configuration](https://os.mbed.com/docs/latest/reference/configuration.html). +* [Mbed OS Serial Communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html). +* [Mbed OS bare metal](https://os.mbed.com/docs/mbed-os/latest/reference/mbed-os-bare-metal.html). +* [Mbed boards](https://os.mbed.com/platforms/). + +### License and contributions + +The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more info. + +This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.
diff -r 000000000000 -r 0093b6d26596 Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r 0093b6d26596 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,235 @@ +/* + +This code implements MQTT client for Smart Industry system. +Proximity sensor detects the presence of the cup and +the information is sent to the broker. +If the cap is on the second stage, command for the servo motor, calculated by the broker, +is sent to the client. + +Faculty of Electrical Engineering, University of Belgrade. +Version (1), July 2022. + +*/ + +// Include the libraries +#include "mbed.h" +#include "platform/mbed_thread.h" +#include <string> +#include <algorithm> +#include "Servo.h" +#include "MQTTClientMbedOs.h" + +#define MAX_NETWORKS 10 +#define PRINTF_DELAY_MS 10 +#define YIELD_TIMEOUT_MS 1000 + +#define PSAdress (0x38<<1) + +Servo myservo(D7); // servo motor control signal pin +Serial pc(USBTX,USBRX); + +// Proximity sensor parameters +I2C i2c(PB_14,PB_13); + +int PSresult = 0; +char modeControlRegister[2] = {0x41, 0xC6}; // Mode Control Register - {Adress, Value} +char PSregadRegister [2] = {0x44, 0x45}; // PS register values - {PSLSB Adress, PSMSB Adress} +char PSReading [2]; +int poximityTreshold = 280; + +TCPSocket socket; +MQTTClient client(&socket); +MQTT::Message message; + +char* topic_pub_proximity2 = "PMK_industry/micro/proximity2"; +char* topic_sub_servo2 = "PMK_industry/micro/servo2"; + +char* receavedMessage; + +WiFiInterface *wifi; +volatile int mems_event = 0; +uint32_t previous_tick = 0; +uint32_t current_tick = 0; +uint8_t high = 0, low = 0; +float temperature = 0.0f; +char buffer[32]; + +int flag = 0; + +const char *sec2str(nsapi_security_t sec) +{ + switch (sec) + { + case NSAPI_SECURITY_NONE: + return "None"; + case NSAPI_SECURITY_WEP: + return "WEP"; + case NSAPI_SECURITY_WPA: + return "WPA"; + case NSAPI_SECURITY_WPA2: + return "WPA2"; + case NSAPI_SECURITY_WPA_WPA2: + return "WPA/WPA2"; + case NSAPI_SECURITY_UNKNOWN: + default: + return "Unknown"; + } +} + +int scan_networks(WiFiInterface *wifi) +{ + printf("Scan:\n"); + + // Scan only for the number of networks, first parameter is NULL + int count = wifi->scan(NULL, 0); + + // If there are no networks, count == 0, if there is an error, counter < 0 + if (count <= 0) + { + printf("scan() failed with return value: %d\n", count); + return 0; + } + + // Limit number of network arbitrary to some reasonable number + count = count < MAX_NETWORKS ? count : MAX_NETWORKS; + + // Create a local pointer to an object, which is an array of WiFi APs + WiFiAccessPoint *ap = new WiFiAccessPoint[count]; + + // Now scan again for 'count' networks and populate the array of APs + count = wifi->scan(ap, count); + + // This time, the number of entries to 'ap' is returned + if (count <= 0) + { + printf("scan() failed with return value: %d\n", count); + return 0; + } + + // Print out the parameters of each AP: + for (int i = 0; i < count; i++) + { + printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), + sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], + ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); + thread_sleep_for(PRINTF_DELAY_MS); + } + printf("%d networks available.\n", count); + + // Since 'ap' is dynamically allocated pointer to the array of objects, it + // needs to be deleted: + delete[] ap; + return count; +} + +void messageArrivedServo2(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + receavedMessage = (char*)message.payload; + + if (strcmp(receavedMessage,"zero") == 0){ flag = 0;} + else if (strcmp(receavedMessage,"left") == 0){ flag = 1;} + else if (strcmp(receavedMessage,"right") == 0) { flag = 2;} + +} + + +int main() + +{ + const char* hostname = "broker.hivemq.com"; + int port = 1883; + + // Create a default network interface + wifi = WiFiInterface::get_default_instance(); + if (!wifi) { + printf("ERROR: No WiFiInterface found.\n"); + return -1; + } + + // Scan for available networks and aquire information about Access Points + int count = scan_networks(wifi); + if (count == 0) { + printf("No WIFI APs found - can't continue further.\n"); + return -1; + } + + // Connect to the network with the parameters specified in 'mbed_app.json' + printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); + int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); + if (ret != 0) { + printf("\nConnection error: %d\n", ret); + return -1; + } + + // Print out the information aquired: + printf("Success\n\n"); + printf("MAC: %s\n", wifi->get_mac_address()); + printf("IP: %s\n", wifi->get_ip_address()); + printf("Netmask: %s\n", wifi->get_netmask()); + printf("Gateway: %s\n", wifi->get_gateway()); + printf("RSSI: %d\n\n", wifi->get_rssi()); + + socket.open(wifi); + socket.connect(hostname, port); + + int rc=0; + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "Client2"; + + if ((rc = client.connect(data)) != 0) + printf("rc from MQTT connect is %d\r\n", rc); + + + if ((rc = client.subscribe(topic_sub_servo2, MQTT::QOS2, messageArrivedServo2)) != 0) + printf("rc from MQTT subscribe is %d\r\n", rc); + + // Start I2C communication + i2c.start(); + + // Set the Mode Control Register, so the sensor is waken up from the standby mode (default) + i2c.write(PSAdress, modeControlRegister, 2, 1); + + myservo.position(0); + + while (true) { + + i2c.write(PSAdress, PSregadRegister, 2, 1); // Select the registers to read from, no I2C Stop + i2c.read(PSAdress, PSReading, 2, 0); // Read data from register + PSresult = (int) ((int)PSReading[1] << 8) | PSReading[0]; + + char buf[100]; + sprintf(buf, "%d", PSresult); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + client.publish(topic_pub_proximity2, message); + + // Servo commands + + if (flag == 0) + { + myservo.position(0); + + } + + else if (flag == 1) + { + myservo.position(25); + + } + + else if (flag == 2) + { + myservo.position(-25); + + } + + client.yield(YIELD_TIMEOUT_MS); // Need to call yield API to maintain connection + } +} +
diff -r 000000000000 -r 0093b6d26596 mbed-mqtt.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-mqtt.lib Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-mqtt/#7fa219e87b3355e8a6fd281659697fe3a0c87630
diff -r 000000000000 -r 0093b6d26596 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#cf4f12a123c05fcae83fc56d76442015cb8a39e9
diff -r 000000000000 -r 0093b6d26596 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
diff -r 000000000000 -r 0093b6d26596 mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Wed Jul 13 15:08:13 2022 +0000 @@ -0,0 +1,65 @@ +{ + "config": { + "wifi-ssid": { + "help": "WiFi SSID", + "value": "\"oljko\"" + }, + "wifi-password": { + "help": "WiFi Password", + "value": "\"oljaolja\"" + } + }, + "target_overrides": { + "*": { + "platform.stdio-convert-newlines": true + }, + "NUCLEO_L476RG": { + "target.network-default-interface-type": "WIFI", + "esp8266.tx" : "PC_4", + "esp8266.rx" : "PC_5", + "esp8266.provide-default" : true, + "drivers.uart-serial-rxbuf-size" : 1024, + "drivers.uart-serial-txbuf-size" : 1024 + }, + "NUCLEO_F746ZG": { + "target.network-default-interface-type" : "WIFI", + "esp8266.tx" : "D1", + "esp8266.rx" : "D0", + "esp8266.provide-default" : true, + "drivers.uart-serial-rxbuf-size" : 1024, + "drivers.uart-serial-txbuf-size" : 1024 + }, + "NUCLEO_L4R5ZI": { + "target.network-default-interface-type" : "WIFI", + "esp8266.tx" : "D1", + "esp8266.rx" : "D0", + "esp8266.provide-default" : true, + "drivers.uart-serial-rxbuf-size" : 1024, + "drivers.uart-serial-txbuf-size" : 1024 + }, + "NUCLEO_H743ZI2": { + "target.network-default-interface-type" : "WIFI", + "esp8266.tx" : "D1", + "esp8266.rx" : "D0", + "esp8266.provide-default" : true, + "drivers.uart-serial-rxbuf-size" : 1024, + "drivers.uart-serial-txbuf-size" : 1024 + }, + "NUCLEO_F103RB": { + "target.network-default-interface-type" : "WIFI", + "esp8266.tx" : "D8", + "esp8266.rx" : "D2", + "esp8266.provide-default" : true, + "drivers.uart-serial-rxbuf-size" : 1024, + "drivers.uart-serial-txbuf-size" : 1024 + }, + "NUCLEO_F401RE": { + "target.network-default-interface-type" : "WIFI", + "esp8266.tx" : "D8", + "esp8266.rx" : "D2", + "esp8266.provide-default" : true, + "drivers.uart-serial-rxbuf-size" : 1024, + "drivers.uart-serial-txbuf-size" : 1024 + } + } +}
diff -r 000000000000 -r 0093b6d26596 resources/official_armmbed_example_badge.png Binary file resources/official_armmbed_example_badge.png has changed