我正在根据网络电话的结果绘制草图,以帮助设置远程开门器。我有一个运行WiServer的BlackWidow Arduino,并且wifi正常,可以从URL中得到结果。我只是返回0或1作为内容。

问题是在我的循环中relayControlState始终为HIGH,而我似乎无法获得使继电器关闭/打开的循环。

当我只使用一个简单的“眨眼”草图时,我就可以使中继正常工作,只有当中继与服务器获取代码交织在一起时,中继才起作用。我想念什么?代码如下。为什么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);

    }

最佳答案

这是最终可以工作的代码,但也可能只是代码加载到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);
}

关于c - Arduino WiServer中继控制-无法获取中继进行切换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13816590/

10-09 13:31