<repo_name>kotatsumuri/otenkikun<file_sep>/wdata.py
import serial,time

def write_data(ser,status):
    #print(hex(status))
    ser.write(chr(status).encode())
    #time.sleep(1);
<file_sep>/rand.py
import random
import time
import sys
while True:
    for i in range(208):
        print(chr(random.randint(33,126)),end="")
        print("\r")
    sys.stdout.flush()
    time.sleep(0.05)
<file_sep>/jread.py
import json

def read(jsonfile,s):
    r=open(jsonfile,"r")
    dic=json.load(r)
    return str(dic[str(s)])
<file_sep>/otenkikun.php
<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="otenkikun.css">
  <title>NAMAEHAMADANAI</title>
  <link rel="shortcut icon" href="icon.ico">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
</head>
<body class='section1'>
  <div class="title">SETUP</br></div>
        <label class="control-label"></label>
          <!--<div class="select-wrap select-primary">-->
  <form action="form.php" method="post">
    <?php
    while(date('s')%2==0){
      sleep(1);
    }
    $url = "info.json";
    $json = file_get_contents($url);
    $arr = json_decode($json,TRUE);
    echo'<div class="time_title">ON TIME</div>';
    echo '<div class="time_label">';
    echo'<div class="select-wrap select-primary" >';
    echo '<select  class="time" id="hour" name="hour" >';
    for($i=0;$i<=23;$i++){
      if($i==$arr['hour']){
        echo '<option value="'.$i.'"selected>'.$i.'h</option>';
      }
      else{
        echo '<option value="'.$i.'">'.$i.'h</option>';
      }
    }
    echo '</select>';
    echo '<select class="time" id="minute" name="minute">';
    for($i=0;$i<=11;$i++){
      if(($i*5)==$arr['minute']){
        echo '<option value="'.($i*5).'"selected>'.($i*5).'m</option>';
      }
      else{
      echo '<option value="'.($i*5).'">'.($i*5).'m</option>';
      }
    }
    echo '</select>';
    echo '</br>';
    echo '</div>';
    echo '</div>';
    echo'<div class="place_title">PLACE</div>';
    echo'<div class="select-wrap select-primary">';
    echo '<select id="place" name="place">';
    // ◯◯◯.csvに先程保存したCSVファイル名を入力します。
      $filename = 'place.csv';

      // fopenでファイルを開きます。
      $fp = fopen($filename, 'r');
      $i=0;
          // CSVファイルの行数の数だけ実行
          while($data = fgetcsv($fp)){

              // csvファイルの列数だけ実行
              if($data[1]==$arr['place']){
                echo '<option value="'.$data[1].'"selected>'.$data[0].'</option>';
              }
              else{
              echo '<option value="'.$data[1].'">'.$data[0].'</option>';
              }
              $i++;
          }
      // fcloseでファイルを閉じます。
      fclose($fp);
      echo'</select>';
      echo '</br>';
      echo '</div>';
     ?>
     <label class="label-checkbox">
       <input type="checkbox"  id='light' name='light' value='1'/>
       <span class="lever">LIGHT</span>
     </label>
    <input class='send' type="submit"　 value="SEND">
  </form>
</body>
</html>
<file_sep>/form.php
<html>
<head><title>form.php</title>
<link rel="stylesheet" type="text/css" href="form.css"></head>
<body>
<?php

  $place = $_POST['place'];
  $hour =$_POST['hour'];
  $minute =$_POST['minute'];
  $light=$_POST['light'];

  $value_arry=[
  'place'=>$place,
  'hour'=>(int)$hour,
  'minute'=>(int)$minute,
  'light'=>(int)$light,
  'now'=>(int)date('m')*60+(int)date('s')
  ];

  while(date('s')%2==0){
    sleep(1);
  }

  $jsonstr=json_encode($value_arry,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  file_put_contents("info.json",$jsonstr);
  //print($place);

?>
<meta http-equiv ='Refresh' content='0;url=otenkikun.php'>
</body>
</html>
<file_sep>/otenkikun/otenkikun.ino
#include <Adafruit_NeoPixel.h>
#define RGBLED_OUTPIN    8
#define NUMRGBLED        1

Adafruit_NeoPixel RGBLED = Adafruit_NeoPixel(NUMRGBLED, RGBLED_OUTPIN, NEO_RGB + NEO_KHZ800);

int bright_var;

int noti_counter=0;
boolean noti_status=0;

int tokidoki_counter=0;
boolean tokidoki_status=0;

byte get_value=0;

int b_status=0;
int b_weather=4;
int a_weather=4;
int weather_status=0;

void setup() {
  Serial.begin(115200);
  RGBLED.begin() ;
  pinMode(9,INPUT);
}

int weather_pattarn[5][3]={{0xff,0x4f,0x00},{0x00,0x60,0xff},{0xb5,0xff,0xff},{0x00,0xff,0xa5},{0,0,0}};
//晴、雨、曇、雪

void loop() {

  if (Serial.available() > 0) { // 受信したデータが存在する
     get_value= Serial.read(); // 受信データを読み込む
  }
  
  
  date_sorting(get_value);

  btn();
  
  if(b_status==1){
      bright_var=bright_volume();
      switch(weather_status){
        case 0:
        only(b_weather,bright_var);
        break;
        
        case 1:
        tokidoki(b_weather,a_weather,bright_var);
        break;
        
        case 2:
        noti(b_weather,a_weather,bright_var);
        break;
      }
  }
  else if(b_status==2){
    status_reset();
    off();
  }
}

void btn(){
  int btn_value=digitalRead(9);
  switch(btn_value){
    case 0:
    Serial.println('0');
    break;
    case 1:
    Serial.println('1');
    break;
  }
}

void status_reset(){
  noti_counter=0;
  noti_status=0;
  tokidoki_counter=0;
  tokidoki_status=0;
}

int bright_volume(){
  int var=analogRead(4);
  
  if(var>512)
    var=512;
    
  int bright=map(var,0,512,0,255);

  return bright;
}

void date_sorting(int var){
  b_status=var/0x40;
  var=var%0x40;
  if(b_status==0){
    a_weather=var/0x10;
    var=var%0x10;
    weather_status=var/0x04;
    var=var%0x04;
    b_weather=var;
  }
}

void only(int weather,int bright){
  RGBLED.setBrightness(bright) ;        
  RGBLED.setPixelColor(0, weather_pattarn[weather][0],weather_pattarn[weather][1],weather_pattarn[weather][2]) ;
  RGBLED.show() ;
  delay(1);
}

void noti(int before,int after,int bright){
  
  int weather[2]={before,after};
  
  RGBLED.setBrightness(bright*(-0.5*cos(PI/180*noti_counter)+0.5)) ;        
  RGBLED.setPixelColor(0, weather_pattarn[weather[noti_status]][0],weather_pattarn[weather[noti_status]][1],weather_pattarn[weather[noti_status]][2]) ;
  RGBLED.show() ; 
  delay(5);
  
  if(noti_status==true&&noti_counter==360)
    delay(1000);
  
  if(noti_counter==360){
    noti_counter=0;
    noti_status=!noti_status;
  }
  
  noti_counter++;
  
}

void tokidoki(int before,int after,int bright){
  
  int weather[2]={before,after};

  if(tokidoki_counter<150||(tokidoki_counter>=250&&tokidoki_counter<400)){
    RGBLED.setBrightness(bright) ;
    RGBLED.setPixelColor(0, weather_pattarn[weather[tokidoki_status]][0],weather_pattarn[weather[tokidoki_status]][1],weather_pattarn[weather[tokidoki_status]][2]) ;
    RGBLED.show();
    delay(1); 
  }
  if((tokidoki_counter>=150&&tokidoki_counter<250)||tokidoki_counter>=400){
  RGBLED.setBrightness(0);
  RGBLED.show() ; 
  delay(1);
  }
  
  tokidoki_counter++;
  if(tokidoki_counter==250 ||tokidoki_counter==1400)
  tokidoki_status=!tokidoki_status;
  if(tokidoki_counter>=1400)
  tokidoki_counter=0;
}

void off(){
  RGBLED.setBrightness(0) ;
  RGBLED.show() ;
}


<file_sep>/main.py
#!/anaconda3/bin/python3
import time
from datetime import datetime, timezone, timedelta
import wthr,wdata,jread
import serial
import os

ser=0
for file in os.listdir('/dev'):
    if "cu.usb" in file:
        ser=serial.Serial('/dev/'+str(file),115200)
time.sleep(1.5)

info_name=['place','hour','minute','light','now']
info=[]
for i in info_name:
    info.append(jread.read('info.json',str(i)))
status=wthr.get_weather(str(info[0]))
wdata.write_data(ser,status)

wdata.write_data(ser,0x40)

now =time.time()
JST = timezone(timedelta(hours=+9), 'JST')
loc = datetime.fromtimestamp(now,JST)

start_second=loc.hour*3600+loc.minute*60+loc.second

p_info=''
p_info=str(info[0])

p=0
p=int(info[4])

while True:

    now =time.time()
    JST = timezone(timedelta(hours=+9), 'JST')
    loc = datetime.fromtimestamp(now,JST)

    r=ser.readline()

    if (loc.second)%2==0:
        p_info=str(info[0])
        p=int(info[4])
        for i in range(5):
            info[i]=jread.read('info.json',str(info_name[i]))
    if (loc.hour==5 or loc.hour==11 or loc.hour==17) and loc.minute==0 and loc.second==30:
        status=wthr.get_weather(str(info[0]))
        wdata.write_data(ser,status)

    now_second=loc.hour*3600+loc.minute*60+loc.second
    light_second=int(info[1])*3600+int(info[2])*60
    s_second=light_second-600
    f_second=light_second+600

    if p_info!=str(info[0]):
        status=wthr.get_weather(str(info[0]))
        wdata.write_data(ser,status)
        wdata.write_data(ser,0x40)
        start_second=loc.hour*3600+loc.minute*60+loc.second
    elif p!=int(info[4]) and int(info[3])==0 :
        start_second=now_second-61


    if s_second<0:
        s_second=24*3600+s_second

    if f_second>24*3600:
        f_second=s_second-24*3600

    if now_second>s_second and now_second<f_second or r==b'0\r\n' or (p!=int(info[4]) and int(info[3])==1):
        wdata.write_data(ser,0x40)
        start_second=loc.hour*3600+loc.minute*60+loc.second
    if abs(now_second-start_second)>60 :
        wdata.write_data(ser,0x80)
ser.close()
<file_sep>/LED_test/LED_test.ino
#include <Adafruit_NeoPixel.h>
#define RGBLED_OUTPIN    4
#define NUMRGBLED        1

Adafruit_NeoPixel RGBLED = Adafruit_NeoPixel(NUMRGBLED, RGBLED_OUTPIN, NEO_RGB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  RGBLED.begin() ;
  pinMode(6,INPUT);
}
int val;
void loop() {
    /*int var=analogRead(0);
    if(var>512)
    var=512;
    int bright=map(var,0,512,0,255);
    //Serial.println(var);
  RGBLED.setBrightness(bright) ;         
  RGBLED.setPixelColor(0, 255,255,255) ; 
  RGBLED.show() ; */
  val=digitalRead(6);
  Serial.println(val);
}
<file_sep>/wthr.py
# coding: UTF-8
import urllib.request
from bs4 import BeautifulSoup
import re
import requests



def get_weather(url):
    if url[0]=='h':
        html = urllib.request.urlopen(url)
        soup = BeautifulSoup(html, "html.parser")

        dic=soup.find_all(class_="pict")
        string=re.split('[><]',str(dic[0]))
    else:
        string=['0']*5
        string[4]=url
    print(string[4])
    array=[]

    char_pattern=['晴','雨','曇','雪','時','の']

    for c in string[4]:
        for i in char_pattern:
            if c==i:
                array.append(c)
    status=0

    for i in range(len(array)):

        for j in range(4):
            if array[i]==char_pattern[j]:
                status=status+j*pow(4,i)

        for k in range(2):
            if array[i]==char_pattern[k+4]:
                status=status+(k+1)*4


    return status
