目的是用wifi远程控制继电器的开关,并且在wifi不稳定的时候继电器保持原位
下面是Arduino IDE的编码,基本工作没太大问题,现在有两个毛病
一个是控制继电器开启之后,大概保持5-10分钟就会自己关闭重启,怎么才能让继电器持续工作?
第二个是wifi自己断开的问题只是因为信号不稳定吗,还是有别的原因
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "ThingSpeak.h"
#include <EEPROM.h>
WiFiClient client;
const char* ssid = "MichaelY"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "11223344"; // The password of the Wi-Fi network
//const char* ssid = "325818fb7670b9a04"; // The SSID (name) of the Wi-Fi network you want to connect to
//const char* password = "provelookbeancultivator9"; // The password of the Wi-Fi network
unsigned long channelID=590710;
int RELAY=1;
int relaydata;
int RELAY2=2;
int relaydata2;
int connection=D0;
int store=0;
int y;
String API="9JIZOEYAQ2NLELQF"; //For thingspeak
const char* thinkspeakwebsite="api.thingspeak.com";
ESP8266WebServer server(80);
int relay=D1;
float analogpin=A0;
String value1,value2,value3;
void handleRoot() {
server.send(200, "text/plain", "Hello from your ESP8266!");
}
//----------------------------------------------------------------------
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";//
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
//*****************************************************************************************
void handleLEDon()
{
if(relaydata2==0)
{
digitalWrite(relay,HIGH);
Serial.println("**********RELAY ON*********");
}
else if(relaydata2=1)
{
}
server.send(200, "text/html", "Relay is ON");
}
//*****************************************************************************************
void handleLEDoff() {
if(relaydata2==0)
{
digitalWrite(relay,LOW);//relayoff
Serial.println("**********RELAY OFF*********");
}
else if(relaydata2=1)
{
}
server.send(200, "text/html", "Relay is OFF");
}
//*****************************************************************************************
void setup()
{
pinMode(relay,OUTPUT);
digitalWrite(relay,LOW);
pinMode(connection,OUTPUT);
digitalWrite(connection,HIGH);//the output is off when its high
pinMode(analogpin, INPUT);
pinMode(D5,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
Serial.begin(115200);
delay(10);
//connecting to a WiFi network
Serial.println("Connecting to "+String(ssid));
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("+");
}
Serial.println("");
Serial.println("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/relayOn", handleLEDon);
server.on("/relayOff", handleLEDoff);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
Serial.flush();//wait for outgoing data to be sent completely before moving to the next next
ThingSpeak.begin(client); //client needed for Thingspeak
}// end of setup
//*****************************************************************************************
void loop()
{
//sensor1
digitalWrite(D5,LOW);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
value1=analogRead(analogpin);
//current=value1
//sensor2
digitalWrite(D5,HIGH);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
value2=analogRead(analogpin);
//analogpin = String(analogRead(A0), DEC);// A0 is in decimal(DEC) format
if ((WiFi.status() == WL_CONNECTED))
{
digitalWrite(connection, LOW);
thingspeak("field1="+value1+"&field2="+value2);// sends value to field 1
relaydata2 = ThingSpeak.readFloatField(channelID,RELAY2);// get latest data from Thingspeak field2
relaydata = ThingSpeak.readFloatField(channelID,RELAY);// get latest data from Thingspeak field1
EEPROM.write(store,relaydata);
y=EEPROM.read(store);
Serial.print(y);
if(relaydata2==1)//helps to make sure that LED can be controlled via IP or Thingspeak
{
if(relaydata==1)
{
digitalWrite(relay,HIGH);
Serial.println("Relay is ON-");
}
else if(relaydata==0)
{
digitalWrite(relay,LOW);
Serial.println("Relay is OFF-");
}
}//relaydata2
}
else if (WiFi.status() != WL_CONNECTED)
{
digitalWrite(connection,HIGH);
wifireconnected();
}
server.handleClient();
}//loop
//*****************************************************************************************************
void wifireconnected()
{
WiFi.disconnect();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("+");
}
Serial.println("");
Serial.println("Wifi reconnected");
}
void thingspeak (String data)
{
int httpPORT=80;
if(client.connect(thinkspeakwebsite,httpPORT))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+API+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(data.length());
client.print("\n\n");
client.print(data);
delay(15);
Serial.print("Sensor 1 ");
Serial.println("value sent="+value1);
Serial.print("Sensor 2 ");
Serial.println("value sent="+value2);
Serial.println("values sent to thingspeak");
delay(1000);
}//end of client connect
else
{
delay(15);
Serial.println("fail connection to thinkspeak");
delay(2000);
}
}//voidthinkspeak
下面是Arduino IDE的编码,基本工作没太大问题,现在有两个毛病
一个是控制继电器开启之后,大概保持5-10分钟就会自己关闭重启,怎么才能让继电器持续工作?
第二个是wifi自己断开的问题只是因为信号不稳定吗,还是有别的原因
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "ThingSpeak.h"
#include <EEPROM.h>
WiFiClient client;
const char* ssid = "MichaelY"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "11223344"; // The password of the Wi-Fi network
//const char* ssid = "325818fb7670b9a04"; // The SSID (name) of the Wi-Fi network you want to connect to
//const char* password = "provelookbeancultivator9"; // The password of the Wi-Fi network
unsigned long channelID=590710;
int RELAY=1;
int relaydata;
int RELAY2=2;
int relaydata2;
int connection=D0;
int store=0;
int y;
String API="9JIZOEYAQ2NLELQF"; //For thingspeak
const char* thinkspeakwebsite="api.thingspeak.com";
ESP8266WebServer server(80);
int relay=D1;
float analogpin=A0;
String value1,value2,value3;
void handleRoot() {
server.send(200, "text/plain", "Hello from your ESP8266!");
}
//----------------------------------------------------------------------
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";//
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
//*****************************************************************************************
void handleLEDon()
{
if(relaydata2==0)
{
digitalWrite(relay,HIGH);
Serial.println("**********RELAY ON*********");
}
else if(relaydata2=1)
{
}
server.send(200, "text/html", "Relay is ON");
}
//*****************************************************************************************
void handleLEDoff() {
if(relaydata2==0)
{
digitalWrite(relay,LOW);//relayoff
Serial.println("**********RELAY OFF*********");
}
else if(relaydata2=1)
{
}
server.send(200, "text/html", "Relay is OFF");
}
//*****************************************************************************************
void setup()
{
pinMode(relay,OUTPUT);
digitalWrite(relay,LOW);
pinMode(connection,OUTPUT);
digitalWrite(connection,HIGH);//the output is off when its high
pinMode(analogpin, INPUT);
pinMode(D5,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
Serial.begin(115200);
delay(10);
//connecting to a WiFi network
Serial.println("Connecting to "+String(ssid));
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("+");
}
Serial.println("");
Serial.println("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/relayOn", handleLEDon);
server.on("/relayOff", handleLEDoff);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
Serial.flush();//wait for outgoing data to be sent completely before moving to the next next
ThingSpeak.begin(client); //client needed for Thingspeak
}// end of setup
//*****************************************************************************************
void loop()
{
//sensor1
digitalWrite(D5,LOW);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
value1=analogRead(analogpin);
//current=value1
//sensor2
digitalWrite(D5,HIGH);
digitalWrite(D6,LOW);
digitalWrite(D7,LOW);
value2=analogRead(analogpin);
//analogpin = String(analogRead(A0), DEC);// A0 is in decimal(DEC) format
if ((WiFi.status() == WL_CONNECTED))
{
digitalWrite(connection, LOW);
thingspeak("field1="+value1+"&field2="+value2);// sends value to field 1
relaydata2 = ThingSpeak.readFloatField(channelID,RELAY2);// get latest data from Thingspeak field2
relaydata = ThingSpeak.readFloatField(channelID,RELAY);// get latest data from Thingspeak field1
EEPROM.write(store,relaydata);
y=EEPROM.read(store);
Serial.print(y);
if(relaydata2==1)//helps to make sure that LED can be controlled via IP or Thingspeak
{
if(relaydata==1)
{
digitalWrite(relay,HIGH);
Serial.println("Relay is ON-");
}
else if(relaydata==0)
{
digitalWrite(relay,LOW);
Serial.println("Relay is OFF-");
}
}//relaydata2
}
else if (WiFi.status() != WL_CONNECTED)
{
digitalWrite(connection,HIGH);
wifireconnected();
}
server.handleClient();
}//loop
//*****************************************************************************************************
void wifireconnected()
{
WiFi.disconnect();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("+");
}
Serial.println("");
Serial.println("Wifi reconnected");
}
void thingspeak (String data)
{
int httpPORT=80;
if(client.connect(thinkspeakwebsite,httpPORT))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+API+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(data.length());
client.print("\n\n");
client.print(data);
delay(15);
Serial.print("Sensor 1 ");
Serial.println("value sent="+value1);
Serial.print("Sensor 2 ");
Serial.println("value sent="+value2);
Serial.println("values sent to thingspeak");
delay(1000);
}//end of client connect
else
{
delay(15);
Serial.println("fail connection to thinkspeak");
delay(2000);
}
}//voidthinkspeak