{"id":7337,"date":"2022-01-14T06:15:56","date_gmt":"2022-01-14T06:15:56","guid":{"rendered":"https:\/\/www.qec.tw\/?p=7337"},"modified":"2024-07-22T06:21:19","modified_gmt":"2024-07-22T06:21:19","slug":"qec-m-043t-sending-data-to-mysql-mariadb-server","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/ja\/ethercat\/ethercat-application\/qec-m-043t-sending-data-to-mysql-mariadb-server\/","title":{"rendered":"QEC-M-043T Sending Data to MySQL (MariaDB) Server"},"content":{"rendered":"<p class=\"wp-block-paragraph\">This tutorial works with QEC-M-043T, which includes the switch button to control MySQL connection and send simple data by the switch button of the UI interfaces to the MariaDB database. This project is a simple data gathering and storage program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You must have configured a server, database and server-side scripting language to process the simple data type. We\u2019ve configured a Debian 9 machine to run a MariaDB server in this tutorial.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Reference: <\/em><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-the-latest-mysql-on-debian-9\" target=\"_blank\" rel=\"noopener\"><em>https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-the-latest-mysql-on-debian-9<\/em><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Goals<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3067\u306f\u3001\u4ee5\u4e0b\u3092\u304a\u3053\u306a\u3044\u307e\u3059 :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Database Configuration<\/li><li>Import the related library (Ethernet, MySQL and LVGL)<\/li><li>Ethernet setting steps<\/li><li>MySQL (MariaDB) connection<\/li><li>LVGL UI interface<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Required Hardware and Software<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.qec.tw\/ja\/software\/\">86Duino IDE 500+<\/a><\/li><li><a href=\"https:\/\/www.qec.tw\/ja\/product\/qec-m-043t\/\">QEC-M-043T<\/a><\/li><li>EB-SQL<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial will focus on getting data from the user interface into our database and then displaying the data on the local machine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Database Configuration:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For this project, I created a database named &#8220;test&#8221; and a table in that database named &#8220;acen&#8221;. We want to record three things in this database: name, age and class name. So, we need three fields that I&#8217;ve named: name, age and class_name. This is what it looks like in the text mode Debian 9:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1004\" height=\"563\" src=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mariadb_testTable1.jpg\" alt=\"\" class=\"wp-image-7338\" srcset=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mariadb_testTable1.jpg 1004w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mariadb_testTable1-300x168.jpg 300w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mariadb_testTable1-768x431.jpg 768w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mariadb_testTable1-600x336.jpg 600w\" sizes=\"(max-width: 1004px) 100vw, 1004px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the script below for MariaDB to create the database &#8220;test&#8221; and table &#8220;acen&#8221;.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\/* Create Database *\/\nCREATE DATABASE test;\n\/* Create Table *\/\nCREATE TABLE acen ( \nname varchar(30),\nage int(11),\nclass_name varchar(30)\n);<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Start to send data from QEC-M-043T to MySQL (MariaDB) Server<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If this is your first time running 86Duino sketch files, we suggest you check out <a href=\"https:\/\/www.qec.tw\/ja\/ethercat\/qec-example\/set-up-the-qec-m-043t-for-86duino\/\">how to set up the QEC-M-043T for 86Duino<\/a> before proceeding.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_mp3-1024x576.jpg\" alt=\"mysqlConnectionUseGlan_mp3\" class=\"wp-image-7357\" srcset=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_mp3-1024x576.jpg 1024w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_mp3-300x169.jpg 300w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_mp3-768x432.jpg 768w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_mp3-600x338.jpg 600w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_mp3.jpg 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1. Check if the device is connected correctly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">QEC-M-043T and EB-SQL need to join the same network.<\/p>\n\n\n\n<figure class=\"wp-block-image size-medium\"><img decoding=\"async\" width=\"300\" height=\"300\" src=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_img3-300x300.jpg\" alt=\"\" class=\"wp-image-7340\" srcset=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_img3-300x300.jpg 300w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_img3-150x150.jpg 150w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_img3-600x599.jpg 600w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_img3-100x100.jpg 100w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/mysqlConnectionUseGlan_img3.jpg 719w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Note: The Giga Lan on QEC-M-043T is with a blue connector.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"219\" src=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2021\/10\/qec-m-043tback1-300x219.png\" alt=\"qec-m-043t(back1)\" class=\"wp-image-7199\" srcset=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2021\/10\/qec-m-043tback1-300x219.png 300w, https:\/\/www.qec.tw\/wp-content\/uploads\/2021\/10\/qec-m-043tback1.png 527w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2. Open 86Duino IDE.<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The 86Duino IDE can be downloaded from the official website. (<a href=\"https:\/\/www.qec.tw\/ja\/software\/\">https:\/\/www.qec.tw\/software\/<\/a>)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3. Copy the code given below into the editor.<\/h3>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\/\/ Import the Related Libraries\n#include &lt;Ethernet.h&gt;\n#include &lt;MySQL_Connection.h&gt;\n#include &lt;MySQL_Cursor.h&gt;\n#include &quot;lvgl86.h&quot;\n\nbyte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; \/\/ MAC Address\n\nIPAddress server_addr(192,168,2,130);  \/\/ IP of the MySQL *server* here\nchar user[] = &quot;root&quot;;              \/\/ MySQL user login username\nchar password[] = &quot;roboard&quot;;        \/\/ MySQL user login password\n\n\/\/ Sample query\nchar INSERT_SQL[] = &quot;INSERT INTO test.acen (name, age, class_name) values ('acen', 34, '405')&quot;;\n\nEthernetClient client; \/\/ Ethernet Client\nunsigned long t = 0;\nMySQL_Connection conn((Client *)&amp;client); \/\/ MySQL Connection\n\nstatic void event_handler1(lv_obj_t * obj, lv_event_t event)\n{\n    if(event == LV_EVENT_VALUE_CHANGED) {\n        Serial.println(&quot;Recording data.&quot;);\n        MySQL_Cursor *cur_mem = new MySQL_Cursor(&amp;conn);\n        cur_mem-&gt;execute(INSERT_SQL);\n        delete cur_mem;\n    }\n}\n\nlv_obj_t *sw2;\nint sw_state = 0; \/\/ 0: OFF, 1: ON\nstatic void event_handler2(lv_obj_t * obj, lv_event_t event)\n{\n    if(event == LV_EVENT_VALUE_CHANGED) {\n        if (lv_switch_get_state(obj)) {\n          if (conn.connect(server_addr, 3306, user, password)) {\n            Serial.println(&quot;SQL Connection Success.&quot;);\n            delay(1000);\n            sw_state = 1;\n          } else\n            Serial.println(&quot;Connection failed.&quot;);\n        } else {\n          Serial.println(&quot;SQL Close.&quot;);\n          conn.close();\n          sw_state = 0;\n        }\n    }\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  while (!Serial); \/\/ wait for serial port to connect\n  lv86_init();\n  \n  Ethernet.begin();\n  Serial.println(&quot;Connecting...&quot;);\n  if (conn.connect(server_addr, 3306, user, password)) {\n    delay(1000);\n  }\n  else\n    Serial.println(&quot;Connection failed.&quot;);\n  t = millis();\n\n  lv_obj_t *sw1 = lv_switch_create(lv_scr_act(), NULL); \/\/ This switch is for insert data\n  lv_obj_align(sw1, NULL, LV_ALIGN_CENTER, 0, -30);\n  lv_obj_set_event_cb(sw1, event_handler1);\n\n  sw2 = lv_switch_create(lv_scr_act(), NULL); \/\/ This switch is for the Database Connection status\n  lv_obj_align(sw2, NULL, LV_ALIGN_CENTER, 0, 30);\n  lv_obj_set_event_cb(sw2, event_handler2);\n}\n\n\nvoid loop() {\n  conn.handleData();\n  lv_task_handler();\n  if (sw_state == 1 &amp;&amp; !conn.connected()) {lv_switch_off(sw2, LV_ANIM_OFF); sw_state = 0;} \/\/ Auto judge the connection, close switch if disconnected\n  if (sw_state == 0 &amp;&amp; conn.connected())  {lv_switch_on(sw2, LV_ANIM_OFF);  sw_state = 1;} \/\/ Auto judge the connection, open switch if connected\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4. Verify that the information is different<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We need to have the correct IP Address, MySQL (MariaDB) account, Ethernet connection, etc. That can connect with the database. Please give a double-check of all information about your script.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\/*\n  1.\tMAC Address\n  2.\tServer IP Address\n  3.\tMySQL (MariaDB) account (username and password)\n  4.\tMySQL command\n*\/\nbyte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; \/\/ MAC Address\n\nIPAddress server_addr(192,168,2,130);  \/\/ IP of the MySQL *server* here\nchar user[] = &quot;root&quot;;              \/\/ MySQL user login username\nchar password[] = &quot;roboard&quot;;        \/\/ MySQL user login password\n\n\/\/ Sample query\nchar INSERT_SQL[] = &quot;INSERT INTO test.acen (name, age, class_name) values ('acen', 34, '405')&quot;;\n\nEthernetClient client; \/\/ Ethernet Client\nunsigned long t = 0;\nMySQL_Connection conn((Client *)&amp;client); \/\/ MySQL Connection<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5. Upload your sketch!<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Hit the right arrow button (upload) next to the upload button. After the code uploads, you should see two switches on the user interface.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The switch on top is the data insert button, and the QEC-M-043T will transfer the data after you open the switch.<\/li><li>The switch on the bottom is the MySQL connection, and you will see it pull on after you open the QEC-M-043T.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"QEC-M-043T Sent Data to Database\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/f8TDUxrwpx4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We can use the SQL language to check the data with the EB-SQL.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>SELECT * FROM test.acen;<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"300\" src=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/dataconfirm.png\" alt=\"dataconfirm\" class=\"wp-image-7359\" srcset=\"https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/dataconfirm.png 522w, https:\/\/www.qec.tw\/wp-content\/uploads\/2022\/01\/dataconfirm-300x172.png 300w\" sizes=\"(max-width: 522px) 100vw, 522px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u5b8c\u4e86\u3067\u3059<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you can find data in your database, which means you have successfully collected your first data into the database using the QEC-M-043T.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stay tuned for more projects from QEC!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to comment if you have any questions\/problems!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph translation-block\"><em>86Duino\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306e\u30c6\u30ad\u30b9\u30c8\u306f\u3001<a href=\"http:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_self\">Arduino\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9<\/a>\u3092\u6539\u5909\u3057\u305f\u3082\u306e\u3067\u3001<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_self\">Creative Commons Attribution-ShareAlike 3.0\u30e9\u30a4\u30bb\u30f3\u30b9<\/a>\u306b\u57fa\u3065\u3044\u3066\u30e9\u30a4\u30bb\u30f3\u30b9\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u5185\u306e\u30b3\u30fc\u30c9\u30b5\u30f3\u30d7\u30eb\u306f\u30d1\u30d6\u30ea\u30c3\u30af\u30c9\u30e1\u30a4\u30f3\u3068\u3057\u3066\u516c\u958b\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/em><\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial works with QEC-M-043T, which includes the switch button to control MySQL connection and send simple data by the switch button of the UI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7356,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[196],"tags":[191],"class_list":["post-7337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ethercat-application","tag-qec"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/posts\/7337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/comments?post=7337"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/posts\/7337\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/media\/7356"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/media?parent=7337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/categories?post=7337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/ja\/wp-json\/wp\/v2\/tags?post=7337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}