dasd
Revision 0:3a4451afe1ae, committed 2021-07-01
- Comitter:
- pavledjo
- Date:
- Thu Jul 01 15:45:56 2021 +0000
- Commit message:
- afddsf;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Thu Jul 01 15:45:56 2021 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CONTRIBUTING.md Thu Jul 01 15:45:56 2021 +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).
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Function.h Thu Jul 01 15:45:56 2021 +0000
@@ -0,0 +1,165 @@
+#include "mbed.h"
+#include "platform/mbed_thread.h"
+#include <string>
+#include <MQTTClientMbedOs.h>
+
+#define TOPIC_SUB "PMK_industrija/micro1/#"
+
+char* topic_sub = "PMK_industrija/micro1/#";
+
+char* topic_pub_m2_connect = "PMK_industrija/micro2/connect";
+char* topic_pub_m3_connect = "PMK_industrija/micro3/connect";
+char* topic_pub_m4_connect = "PMK_industrija/micro4/connect";
+
+char* topic_pub_m2_servo0 = "PMK_industrija/micro2/servo0";
+char* topic_pub_m2_color1 = "PMK_industrija/micro2/color1";
+char* topic_pub_m2_servo1 = "PMK_industrija/micro2/servo1";
+char* topic_pub_m3_proxy1 = "PMK_industrija/micro3/proxy1";
+char* topic_pub_m3_proxy2 = "PMK_industrija/micro3/proxy2";
+char* topic_pub_m3_servo2 = "PMK_industrija/micro3/servo2";
+char* topic_pub_m3_color2 = "PMK_industrija/micro3/color2";
+char* topic_pub_m4_servo3 = "PMK_industrija/micro4/servo3";
+char* topic_pub_m4_proxy3 = "PMK_industrija/micro4/proxy3";
+char* topic_pub_m4_color3 = "PMK_industrija/micro4/color3";
+char* topic_pub_m4_LCD = "PMK_industrija/micro4/LCD";
+
+
+TCPSocket socket;
+MQTTClient client(&socket);
+MQTT::Message message;
+bool connectFlag[3] = {false,false,false};
+bool allConnected = false;
+
+
+
+
+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--);
+
+ /* calculate integer & fractinal parts */
+ intPart = (int)v;
+ fractPart = (int)((v - (double)(int)v) * i);
+
+ /* fill in integer part */
+ sprintf(str, "%i.", intPart);
+
+ /* 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 (rest of) fractional part */
+ sprintf(ptr, "%i", fractPart);
+
+ return str;
+}
+
+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_demo(WiFiInterface *wifi)
+{
+ WiFiAccessPoint *ap;
+ printf("Scan:\n");
+ int count = wifi->scan(NULL,0);
+ if (count <= 0) {
+ printf("scan() failed with return value: %d\n", count);
+ return 0;
+ }
+ /* Limit number of network arbitrary to 15 */
+ count = count < 15 ? count : 15;
+ ap = new WiFiAccessPoint[count];
+ count = wifi->scan(ap, count);
+ if (count <= 0) {
+ printf("scan() failed with return value: %d\n", count);
+ return 0;
+ }
+ 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());
+ }
+ printf("%d networks available.\n", count);
+ delete[] ap;
+ return count;
+}
+
+void publish(MQTTClient client, char* topic, char* buf ){
+ MQTT::Message message;
+ message.qos = MQTT::QOS0;
+ message.retained = false;
+ message.dup = false;
+ message.payload = (void*)buf;
+ message.payloadlen = strlen(buf)+1;
+ client.publish(topic, message);
+
+ }
+
+void microOnline(int n){
+ connectFlag[n-2] = true;
+ for(int i =0 ; i < 3; i++){
+ if(!connectFlag[n-2]) return;
+ }
+ allConnected = true;
+}
+
+void checkOtherConnection(){
+ char buf[100];
+ sprintf(buf, "Jesi li online?");
+ while(true){
+ printf("Porovera konekcije ostalih uredjaja! \r\n");
+ publish(client,topic_pub_m2_connect,buf);
+ publish(client,topic_pub_m3_connect,buf);
+ publish(client,topic_pub_m4_connect,buf);
+ wait(60);
+ if (allConnected) {
+ break;
+ printf("SVI UREDJAJI SU ONLINE \r\n");
+ }
+ printf("Nisu svi povezani\r\n\r\n");
+ }
+ }
+
+
+bool colorCheck(string poruka,int &boja,char* topic){
+ char buf[100];
+ bool levo = false;
+ if (poruka.find("Jeste") !=std::string::npos) {levo = true; boja++; }
+ sprintf(buf, "%b",levo );
+ publish(client,topic,buf);
+ if(levo) return true;
+ return false;
+ }
+void lcdUpdate(char* Boja){
+ publish(client,topic_pub_m4_LCD,Boja);
+ }
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Thu Jul 01 15:45:56 2021 +0000 @@ -0,0 +1,64 @@ + +# 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.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Jul 01 15:45:56 2021 +0000
@@ -0,0 +1,186 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2019 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#include "Function.h"
+
+// Blinking rate in milliseconds
+#define BLINKING_RATE_MS 500
+
+Serial pc(USBTX,USBRX);
+InterruptIn button(USER_BUTTON);
+int arrivedcount = 0;
+
+int button_pressed=0;
+
+
+
+WiFiInterface *wifi;
+
+
+volatile int mems_event = 0;
+uint32_t previous_tick = 0;
+uint32_t current_tick = 0;
+
+char buffer[32];
+
+enum Boja {CRVENA,ZELENA,PLAVA,DRUGA};
+Boja BojaCepa[3];
+Timeout Tmicro2,Tmicro3,Tmicro4;
+bool echoFlag[3];
+
+
+
+void timeoutMCU1(){
+ Tmicro2.detach();
+ if (!echoFlag[0]){
+ printf("Nema odgovora sa micro1!\r\n");
+ printf("Pokusaj ponovnog kacenja na sve kontrolere\r\n");
+ //Upadam u while??? Nista se ne desava dok se ne uspostavi konekciju??
+ lcdUpdate("MCU1_FAIL");
+ checkOtherConnections();
+ }
+ }
+
+
+
+
+void messageArrived(MQTT::MessageData& md)
+{
+ char buf[100];
+ MQTTString &topic = md.topicName;
+ string topic_name = topic.lenstring.data;
+ printf("Topic name %d : %s\r\n",topic.lenstring.len,topic_name.c_str() );
+ MQTT::Message &message = md.message;
+ printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
+ printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
+ string poruka = (char*)message.payload;
+ if(topic_name.find("proxy1")!=std::string::npos) {
+ printf("Ima cepa!!! proxy1 \r\n");
+ sprintf(buf, "Daj mi boju tu je cep");
+ publish(client,topic_pub_m2_color1,buf);
+ echoFlag[0] = false;
+ Tmicro2.attach(&timeoutMCU1,5.0);
+ }
+ else if(topic_name.find("color1")!=std::string::npos) {
+ printf("Vidi boje !!! color1 \r\n");
+ if (colorCheck(poruka,crvena,topic_pub_m2_servo1)) lcdUpdate("CRVENA");
+ servo0Flag = true;
+ }
+ else if(topic_name.find("proxy2")!=std::string::npos) {
+ printf("Ima cepa!!! proxy2 \r\n");
+ sprintf(buf, "Daj mi boju tu je cep");
+ publish(client,topic_pub_m3_color2,buf);
+ }
+ else if(topic_name.find("color2")!=std::string::npos) {
+ printf("Vidi boje !!! color2 \r\n");
+ if(colorCheck(poruka,zelena,topic_pub_m3_servo2)) lcdUpdate("ZELENA");
+
+ }
+ else if(topic_name.find("proxy3")!=std::string::npos) {
+ printf("Ima cepa!!!proxy3 \r\n");
+ sprintf(buf, "Daj mi boju tu je cep");
+ publish(client,topic_pub_m3_color2,buf);
+
+ }
+ else if(topic_name.find("color3")!=std::string::npos) {
+ printf("Vidi boje !!! color3 \r\n");
+ if(colorCheck(poruka,zelena,topic_pub_m4_servo3)){lcdUpdate("PLAVA");}
+ else {drugaBoja++; lcdUpdate("DRUGA");}
+ }
+ else if(topic_name.find("connect2")!=std::string::npos) microOnline(2);
+ else if(topic_name.find("connect3")!=std::string::npos) microOnline(3);
+ else if(topic_name.find("connect4")!=std::string::npos) microOnline(4);
+ ++arrivedcount;
+}
+
+
+void buttonFunction() {
+ button_pressed=1;
+}
+
+int main()
+{
+ // Initialise the digital pin LED1 as an output
+
+ DigitalOut led(LED1);
+ const char* hostname = "broker.mqttdashboard.com";
+ int port = 1883;
+
+
+
+ wifi = WiFiInterface::get_default_instance();
+ if (!wifi) {
+ printf("ERROR: No WiFiInterface found.\n");
+ return -1;
+ }
+
+ int count = scan_demo(wifi);
+ if (count == 0) {
+ printf("No WIFI APs found - can't continue further.\n");
+ return -1;
+ }
+
+ 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;
+ }
+
+ 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 = "PMK-industrija-master";
+ //data.username.cstring = "testuser";
+ //data.password.cstring = "testpassword";
+
+ if ((rc = client.connect(data)) != 0)
+ printf("rc from MQTT connect is %d\r\n", rc);
+
+
+ if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0)
+ printf("rc from MQTT subscribe is %d\r\n", rc);
+
+
+ checkOtherConnection();
+
+ char buf[100];
+
+ while (true) {
+ led = !led;
+ thread_sleep_for(BLINKING_RATE_MS);
+
+ if (servo0Flag){
+ servo0Flag = false;
+ sprintf(buf,"Otvori");
+ publish(client,topic_pub_m2_servo0,buf);
+ }
+
+
+ if (button_pressed==1) {
+ button_pressed=0;
+ printf("Publishing data\r\n");
+ // QoS 0
+
+
+ //sprintf(buf, "RGB: %7s C\r\n", print_double(buffer, cs.getRed()*100000000+cs.getGreen()*10000+cs.getBlue()));
+
+
+ }
+ // printf("Yielding");
+ client.yield(1000);
+ //printf(" -> Yielded\r\n");
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-mqtt.lib Thu Jul 01 15:45:56 2021 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-mqtt/#2a83f4993401cca786e0843faeed82219ed9ae77
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Jul 01 15:45:56 2021 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#cf4f12a123c05fcae83fc56d76442015cb8a39e9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json Thu Jul 01 15:45:56 2021 +0000
@@ -0,0 +1,65 @@
+{
+ "config": {
+ "wifi-ssid": {
+ "help": "WiFi SSID",
+ "value": "\"rcopen\""
+ },
+ "wifi-password": {
+ "help": "WiFi Password",
+ "value": "\"12345678\""
+ }
+ },
+ "target_overrides": {
+ "*": {
+ "platform.stdio-convert-newlines": true
+ },
+ "NUCLEO_L476RG": {
+ "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_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
+ }
+ }
+}
Binary file resources/official_armmbed_example_badge.png has changed