Showing posts with label EPS32. Show all posts
Showing posts with label EPS32. Show all posts

2024/10/10

Stonez56 創客工坊 開幕了~ 誠摯的邀請大家加入FB社團~

Stonez56 創客工坊 開幕了~ 誠摯的邀請大家加入FB社團~
https://www.facebook.com/groups/stonez56




您好!我是 Kevin Chen,也是 Stonez56。作為一位資訊/電子技術愛好者,我在各種平台上分享我的知識和專案,旨在幫助他人學習和實踐技術。

在這個社團中,我將分享我的經驗和知識,包括:

Arduino 和 IoT 的教學和專案分享:從基本的 Arduino 教學到 IoT 專案的實踐,我將分享我的經驗和知識。

應用程式開發:我將分享我的 App Inventor 教程,幫助您創建自己的 App Inventor 應用程式。

博客分享:我將分享我的週末專案和其他技術相關內容,讓您了解最新的技術趨勢。

我的目標是教育性地幫助他人學習和實踐技術,特別是在 Arduino 程式設計和App應用程式開發及AI等領域。您可以在這裡與我和其他技術愛好者交流、學習和分享您的經驗。

我住在台灣新竹,歡迎您加入這個社團,與我一起探索技術世界!讓我們一起學習、分享和創造!

從 Arduino, ESP32, App Inventor, Raspberry Pi 都有. 如果你有需要個人教學或是團體授課, 請聯絡我!

 

2022/07/09

一起來學樹莓派系列 - “如何使用 Log2RAM” 來增加SD卡的壽命, 及Pi運作的速度.

 大家好, 歡迎收看本週的weekend project with Stonez56. 

今天來和大家分享 “一起來學樹莓派” 系列 - “如何使用 Log2RAM” 來增加SD卡的壽命, 及Pi運作的速度. 


Log 主要是由系統上執行的一些程式在背景寫入到你的磁碟裡.  它可能是幾分鐘寫入一次, 或是幾秒鐘就寫入一次, 頻繁的讀寫 很容易造成SD卡的損毀並且影響運作的速度.


如果我們將 Log 直接寫入RAM, 就可以減少讀寫的次數, 提升Pi的運作速度, 及延長SD卡的壽命. 這些寫入到RAM的 Log 並不會消失, 它會每天再把這些 Log 寫回到SD卡. 所以你還是可以存取這些 Log的.  


使用 Log2RAM 也不是沒缺點, 如果你的 Pi 突然斷掉電源, 那麼你的 Log 就會完全消失了.

但仔細想想, 你上次查看 Log 是什麼時候呢? 如果不常使用 Log, 那麼這個 Log2RAM 就對你非常有幫助. 另外如果你的 Pi 的記憶體只有1GB或是512MB, 那可能就不合適來使用這個軟體的. 


半年前我曾經做過一個簡單的線上調查, 看看各位使用樹莓派時, 用什麼儲存裝置.
調查的結果有75%的朋友都是使用SD卡. 那我們就開始今天的練習吧!

Step: 

  1. 更新你的 Pi

    • >sudo apt update

    • >sudo apt full-upgrade

  2. 安裝 rsync (用來同步兩地的資料)

    • >sudo apt install rsync

  3. 取得 Log2RAM

    • >wget https://github.com/azlux/log2ram/archive/master.tar.gz -O log2ram.tar.gz

  4. 使用 tar 來解壓縮

    • >tar xf log2ram.tar.gz

    • >cd /home/pi/log2ram-master

    • >sudo ./install.sh

  5. 先檢查system log 的大小

    • >sudo du -sh /var/log 41M

    • 先用3倍大小 = 120M

  6. 重新開機

    • >sudo reboot

  7. 檢查 log2ram 是否正常運作

    • systemctl status log2ram

  8. 如果想要檢查 log 怎麼辦呢? 把 log 寫回到 /var/log

    • >log2ram write

  9. 如果想要暫時停止或重新開始 log2RAM 怎麼辦呢?

    • >log2ram stop

    • >log2ram start



以後任何程式有寫入Log 到  /var/log, 就會被寫入到RAM裡面 


如何設定 Log2RAM

  1. 使用 Nano 來編輯設定檔 log2ram.conf

    • >sudo nano /etc/log2ram.conf

    • 找到 SIZE=40M, 看你係統的設定如果你的系統有產生很多少個話可以把設成120MB

    • PATH_DISK=”/var/log” 如果你還有其他的程式會時時寫入到SD, 可以在PATH_DISK後面加入分號, 再加入其它的路徑, 如: PATH_DISK=”/var/log; /var/mylog”

    • 儲存後並退出

    • 重新啟動樹莓派


想了解有哪些Log會寫入SD卡, 請到 /var/hdd.log/ 去看看有那些檔案被寫入.


2022/07/03

App Inventor #5 無線搖控 ESP32-CAM APP (Part 2)

 大家好, 歡迎收看本週的Weekend project with Stonez56。 本周要進行的是 App Inventor 第5集 - “無線搖控 ESP32-CAM APP Part 2”! 這個是一個專門為初學者來開發的一個系列, 所以會講的比較詳細, 相信各位只要仔細收看內容並進行實作, 一定會收獲滿滿滿。


因內容較多, 本影片分為上, 下兩集. 本集為 App Inventor程式設計. 

而上集為 UI 界面設計(https://youtu.be/x545mFSZWgg ),
如果你還沒有觀看的朋友, 請記得先去看一下, 再回來看這一集


再前一集是ESP32 程式的說明在這:
Arduino #35 使用 MQTT 無線搖控 ESP32-CAM 模組https://youtu.be/ECHBRdtV-yw 


好, 那我們就開始吧今天的練習吧~



2022/07/02

App Inventor #4 無線搖控 ESP32-CAM APP (Part 1)

 大家好, 歡迎收看本週的Weekend project with Stonez56。 本周要進行的是 App Inventor 第4集 - “無線搖控 ESP32-CAM APP Part 1”! 這個是一個專門為初學者來開發的一個系列, 所以會講的比較詳細, 相信各位只要仔細收看內容並進行實作, 一定會收獲滿滿滿。


因內容較多, 本影片分為上, 下兩集. 上集為 UI 界面設計, 下集為 App Inventor程式設計. 


上次為大家示範了如何使用HTML + CSS + Javascript來寫網頁程式(Arduino #35  https://youtu.be/ECHBRdtV-yw), 利用網頁版的MQTT來遠端搖控ESP32 , 收到了不錯的回響.

今天我們使用 App Inventor 2來開發遠端搖控的 APP來取代網頁版本. 在ESP32程式的撰寫不需做任何的改變, 因為只要 MQTT Topic 一樣, APP inventor 就可以發佈訊息到ESP32了! 超方便的!


App Inventor 使用 WebView 想串流影像時, 會出現: header fields are too long for the server to interpret. 這問題會在下集時, 跟各位說明. 


另外, 針對 ESP32 C++上的程式說明, 請看 Arduino #35  https://youtu.be/ECHBRdtV-yw


好, 那我們就開始吧今天的練習吧~



2022/06/18

Android #35 使用 MQTT 無線搖控 ESP32-CAM 模組

 大家好, 歡迎收看本週的Weekend project with Stonez56。

本周要進行的是 Arduino 第35集,"使用 MQTT 無線搖控 ESP32-CAM 模組”。這個是一個專門為初學者來開發的一個系列, 所以會講的比較詳細, 相信各位只要仔細收看內容並進行實作, 一定會收獲滿滿滿。好, 那我們就開始吧今天的練習吧~


!!! ESP32-CAM streaming 的程式是由這裡取得:
https://github.com/wjsanek/wjsanek/blob/main/Working_ESP32_CameraWebServer_V2_with_OTA.zip

 這位高手把一大堆沒有用到的ESP32-CAM的程式移除, 所以我才有機會把程式寫成可以OTA.


在 VScode 裡, ESP32-CAM 的模組, 設定如下:

Board: ESP32 Wrover Module

  • Upload Speed: 115200

  • Flash Frequency: 80MHz

  • Flash Mode: QIO

  • Partition Scheme: Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)


本次學習的目標是能遠端遙控 ESP32-CAM, 所以會用到下列的程式

  • ESP32-CAM - Camera 伺服器 串流影像

  • Servo - 控制 SG90 伺服器擺動

  • ESPAsyncWifimanager - 隨時改變 Wi-Fi 設定

  • AsyncElegantOTA - FOTA - 隨時隨地的更新 firmware

  • MQTT - PubSubClient - 由ESP32-CAM 收送訊息

  • Paho MQTT Javascript client - 由瀏覽器 收送訊息 / HTML 編寫



本專案會用到下列的電子元件:

 

  • ESP32-CAM * 1

  • 天線 * 1

  • SG90 Servo 伺服器 * 2 - 上下 & 左右擺動各一個

  • 1000uf 電容一顆 * 1 - 過濾影像的水波紋

  • 杜邦線 * 4 - Power, GND, IO12, IO13




參考資料:


2021/10/31

#6 監測資料寫入InfluxDB資料庫, 長期保存並在NodeRED資訊看板裡顯示

大家好, 今天來和大家分享 “一起來學樹莓派, 寫入InfluxDB 資料庫”. 上集分享的是資料送到 MQTT 顯示到資訊看板後, 時間後後資料就消失了. 本集是把所有的資料收集起來之後, 可以用來以後做為資料分析的依據. 以前你可能要把資料寫到 Thinkspeak server上, 現在自己家裡的樹莓派上就可以做到了! 一起來學樹莓派吧!


這影片內容有:
  • 安裝 InfluxDB 資料庫
  • 比較 InfluxDB 與關聯式資庫的不同
  • 如何把ESP32送到 MQTT 資料寫入InfluxDB 資料庫
  • 讀取InfluxDB 資料庫, 再把這些資料在資訊看板上顯示出來




Format code for writing InfluxDB

// Create the object key to match DB filed name from topic name(studio/temp1)
msg.payload["temp"] = msg.payload["studio/temp1"];
// Delete the unwanted studio/temp1
delete msg.payload["studio/temp1"];
// Update temp from string to float
msg.payload["temp"] = parseFloat(msg.payload["temp"]);

msg.payload["humd"] = msg.payload["studio/humd1"];
delete msg.payload["studio/humd1"];
msg.payload["humd"] = parseFloat(msg.payload["humd"]);

//remove unused topic
delete msg.topic;

//Create a temporary array
var data = [];
data.push(msg.payload);


//Add object to DB Tag(index field)
data.push({"location":"studio"});
//msg.payload.push({"location":"studio"});

//Copy temporary array to msg.payload
msg.payload = data;
return msg;

Format code for reading historical InfluxDB data


//Source from: https://discourse.nodered.org/t/display-2-extracts-from-influxdb-in-an-ui-chart/29665 var series = ["Temperature °C", "Humdity %"]; var labels = ["temp", "Humdity"]; var data0 = "["; var thetime0; for (var i=0; i < msg.payload.results[0].series[0].values.length; i++) { thetime0 = (msg.payload.results[0].series[0].values[i][0]); // Some manipulation of the time may be required thetime0 = Date.parse(thetime0); data0 += '{ "x":' + thetime0 + ', "y":' + (msg.payload.results[0].series[0].values[i][1]) + '}'; if (i < (msg.payload.results[0].series[0].values.length - 1)) { data0 += "," } else { data0 += "]" } } var data1 = "["; var thetime1; for (var j=0; j < msg.payload.results[0].series[0].values.length; j++) { thetime1 = (msg.payload.results[0].series[0].values[j][0]); // Some manipulation of the time may be required thetime1 = Date.parse(thetime1); data1 += '{ "x":' + thetime1 + ', "y":' + (msg.payload.results[0].series[0].values[j][2]) + '}'; if (j < (msg.payload.results[0].series[0].values.length - 1)) { data1 += "," } else { data1 += "]" } } var data = [data0, data1]; var jsondata = [JSON.parse(data0), JSON.parse(data1)]; msg.payload = [{"series": series, "data": jsondata, "labels": labels}]; msg.dataPoints = data; return msg;

請在此下載本篇教學的 NodeRead 程式: #6 監測資料寫入InfluxDB資料庫, 長期保存並在NodeRED資訊看板裡顯示

更新:
影片內容不小心略過這個 InfluxDB Out Node (Write to studioTempDB)的內容:

InfluxDB out node 內容有兩項資料要輸入:
(1) 定義 InfluxDB Server, 按下鉛筆符號(見下圖)
(2) 寫入的 Measurement 資料表名稱,我們的範例裡,Measurement = studioTemp

定義 InfluxDB Server:
(1) InfluxDB - 在 Flow 裡看到的名稱
(2) InfluxDB 版本 - 教學裡案裝的是 v1.64
(3) Host: InfluxDB 的主機: 由於我們裝在本機,所以用 : raspberrypi.local, Port: 8086
(4) 資料庫名稱: 範例裡使用 studioTemp
(5) 資料庫帳號及密碼: 如果未設定可略過


Stonez56 一起來學樹莓派系列影片: 


2021/10/23

#5 2021 一起學 Raspberry Pi 4 - 溫溼度感測器DHT11由 ESP32 經 MQTT 送到 Node-RED

大家好, 今天來和大家分享如何把 DHT11溫溼度感測器上的資料由ESP32 讀取, 並用MQTT Client 發佈到 Mosquitto Server(MQTT), 再送到 Node-RED上, 再把這些資料在資訊看板上顯示出來. 


本篇使用到的程式碼:

//---- Setup update frequency -----//
const long TEMP_updateInterval = 10000; // How long to change temp and update, 10000 = 10 sec
unsigned long TEMP_currentMillis = 0;
unsigned long TEMP_previousMillis = 0; // store last time temp update

#include "DHT.h"
#define DHTPIN 23  //ESP32 pin 23
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor

/*
  SimpleMQTTClient.ino
  The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
  Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
  It will also send a message delayed 5 seconds later.
*/

#include "EspMQTTClient.h"

EspMQTTClient client(
    "Your_WiFi_AP",     // Wi-Fi AP name
    "Wi-FI_Password", // Wi-Fi Password
    "192.168.0.119", // MQTT Broker server ip
    "",                  // Broker user name; Can be omitted if not needed
    "",                  // Broker password; Can be omitted if not needed
    "ESP32_Client",      // Client name that uniquely identify your device
    1883                 // The MQTT port, default to 1883. this line can be omitted
);

void setup()
{
    Serial.begin(115200);
    //Start DH11 sensor
    dht.begin();
    
    // Optionnal functionnalities of EspMQTTClient :
    client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
    //client.enableHTTPWebUpdater();  // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
    //client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true
}

// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
    // Subscribe to "mytopic/test" and display received message to Serial
    client.subscribe("message/hello", [](const String &payload)
                     { Serial.println(payload); });

    // Subscribe to "mytopic/wildcardtest/#" and display received message to Serial
    //client.subscribe("mytopic/wildcardtest/#", [](const String &topic, const String &payload)
    //                 { Serial.println("(From wildcard) topic: " + topic + ", payload: " + payload); });

    // Publish a message to "mytopic/test"
    client.publish("studio/temp1", "Let's go!"); // You can activate the retain flag by setting the third parameter to true

    // Execute delayed instructions
    //client.executeDelayed(5 * 1000, []()
    //                      { client.publish("mytopic/wildcardtest/test123", "This is a message sent 5 seconds later"); });
}



void loop()
{

    TEMP_currentMillis = millis();
    if(TEMP_currentMillis - TEMP_previousMillis >= TEMP_updateInterval){
        TEMP_previousMillis = TEMP_currentMillis;

        // Reading temperature or humidity takes about 250 milliseconds!
        // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
        float humidity = dht.readHumidity();
        float temperature = dht.readTemperature();
        // Read temperature as Fahrenheit (isFahrenheit = true)
        //float f = dht.readTemperature(true);

        // Check if any reads failed and exit early (to try again).
        if (isnan(humidity) || isnan(temperature))
        {
            Serial.println(F("Failed to read from DHT sensor!"));
            return;
        }

        String tmp = "Current Temperature: ";
        String hud = "Current Humidity: ";
        Serial.print(tmp);
        Serial.println(String(temperature));
        Serial.print(hud);
        Serial.println(String(humidity));
        //Publish
        client.publish("studio/temp1", String(temperature));
        client.publish("studio/humd1", String(humidity));

        // Here is how to subscribe
        //client.subscribe("message/hello", [](const String &payload) { Serial.println(payload); });
    }

    client.loop();
}

*** 影片中忘了說明, 在 VS Code 裡, 按下 F1 到 Arduino Library Manager 裡, 搜尋 EspMQTTClient V.11.1版本, 並按下安裝 , 程式才能正確執行喔~~ ***


如果時間不夠的朋友, 也可以從以下的時間軸挑選想看的部份即可!

影片時間軸:

00:29 課程開始 00:52 如何將 DHT11 連接到 ESP32 (接線圖) 00:59 在 ESP32 利用 EspMQTTClient 來寫程式 01:22 安裝 DHT11 程式庫 02:16 設定 EspMQTTClient 02:45 由 Raspberry Pi Terminal 找出 Mosquitto(MQTT) IP 地址 04:51 不要用 delay() 在程式 Loop 裡! 06:50 上傳並檢查發佈/訂閱的 messages 07:54 安裝 Node-RED 資訊看板(在 Node-RED 中) 08:57 按主題來訂閱消息 09:58 設置 MQTT 服務器的位置 11:34 建立資訊看板選項 Tabs Links & Groups 17:00 建立歷史資料資訊看板 20:32 查看資訊看板 20:50 資訊看板中的選項 Tabs Links & Groups在哪裡

下一集將介紹如何把歷史資料存儲到樹莓派的資料庫(influxDBvor MySQL)中, 以取得長時間的記錄資料. 


Stonez56 一起來學樹莓派系列影片: 


參考資料:

EspMQTTClient: https://github.com/plapointe6/EspMQTTClient 

2021/08/08

Arduino - How to use U8g2 library to show graphic & UTF8 characters for monochrome displays

In order to use the newly purchased 0.96" OLED SD1306 display, I studied the u8g2 library and made this video to share with you on how to use U8g2 library with monochrome display. In the video, I also included a section on how to use the free 5000+ traditional Chinese font created by Mr. Yang. The file is free for anyone to use. Really appreciated what he did for Arduino community! For those who do not have time to watch the entire video , here is the video timeline.

Video Timeline:

00:22 Install u8g2 01:04 Open PrintUTF8 example 02:00 Define display used 02:23 Explain display naming rules 04:13 Uncomment the line selected 04:33 Explain the code 04:42 enableUTF8Print() for symbol and Chinese 05:14 u8g2 supported font list 06:23 display location setCursor(X,Y) 07:05 Upload the sketch & show characters 07:23 Can't overwrite characters! 08:08 Char overwrite solution 08:37 OledOverlayPrint() function 09:06 Use OledOverlayPrint() in code 10:08 Use Open Iconic Fonts 11:29 Display cloud symbol 11:52 \u with number to show cloud 13:02 Change fonts for different lines 14:34 Download 5000+ Chinese font 15:11 Install 5000+Chinese font 15:39 Test missing fonts again 16:42 Reminder of memory usage 17:45 End of tutorial

References:

U8g2 library: https://github.com/olikraus/u8g2 U8g2 plain font: https://github.com/olikraus/u8g2/wiki... U8g2 setCursor command: https://github.com/olikraus/u8g2/wiki... U8g2 iconic font: https://github.com/olikraus/u8g2/wiki... How to make your own font: https://blog.jmaker.com.tw/chinese_oled/ MQTT & IoT Integration FB group: https://www.facebook.com/groups/36360... Download 5000+ Chinese font: https://drive.google.com/drive/folder...

 END

------------------------------------------------------------------------------------------------------------------------------------------------