我正在制作草图,根据网络电话的结果帮助设置远程开门器。我有一个运行WiServer的BlackWidow Arduino,wifi工作正常,我可以从我的URL获得结果。我只是返回0或1作为内容。
问题出在我的循环中,relayControlState总是为高电平,我似乎无法通过循环使继电器关闭/打开。
当我只使用一个简单的“闪烁”草图时,我可以让继电器正常工作,只有当它与我的服务器获取代码交织在一起才能正常工作时。我错过了什么?代码如下。为什么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 :(得分: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);
}