WikiJS/test_transfer_odt_5.html

559 lines
36 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!--
title: Transfer Linux Lamp
description:
published: 1
date: 2024-02-02T20:07:19.635Z
tags:
editor: code
dateCreated: 2024-02-01T23:17:04.906Z
-->
<pre v-pre="true" class="prismjs line-numbers"><code class="language-">@page { size: 8.5in 11in; margin: 0.79in } p { line-height: 115%; margin-bottom: 0.1in; background: transparent } h2 { margin-top: 0.14in; margin-bottom: 0.08in; background: transparent; page-break-after: avoid } h2.western { font-family: "Liberation Serif", serif; font-size: 18pt; font-weight: bold } h2.cjk { font-family: "Noto Serif CJK SC"; font-size: 18pt; font-weight: bold } h2.ctl { font-family: "Lohit Devanagari"; font-size: 18pt; font-weight: bold } h1 { margin-bottom: 0.08in; background: transparent; page-break-after: avoid } h1.western { font-family: "Liberation Sans", sans-serif; font-size: 18pt; font-weight: bold } h1.cjk { font-family: "Noto Sans CJK SC"; font-size: 18pt; font-weight: bold } h1.ctl { font-family: "Lohit Devanagari"; font-size: 18pt; font-weight: bold } a:link { color: #000080; so-language: zxx; text-decoration: underline } code.western { font-family: "Liberation Mono", monospace } code.cjk { font-family: "Noto Sans Mono CJK SC", monospace } code.ctl { font-family: "Liberation Mono", monospace } a:visited { color: #800000; so-language: zxx; text-decoration: underline }
</code></pre>
<p><code>enabling SSH</code></p>
<p><code>[https://randomnerdtutorials.com/installing-raspbian-lite-enabling-and-connecting-with-ssh/](https://randomnerdtutorials.com/installing-raspbian-lite-enabling-and-connecting-with-ssh/)</code></p>
<ul>
<li><code>add file “ssh” into boot folder</code></li>
</ul>
<p><code>Establishing an SSH Communication</code></p>
<p><code>$ sudo ssh [pi@192.168.178.25](mailto:pi@192.168.178.25)</code></p>
<p><code>user: pi, pass: pi</code></p>
<p><code>Shutting Down</code></p>
<p><code>$ sudo poweroff</code></p>
<p><code>[https://randomnerdtutorials.com/raspberry-pi-apache-mysql-php-lamp-server/](https://randomnerdtutorials.com/raspberry-pi-apache-mysql-php-lamp-server/)</code></p>
<p><code>[https://www.instructables.com/Installing-LAMP-Linux-Apache-MySQL-PHP-on-a-Raspbe/](https://www.instructables.com/Installing-LAMP-Linux-Apache-MySQL-PHP-on-a-Raspbe/)</code></p>
<p><code>Updating and Upgrading</code></p>
<p><code>$ sudo apt update &amp;&amp; sudo apt upgrade -y</code></p>
<p><code>Install Apache2</code></p>
<p><code>$ sudo apt install apache2 -y</code></p>
<p><code>Testing Apache installation</code></p>
<p><code>$ cd /var/www/html</code></p>
<p><code>$ ls -al</code></p>
<p><code>This should show “index.html”</code></p>
<p><code>get RasPi IP address</code></p>
<p><code>$ hostname -I</code></p>
<p><code>enter IP address into Browser → the Apache Debian page should appear</code></p>
<p><code>Install PHP</code></p>
<p><code>$ cd /var/www/html</code></p>
<p><code>$ sudo apt install php -y</code></p>
<p><code>$ sudo rm index.html</code></p>
<p><code>$ sudo nano index.php</code></p>
<p><code>Add following script into the php file:</code></p>
<p><code>&lt;?php echo "hello world"; ?&gt;</code></p>
<p><code>Save with Ctrl+X &gt;&gt; y &gt;&gt; Enter, ``or Ctrl+O &gt;&gt; Enter &gt;&gt; Ctrl+X</code></p>
<p><code>OR</code></p>
<p><code>&lt;?php phpinfo(); ?&gt;</code></p>
<p><code>OR</code></p>
<p><code>$ echo "&lt;?php phpinfo ();?&gt;" &gt; /var/www/html/index.php</code></p>
<p><code>Restart Apache</code></p>
<p><code>$ sudo service apache2 restart</code></p>
<p><code>test if Apache2 is serving .php files by opening RasPi IP address in browser </code></p>
<p><code>if all is fine, index.php can be removed</code></p>
<p><code>$ sudo rm index.php</code></p>
<p><code>Install MySQL (MariaDB Server)</code></p>
<p><code>$ sudo apt install mariadb-server php-mysql -y</code></p>
<p><code>$ sudo service apache2 restart</code></p>
<p><code>$ sudo mysql_secure_installation</code></p>
<p><code>In all cases, answer with y</code></p>
<p><code>create new user “``admin” with password “``your_password”</code></p>
<p><code>$ sudo mysql --user=root --password</code></p>
<p><code>&gt; create user admin@localhost identified by 'your_password';</code></p>
<p><code>&gt; grant all privileges on *.* to admin@localhost;</code></p>
<p><code>&gt; FLUSH PRIVILEGES;</code></p>
<p><code>&gt; exit;</code></p>
<p><code>Important to generate user that is not limited to a certain domain!!</code></p>
<p><code>&gt; ``grant all privileges on *.* to 'admin3'@'%';</code></p>
<p><code>Install phpMyAdmin</code></p>
<p><code>$ cd /var/www/html</code></p>
<p><code>$ sudo apt install phpmyadmin -y</code></p>
<p><code>Options:</code></p>
<ul>
<li>
<p><code>dbconfig-common</code></p>
</li>
<li>
<p><code>Apache2</code></p>
</li>
<li>
<p><code>Configuring phpmyadmin? OK</code></p>
</li>
<li>
<p><code>Configure database for phpmyadmin with dbconfig-common? Yes</code></p>
</li>
<li>
<p><code>Password: pi</code></p>
</li>
</ul>
<p><code>$ sudo phpenmod mysqli</code></p>
<p><code>$ sudo service apache2 restart</code></p>
<p><code>open following address</code></p>
<p><code>http://192.168.``178.25``/phpmyadmin</code></p>
<p><code>if “Not Found”</code></p>
<p><code>$ cd /var/www/html</code></p>
<p><code>$ sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin</code></p>
<p><code>$ ls</code></p>
<p><code>&gt;&gt; ``phpmyadmin</code></p>
<p><code>again, open following address</code></p>
<p><code>[http://192.168.178.25/phpmyadmin](http://192.168.178.25/phpmyadmin)</code></p>
<p><code>login with username=root, password=pi</code></p>
<p><code>change the permissions for your /var/www/html/ folder.</code></p>
<p><code>$ cd ~</code></p>
<p><code>$ ls -lh /var/www/</code></p>
<p><code>$ sudo chown -R pi:www-data /var/www/html/</code></p>
<p><code>$ sudo chmod -R 770 /var/www/html/</code></p>
<p><code>$ ls -lh /var/www/</code></p>
<p><code>[https://randomnerdtutorials.com/esp32-esp8266-raspberry-pi-lamp-server/](https://randomnerdtutorials.com/esp32-esp8266-raspberry-pi-lamp-server/)</code></p>
<p><code>Create database:</code></p>
<ul>
<li>
<p><code>esp_data</code></p>
</li>
<li>
<p><code>utf8mb4_general_ci</code></p>
</li>
</ul>
<p><code>Create table by calling SQL query:</code></p>
<p><code>CREATE TABLE SensorData (</code></p>
<p><code>id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,</code></p>
<p><code>sensor VARCHAR(30) NOT NULL,</code></p>
<p><code>location VARCHAR(30) NOT NULL,</code></p>
<p><code>value1 VARCHAR(10),</code></p>
<p><code>value2 VARCHAR(10),</code></p>
<p><code>value3 VARCHAR(10),</code></p>
<p><code>reading_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP</code></p>
<p><code>)</code></p>
<p><code>PHP script that is responsible for receiving incoming requests from the ESP32 or ESP8266 and inserting the data into a MySQL database.</code></p>
<p><code>$ cd ~</code></p>
<p><code>$ ``nano /var/www/html/post-esp-data.php</code></p>
<p><code>[https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki/Troubleshooting](https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki/Troubleshooting)</code></p>
<p><code>tr to connect to msql from another computer:</code></p>
<p><code>$ ``mysql -u``admin`` -p``pi`` h192.168.178.25 port=3306</code></p>
<p><code>$ ``mysql --u``ser=``admin`` --p``assword=``pi`` --h``ost=``192.168.178.25 --port=3306</code></p>
<p><code>$ ``mysql --u``ser=``admin`` --p``assword=``pi`` --h``ost= 127.0.0.1`` --port=3306</code></p>
<p><code>$ ``mysql -u ``admin`` -p ``pi`` -h 127.0.0.1 test_arduino</code></p>
<p><code>Important in case of “ERROR 2003 (HY000): Can't connect to local MySQL server through socket”</code></p>
<p><code>$ sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf </code></p>
<p><code>Here, the binding address = 127.0.0.1 must be commented out!!!</code></p>
<p><code>[https://pawelrychlicki.pl/Article/Details/65/fixing-sqlstatehy000-2002-in-the-nextcloudowncloud-with-the-mysqlmariadb-and-the-tlsssl-ubuntu-linux-1804-server](https://pawelrychlicki.pl/Article/Details/65/fixing-sqlstatehy000-2002-in-the-nextcloudowncloud-with-the-mysqlmariadb-and-the-tlsssl-ubuntu-linux-1804-server)</code></p>
<h1 class="toc-header" id="h-1configuring-wifi-in-headless-mode"> 1<code>Configuring WiFi in headless mode</code></h1>
<p><code>[https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md](https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md)</code></p>
<p><code>[https://www.seeedstudio.com/blog/2021/01/25/three-methods-to-configure-raspberry-pi-wifi/](https://www.seeedstudio.com/blog/2021/01/25/three-methods-to-configure-raspberry-pi-wifi/)</code></p>
<h2 class="toc-header" id="h-11ssh"> 1.1<code>ssh</code></h2>
<p><code>To enable SSH on a headless Raspberry Pi, simply place a file named “ssh”, without any extension, onto the boot partition of the SD card from another computer. This will tell Raspberry Pi to enable SSH when booting. The content of the file does not matter.</code></p>
<h2 class="toc-header" id="h-12wpa_supplicantconf"> 1.2<code>wpa_supplicant.conf </code></h2>
<p><code>$ ``sudo nano /etc/wpa_supplicant/wpa_supplicant.conf</code></p>
<p><code>ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev</code></p>
<p><code>update_config=1</code></p>
<p><code>country=``&lt;Country Code&gt;</code></p>
<p><code>network={</code></p>
<p><code>ssid="``&lt;SSID&gt;``"</code></p>
<p><code>psk="``&lt;PASSWORD&gt;``"</code></p>
<p><code>scan_ssid=1</code></p>
<p><code>}</code></p>
<h1 class="toc-header" id="h-2arduino-script-for-writing-to-mariadbmysql-database"> 2<code>Arduino script for writing to MariaDB/MySQL database</code></h1>
<p><code>Check sketch “Complex_Insert_WiFi_BE”</code></p>
<h1 class="toc-header" id="h-3showing-db-content-as-html-page"> 3<code>Showing DB content as html page</code></h1>
<p><code>[https://randomnerdtutorials.com/esp32-esp8266-raspberry-pi-lamp-server/](https://randomnerdtutorials.com/esp32-esp8266-raspberry-pi-lamp-server/)</code></p>
<p><code>$ nano /var/www/html/esp-data.php</code></p>
<p><code>open page http://192.168.178.25/esp-data.php</code></p>
<h2 class="toc-header" id="h-31esp-dataphp"> 3.1<code>esp-data.php</code></h2>
<p><code>&lt;!DOCTYPE html&gt;</code></p>
<p><code>&lt;html&gt;&lt;body&gt;</code></p>
<p><code>&lt;?php</code></p>
<p><code>/*</code></p>
<p><code>Rui Santos</code></p>
<p><code>Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/</code></p>
<p><code>Permission is hereby granted, free of charge, to any person obtaining a copy</code></p>
<p><code>of this software and associated documentation files.</code></p>
<p><code>The above copyright notice and this permission notice shall be included in all</code></p>
<p><code>copies or substantial portions of the Software.</code></p>
<p><code>*/</code></p>
<p><code>$servername = "``192.168.178.25``";</code></p>
<p><code>// REPLACE with your Database name</code></p>
<p><code>$dbname = "``test_arduino``";</code></p>
<p><code>// REPLACE with Database user</code></p>
<p><code>$username = "``admin3``";</code></p>
<p><code>// REPLACE with Database user password</code></p>
<p><code>$password = "``pi``";</code></p>
<p><code>// Create connection</code></p>
<p><code>$conn = new mysqli($servername, $username, $password, $dbname);</code></p>
<p><code>// Check connection</code></p>
<p><code>if ($conn-&gt;connect_error) {</code></p>
<p><code>die("Connection failed: " . $conn-&gt;connect_error);</code></p>
<p><code>} </code></p>
<p><code>$sql = "SELECT ``num``, ``message, ``sensor_``num``, ``value``, ``recorded`` FROM ``hello_sensor`` ORDER BY ``num`` DESC";</code></p>
<p><code>echo '&lt;table cellspacing="5" cellpadding="5"&gt;</code></p>
<p><code>&lt;tr&gt; </code></p>
<p><code>&lt;td&gt;``num``&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;``message``&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;``sensor``_num``&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;``v``alue``&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;``recorded``&lt;/td&gt; </code></p>
<p><code>&lt;/tr&gt;';</code></p>
<p><code>if ($result = $conn-&gt;query($sql)) {</code></p>
<p><code>while ($row = $result-&gt;fetch_assoc()) {</code></p>
<p><code>$row_``num`` = $row["``num``"];</code></p>
<p><code>$row_``message`` = $row["``message``"];</code></p>
<p><code>$row_``sensor_num`` = $row["``sensor_num``"];</code></p>
<p><code>$row_``value`` = $row["``value``"];</code></p>
<p><code>$row_``re``corded`` = $row["``recorded``"];</code></p>
<p><code>// Uncomment to set timezone to - 1 hour (you can change 1 to any number)</code></p>
<p><code>//$row_reading_time = date("Y-m-d H:i:s", strtotime("$row_reading_time - 1 hours"));</code></p>
<p><code>// Uncomment to set timezone to + 4 hours (you can change 4 to any number)</code></p>
<p><code>//$row_reading_time = date("Y-m-d H:i:s", strtotime("$row_reading_time + 4 hours"));</code></p>
<p><code>echo '&lt;tr&gt; </code></p>
<p><code>&lt;td&gt;' . $row_``num`` . '&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;' . $row_``message`` . '&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;' . $row_``sensor_``num`` . '&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;' . $row_``value`` . '&lt;/td&gt; </code></p>
<p><code>&lt;td&gt;' . $row_``r``ecorded`` . '&lt;/td&gt; </code></p>
<p><code>&lt;/tr&gt;';</code></p>
<p><code>}</code></p>
<p><code>$result-&gt;free();</code></p>
<p><code>}</code></p>
<p><code>$conn-&gt;close();</code></p>
<p><code>?&gt; </code></p>
<p><code>&lt;/table&gt;</code></p>
<p><code>&lt;/body&gt;</code></p>
<p><code>&lt;/html&gt;</code></p>
<h1 class="toc-header" id="h-4plotting-of-data-from-mariadbmysql-database"> 4<code>Plotting of data from ``MariaDB/MySQL database</code></h1>
<p><code>[https://randomnerdtutorials.com/visualize-esp32-esp8266-sensor-readings-from-anywhere/](https://randomnerdtutorials.com/visualize-esp32-esp8266-sensor-readings-from-anywhere/)</code></p>
<p><code>$ ``sudo nano /var/www/html/esp-``chart``.php</code></p>
<h2 class="toc-header" id="h-41esp-chartphp"> 4.1<code>esp-chart.php</code></h2>
<p><code>&lt;!--</code></p>
<p><code>Rui Santos</code></p>
<p><code>Complete project details at https://RandomNerdTutorials.com</code></p>
<p><code>Permission is hereby granted, free of charge, to any person obtaining a copy</code></p>
<p><code>of this software and associated documentation files.</code></p>
<p><code>The above copyright notice and this permission notice shall be included in all</code></p>
<p><code>copies or substantial portions of the Software.</code></p>
<p><code>--&gt;</code></p>
<p><code>&lt;?php</code></p>
<p><code>$servername = "``192.168.178.25``";</code></p>
<p><code>// REPLACE with your Database name</code></p>
<p><code>$dbname = "``test_arduino``";</code></p>
<p><code>// REPLACE with Database user</code></p>
<p><code>$username = "``admin3``";</code></p>
<p><code>// REPLACE with Database user password</code></p>
<p><code>$password = "``pi``";</code></p>
<p><code>// Create connection</code></p>
<p><code>$conn = new mysqli($servername, $username, $password, $dbname);</code></p>
<p><code>// Check connection</code></p>
<p><code>if ($conn-&gt;connect_error) {</code></p>
<p><code>die("Connection failed: " . $conn-&gt;connect_error);</code></p>
<p><code>} </code></p>
<p><code>$sql = "SELECT ``num``, ``message, ``sensor_``num``, ``value, ``value_h``, ``recorded`` FROM ``hello_sensor ``WHERE sensor_num = 0`` ORDER BY ``num`` DESC limit 40";</code></p>
<p><code>$result = $conn-&gt;query($sql);</code></p>
<p><code>while ($data = $result-&gt;fetch_assoc()){</code></p>
<p><code>$sensor_data[] = $data;</code></p>
<p><code>}</code></p>
<p><code>$``recorded`` = array_column($sensor_data, '``recorded``');</code></p>
<p><code>// ******* Uncomment to convert readings time array to your timezone ********</code></p>
<p><code>/*$i = 0;</code></p>
<p><code>foreach ($readings_time as $reading){</code></p>
<p><code>// Uncomment to set timezone to - 1 hour (you can change 1 to any number)</code></p>
<p><code>$readings_time[$i] = date("Y-m-d H:i:s", strtotime("$reading - 1 hours"));</code></p>
<p><code>// Uncomment to set timezone to + 4 hours (you can change 4 to any number)</code></p>
<p><code>//$readings_time[$i] = date("Y-m-d H:i:s", strtotime("$reading + 4 hours"));</code></p>
<p><code>$i += 1;</code></p>
<p><code>}*/</code></p>
<p><code>$``value`` = json_encode(array_reverse(array_column($sensor_data, '``value``')), JSON_NUMERIC_CHECK);</code></p>
<p><code>$``value_``h`` = json_encode(array_reverse(array_column($sensor_data, '``value_``h``')), JSON_NUMERIC_CHECK);</code></p>
<p><code>$``recorded`` = json_encode(array_reverse($``recorded``), JSON_NUMERIC_CHECK);</code></p>
<p><code>/*echo $value1;</code></p>
<p><code>echo $value2;</code></p>
<p><code>echo $value3;</code></p>
<p><code>echo $reading_time;*/</code></p>
<p><code>$result-&gt;free();</code></p>
<p><code>$conn-&gt;close();</code></p>
<p><code>?&gt;</code></p>
<p><code>&lt;!DOCTYPE html&gt;</code></p>
<p><code>&lt;html&gt;</code></p>
<p><code>&lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;</code></p>
<p><code>&lt;script src="https://code.highcharts.com/highcharts.js"&gt;&lt;/script&gt;</code></p>
<p><code>&lt;style&gt;</code></p>
<p><code>body {</code></p>
<p><code>min-width: 310px;</code></p>
<p><code> max-width: 1280px;</code></p>
<p><code> height: 500px;</code></p>
<p><code>margin: 0 auto;</code></p>
<p><code>}</code></p>
<p><code>h2 {</code></p>
<p><code>font-family: Arial;</code></p>
<p><code>font-size: 2.5rem;</code></p>
<p><code>text-align: center;</code></p>
<p><code>}</code></p>
<p><code>&lt;/style&gt;</code></p>
<p><code>&lt;body&gt;</code></p>
<p><code>&lt;h2&gt;ESP Weather Station&lt;/h2&gt;</code></p>
<p><code>&lt;div id="chart-temperature" class="container"&gt;&lt;/div&gt;</code></p>
<p><code>&lt;div id="chart-humidity" class="container"&gt;&lt;/div&gt;</code></p>
<p><code>&lt;script&gt;</code></p>
<p><code>var ``value`` = &lt;?php echo $``value``; ?&gt;;</code></p>
<p><code>var ``value_``h`` = &lt;?php echo $``value_``h``; ?&gt;;</code></p>
<p><code>var ``recorded`` = &lt;?php echo $``recorded``; ?&gt;;</code></p>
<p><code>var chartT = new Highcharts.Chart({</code></p>
<p><code>chart:{ renderTo : 'chart-temperature' },</code></p>
<p><code>title: { text: 'BME280 Temperature' },</code></p>
<p><code>series: [{</code></p>
<p><code>showInLegend: false,</code></p>
<p><code>data: ``value</code></p>
<p><code>}],</code></p>
<p><code>plotOptions: {</code></p>
<p><code>line: { animation: false,</code></p>
<p><code>dataLabels: { enabled: true }</code></p>
<p><code>},</code></p>
<p><code>series: { color: '#059e8a' }</code></p>
<p><code>},</code></p>
<p><code>xAxis: { </code></p>
<p><code>type: 'datetime',</code></p>
<p><code>categories: ``recorded</code></p>
<p><code>},</code></p>
<p><code>yAxis: {</code></p>
<p><code>title: { text: 'Temperature (Celsius)' }</code></p>
<p><code>//title: { text: 'Temperature (Fahrenheit)' }</code></p>
<p><code>},</code></p>
<p><code>credits: { enabled: false }</code></p>
<p><code>});</code></p>
<p><code>var chartH = new Highcharts.Chart({</code></p>
<p><code>chart:{ renderTo:'chart-humidity' },</code></p>
<p><code>title: { text: 'BME280 Humidity' },</code></p>
<p><code>series: [{</code></p>
<p><code>showInLegend: false,</code></p>
<p><code>data: ``value_``h</code></p>
<p><code>}],</code></p>
<p><code>plotOptions: {</code></p>
<p><code>line: { animation: false,</code></p>
<p><code>dataLabels: { enabled: true }</code></p>
<p><code>}</code></p>
<p><code>},</code></p>
<p><code>xAxis: {</code></p>
<p><code>type: 'datetime',</code></p>
<p><code>//dateTimeLabelFormats: { second: '%H:%M:%S' },</code></p>
<p><code>categories: ``recorded</code></p>
<p><code>},</code></p>
<p><code>yAxis: {</code></p>
<p><code>title: { text: 'Humidity (%)' }</code></p>
<p><code>},</code></p>
<p><code>credits: { enabled: false }</code></p>
<p><code>});</code></p>
<p><code>&lt;/script&gt;</code></p>
<p><code>&lt;/body&gt;</code></p>
<p><code>&lt;/html&gt;</code></p>
<h1 class="toc-header" id="h-5using-canvasjs"> 5<code>Using canvas.js</code></h1>
<p><code>[https://canvasjs.com/php-charts/multiseries-chart/](https://canvasjs.com/php-charts/multiseries-chart/)</code></p>
<h1 class="toc-header" id="h-6external-access-to-lamp-server"> 6<code>External access to LAMP server</code></h1>
<p><code>port 22 (ssh), port 80 (http)</code></p>
<p><code>[https://raspberrypi.stackexchange.com/questions/105080/how-to-remote-to-raspberry-pi-from-outside-local-network](https://raspberrypi.stackexchange.com/questions/105080/how-to-remote-to-raspberry-pi-from-outside-local-network)</code></p>
<p><code>[https://howtoraspberrypi.com/enable-mysql-remote-connection-raspberry-pi/](https://howtoraspberrypi.com/enable-mysql-remote-connection-raspberry-pi/)</code></p>
<p><code>Configure MySQL to accept external connections to Raspberry Pi</code></p>
<p><code>$ sudo nano /etc/mysql/my.cnf</code></p>
<p><code>#bind-address = 127.0.0.1 ``&lt;&lt;&lt;&lt;&lt; comment the line bind-address</code></p>
<p><code>optionall``y ``m``odify the MySQL port ``to reduce risk of attacks, e.g.</code></p>
<p><code>port = 8457</code></p>
<p><code>$ ``/etc/init.d/mysql restart</code></p>
<h1 class="toc-header" id="h-7raspi-as-samba-server"> 7<code>RasPi as Samba server</code></h1>
<p><code>[https://aallan.medium.com/adding-an-external-disk-to-a-raspberry-pi-and-sharing-it-over-the-network-5b321efce86a](https://aallan.medium.com/adding-an-external-disk-to-a-raspberry-pi-and-sharing-it-over-the-network-5b321efce86a)</code></p>
<p><code>SMB is the best protocol for use on multiple platforms due to superior interoperability</code></p>
<p><code>in case of issue “chown: changing ownership of '...': Operation not permitted”:</code></p>
<p><code>[https://stackoverflow.com/questions/25559700/chown-command-returning-operation-not-permitted](https://stackoverflow.com/questions/25559700/chown-command-returning-operation-not-permitted)</code></p>
<p><code>get uid:</code></p>
<p><code>$ cat /etc/passwd |grep pi</code></p>
<p><code>get gid:</code></p>
<p><code>$ ``cat /etc/group |grep pi</code></p>
<p><code>Give pi:pi access to the entire /``mnt/usb`` mount:</code></p>
<p><code>$ sudo mount -o remount,gid=``&lt;pi's gid&gt;``,uid=``&lt;pi's uid&gt;`` /mnt/usb</code></p>
<p><code>Attention: ``FAT file system doesn't support such permissions, so they can't be applied after the drive is mounted</code></p>
<p><code>[https://raspberrypi.stackexchange.com/questions/40408/how-to-change-permissions-on-external-hard-drive-connected-to-raspberry](https://raspberrypi.stackexchange.com/questions/40408/how-to-change-permissions-on-external-hard-drive-connected-to-raspberry)</code></p>
<p><code>Instead, drive should be formatted with pi user ``and/or ntfs file sstem``?</code></p>
<p><code>Mounting USB drive ``that is``**NTFS**``**???-**``formatted`` under specific name:</code></p>
<p><code>$ ``sudo ``fdisk -l</code></p>
<p><code>$ sudo umount ``/dev/sda1</code></p>
<p><code>$ sudo mkdir /mnt/usb</code></p>
<p><code>$ sudo chown -R pi:pi /mnt/usb</code></p>
<p><code>seems like this command has to be used each time the usb drive is connected: ``$ ``sudo mount /dev/sda1 /mnt/usb -o uid=pi,gid=pi</code></p>
<p><code>append following text to this file for automatic mounting under the specified path:</code></p>
<p><code>$ sudo nano ``/etc/fstab</code></p>
<p><code>/dev/sda1 /mnt/usb auto defaults,user 0 1</code></p>
<p><code>OR:</code></p>
<p><code>/dev/sda1 /mnt/usb ``ntfs-3g`` defaults,user,uid=``1000``,gid=``1000``,noatime 0 0</code></p>
<p><code>[https://raspberrypi.stackexchange.com/questions/40408/how-to-change-permissions-on-external-hard-drive-connected-to-raspberry](https://raspberrypi.stackexchange.com/questions/40408/how-to-change-permissions-on-external-hard-drive-connected-to-raspberry)</code></p>
<p><code>Make the drive accessible from the network ``by adding following text``:</code></p>
<p><code>$ sudo apt-get install samba samba-common-bin</code></p>
<p><code>$ sudo nano /etc/samba/smb.conf</code></p>
<p><code>[share]</code></p>
<p><code>Comment`` = Shared Folder</code></p>
<p><code>Path = /mnt/usb</code></p>
<p><code>Browseable = yes</code></p>
<p><code>Writeable = Yes</code></p>
<p><code>only guest = no</code></p>
<p><code>create mask = 0777</code></p>
<p><code>directory mask = 0777</code></p>
<p><code>Public = yes</code></p>
<p><code>Guest ok = yes</code></p>
<p><code>$ chmod 1777 ``/mnt/usb ``**&lt;&lt;&lt;&lt; the uSB should be mounted under this name**</code></p>
<p><code>$ sudo /etc/init.d/samba restart</code></p>
<p><code>sudo service smbd restart</code></p>
<p><code>sudo service smbd status</code></p>
<p><code>[https://www.raspberrypi.org/forums/viewtopic.php?t=56149](https://www.raspberrypi.org/forums/viewtopic.php?t=56149)</code></p>
<p><code>$ ``sudo df</code></p>
<p><code>$ ``cd /media</code></p>
<p><code>$ ``sudo mkdir HDD</code></p>
<p><code>$ ``sudo nano /etc/fstab</code></p>
<p><code>/dev/sda1 /media/HDD ntfs-3g defaults,noatime 0 0</code></p>
<p><code>$ cp /etc/samba/smb.conf /etc/samba/smb.conf.bak //Make a backup of the config file</code></p>
<p><code>$ ``nano /etc/samba/smb.conf</code></p>
<p><code>[media] </code></p>
<p><code>comment = Media share</code></p>
<p><code>path = /media/HDD</code></p>
<p><code>valid users = @users</code></p>
<p><code>force group = users</code></p>
<p><code>create mask = 0660</code></p>
<p><code>directory mask = 0771</code></p>
<p><code>read only = no</code></p>
<p><code>sudo service samba restart</code></p>
<p><code>sudo useradd ChooseUsernameHere -m -G users</code></p>
<p><code>sudo passwd TheChosenUsername //after this choose a password for the new user</code></p>
<p><code>sudo smbpasswd -a TheChosenUsername //after this enter passwd again</code></p>
<p><code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; ``userA pi</code></p>
<p><code>mount in Win10/Linux/etc. ``a``s ``smb://192.``1``68.178.25/</code></p>
<p><code>[https://raspberrytips.com/format-mount-usb-drive/](https://raspberrytips.com/format-mount-usb-drive/)</code></p>
<p><code>Formatting USB stick with RasPi:</code></p>
<p><code>$ ``sudo fdisk -l</code></p>
<p><code>$ ``sudo fdisk /dev/sda``1</code></p>
<p><code>Create a new partition table : g (for GPT, use help for other format)</code></p>
<p><code>Create a new partition : n</code></p>
<p><code>You can keep the default values for a single partition</code></p>
<p><code>Confirm with Y to remove the signature</code></p>
<p><code>And finally write and exit fdisk: w</code></p>
<p><code>$ ``sudo mkfs.ext4 /dev/sda1 ``OR: $ ``sudo mkfs.vfat /dev/sda1 ``OR: $ ``sudo mkfs.ntfs /dev/sda1</code></p>
<p><code>[https://thepihut.com/blogs/raspberry-pi-tutorials/17699796-formatting-and-mounting-a-usb-drive-from-a-terminal-window](https://thepihut.com/blogs/raspberry-pi-tutorials/17699796-formatting-and-mounting-a-usb-drive-from-a-terminal-window)</code></p>
<p><code>$ ``sudo mkfs -t vfat -I /dev/sd``a``1</code></p>
<p><code>$ ``sudo mount /dev/sd``a``1 /``mnt/u``sb`` -o umask=000</code></p>
<p><code>$ sudo umount /mnt/usb</code></p>
<h1 class="toc-header" id="h-8filtering-rows-with-sql"> 8<code>Filtering rows with SQL</code></h1>
<p><code>[https://chartio.com/blog/simple-sql-filtering-rows/](https://chartio.com/blog/simple-sql-filtering-rows/)</code></p>
<p><code>SELECT column_name1, column_name2</code></p>
<p><code>FROM table_name</code></p>
<p><code>WHERE column_name1 operator value;</code></p>
<p><code>SELECT *</code></p>
<p><code>FROM hello_sensor</code></p>
<p><code>WHERE sensor_num = 0 </code></p>
<p><code>ORDER BY `hello_sensor`.`recorded` DESC</code></p>
<h1 class="toc-header" id="h-9python-mysql"> 9<code>P``ython MySQL</code></h1>
<h2 class="toc-header" id="h-91python-mysql-querying-data"> 9.1<code>Python MySQL Querying Data</code></h2>
<p><code>[https://stackoverflow.com/questions/44634810/fastest-way-to-fetch-table-from-mysql-into-pandas](https://stackoverflow.com/questions/44634810/fastest-way-to-fetch-table-from-mysql-into-pandas)</code></p>
<p><code>MyEngine=create_engine('[YourDatabase]://[User]:[Pass]@[Host]/[DatabaseName]', echo = True)</code></p>
<p><code>**Downloading:**</code></p>
<p><code>df = pd.read_sql_query('select * from [TableName]', con= MyEngine)</code></p>
<p><code>**Uploading:**</code></p>
<p><code>df.to_sql([TableName], MyEngine, if_exists = 'append', index=False)</code></p>
<h2 class="toc-header" id="h-92using-mysql-module"> 9.2<code>Using ``mysql ``module</code></h2>
<p><code>[https://www.mysqltutorial.org/python-mysql-query/](https://www.mysqltutorial.org/python-mysql-query/)</code></p>
<p><code>[https://www.codespeedy.com/fetch-data-from-mysql-table-in-python-program/](https://www.codespeedy.com/fetch-data-from-mysql-table-in-python-program/)</code></p>
<p><code>[https://www.krazyprogrammer.com/2020/11/fetch-and-display-data-from-mysql-using.html](https://www.krazyprogrammer.com/2020/11/fetch-and-display-data-from-mysql-using.html)</code></p>
<p><code>pip install mysql-connector</code></p>
<p><code>[https://www.tutorialspoint.com/python_mysql/python_mysql_select_data.htm](https://www.tutorialspoint.com/python_mysql/python_mysql_select_data.htm)</code></p>
<p><code>import mysql.connector</code></p>
<p><code>#establishing the connection</code></p>
<p><code>conn = mysql.connector.connect(</code></p>
<p><code>user='``admin3``', password='``pi``', host='192.168.178.25', database='``test_arduino``'</code></p>
<p><code>)</code></p>
<p><code>#Creating a cursor object using the cursor() method</code></p>
<p><code>cursor = conn.cursor()</code></p>
<p><code>#Retrieving single row</code></p>
<p><code>quer``y`` = '''SELECT * from ``hello_sensor``'''</code></p>
<p><code>#Executing the query</code></p>
<p><code>cursor.execute(``quer``y``)</code></p>
<p><code>#Fetching 1st row from the table</code></p>
<p><code>result = cursor.fetchone();</code></p>
<p><code>print(result)</code></p>
<p><code>#Fetching ``first two`` row``s`` from the table</code></p>
<p><code>result = cursor.fetchmany(size=2);</code></p>
<p><code>print(result)</code></p>
<p><code>#Fetching ``all`` row``s`` from the table</code></p>
<p><code>result = cursor.fetchall();</code></p>
<p><code>print(result)</code></p>
<p><code>#Closing the connection</code></p>
<p><code>conn.close()</code></p>
<h2 class="toc-header" id="h-93create-table"> 9.3<code>Create table</code></h2>
<p><code>[https://www.tutorialspoint.com/python_mysql/python_mysql_create_table.htm](https://www.tutorialspoint.com/python_mysql/python_mysql_create_table.htm)</code></p>
<p><code>import mysql.connector</code></p>
<p><code>#establishing the connection</code></p>
<p><code>conn = mysql.connector.connect(</code></p>
<p><code>user='root', password='password', host='127.0.0.1', database='mydb'</code></p>
<p><code>)</code></p>
<p><code>#Creating a cursor object using the cursor() method</code></p>
<p><code>cursor = conn.cursor()</code></p>
<p><code>#Dropping EMPLOYEE table if already exists.</code></p>
<p><code>cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")</code></p>
<p><code>#Creating table as per requirement</code></p>
<p><code>sql ='''CREATE TABLE EMPLOYEE(</code></p>
<p><code>FIRST_NAME CHAR(20) NOT NULL,</code></p>
<p><code>LAST_NAME CHAR(20),</code></p>
<p><code>AGE INT,</code></p>
<p><code>SEX CHAR(1),</code></p>
<p><code>INCOME FLOAT</code></p>
<p><code>)'''</code></p>
<p><code>cursor.execute(sql)</code></p>
<p><code>#Closing the connection</code></p>
<p><code>conn.close()</code></p>
<h2 class="toc-header" id="h-94insert-data"> 9.4<code>Insert data</code></h2>
<p><code>[https://www.tutorialspoint.com/python_mysql/python_mysql_insert_data.htm](https://www.tutorialspoint.com/python_mysql/python_mysql_insert_data.htm)</code></p>
<p><code>import mysql.connector</code></p>
<p><code>#establishing the connection</code></p>
<p><code>conn = mysql.connector.connect(</code></p>
<p><code>user='root', password='password', host='127.0.0.1', database='mydb'</code></p>
<p><code>)</code></p>
<p><code>#Creating a cursor object using the cursor() method</code></p>
<p><code>cursor = conn.cursor()</code></p>
<p><code># Preparing SQL query to INSERT a record into the database.</code></p>
<p><code>insert_stmt = (</code></p>
<p><code>"INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)"</code></p>
<p><code>"VALUES (%s, %s, %s, %s, %s)"</code></p>
<p><code>)</code></p>
<p><code>data = ('Ramya', 'Ramapriya', 25, 'F', 5000)</code></p>
<p><code>try:</code></p>
<p><code># Executing the SQL command</code></p>
<p><code>cursor.execute(insert_stmt, data)</code></p>
<p><code># Commit your changes in the database</code></p>
<p><code>conn.commit()</code></p>
<p><code>except:</code></p>
<p><code># Rolling back in case of error</code></p>
<p><code>conn.rollback()</code></p>
<p><code># Closing the connection</code></p>
<p><code>conn.close()</code></p>
<h1 class="toc-header" id="h-10schedule-periodic-raspi-reboot"> 10<code>Schedule periodic RasPi reboot</code></h1>
<p><code>[https://raspberrypi.stackexchange.com/questions/2150/how-do-i-reboot-at-a-specific-time](https://raspberrypi.stackexchange.com/questions/2150/how-do-i-reboot-at-a-specific-time)</code></p>
<p><code>[https://smarthomepursuits.com/how-to-reboot-raspberry-pi-on-a-schedule/](https://smarthomepursuits.com/how-to-reboot-raspberry-pi-on-a-schedule/)</code></p>
<p><code>$ ``sudo crontab -e</code></p>
<p><code>&gt;&gt;&gt;&gt;&gt; choose nano editor</code></p>
<p><code>&gt;&gt;&gt;&gt;&gt; enter following command at end of file for ``daily ``reboot at 13:03:</code></p>
<p><code>03 13 * * * /sbin/shutdown -r now</code></p>
<p><code>Check time in Linux commandline:</code></p>
<p><code>[https://www.lifewire.com/display-date-time-using-linux-command-line-4032698](https://www.lifewire.com/display-date-time-using-linux-command-line-4032698)</code></p>
<p><code>$ date</code></p>
<h1 class="toc-header" id="h-11install-keepass"> 11<code>Install KeePass</code></h1>
<p><code>[https://keepass.info/download.html](https://keepass.info/download.html)</code></p>
<p><code>[https://sourceforge.net/p/keepass/discussion/329220/thread/17d1bd26/](https://sourceforge.net/p/keepass/discussion/329220/thread/17d1bd26/)</code></p>
<p><code>sudo apt-add-repository ppa:jtaylor/keepass</code></p>
<p><code>sudo apt-get update</code></p>
<p><code>sudo apt-get install keepass2 </code></p>
<h1 class="toc-header" id="h-12clean-the-repository-of-downloaded-packages"> 12<code>Clean`` the repository of downloaded packages</code></h1>
<p><code>[https://askubuntu.com/questions/634820/how-can-i-delete-ubuntu-temp-files-which-are-created-during-software-installatio](https://askubuntu.com/questions/634820/how-can-i-delete-ubuntu-temp-files-which-are-created-during-software-installatio)</code></p>
<p><code>sudo ``apt-get autoclean</code></p>
<p><code>sudo ``apt-get clean</code></p>
<h1 class="toc-header" id="h-13cleanup-a-varlogjournal-in-linux"> 13<code>cleanup a /var/log/journal in Linux</code></h1>
<p><code>How to tell how much space is being taken?</code></p>
<p><code>$ ``journalctl --disk-usage</code></p>
<p><code>Fix: Option 3 (RECOMMENDED!):</code></p>
<p><code>$ journalctl --vacuum-size=500M</code></p>
<p><code>This will delete old log files until the directory reaches the threshold size stipulated, in our case, 500M.</code></p>
<h1 class="toc-header" id="h-14change-permission-of-mounted-drive"> 14<code>Change permission of mounted drive</code></h1>
<p><code>$ ``sudo chmod -R 770 /media/bora/Medien</code></p>
<p><code>sudo mount -o rw,remount /``media/bora/Medien</code></p>
<p><code>[https://askubuntu.com/questions/948416/changing-ownership-of-read-only-file-system-i-cant-move-delete-rename-any-f](https://askubuntu.com/questions/948416/changing-ownership-of-read-only-file-system-i-cant-move-delete-rename-any-f)</code></p>
<p><code>$ ``mount -v | grep "^/" | awk '{print "\nPartition identifier: " $1 "\n Mountpoint: " $3}'</code></p>
<p><code>$ ``sudo mount -o remount,rw /``dev/sda5`` /``media/bora/Medien</code></p>
<p><code>“``If you are on a dual boot system and getting the error related to the read-only file system. One probable cause could be that your other system is not correctly shut down and might be in hibernation or sleep. So login to that system and shut it down properly and religion to your Linux system. this resolved the issue in my system”</code></p>