Hi Coopermaa 大大你好: 
那個應該是叫 USB 2 Serial Light.  
負責arduino Ethernet 接電腦的板子 
 
回復一下: 
我arduino送出http request , server端接收到 server會有log出來(我是用java servlet) 
 
問題在於我只要接電腦 Arduino會乖乖地送出訊號給server 
但是如果不接電腦 只接網路線 arduino就很像是斷了線 送不出request 
實在想不出到底是甚麼原因 
 
以下是arduino的code 
#include <SPI.h> 
#include <Ethernet.h> 
#include <Time.h> 
byte mac[] ={ 
  0x90,0xA2,0xDA,0x00,0xEE,0x50}; 
byte ip[]={192,168,2,101}; 
byte gateway[]={192,168,2,1}; 
byte subnet[]={255,255,255,0}; 
IPAddress  server(192,168,2,100); 
EthernetClient client; 
EthernetServer self_server = EthernetServer(8888);//telnet port 23 
 
boolean init_status = false;//初始化 
 
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 setup(){ 
  pinMode(counterPin,INPUT); 
  pinMode(flashPin,INPUT); 
  pinMode(resetPin,INPUT); 
  Serial.begin(9600); 
   
  //網路初始化 
  Ethernet.begin(mac,ip,gateway,subnet); 
  //server init 
  self_server.begin(); 
  Serial.println("self server begin......"); 
   
  //send_http_request(6,0);//告訴server counter 虛設為0 
  if(client.connect(server,8080)){ 
    Serial.println("connected"); 
    client.print("GET /MachineRecorder/counter_receive.do"); 
    client.print("?machine_id=F01"); 
    client.print("&action=6");// init number 6 
    client.print("&time_info=0");//now is zero 
    client.print("&counter=0");//counter init is zero 
    client.print("&source=arduino");//source = arudio request 
    client.print("&ip=192.168.2.101");//ip address F01 
    client.println(" HTTP/1.0");     
    client.println(""); 
    client.stop(); 
    delay(1000); 
    Serial.println("message sent to server"); 
  }else{ 
    Serial.println("connection fail"); 
  } 
 
 
 
} 
boolean last_step=false; 
 
 
/*把計數器資料送給主機*/ 
 
void send_http_request(int action,int time){ 
  if(client.connect(server,8080)){ 
    Serial.println("connected"); 
    client.print("GET /MachineRecorder/counter_receive.do"); 
    client.print("?machine_id=F01"); 
    client.print("&action="); 
    client.print(action);//生產時間(秒) 
    client.print("&time_info="); 
    client.print(time);//拔取時間(秒) 
    client.print("&counter="); 
    client.print(counter);//拔取時間(秒) 
    client.print("&source=arduino"); 
    client.println(" HTTP/1.0");     
    client.println(""); 
    client.stop(); 
    Serial.println("message sent to server"); 
  }else{ 
    Serial.println("connection fail"); 
  } 
 
} 
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); 
           
        } 
        last_message = time;// 
      }else{ 
      counter =0;//代表server 端有更改規格 需要把計數器歸零 
      Serial.println("received from Servlet"); 
      message=false; 
      } 
  } 
   
  //每完成一粒 自動累加 計數模式 
  //再啟動 
   if(digitalRead(counterPin)==HIGH){ 
    counter++;//數量+1 
     
    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); 
 
    if(flash_counter==1){//如果起始計數為1 表示計數器啟動      
      status3_sent=false; 
      unsigned long take_off_period=flash_this-last_time;//得到拿下時間(毫秒) 
      if(status4_sent==true&&take_off_period>600){//如果是人員離開 而且離開超過10分鐘 
         //平均拔取時間+總區間 
         take_off_total_period +=average_take_off_time; 
          
      }else{ 
        take_off_total_period+=take_off_period; 
      } 
      status4_sent=false; 
      average_take_off_time = (take_off_total_period)/counter; //算出平均拿下時間 
       
      flash_total_period=0;//電眼跳的總區間 
      flash_average_time=0;//電眼跳的平均時間歸零 
      last_time = now();//counter start 
      Serial.print("average take off time="); 
      Serial.println(average_take_off_time); 
      send_http_request(2,take_off_period);/*------更改狀態為2開始啟動 送出拿下時間----目前狀態 拿取時間*/ 
      Serial.print("status 2 machine start running:.........take off period:"); 
      Serial.println(take_off_period); 
      //送起始時間 跟終止 時間   
    } 
 
    Serial.print("flash counter=");  
    Serial.println(flash_counter); 
    delay(300);//在0.3秒內 不接受任何電眼訊號 
 
  } 
  else if(digitalRead(flashPin)==LOW){//電眼在休息狀態 
    unsigned long flash_waiting = now(); 
    if(flash_counter==0){//停機狀態 
      if((flash_waiting - flash_last)>average_take_off_time){//如果等待時間 比平均拿取時間長 
        if(!status4_sent){ 
        send_http_request(4,flash_waiting);/*------更改狀態為4等待拿取----目前狀態 停機開始時間*/ 
        status4_sent = true; 
        Serial.println("status 4 machine stop and waiting...................");  
        } 
      } 
    } 
    else{//flash counter >0 計數器 運轉狀態 做到一半停下來 
      unsigned long waiting_time =flash_waiting - flash_last ; 
      unsigned long buffer = flash_average_time + line_break_sec;//flash如果沒有閃 超過5秒 
      if(waiting_time >buffer){ 
        if(!status3_sent){ 
          status3_sent = true; 
          unsigned long break_time=now(); 
          wire_break_start=flash_waiting; 
          send_http_request(3,break_time);/*--更改狀態為3斷線 送回 等待處理時間 */ 
          Serial.println("status 3 machine hold.....wire break or adjusting..........");  
        } 
      } 
       
      if(digitalRead(resetPin)==HIGH){//作業員按下歸零 表示斷線排除完畢 
         flash_counter=0;//感應 歸零 
         Serial.println("restPin  activate"); 
         status3_sent=false; 
         unsigned long waiting = now(); 
         unsigned long break_finish = waiting - wire_break_start;//得到總共斷線時間 
         send_http_request(5,break_finish);/*--送出重新啟動訊號-  更改狀態五 重新啟動  回傳斷線處理時間--*/ 
         Serial.println("status 5 operator rest counter and restart machine.........");  
      }  
    } 
  } 
 
 
} 
// 當電被切掉時...自動歸0 |