【ESP desktop IoT化:動作 】 赤外線&有線で、モニターセレクタ、USBセレクタ、照明、エアコンをandroid端末からWifi経由で制御するモジュール作成

 https://funasover.blogspot.com/2025/04/esp-desktop-iot-usbandroidwifi.html

の続き。

【書き込みアダプターの作成】
ESPに書き込む際に、IO0をGNDに落としながらリセットをGND落として書き込みモードにして・・と書き込み儀式があるのでスイッチを使うが、書き込み時以外には使わないので、本体のボードには載せず、書き込み用のモジュールを作成してあげて書き込みを行う。


IO16のピンに緑のLEDをオンボードで用意しているが、”書き込み儀式”を行うと、点灯。書き込みモードに入ったサイン。この状態で、書き込みを実施。完了すると消える。

【赤外LED】
XHコネクタ

を圧着し、反対側にはLEDはめるソケットを半田で固定。

カソード、アノードが分からくならないために、赤いマークをケーブルには入れている。
2チャンネル用意しているのは、エアコンはデスクと反対側にあるし、結構距離があるので、方向や位置を反応する方向に向ける必要がある。かたや、デスク回りの照明のLED受光部やHDMIの切り替えの受光部は別のエアコンとは真逆だしという理由。

【UEBセレクター】
で既に作っているので、そのまま。

【スケッチ】
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include "CH9329_Keyboard.h"
#include "fujitsuAC.h"

const char *ssid = "pr500m-aa8183-1";
const char *password = "64b69e0c8de78";
int usb_state;
// const uint16_t kIrLed = 13;
#define pin_IrLed1 13
#define pin_IrLed2 12
#define pin_set_USBselector 14
#define pin_read_USBselector 4

IRsend irsend1(pin_IrLed1);  // Set the GPIO to be used to sending
IRsend irsend2(pin_IrLed2);  // Set the GPIO to be used to sending
WiFiServer server(5000);

void connectToWiFi() {
  WiFi.begin(ssid, password);
  // WiFi.setSleep(false); // WiFiスリープを無効にする


  // WiFi接続が確立されるまで待機
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // WiFi接続が確立した場合の処理
  String ip = WiFi.localIP().toString().c_str();

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.printf("IP Address  : ");
  Serial.println(WiFi.localIP());
  //サーバー立ち上げ
  server.begin();
  Serial.println("server.begin()");
  Serial.println(WiFi.macAddress());
}

bool timeout(WiFiClient client) {
  int waitloops = 0;
  while (!client.available()) {
    waitloops++;
    delay(1);  //delay 1 msec
    if (10000 < waitloops) {
      return true;
    }
  }
  return false;
}

void change_usb_channel() {
  digitalWrite(pin_set_USBselector, HIGH);  // push button on.
  delay(100);                               // set usb pin low a quit short time
  digitalWrite(pin_set_USBselector, LOW);
}

void sendKeyPacket(uint8_t ckey, uint8_t ukey) {
  uint8_t sum = 0x10C + ckey + ukey;
  uint8_t packet[14] = { 0x57, 0xAB, 0x00, 0x02, 0x08, ckey, 0x00,
                         ukey, 0x00, 0x00, 0x00, 0x00, 0x00, sum };
  Serial.write(packet, 14);
}
void press1key(uint8_t ckey, uint8_t ukey) {
  sendKeyPacket(ckey, ukey);
  //delay(1);
  sendKeyPacket(0x00, 0x00);
}

void setup() {
  // put your setup code here, to run once:LEDONLEDONLEDON
  Serial.begin(CH9329_DEFAULT_BAUDRATE);                //115200
  CH9329_Keyboard.begin(Serial, KeyboardLayout_jp_JP);  //KeyboardLayout_jp_JP
  pinMode(pin_set_USBselector, OUTPUT);                 // Initialize the LED_BUILTIN pin as an output
  pinMode(pin_read_USBselector, INPUT);                 // readiing status of USB selector
  connectToWiFi();

  irsend1.begin();
  irsend2.begin();

  // Read status of USB selector
  digitalWrite(pin_set_USBselector, LOW);
  int usb_state = digitalRead(pin_read_USBselector);
  Serial.println(digitalRead(pin_read_USBselector));
  if (usb_state == 1) {
    Serial.println("USB channel == 1");
  } else if (usb_state == 0) {
    Serial.println("USB channel == 2");
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  WiFiClient client = server.available();
  usb_state = digitalRead(pin_read_USBselector);
  size_t length = 0;

  if (client) {
    Serial.println("New Client.");
    while (client.connected()) {
      //if( timeout(client) ) break;
      int size = client.available();
      if (size) {
        Serial.println();
        Serial.print("size:");
        Serial.println(size);

        char cmd[size + 1];  // command
        memset(cmd, 0, sizeof(cmd));
        client.read((uint8_t *)cmd, size);
        cmd[size] = '\0';  // Add null terminator
        Serial.println(cmd);
        String str;
        str = cmd;


        if (strcmp(cmd, "PRESET1") == 0) {
          Serial.println("receive PRESET1");

          client.stop();

          //OUT1 set input2
          irsend1.sendNEC(0x1FE609F, 32, 2);
          delay(300);
          //OUT2 set input1
          irsend1.sendNEC(0x1FEA45B, 32, 2);
          delay(300);
          if (usb_state == 0) {
            change_usb_channel();
            usb_state = 1;
          }
        } else if (strcmp(cmd, "PRESET2") == 0) {
          client.println("Hello from arduino\n");
          Serial.println("receive PRESET2");
          client.stop();
          //OUT1 set input4
          irsend1.sendNEC(0x1FE50AF, 32, 2);
          delay(300);
          //OUT2 set input3
          irsend1.sendNEC(0x1FEE41B, 32, 2);
          delay(300);
          if (usb_state == 1) {
            change_usb_channel();
            usb_state = 0;
          }



        } else if (strcmp(cmd, "USB1") == 0) {
          Serial.println("receive USB1");
          //if (usb_state==1){
          change_usb_channel();
          usb_state = 0;
          //}
          client.stop();

        } else if (strcmp(cmd, "USB2") == 0) {
          Serial.println("receive USB2");
          //if (usb_state==1){
          change_usb_channel();
          usb_state = 0;
          //}
          client.stop();

        } else if (strcmp(cmd, "LEDON") == 0) {
          Serial.println("receive LEDON");
          irsend1.sendNEC(0xF7C03FUL);



          client.stop();
        } else if (str.indexOf("MAIL:") == 0) {
          Serial.println("receive MAIL");
          CH9329_Keyboard.print(str.substring(5));

                    // CH9329_Keyboard.print("1234567890-^");
          // delay(1);
          // press1key(0x00, 135);  //<- \ (yen) is output. it works.
          // CH9329_Keyboard.println("");
          // CH9329_Keyboard.println("qwertyuiop@[");
          // delay(1);
          // CH9329_Keyboard.println("asdfghjkl;:]");
          // delay(1);
          // CH9329_Keyboard.print("zxcvbnm,./");
          // press1key(0x00, 135);  //<- \ (yen) is output. it works.
          // CH9329_Keyboard.println("");
          // delay(1);
          // CH9329_Keyboard.print("!\"#$%&'()=~");
          // press1key(0x02, 137);  //<- | is output. it works.
          // CH9329_Keyboard.println("");
          // delay(1);
          // CH9329_Keyboard.println("QWERTYUIOP`{");
          // delay(1);
          // CH9329_Keyboard.println("ASDFGHJKL+*}");
          // delay(1);
          // CH9329_Keyboard.print("ZXCVBNM<>?");
          // press1key(0x02, 135);  //<- _ is output. it works.
          // CH9329_Keyboard.println("");

          // CH9329_Keyboard.press(KEY_LEFT_GUI);
          // CH9329_Keyboard.release(KEY_LEFT_GUI);
          // delay(1);
          // CH9329_Keyboard.println("notepad");

          client.stop();

        } else if (strcmp(cmd, "LEDOFF") == 0) {
          Serial.println("receive LEDOFF");
          irsend1.sendNEC(0xF740BFUL);
          client.stop();
         
        } else if (strcmp(cmd, "HDMI11") == 0) {
          Serial.println("receive HDMI11");
          //OUT1 set input2
          irsend1.sendNEC(0x1FE609F, 32, 2);
          delay(300);
          client.stop();

        } else if (strcmp(cmd, "HDMI12") == 0) {
          Serial.println("receive HDMI12");
          //OUT1 set input4
          irsend1.sendNEC(0x1FE50AF, 32, 2);
          client.stop();

        } else if (strcmp(cmd, "HDMI21") == 0) {
          Serial.println("receive HDMI21");
          //OUT2 set input1
          irsend1.sendNEC(0x1FEA45B, 32, 2);
          delay(300);

          client.stop();
        } else if (strcmp(cmd, "HDMI22") == 0) {
          Serial.println("receive HDMI22");
          //OUT2 set input3
          irsend1.sendNEC(0x1FEE41B, 32, 2);
          delay(300);

          client.stop();
        } else if (strcmp(cmd, "HEAT") == 0) {
          Serial.println("receive HEAT");
          irsend2.sendRaw(rawData_heat, 291, 38);  // Send a raw data capture at 38kHz.
          client.stop();
        } else if (strcmp(cmd, "COOL") == 0) {
          Serial.println("receive COOL");
          irsend2.sendRaw(rawData_cool, 291, 38);  // Send a raw data capture at 38kHz.
          client.stop();
        } else if (strcmp(cmd, "STOP") == 0) {
          Serial.println("receive STOP");
          irsend2.sendRaw(rawData_stop, 115, 38);  // Send a raw data capture at 38kHz.

          client.stop();
        } else if (str.indexOf("TEMP") >= 0) {
          Serial.println("receive TEMP");
          String str_temp;
          str_temp = str.substring(4);
          float temperature;

          temperature = str_temp.toFloat();
          int int_temp;
          int_temp = temperature / 0.5 - 32;

          uint16_t rawData1D[291];
          for (int i = 0; i < 291; i++) {
            rawData1D[i] = rawData[int_temp][i];
          }

          irsend2.sendRaw(rawData1D, 291, 38);  // Send a raw data capture at 38kHz.

          client.stop();
        }
      }

      delay(1);
    }
    client.stop();
    Serial.println();
    Serial.println("client.stop()");
  }
  if (WiFi.status() != WL_CONNECTED) {
    connectToWiFi();
  }
  delay(1);
}


【アンドロイドアプリ】



Android studioで作成。アイコンなんかはInkscapeで書いて一つ一つ選択箇所をエクスポートしてAndroid studioに取り組んで作成。
参考にしかならいですが、アンドロイド側のMainActivetyはこんな感じ。
package com.example.myremocon2;

import static android.os.SystemClock.sleep;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.MaskFilterSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {

private static Socket socket;
private static PrintWriter printWriter;
private InputStream in;


String message="";
String Rmessage="";
private static String ip="192.168.1.29";
private static int port=5000;

int preset_status =1;
int usb_status =1;

String led_status="";

double temperature = 24.0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
/*TEXT*/
TextView text_preset = findViewById(R.id.text_preset);
BlurMaskFilter filter2 = new BlurMaskFilter(6.0f, BlurMaskFilter.Blur.SOLID);
MaskFilterSpan span2 = new MaskFilterSpan(filter2);
SpannableString spannable = new SpannableString("Preset");
spannable.setSpan(span2, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text_preset.setText(spannable);

TextView text_USB_selector= findViewById(R.id.text_USB_selector);
spannable = new SpannableString("USB selector");
spannable.setSpan(span2, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text_USB_selector.setText(spannable);

TextView text_Monitor_selector= findViewById(R.id.text_Monitor_selector);
spannable = new SpannableString("Monitor selector");
spannable.setSpan(span2, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text_Monitor_selector.setText(spannable);

TextView text_Air_conditioner= findViewById(R.id.text_Air_conditioner);
spannable = new SpannableString("Air conditioner");
spannable.setSpan(span2, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text_Air_conditioner.setText(spannable);

TextView text_LED_controller= findViewById(R.id.text_LED_controller);
spannable = new SpannableString("LED controller");
spannable.setSpan(span2, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text_LED_controller.setText(spannable);

//TextClock clockTC = findViewById(R.id.textClock);

}
public void ac_temp_up(View v)
{
if (temperature<30 && temperature>=16) {
temperature += 0.5;
TextView tw = findViewById(R.id.text_temp);
tw.setText(String.valueOf(temperature));
}

message="TEMP"+String.valueOf(temperature);
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText(String.format("Temperature up to" + temperature));
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

}
public void ac_temp_down(View v)
{
if (temperature<=30 && temperature>16) {
temperature -= 0.5;
TextView tw = findViewById(R.id.text_temp);
tw.setText(String.valueOf(temperature));
}
message="TEMP"+String.valueOf(temperature);
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText(String.format("Temperature down to" + temperature));
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

}

public void ac_cool (View v)
{
message="COOL";

TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("COOL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();
}

public void ac_heat(View v)
{
message="HEAT";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("HEAT button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();
}

public void ac_stop(View v)
{
message="STOP";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("STOP button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();
}

public void preset1(View v)
{
message="PRESET1";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("PRESET1 button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();
TextView tw= findViewById(R.id.pc_number);
tw.setText("1");
}

public void preset2(View v)
{
message="PRESET2";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("PRESET2 button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.pc_number);
tw.setText("2");
}


public void ledoff(View v)
{
message="LEDOFF";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("LEDOFF button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("OFF");

ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(VISIBLE);
ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(INVISIBLE);
image_ledoff.setEnabled(false);
image_ledon.setEnabled(true);
}
public void ledon(View v)
{
message="LEDON";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("LEDON button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("ON");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}

public void macro1(View v)
{
message="MAIL:xxxxxx@xxxxx.com";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro1:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO1");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}
public void macro2(View v)
{
message="MAIL:xxxxxxx@xxxxx.com";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro2:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO2");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}
public void macro3(View v)
{
message="MAIL:xxxxxxx@xxxxxxxx.com";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro1:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO3");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}
public void macro4(View v)
{
message="MAIL:xxxxx@xxxxxxx";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro4:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO4");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}

public void macro5(View v)
{
message="MAIL:xxxxxx@xxxxxxxxx";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro5:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO5");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}

public void macro6(View v)
{
message="MAIL:xxxxxxx@xxxxxx";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro6:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO6");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}
public void macro7(View v)
{
message="MAIL:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro7:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO7");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}
public void macro8(View v)
{
message="MAIL:xxxxxxxxxxxxx";
TextView tv_comment = findViewById(R.id.text_comment);
tv_comment.setText("macro8:MAIL button was pressed");
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw= findViewById(R.id.led_status);
tw.setText("MACRO8");

ImageView image_ledoff= findViewById(R.id.button_ledoff);
image_ledoff.setVisibility(VISIBLE);
ImageView image_ledon= findViewById(R.id.button_ledon);
image_ledon.setVisibility(INVISIBLE);
image_ledoff.setEnabled(true);
image_ledon.setEnabled(false);
}
public void usb1(View v)
{
message="USB1";
TextView tv_comment = findViewById(R.id.text_comment);
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = "USB1";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw = findViewById(R.id.usb_number);
tw.setText("1");

ImageView image_usb2= findViewById(R.id.button_usb2);
image_usb2.setVisibility(VISIBLE);
ImageView image_usb1= findViewById(R.id.button_usb1);
image_usb1.setVisibility(INVISIBLE);
image_usb2.setEnabled(true);
image_usb1.setEnabled(false);

}
public void usb2(View v)
{
message="USB2";
TextView tv_comment = findViewById(R.id.text_comment);
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = "USB2";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

TextView tw = findViewById(R.id.usb_number);
tw.setText("2");

ImageView image_usb1= findViewById(R.id.button_usb1);
image_usb1.setVisibility(VISIBLE);
ImageView image_usb2= findViewById(R.id.button_usb2);
image_usb2.setVisibility(INVISIBLE);
image_usb1.setEnabled(true);
image_usb2.setEnabled(false);

}

public void hdmi11(View v)
{
message="HDMI11";
TextView tv_comment = findViewById(R.id.text_comment);
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

ImageView image_hdmi2= findViewById(R.id.button_hdmi12);
image_hdmi2.setVisibility(VISIBLE);
ImageView image_hdmi1= findViewById(R.id.button_hdmi11);
image_hdmi1.setVisibility(INVISIBLE);
image_hdmi2.setEnabled(true);
image_hdmi1.setEnabled(false);

}
public void hdmi12(View v)
{
message="HDMI12";
TextView tv_comment = findViewById(R.id.text_comment);
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();
ImageView image_hdm1= findViewById(R.id.button_hdmi11);
image_hdm1.setVisibility(VISIBLE);
ImageView image_hdmi2= findViewById(R.id.button_hdmi12);
image_hdmi2.setVisibility(INVISIBLE);
image_hdm1.setEnabled(true);
image_hdmi2.setEnabled(false);
}

public void hdmi21(View v)
{
message="HDMI21";
TextView tv_comment = findViewById(R.id.text_comment);
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();

ImageView image_hdmi2= findViewById(R.id.button_hdmi22);
image_hdmi2.setVisibility(VISIBLE);
ImageView image_hdmi1= findViewById(R.id.button_hdmi21);
image_hdmi1.setVisibility(INVISIBLE);
image_hdmi2.setEnabled(true);
image_hdmi1.setEnabled(false);

}
public void hdmi22(View v)
{
message="HDMI22";
TextView tv_comment = findViewById(R.id.text_comment);
myTask mt =new myTask(tv_comment);
mt.execute();
CharSequence text = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this /* MyActivity */, text, duration);
toast.show();
ImageView image_hdm1= findViewById(R.id.button_hdmi21);
image_hdm1.setVisibility(VISIBLE);
ImageView image_hdmi2= findViewById(R.id.button_hdmi22);
image_hdmi2.setVisibility(INVISIBLE);
image_hdm1.setEnabled(true);
image_hdmi2.setEnabled(false);
}

class myTask extends AsyncTask<Void,Void,String> {
final TextView mSocketText;
public myTask(TextView v) {mSocketText = v;}

@Override
protected void onPreExecute() {
mSocketText.setText("Communicating....");
return ;
}
protected String doInBackground(Void... voids) {
try {
socket= new Socket(ip, 5000);
printWriter = new PrintWriter(socket.getOutputStream());
sleep(50);
printWriter.write(message);
printWriter.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Rmessage = br.readLine();
printWriter.close();
br.close();
socket.close();


}catch (IOException e){
e.printStackTrace();
}
return Rmessage;
}
@Override
protected void onPostExecute(String message) {
mSocketText.setText(message);
}
}



}



一応動作は、ちゃんとしていて、HDMIの切り替え、USBの切り替え、LED照明のON/OFF、エアコンの操作、あとマクロ的にボタン一つで、登録したキーボード出力ができる。



3Dプリンターで固定治具を打ち出して、USBセレクタ、MDMIセレクターを固定。


作成した基板は、アクリルで簡易的なケースを作成してて、固定。
LEDは、3DプリンターでLEDを固定する治具で固定してIR(赤外線受光部)の前に固定。






コメント

このブログの人気の投稿

Attiny85とAQM0802A(LCD)のI2C接続

CH9329で日本語キーボード109で正しく表示する方法