0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

タニーさんは水中ポンプでやらかしたので花や野菜の自動水やり機を作る。

Last updated at Posted at 2024-12-28

自動水やり機

回路

ACアダプタ -> TLP-A100.Type-C.INPUT
TLP-A100.VBUS.OUTPUT -> TLP-P050.VIN
TLP-A100.VBUS.GND -> TLP-P050.GND
TLP-P050.VOUT -> Pump1.Posi
TLP-P050.VOUT -> Pump2.Posi
TLP-P050.GND -> Pump1.Nega
TLP-P050.GND -> Pump2.Nega
TLP-A100.Type-C.OUTPUT -> ESP32.MicroUSB
TLP-A100.MicroUSB -> PC

ESP32.PIN17 -> TLP-P050.EN

参考

https://lang-ship.com/blog/work/esp32-tcp-ip-socket/
https://dev.classmethod.jp/articles/esp32_flash_io/

プログラム

#include <WiFi.h>
#include <FS.h>
#include <SPIFFS.h>

const char *ssid = "ssid";
const char *password = "password";

struct tm timeInfo;
#include <WiFiClient.h>

WiFiServer server(5000);
char tstr[20];
char tcp_buf[256];

long cmds[10];
int cc;

byte bMT;
const int PIN_MT = 17;

void WifiDateTime( void ){
  if (WiFi.begin(ssid, password) != WL_DISCONNECTED) {
    ESP.restart();
  }

  Serial.println("Connecting.\n");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println(".");
    delay(1000);
  }

  Serial.println("Connected!");
  Serial.printf("IP Address  : ");
  Serial.println(WiFi.localIP());
  
  configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");

  getLocalTime(&timeInfo);
  
  server.begin();
}

void GetSetting( void ){
  for ( int i=0; i<10; i++ ){
    cmds[i] = 0;
  }
  bMT= LOW;
  cc=0;
  SPIFFS.begin( true );

  fs::FS fs = SPIFFS;
  File file = fs.open("/hello.txt");
  while(file.available()){
    String s = file.readStringUntil('\n'); 
    char b[ 255 ];
    sprintf( b, "%s", s );
    cmds[cc] = atol( b );
    cc++;
    Serial.println( s );
  }
  file.close();
  
}

long GetTimeSerial( void ){
  sprintf( tstr, "%04d/%02d/%02d",
          timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday );
  Serial.println( tstr );
  
  sprintf( tstr,"%02d%02d%02d",
          timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec );
  Serial.println( tstr );
  return atol( tstr );
}

void checkWifiClient( void ){
  WiFiClient client = server.available();
  if (client) {
    int readLength=0;
    memset( tcp_buf, 0, 255 );
    String s;
    cc=0;
    while (client.connected()) {
      int size = client.available();

      if (size > 0) {
        for (int i = 0; i < size; i++) {
          uint8_t c = client.read();
          tcp_buf[ readLength ] = c;
          readLength++;
        }
      }            
      delay(1);
    }
    client.stop();
    
    Serial.println( readLength );
    Serial.println( tcp_buf );

    char* c;
    c = strtok(tcp_buf, ":");
    while( 1 ) {
      if ( atoi( c ) == 0 ) { 
        if ( cc == 0) {
          c++;
        }else{
          break;
        }
      }
      cmds[cc] = atol( c )*1000000;
      c=strtok(NULL, ":");
      cmds[cc] += atol( c )*10000;
      c=strtok(NULL, ":");
      cmds[cc] += atol( c )*100;
      c=strtok(NULL, ";");
      cmds[cc] += atol( c );
      cc++;
      c=strtok(NULL, ":");
    }

    fs::FS fs = SPIFFS;
    File file = fs.open("/hello.txt", FILE_WRITE);
    char cb[255];
    for ( int i=0; i<cc; i+=2 ){
      Serial.println( cmds[ i ] );
      Serial.println( cmds[ i+1 ] );
      sprintf( cb, "%d", cmds[ i ] );
      file.println(cb);
      sprintf( cb, "%d", cmds[ i+1 ] );
      file.println(cb);
    }
    file.close();
    

  }
  
}

long getLocalTimeProc( void ){
  long si=0;
  getLocalTime(&timeInfo);
  
  if (timeInfo.tm_year != 70) {
    si = GetTimeSerial();

    checkWifiClient();

    for ( int i=0; i<cc; i+=2 ){
      if ( ( cmds[ i ] / 1000000 ) == 1 ) {
        long t1=cmds[ i+0 ] - 1000000;
        long t2=cmds[ i+1 ] - 2000000;
        if ( ( si > t1 ) && ( si < t2 ) ){
          if ( bMT == LOW ){
            bMT = HIGH;
            digitalWrite(PIN_MT, HIGH);
            Serial.println( "MT ON.");
          }
        }else{
          if ( bMT == HIGH ) {
            bMT = LOW;
            digitalWrite(PIN_MT, LOW);
            Serial.println( "MT OFF.");
          }
        }
      }
    }
  }

}

void setup() {
  Serial.begin(115200);
  pinMode(PIN_MT, OUTPUT);
  digitalWrite(PIN_MT, LOW);

  WifiDateTime();
  GetSetting();
  
}

void loop() {
  long si;
  si = getLocalTimeProc();
  if ( si == 0 ){
    Serial.println( "...sync.");
  }
  delay(1000);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?