2025年2月26日水曜日

ESP8266 pedia

 WEMOS/ESP01を所有するが前者がピンが多く便利でほぼ一択状態

DHt11,LCD1602はRANDOMNERD TUTORIALでいけた

SSD1306はARDUINO-IDEサンプルのGRAPHICはいけたが文字があかん

https://github.com/datasith/Ai_Ardulib_SSD1306/tree/mainをインストして成功

#include <Wire.h>

#include "ACROBOTIC_SSD1306.h" // これが重要!

void setup()

{

  Wire.begin();

  oled.init();                      // Initialze SSD1306 OLED display

  oled.clearDisplay();              // Clear screen

  oled.setTextXY(0,0);              // Set cursor position, start of line 0

  oled.putString("ACROBOTIC");

  oled.setTextXY(1,0);              // Set cursor position, start of line 1

  oled.putString("industries");

  oled.setTextXY(2,0);              // Set cursor position, start of line 2

  oled.putString("Pasadena,");

  oled.setTextXY(2,10);             // Set cursor position, line 2 10th character

  oled.putString("CA");

}

void loop()

{

}

2025年2月23日日曜日

mqtt pedia :: Raspi,ESP32,ESP8266,

-------------------mqttx OK (mosquitto als ok)-------------------https://qiita.com/emqx_japan/items/634a5b91f0b760664711 にある

mqttx client for desktop::arm64version get and click deb file for install success!

ubuntu/raspiで成功 mqttxでport1883で起動

https://mqttx.app/web-client#/recent_connections/32a3a13e-7aef-4224-8ddc-6226007aa6ca でもうごくがもはやwebsocketのみ対応している!間違えないこと

cf 

https://qiita.com/ekzemplaro/items/168a4a98fe65aa24abb9 でbroker.emqx.ioに

sample/imageTopicをsubしておいて(mqttxにて)mosquitto.shでpubして成功

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

https://www.emqx.com/ja/blog/use-mqtt-with-raspberry-pi より

# subscriber.py

from gpiozero import LED # もらったメッセージでLED操作する

from time import sleep,time #そのため、この2行必要

import paho.mqtt.client as mqtt

led = LED(17)


def on_connect(client, userdata, flags, rc):

    print(f"Connected with result code {rc}")

    # Subscribe, which need to put into on_connect

    # If reconnect after losing the connection with the broker, 

 # it will continue to subscribe to the raspberry/topic topic

    client.subscribe("raspberry/topic")


# The callback function, it will be triggered when receiving messages

def on_message(client, userdata, msg):

    print(f"{msg.topic} {msg.payload}")

    if msg.payload == b'4':

        led.on()

        sleep(10)

        led.off()

client = mqtt.Client()

client.on_connect = on_connect

client.on_message = on_message

# Set the will message, when the Raspberry Pi is powered off, or the network is interrupted abnormally, it will send the will message to other clients

client.will_set('rasp', b'{"status": "Off"}')

# Create connection, the three parameters are broker address, broker port number, and keep-alive time respectively

client.connect("broker.emqx.io", 1883, 60)

# Set the network loop blocking, it will not actively end the program before calling disconnect() or the program crash

client.loop_forever()

上記を起動しておいて、下記を実行するとサブにでてくる

# publish.py

import paho.mqtt.client as mqtt
import time

def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")

    # Send a message to the raspberry/topic every 1 second, 5 times in a row
    for i in range(5):
        # The four parameters are topic, sending content, QoS and whether retaining the message respectively
        client.publish('raspberry/topic', payload=i, qos=0, retain=False)
        print(f"send {i} to raspberry/topic")

client = mqtt.Client()
client.on_connect = on_connect
client.connect("broker.emqx.io", 1883, 60)

client.loop_forever()

-------mqtt and esp32------------------------------------

https://diysmartmatter.com/archives/355 にてモスキートクライアントをインスト

https://qiita.com/koichi_baseball/items/8fa9e0bdbe6d0aebe57d also refferd!

broker.emqx.ioにかえて成功 -t # でなく -t test/# -vだった 上記パブサブ成功

https://qiita.com/emqx_japan/items/6ddf82d90c506312d875 をインストして

mosquitto_sub -h broker.emqx.io -t emqx/esp32 -v でサブモードで

こんにちは、ESP32です ^^ うけとれた

mosquitto_pub -h broker.emqx.io -t emqx/esp32  -m goodbye でパブモードで

goodbyeおくれた

-------------mqtt and esp8266------------------------------------------------

  https://qiita.com/emqx_japan/items/f74e9d108c7ecaa4f2ddを改変して成功

公開emqxブローカーへのコネクションはどんな名前でもログインOK

esp8266/ledなどのトピックをつくる!mqttx画面でplain textモードになっていること!

#include <ESP8266WiFi.h>

#include <PubSubClient.h>


// GPIO 5 D1

#define LED 5


// WiFi

const char *ssid = "HUAWEI@nova@lite@3+"; // WiFi名を入力

const char *password = "99991111";  // WiFiパスワードを入力


// MQTTブローカー

const char *mqtt_broker = "broker.emqx.io";

const char *topic = "esp8266/led";

const char *mqtt_username = "emqx";

const char *mqtt_password = "public";

const int mqtt_port = 1883;


bool ledState = false;


WiFiClient espClient;

PubSubClient client(espClient);


void setup() {

    

    // ソフトウェアシリアルのボーレートを115200に設定;

    Serial.begin(115200);

    delay(1000); // 安定性のために遅延


    // WiFiネットワークに接続

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {

        delay(500);

        Serial.println("WiFiに接続中...");

    }

    Serial.println("WiFiネットワークに接続しました");


    // LEDピンを出力として設定

    pinMode(LED, OUTPUT);

    digitalWrite(LED,HIGH);

    delay(1000);

    digitalWrite(LED, LOW);  // 最初はLEDを消灯


    // MQTTブローカーに接続

    client.setServer(mqtt_broker, mqtt_port);

    client.setCallback(callback);

    while (!client.connected()) {

        String client_id = "esp8266-client-";

        client_id += String(WiFi.macAddress());

        Serial.printf("クライアント %s が公開MQTTブローカーに接続しています\n", client_id.c_str());

        if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {

            Serial.println("公開EMQX MQTTブローカーに接続しました");

        } else {

            Serial.print("失敗しました ステート ");

            Serial.print(client.state());

            delay(2000);

        }

    }


    // メッセージの公開と購読

    client.publish(topic, "hello emqx");

    client.subscribe(topic);

}


void callback(char *topic, byte *payload, unsigned int length) {

    Serial.print("トピックにメッセージが到着: ");

    Serial.println(topic);

    Serial.print("メッセージ: ");

    String message;

    for (int i = 0; i < length; i++) {

        message += (char) payload[i];  // byteをStringに変換

    }

    Serial.println(message);

    if (message.indexOf("on")==1 && !ledState) { // replaced by me

        Serial.println("ledon");

        digitalWrite(LED, HIGH);  // LEDを点灯

        ledState = true;

    }

    if (message.indexOf("off")==1 && ledState) {   // replaced by me

          Serial.println("ledoff");

        digitalWrite(LED, LOW); // LEDを消灯

        ledState = false;

    }

    Serial.println();

    Serial.println("-----------------------");

}


void loop() {

    client.loop();

    delay(100); // ループの各反復で短い遅延

}

2025年2月16日日曜日

Raspberry pi :: video / libcamera / gpiozero

 

zero は2になっても2GしかWifi未対応 3a+は5Gも対応している

install時のusキーボード配列は、起動後にraspi-configでjpキーボードに変更できた

https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox 

gpiozero で 直感的にデバイスを制御する



https://ponkichi.blog/mjpg-streamer/ これも参考にして以下を

https://raspi-katsuyou.com/index.php/2020/06/30/11/10/44/644/
 これはブルーバックスと同じだが、ブルーバックスは
 全コマンドを/opt以下にmvしたので、10-02-stream.shを使用!

単純にヴィデオクリップするのならguvcviewがいい パイカメラの場合はv4l2が必要?
USBウェブカメラ(マイク付き)なら録音もできるからベターかも

https://gihyo.jp/admin/serial/01/ubuntu-recipe/0303

https://mekou.com/linux-magazine/web%e3%82%ab%e3%83%a1%e3%83%a9%e6%92%ae%e5%bd%b1guvcview/
以上2個は同上 ----------------------------------------------
Raspberry Pi(ラズパイ)のOSが32ビットか64ビットかを確認するには、コマンド「lsb_release -a」を使用します
【確認方法】
  1. コマンド「lsb_release -a」を実行します。
また、OSが対応するbitを確認するには「getconf LONG_BIT」を使用

legacyOS::bullseye 32bitにはraspistillが入っていたが以下をトライして成功

RasTech Raspberry Pi カメラモジュール Raspberry Pi カメラ 500万画素 Raspberry Pi 5/4B/3B+/3B/2B+/ZERO1.3/ZERO 2W/ZERO W for Raspberry Pi クリアスタンド*1、リボンケーブル*3 をもちいるため

https://qiita.com/yamatonori/items/019d05a19b206cbcf571 
によればOV5647センサーでLIBCAMERAを使う準備をして成功 VNCでも動いた

https://hellobreak.net/raspberry-pi-bullseye-libcamera/ に説明が詳しい

-----------------about gpiozero---------------------------------------------------------------------------------------------

https://gpiozero.readthedocs.io/en/latest/recipes.html#led 英語
https://www.denshi.club/ 日本語

2025年2月11日火曜日

Obnizスマホ両指操作や回転サーボなど

https://thunderblog.org/2019/02/obniz-rc.html これすぐれものサイト

https://docs.obniz.com/ja/guides/obniz-starter-kit/iot-by-html-and-javascript/browser-button-and-servo-motor :: オブニズJs例題 servo motor

https://docs.obniz.com/ja/sdk/parts/DCMotor/README.md

:: ⚠obnizBoard/obnizBoard1YにDCMotorをつなぐときは、obnizBoard/obnizBoard1Y電源供給はPCなどには繋がないでください。ノイズ等によりPCが破損する恐れがあります。

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

https://blog.obniz.com/make/kids-project-simple-rc-car : : スマホオンリー

  <html> <head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>

<link

rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous"
/>
<script
src="https://unpkg.com/obniz@3.x/obniz.js"
crossorigin="anonymous"
></script>
</head>
<body>
<br />
<button
id="lf"
class="btn btn-warning"
style="width:45%;height:100px;font-size:50px;"
>
</button>
<button
id="rf"
class="btn btn-warning"
style="width:45%;height:100px;font-size:50px;"
>
</button>
<br />
<br />
<button
id="lb"
class="btn btn-primary"
style="width:45%;height:100px;font-size:50px;"
>
</button>
<button
id="rb"
class="btn btn-primary"
style="width:45%;height:100px;font-size:50px;"
>
</button>
<script>
let obniz = new Obniz("4998-4317");
obniz.onconnect = async () => {
let motorA = obniz.wired("DCMotor", { forward: 3, back: 2 });
motorA.power(40); // pwm? maybe....
let motorB = obniz.wired("DCMotor", { forward: 0, back: 1 });
motorB.power(40);

$("#lf").on("touchstart mousedown", () => {
motorA.move(true);
}); // during event , front on
$("#lf").on("touchend mouseup", () => {
motorA.stop();
});
$("#lb").on("touchstart mousedown", () => {
motorA.move(false);
}); // during event , back on
$("#lb").on("touchend mouseup", () => {
motorA.stop();
});

$("#rf").on("touchstart mousedown", () => {
motorB.move(true);
});
$("#rf").on("touchend mouseup", () => {
motorB.stop();
});
$("#rb").on("touchstart mousedown", () => {
motorB.move(false);
});
$("#rb").on("touchend mouseup", () => {
motorB.stop();
});
};

</script>
</body>
</html>
-------------------------------------------------------------------

 GUIをhtml/css/jsに変換したもの


<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"
></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"
></script>

<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
/>
</head>
<body>
<h3 id="bploading" style="text-align:center;">LOADING...</h3>
<div id="OBNIZ_OUTPUT"></div>
<br />

<script
src="https://unpkg.com/obniz@latest/obniz.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/iothome/index.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/airobot/index.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ui/index.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ai/howler2.1.2/howler.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ai/opencv3.4/opencv.js"
crossorigin="anonymous"
></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.19.0"> </script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@2.1.1"> </script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8.5/dist/teachablemachine-image.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/posenet@2.2.2"></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ai/clmtrackr/clmtrackr.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ai/clmtrackr/emotion_classifier.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ai/clmtrackr/emotionmodel.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ai/clmtrackr/model_pca_20_svm.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/ai/index.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/obniz-parts-kits@0.18.0/storage/index.js"
crossorigin="anonymous"
></script>


<script>
$("#bploading").text("RUNNING...");
(async function(){  メイン関数
var obniz, servomotor, servometor2, button, button2;
obniz = new Obniz('4998-4317');
await obniz.connectWait(); コネクト定義とモータ定義とボタン定義
servomotor = obniz.wired("ServoMotor",{"signal":0, "vcc":1, "gnd":2});
servometor2 = obniz.wired("ServoMotor",{"signal":3, "vcc":4, "gnd":5});
button = new ObnizUI.Button('forward');
button2 = new ObnizUI.Button('backward');
while (true) {
await ObnizUI.Util.wait(0);
if (button.isClicked()) {
servomotor.angle(180);
servometor2.angle(0);
await obniz.wait(300);
servomotor.angle(91);
servometor2.angle(91);
await obniz.wait(100);
} else if (button2.isClicked()) {
servomotor.angle(0);
servometor2.angle(180);
await obniz.wait(300);
servomotor.angle(91);
servometor2.angle(91);
await obniz.wait(100);
}
}
})();
</script>
</body>
</html>

2025年2月3日月曜日

esp32-cam / esp01(dcm,static IPの怪の解決に成功はしたが?)

 https://qiita.com/Nabeshin/items/b195cad1afe99ce29f1e :: 一番親切なサイト

esp32-camでexampleのcamera web serverに以下の手段でstatic addressを振って成功

ただしesp32-wroverにするだけでは不足で①#define CAMERA_MODEL_AI_THINKERを

有効にして、もとの設定をコメントアウトする②partition schemaをhugeにする必要あり

以下は①②がデフォルトでそうなってるっぽいが困ったら上記を参照すること!

https://randomnerdtutorials.com/projects-esp32-cam/ でesp32-wroverを選んで

esp32-cam robot うごくが通信不安定 電源問題かもしれない。。。。

このコードはstation modeなので、softapもいずれ。。。。たぶんいらんな(以下参照)

おなじsiteのvideo streaming web serverのコードに

  IPAddress local_IP(192,168,43,160);

  IPAddress gateway(192,168,43,1);

  IPAddress subnet(255,155,0,0);

  IPAddress primaryDNS(8,8,8,8);

  IPAddress secondaryDNS(8,8,4,4);

  if(!WiFi.config(local_IP,gateway,subnet,primaryDNS,secondaryDNS)){

    Serial.println("STA failed to configure");

  }

の数行を  

  WiFi.begin(ssid, password);

 の前におくとstatic addressが振られる!


--------arecore no1---------------------------------------

 txdはgpio1,rxdはgpio3だった

以下はdrv8833のPWMでないコード まずは成功したので残すが、arecore noo2に

後述したようにモータはサーボもDCも一個だけしかつかえんようだ。。。。

#define VIN1 1

#define VIN2 3

void setup() 

{

  pinMode(VIN1, OUTPUT);

  pinMode(VIN2, OUTPUT);

   //analogWriteFreq(1000);

   //analogWriteRange(1000);

}


void loop()

{

  int i = 0;


  //正回転

  digitalWrite(VIN1, LOW);

  digitalWrite(VIN2,HIGH);

  //回転速度を上げる

  //for(i = 0; i < 1000; i ++)

 // {

   // analogWrite(VIN1, i);

    //delay(10);

  //}

  delay(500);

  digitalWrite(VIN1,HIGH);

  digitalWrite(VIN2,HIGH);

  delay(500);

  digitalWrite(VIN1,HIGH);

  digitalWrite(VIN2,LOW);

  //回転速度を上げる

  //for(i = 0; i < 1000; i ++)

 // {

   // analogWrite(VIN1, i);

    //delay(10);

  //}

  delay(500);

  digitalWrite(VIN1,HIGH);

  digitalWrite(VIN2,HIGH);

  delay(500);

  //回転速度を下げる

  //for(i = 0; i < 1000; i ++)

  //{

  //  analogWrite(VIN1, 0xff - i);

  //  delay(10);

  //}


  //逆回転

  //digitalWrite(VIN1, LOW);


  //回転速度を上げる

  //for(i = 0; i < 1000; i ++)

  //{

//    analogWrite(VIN2, i);

  //  delay(10);

  //}


  //回転速度を下げる

  //for(i = 0; i < 1000; i ++)

  //{

   // analogWrite(VIN2, 0xff - i);

  //  delay(10);

  //}

}

------------arecore no1---------------static ip -----------------------------------

https://teramorosu.hatenablog.com/entry/2016/10/23/163640でうまく設定できない

https://qiita.com/wamisnet/items/e2aea81f7f800a262be6を参考に改変

以下に成功 

ただしmotor2個につかうために0,2,1,3の組み合わせを使うも失敗

当然サーボ1個とモータ1個の同時制御も不成功

esp8266で再挑戦したら、あっさり成功 esp01では供給電流がたりんようだ

#include <WiFiClient.h>

#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

#define LED 13                    //LED点灯に使用するピン

ESP8266WebServer server(80);


void onroot() {

    String msg ;

    msg += "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title>LED Button</title></head><body>";

    msg += "<script>";

    msg += "function sendOn(){" ;

    msg += "send(\"/on/\");";

    msg += "document.getElementById(\"LEDstatus\").innerHTML=\"ON!\";";

    msg += "}";

    msg += "function sendOff(){";

    msg += "send(\"/off/\");";

    msg += "document.getElementById(\"LEDstatus\").innerHTML=\"OFF!\";";

    msg += "}";

    msg += "function send(url){";

    msg += "var xhr = new XMLHttpRequest();";

    msg += "xhr.open(\"GET\", url, true);";

    msg += "xhr.send();";

    msg += "}";

    msg += "</script>";

    msg += "<button id=\"on\" onClick=sendOn()>LED ON</button>";

    msg += "<button id=\"off\" onClick=sendOff()>LED OFF</button>";

    msg += "<p id=\"LEDstatus\"></p>";

    msg += "</body></html>";


    server.send(200, "text/html", msg);

}


void LedOn(){

  Serial.println("ON");

  digitalWrite(LED,HIGH);

  server.send(200, "text/html","OK");

}


void LedOff(){

  Serial.println("OFF");

  digitalWrite(LED,LOW);

  server.send(200, "text/html","OK");

}


void setup() {


    pinMode(LED, OUTPUT);


    Serial.begin(115200);

    Serial.println("");

    Serial.println("ESP8266 Wifi test");


    // WiFi.config(IPAddress(192, 168, 43, 160), WiFi.gatewayIP(), WiFi.subnetMask());

  WiFi.config(IPAddress(192,168,43,160),IPAddress(192,168,43,1),\

     IPAddress(255,255,255,0)); // これで成功した

    WiFi.begin("HUAWEI@nova@lite@3+","99991111");

    WiFi.mode(WIFI_STA);


    while (WiFi.status() != WL_CONNECTED) {

        delay(500);

        Serial.print(".");

    }


    Serial.println("");

    Serial.print("Connected as ");

    Serial.println(WiFi.localIP());


    // Web server setting

    server.on("/", onroot);

    server.on("/on/", LedOn);

    server.on("/off/", LedOff);

    server.begin();

}


void loop() {

    server.handleClient();

}