2012-12-11 3 views
0

웹 호출 결과에 따라 원격 도어 오프너를 설정하는 데 도움이되는 스케치 작업 중입니다. WiServer를 실행하는 BlackWidow Arduino가 있는데 Wi-Fi가 정상적으로 작동하고 URL에서 결과를 얻을 수 있습니다. 나는 단순히 0 또는 1을 내용으로 반환 할뿐입니다.Arduino WiServer 릴레이 제어 - 스위치를 릴레이 할 수 없습니다.

문제는 루프 내에서 relayControlState가 항상 HIGH이고 릴레이를 끄거나 켤 수있는 루프를 얻을 수없는 것 같습니다.

단순한 "blinker"스케치를 사용하면 릴레이가 작동하도록 할 수 있습니다. 작동하지 않는 서버 가져 오기 코드와 얽혀있을 때만 가능합니다. 내가 뭘 놓치고 있니? 코드는 다음과 같습니다. 왜 relayControlState가 WiServer.getStatus 콜백 내에서 업데이트되지 않습니까? 릴레이가 전환하기에 충분한 주스를 얻지 못하고 있습니까?

#include <WiServer.h> 

    #define WIRELESS_MODE_INFRA 1 
    #define WIRELESS_MODE_ADHOC 2 

    // Wireless configuration parameters ---------------------------------------- 
    unsigned char local_ip[] = {192,168,1,10}; // IP address of WiShield 192.168.1.10 
    unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address 
    unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network 
    char ssid[]     = {"monitored"}; // max 32 bytes 

    unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 

    // WPA/WPA2 passphrase 
    const prog_char security_passphrase[] PROGMEM = {"password"}; // max 64 characters 

    // setup the wireless mode 
    // infrastructure - connect to AP 
    unsigned char wireless_mode = WIRELESS_MODE_INFRA; 
    unsigned char ssid_len; 
    unsigned char security_passphrase_len; 

    // IP Address for macpro.local 
    uint8 ip[] = {192,168,1,12}; 

    // End of wireless configuration parameters ---------------------------------------- 

    // A request that gets the aggregate status of the build system 
    GETrequest getStatus(ip, 80, "macpro.local", "/open-says-me/index.html"); 

    const int relayPin = 12; 
    int relayControlState = HIGH; 

    // Function that sets pin/light states 
    // BEWARE: THIS FUNCTION IS CALLED MULTIPLE (2) TIMES PER HTTP REQ 
    // Hidden call before/after call that returns payload 0, 1, 2, or null 
    void setRelayControlState(char* data, int len) { 

    // Serial.print("=========================\n\nLEN:\n"); 
    // Serial.print(len); 

     if(len > 0) { 

      Serial.print("\nDATA:"); 
      Serial.print(data[len - 1]); 
      Serial.print("\n"); 
    //  Serial.print("\n\nsetRelayControlState\n\n"); 

      if(data[len - 1] == '0') { 
      relayControlState = LOW; 
      Serial.print("SET LOW"); 
      } 

      if(data[len-1] == '1') { 
      relayControlState = HIGH; 
      Serial.print("SET HIGH"); 
      } 

     } else { 
      relayControlState = LOW; 

     } 

    } 

    void setup() { 

     pinMode(relayPin, OUTPUT); 
     Serial.begin(57600); 

     // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages) 
     WiServer.init(NULL); 

     // Enable Serial output and ask WiServer to generate log messages (optional) 

     WiServer.enableVerboseMode(true); 

     // Have the processData function called when data is returned by the server 
     getStatus.setReturnFunc(setRelayControlState); 
    } 

    // Time (in millis) when the data should be retrieved 
    long updateTime = 0; 
    void loop(){ 

     // Check if it's time to get an update 
     if (millis() >= updateTime) { 

     // Get another update 15s from now 
     updateTime += 1000 * 5; 

     getStatus.submit(); 

     } 

     // Run WiServer 
     WiServer.server_task(); 

     // turn on light pins based on stored vals 
     Serial.print("\nrelayControlState: "); 
     Serial.print(relayControlState); 
     Serial.print("\n"); 
     digitalWrite(relayPin, relayControlState); 

     delay(10); 

    } 
+0

WiServer 설정 코드의 어딘가에있는 것 같습니다. 만약 루프에서 간단한 클릭자를 켜고 끄고 WiServer에 대한 설정 만 추가하면 결코 루프에 도달하지 않습니다. 글쎄, 그것은 루프에 도착하고 serial.println을 출력하지만 핀은 절대로 설정되지 않습니다. – Brian

답변

0

이 코드는 결국 효과가 있었지만 BlackWidow에로드되는 코드의 동작이 일관성이 없었을 수도 있습니다. 나는 핀을 바꾸기 시작했다. 새로운 핀을 시도 할 때마다, arduino의 전원을 껐다 켜기 전까지 한 번만 작동했다. 재설정 또는 새 코드 업로드 대신 전원 끄기가 마음에 드는 것 같습니다. 여전히 약간 까다 롭지 만 특정 마지막 문자에 대해 URL을 폴링하는 실제 예입니다. 1이면 핀이 5.5 초 동안 하이로 설정됩니다. 0 일 경우 아무 것도하지 않습니다.

#include <WiServer.h> 

// --------------------------------------------------------------------------------- 
// Wireless configuration parameters 
// --------------------------------------------------------------------------------- 
unsigned char local_ip[] = {192,168,1,10}; // IP address of WiShield 192.168.1.10 
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address 
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network 
char ssid[]     = {"monitored"}; // max 32 bytes 

// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 
unsigned char security_type = 3;  

// WPA/WPA2 passphrase 
const prog_char security_passphrase[] PROGMEM = {"password"}; // max 64 characters 

// WEP 128-bit keys 
prog_uchar wep_keys[] PROGMEM = { 
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3 
}; 

// setup the wireless mode 
// infrastructure - connect to AP 
// adhoc - connect to another WiFi device 
#define WIRELESS_MODE_INFRA 1 
#define WIRELESS_MODE_ADHOC 2 
unsigned char wireless_mode = WIRELESS_MODE_INFRA; 
unsigned char ssid_len; 
unsigned char security_passphrase_len; 


// --------------------------------------------------------------------------------- 
// GET REQUEST 
// --------------------------------------------------------------------------------- 

// IP Address for macpro.local 
uint8 ip[] = {192,168,1,12}; 
// The request URL 
GETrequest getStatus(ip, 80, "macpro.local", "/open-says-me/index.html"); 

const int relayPin = 3; 
int relayControlState = LOW; 

// --------------------------------------------------------------------------------- 
// Callback for WiServer's getStatus 
// --------------------------------------------------------------------------------- 
void setRelayControlState(char* data, int len) { 

    Serial.print("[setRelayControlState] last digit of data: "); 
    Serial.println(data[len-1]); 

    Serial.print("[setRelayControlState] len: "); 
    Serial.println(len); 

    if(len > 0 
     && data[len-1] == '1') { 

     relayControlState = HIGH; 
     Serial.print("\nSET HIGH FOR 5.5s\n"); 

     digitalWrite(relayPin, HIGH); 
     delay(5500); 
     digitalWrite(relayPin, LOW); 

    } 

} 

void setup() { 

    pinMode(relayPin, OUTPUT); 
    Serial.begin(57600); 

    // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages) 
    WiServer.init(NULL); 

    // Enable Serial output and ask WiServer to generate log messages (optional) 

    WiServer.enableVerboseMode(true); 

    // Have the processData function called when data is returned by the server 
    getStatus.setReturnFunc(setRelayControlState); 

} 

// Time (in millis) when the data should be retrieved 
long updateTime = 0; 
void loop(){ 

    // Check if it's time to get an update 
    if (millis() >= updateTime) { 
     // Get another update 15s from now 
     updateTime += 1000 * 15; 
     getStatus.submit(); 
     Serial.print("end update @ ms "); 
     Serial.println(millis()); 
    } 

    WiServer.server_task(); 
    delay(100); 
} 
관련 문제