我非常渴望完成这个项目,并且我确实在寻找解决方案以解决该问题,但是似乎没有任何效果。
我正在尝试将Nodemcu连接到中继,以便由Amazon alexa控制。但是,在上传以下代码时,我也遇到了同样的问题,继电器会发出一些奇怪的闪烁,然后给出一个错误。
连接:
Vin> VCC
GND> GND
GPIO14> IN1
GPIO15> IN2
GPIO3> IN3
GPIO1> IN4
这是我使用的代码:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
#include <Wire.h>
// prototypes
boolean connectWifi();
//on/off callbacks
void lightOneOn();
void lightOneOff();
void lightTwoOn();
void lightTwoOff();
void outletOneOn();
void outletOneOff();
void outletTwoOn();
void outletTwoOff();
const char* ssid = "";
const char* password = "";
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *lightOne = NULL;
Switch *lightTwo = NULL;
Switch *outletOne = NULL;
Switch *outletTwo = NULL;
// Set Relay Pins
int relayOne = 14;
int relayTwo = 15;
int relayThree = 03;
int relayFour = 01;
void setup()
{
//Serial.begin(115200);
// Initialise wifi connection
wifiConnected = connectWifi();
//Serial.print("WiFi Connected");
if (wifiConnected) {
upnpBroadcastResponder.beginUdpMulticast();
// Define your switches here. Max 14
// Format: Alexa invocation name, local port no, on callback, off callback
lightOne = new Switch("Light One", 80, lightOneOn, lightOneOff);
lightTwo = new Switch("Light Two", 81, lightTwoOn, lightTwoOff);
outletOne = new Switch("Outlet One", 82, outletOneOn, outletOneOff);
outletTwo = new Switch("Outlet Two", 83, outletTwoOn, outletTwoOff);
//Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*lightOne);
upnpBroadcastResponder.addDevice(*lightTwo);
upnpBroadcastResponder.addDevice(*outletOne);
upnpBroadcastResponder.addDevice(*outletTwo);
//Set relay pins to outputs
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(03, OUTPUT);
pinMode(01, OUTPUT);
}
}
void loop()
{
if (wifiConnected) {
upnpBroadcastResponder.serverLoop();
lightOne->serverLoop();
lightTwo->serverLoop();
outletOne->serverLoop();
outletTwo->serverLoop();
}
}
void lightOneOn() {
// Serial.print("Switch 1 turn on ...");
digitalWrite(relayOne, LOW); // sets relayOne on
}
void lightOneOff() {
// Serial.print("Switch 1 turn off ...");
digitalWrite(relayOne, HIGH); // sets relayOne off
}
void lightTwoOn() {
// Serial.print("Switch 2 turn on ...");
digitalWrite(relayThree, LOW); // sets relayTwo on
}
void lightTwoOff() {
// Serial.print("Switch 2 turn off ...");
digitalWrite(relayThree, HIGH); // sets relayTwo Off
}
//sockets
void outletOneOn() {
// Serial.print("Socket 1 turn on ...");
digitalWrite(relayFour, LOW); // sets relayThree on
}
void outletOneOff() {
// Serial.print("Socket 1 turn off ...");
digitalWrite(relayFour, HIGH); // sets relayThree off
}
void outletTwoOn() {
// Serial.print("Socket 2 turn on ...");
digitalWrite(relayTwo, LOW); // sets relayFour on
}
void outletTwoOff() {
// Serial.print("Socket 2 turn off ...");
digitalWrite(relayTwo, HIGH); // sets relayFour off
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi() {
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
// Serial.print("Connecting ...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.print(".");
if (i > 10) {
state = false;
break;
}
i++;
}
if (state) {
// Serial.println("");
// Serial.print("Connected to ");
// Serial.println(ssid);
// Serial.print("IP address: ");
// Serial.println(WiFi.localIP());
}
else {
// Serial.println("");
//Serial.println("Connection failed.");
}
return state;
}
出现的错误代码如下:
esptool v0.4.13 - (c) 2014 Ch. Klippel <ck@atelier-klippel.de>
setting board to nodemcu
setting baudrate from 115200 to 115200
setting port from COM1 to COM3
setting address from 0x00000000 to 0x00000000
espcomm_upload_file
espcomm_upload_mem
setting serial port timeouts to 1000 ms
opening bootloader
resetting board
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
resetting board
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
resetting board
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed