First of all, I apologize for my poor English skills.
I might write a terrible sentence even with a translator.
I made a remote for the computer program using Wi-Fi module on MKR1000.
It works very well at first, but after few days or weeks, the pin I set as an Input_pullup's voltage keep it as low.
Because of that, the Board endlessly send 'button pressed' state to the computer.
Here is my schematic and code. What I do wrong here?
#include <SPI.h>
#include <WiFi101.h>
#include "config.h"
const int nodeNo = 1;
const long LoopLampinterval = 500;
const long CallButton_WD_Wait_Lampinterval = 300;
const long CanelButton_WD_Wait_Lampinterval = 300;
const long Msg_Send_Recv_Waitinterval = 30000;
const long WiFiResetinterval = 60000;
const long Button_Debounceinterval = 150;
//////// I worte these variables at "config.h".
//////// But I wrote here for easier to read.
char ssid[] = "ssid";
char pass[] = "password";
const char STX = (char)0x02;
const char ETX = (char)0x03;
String sSendNodeNo = "";
String sACS_Connect_Status_Value_Data = "";
String sBlue_Lamp_State = "0";
String sGreen_Lamp_State = "0";
String sSend_Call_ButtonState = "0";
String sSend_Canel_ButtonState = "0";
String sRecv_CallStatus = "0";
String sRecv_CanelStatus = "0";
String sCallButton_RecvWait_Lamp = "0";
String sCanelButton_RecvWait_Lamp = "0";
String sSend_Canel_ButtonState_Wait = "0";
unsigned long BlueLampMillis = 0;
unsigned long iWiFi_Reset_Millis = 0;
unsigned long CallButton_RecvWait_Millis = 0;
unsigned long CanelButton_RecvWait_Millis = 0;
unsigned long Msg_Send_RecvWait_Millis = 0;
unsigned long CallButton_Debounce_Millis = 0;
unsigned long CanelButton_Debounce_Millis = 0;
int iACS_Comm_Step = 0;
int iACS_Read_Retry = 0;
int Last_Call_ButtonState = 1;
int Last_Canel_ButtonState = 1;
int CallPin = 0;
int CancelPin = 1;
WiFiServer server(5555);
bool alreadyConnected = false;
void setup()
{
pinMode(CallPin, INPUT_PULLUP); //CallButton
pinMode(CancelPin, INPUT_PULLUP); //CanelButton
pinMode(4, OUTPUT); //R
pinMode(5, OUTPUT); //G
pinMode(6, OUTPUT); //B
pinMode(7, OUTPUT); //CALL
pinMode(8, OUTPUT); //CANCEL
WiFiJoin();
server.begin();
}
void loop()
{
WiFiJoin();
Blue_Lamp_Control();
Msg_Send_Recv_ModuleReset();
Call_Button();
Canel_Button();
CallButton_Lamp_Control();
CanelButton_Lamp_Control();
WiFiClient client = server.available();
if (client)
{
if (!alreadyConnected)
{
client.flush();
alreadyConnected = true;
}
if (client.available() > 0)
{
Serial.println("client connected");
Init();
if (ACS_Recv_Response(&client))
{
ErrorHandle();
Serial.println("send to server");
String SendData = "";
SendData += STX;
SendData += sSendNodeNo;
SendData += "RD";
SendData += sSend_Call_ButtonState;
if(sSend_Canel_ButtonState_Wait=="0")
SendData += sSend_Canel_ButtonState;
else SendData += "0";
SendData += ETX;
client.print(SendData);
client.stop();
Update_Green_Lamp("0");
Serial.println("client.stop");
Serial.print("TX:");
Serial.println(SendData);
Msg_Send_RecvWait_Millis = millis();
}
}
}
}
bool ACS_Recv_Response(WiFiClient* p_clinet)
{
bool data_receive_complete = false;
while (p_clinet->available())
{
Update_Green_Lamp("1");
char c = p_clinet->read();
sACS_Connect_Status_Value_Data += c;
Serial.write(c);
if (c == ETX) data_receive_complete = true;
}
if (data_receive_complete)
{
Serial.println("");
Serial.print(">>:");
Serial.println(sACS_Connect_Status_Value_Data);
Serial.println("transfer succes");
String ACS_Connect_State = ACS_Connect_json_parser(sACS_Connect_Status_Value_Data);
Serial.print(F("ACS_Connect_State:"));
Serial.println(ACS_Connect_State);
return true;
}
return false;
}
String ACS_Connect_json_parser(String s)
{
String val;
val = s.substring(1, 9);
sSendNodeNo = val.substring(0, 2);
Serial.println(sSendNodeNo);
sRecv_CallStatus = val.charAt(4);
Button_Reset(sRecv_CallStatus);
sRecv_CanelStatus = val.charAt(5);
//Button_Reset(sRecv_CanelStatus);
Msg_Send_RecvWait_Millis = millis();
return val;
}
void WiFiJoin()
{
if (WiFi.status() == WL_NO_SHIELD)
{
while (true) ;
}
while (WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to ");
Serial.println(ssid);
Update_Green_Lamp("0");
Update_Blue_Lamp("0");
unsigned long ResetMillis = millis();
if (ResetMillis - iWiFi_Reset_Millis >= WiFiResetinterval)
{
iWiFi_Reset_Millis = ResetMillis;
NVIC_SystemReset();
}
Update_Rad_Lamp("1");
delay(500);
IPAddress ip = IPAddress(192, 168, 1, 100 + nodeNo);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, subnet);
WiFi.begin(ssid, pass);
Update_Rad_Lamp("0");
delay(500);
Serial.println("WiFi connected.");
printWiFiStatus();
}
}
void printWiFiStatus()
{
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address:");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength(RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
Serial.print("millis: ");
Serial.println(millis());
Serial.print("Msg_Send_RecvWait_Millis: ");
Serial.println(Msg_Send_RecvWait_Millis);
}
void Blue_Lamp_Control()
{
if (millis() - BlueLampMillis >= LoopLampinterval)
{
BlueLampMillis = millis();
if (sBlue_Lamp_State.equals("0")) sBlue_Lamp_State = "1";
else sBlue_Lamp_State = "0";
}
Update_Blue_Lamp(sBlue_Lamp_State);
}
void CallButton_Lamp_Control()
{
if (sRecv_CallStatus == "2" || sSend_Call_ButtonState == "1")
{
sSend_Call_ButtonState = "1";
if (millis() - CallButton_RecvWait_Millis >= CallButton_WD_Wait_Lampinterval)
{
CallButton_RecvWait_Millis = millis();
if (sRecv_CallStatus == "1")
{
sCallButton_RecvWait_Lamp = "1";
}
else
{
if (sCallButton_RecvWait_Lamp == "0")
sCallButton_RecvWait_Lamp = "1";
else
sCallButton_RecvWait_Lamp = "0";
}
}
Update_Button1_Lamp(sCallButton_RecvWait_Lamp);
}
else Update_Button1_Lamp(sRecv_CallStatus);
}
void CanelButton_Lamp_Control()
{
//if (sRecv_CanelStatus == "0" && sSend_Canel_ButtonState == "1")
if (sRecv_CanelStatus == "2" || sSend_Canel_ButtonState == "1")
{
if (millis() - CanelButton_RecvWait_Millis >= CanelButton_WD_Wait_Lampinterval)
{
CanelButton_RecvWait_Millis = millis();
if (sCanelButton_RecvWait_Lamp == "0") sCanelButton_RecvWait_Lamp = "1";
else sCanelButton_RecvWait_Lamp = "0";
}
Update_Button2_Lamp(sCanelButton_RecvWait_Lamp);
}
else Update_Button2_Lamp(sRecv_CanelStatus);
}
void Msg_Send_Recv_ModuleReset()
{
if (Msg_Send_RecvWait_Millis > 0)
{
if (millis() - Msg_Send_RecvWait_Millis >= Msg_Send_Recv_Waitinterval)
{
//Serial.print("Reset: ");
//Serial.println("Reset");
NVIC_SystemReset();
}
}
}
void Update_Rad_Lamp(String flag)
{
if (flag.equals("1"))
digitalWrite(4, HIGH);
else
digitalWrite(4, LOW);
}
void Update_Green_Lamp(String flag)
{
if (flag.equals("1"))
digitalWrite(5, HIGH);
else
digitalWrite(5, LOW);
}
void Update_Blue_Lamp(String flag)
{
if (flag.equals("1"))
digitalWrite(6, HIGH);
else
digitalWrite(6, LOW);
}
void Update_Button1_Lamp(String flag)
{
if (flag.equals("1"))
digitalWrite(7, HIGH);
else
digitalWrite(7, LOW);
}
void Update_Button2_Lamp(String flag)
{
if (flag.equals("1"))
digitalWrite(8, HIGH);
else
digitalWrite(8, LOW);
}
void Call_Button()
{
int reading = digitalRead(CallPin);
if(reading != Last_Call_ButtonState)
{
CallButton_Debounce_Millis = millis();
}
if (millis() - CallButton_Debounce_Millis > Button_Debounceinterval && reading == LOW)
{
sSend_Call_ButtonState = "1";
}
Last_Call_ButtonState = reading;
}
void Canel_Button()
{
int reading = digitalRead(CancelPin);
if(reading != Last_Canel_ButtonState)
{
CanelButton_Debounce_Millis = millis();
}
if (millis() - CanelButton_Debounce_Millis > Button_Debounceinterval)
{
if (sRecv_CallStatus == "2"||(sRecv_CallStatus == "0" && sSend_Call_ButtonState == "1"))
{
if (reading == LOW) sSend_Canel_ButtonState = "1";
}
}
Last_Canel_ButtonState = reading;
}
void Button_Reset(String Recv_ButtonStatus)
{
if (Recv_ButtonStatus.equals("3"))
{
sSend_Call_ButtonState = "0";
sSend_Canel_ButtonState = "0";
}
}
void ErrorHandle()
{
if(sSend_Canel_ButtonState_Wait=="1")
{
sSend_Canel_ButtonState_Wait = "0";
}
if(sSend_Call_ButtonState == "1" && sSend_Canel_ButtonState == "1" && sRecv_CallStatus == "0" && sRecv_CanelStatus == "0")
{
sSend_Canel_ButtonState_Wait = "1";
}
}
void Init()
{
sACS_Connect_Status_Value_Data = "";
}