int flashPin=8;//回傳電眼訊號
int counterPin=7;//回傳計數器+1訊號
int resetPin =9;//回傳歸零訊號
int counter=0;//計數器計數
int flash_counter=0;//電眼計數
int line_break_sec=5;//斷線時間
unsigned long wire_break_start=0;//
unsigned long time;//
unsigned long last_time=0;
unsigned long flash_average_time=0;//電眼平均閃爍時間
unsigned long flash_last=0;//上次電眼啟動時間
unsigned long flash_total_period=0;
unsigned long average_take_off_time=0;//平均拿取時間
unsigned long take_off_total_period=0;//平均拿取時間
unsigned long total_period;//電眼總共閃爍時間間隔
unsigned long past_time=0;//生產時間
unsigned long stop_time=0;//停機時間
unsigned long start_time=0;// 開機 時間
unsigned long server_react_time=0;//語SERVER保持SESSION
boolean status4_sent = false;//當在機器在等待= ture 由電眼開始動做再改成 true
boolean status3_sent = false;//當斷線機器在等待= ture 由按下rest開始動做再改成 true
}
void save_production_time(){
}
unsigned long last_message =0;
void loop(){
EthernetClient message = self_server.available();
if(message==true){//如果M1330主機有送資料來
char a = message.read();
//Serial.println(a);
if(a=='1'){
unsigned long time = now();
if(time-last_message>500){//如果超過500秒 <-避免短時間又增加SESSION
Serial.println("tell server I am alive");
send_http_request(8,0);
Serial.println("status 1 machine stop: waiting taking off the spool..................");
flash_counter=0;//電眼計數歸零
flash_total_period=0;//電眼算依次的時間歸零
Serial.print("counter=");
Serial.println(counter);
time=now();//取得停止時間
if(last_time!=0){//如果有上次停止時間
past_time = (time-last_time);//算出這次生產時間
Serial.print("how long it takes to produce one spool=");
Serial.print(past_time);
Serial.println("sec");
last_time=time;//
//送計數數量到server
}
else{
last_time =time;
}
send_http_request(1,past_time);/*--傳送給server----- 目前狀態 生產時間-*/
Serial.println("finish and send to server status 1");
delay(5000); //停止2秒運算 確保機器完全停止
}
else if(digitalRead(flashPin)==HIGH){
flash_counter++;// 電眼計數
unsigned long flash_this =now();//取得這次電眼時間
unsigned long flash_period = flash_this-flash_last ;//上次到這次的時間
flash_total_period+= flash_period;
flash_average_time =flash_total_period/flash_counter;//電眼跳依次的平均時間
flash_last = flash_this;
Serial.print("flash light blink average time=");
Serial.println(flash_average_time);