gpt4 book ai didi

c - Arduino WiServer 继电器控制 - 无法让继电器切换

转载 作者:行者123 更新时间:2023-11-30 17:56:05 25 4
gpt4 key购买 nike

我正在制作一个草图,以根据网络调用的结果来帮助设置远程开门器。我有一个运行 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);

}

最佳答案

这是最终有效的代码,但它也可能只是加载到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com