[問題] 硬體中斷頻繁,無法順利網路連線

看板ASM作者 (冒險傳人)時間5年前 (2018/06/09 09:44), 5年前編輯推噓0(002)
留言2則, 2人參與, 5年前最新討論串1/1
  網路上的各位先進您好:   我試著用 Arduino UNO R3 來當計數器,當一個信號進來時, 他在板子上的 5V 與 D13 形成通路,這樣子一來,我可以藉由 板子上的 LED 燈來判斷是否有信號過來。   我想請教的是:當進來的頻率不高時,他能夠順利建立網路連線, 把值拋給 Server 上的資料庫,以利我用網頁來監控。可是當我的頻率 高到約略一秒十次左右時,他的網路連線建立就被迫中斷,也因為這樣子, 我無法把值即時拋給 Server,也就無法達到當初要即時監的目的。 請問,我該如何來進行,才能避掉這個問題呢? 還望您能撥冗不吝告知,謝謝您。 程式與資料庫是參加崑山科大的產業人才投資計中畫的 物聯網智慧之魚菜共生遠端控制系統的內容而來。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.125.235.67 ※ 文章網址: https://www.ptt.cc/bbs/ASM/M.1528508655.A.63C.html

06/09 16:42, 5年前 , 1F
會不會是你中斷的code跑太久? 問這類的問題最好附上完整程
06/09 16:42, 1F
承蒙回答,不勝感激。 這邊是我的程式碼,因為功能簡單,相對應的程式也很簡單 #include <SPI.h> #include <Ethernet.h> byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; IPAddress ip(192,168,101,101); EthernetServer server(80); EthernetClient client; char SFDserver[] = "192.168.0.81"; unsigned long lastConnectionTime = 0; boolean lastConnected = false; const unsigned long postingInterval = 5*1000; const int SFD05 = 13; static unsigned long countValue = 0; // 計數器數值 static unsigned long lastCountValue = 0; // 上次計數器數值 unsigned long lastCountTime = 0; // 計數器數時間 int SFD05Status=0; int SFD05LastStatus=0; boolean realCount = false; String sSFD05Count; const String sSFDNo="F27B_A"; void setup() { // put your setup code here, to run once: Serial.begin(9600); delay(1000); Ethernet.begin(mac,ip); server.begin(); Serial.print("My IP Address: "); Serial.println(Ethernet.localIP()); } void loop() { if(client.available()) { char c = client.read(); Serial.write(c); } // 失去連線 if(!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting"); client.stop(); } // 當目前沒有連線且距離上次連線已有 5 秒鐘,重新連線 if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { if (countValue != lastCountValue) { httpRequest(); lastCountValue = countValue; } } lastConnected = client.connected(); countRequest(); } void httpRequest() { if(client.connect(SFDserver,80)) { Serial.println("connecting..."); //請求連線標頭檔 client.print("GET /SFD/u1.php?type=sensor&"+sSFDNo+"="); client.print(countValue); client.println(" HTTP/1.1"); client.println("Host:192.168.0.190"); client.println("User-Agent:arduino-ethernet"); client.println("Connection:close"); client.println(); lastConnectionTime = millis(); } //http://192.168.0.81/SFD/u1.php?type=sensor&F27B_A=4489 else { Serial.println("connection failed"); Serial.println("disconnecting"); client.stop(); } } void countRequest() { SFD05Status = digitalRead(SFD05); if((SFD05Status == 0) && (SFD05LastStatus == 1) ) { countValue++; /*Serial.print("SFD05 Count Value: "); Serial.println(countValue); //lastCountTime= millis(); */ } SFD05LastStatus = SFD05Status; delay(1); } 在輸入不頻繁的情況下,可以正常運作, 可惜在較頻繁狀態下,網路就無法順利對外作溝通了。 麻煩您了,感恩。 ※ 編輯: sinzen (59.125.235.67), 06/10/2018 21:01:01

08/06 06:25, 5年前 , 2F
解開了嗎?如尚未站內信
08/06 06:25, 2F
  感恩您的回覆,後來我把問題請我同事處理。 他有解決了,雖然不清楚是怎麼回事。   謝謝您。 ※ 編輯: sinzen (59.125.235.67), 08/27/2018 16:56:57
文章代碼(AID): #1R6p3lOy (ASM)