2016-09-25 26 views
0

我在esp8266 201上遇到了一些問題。我用一個(足夠多的)電源爲它供電,它不斷重新啓動。esp8266 restarting.maybe needs yield

即使esp8266有時連接到互聯網並且Cayenne發現它,它也會再次脫機並聯機。有些人告訴我使用收益率,但我不知道如何使用它們。

下面的代碼用於連接到Cayenne並添加一些傳感器。這發生在收益率之前。

#include <SoftwareSerial.h> //SoftwareSerial library required for Arduino communication with the Grove RFID reader and the Grove Serial MP3 player 
#include <BlynkSimpleEsp8266.h> 
#include "CayenneDefines.h" 
#include "CayenneWiFiClient.h" 

char token[] = "";  //Cayenne authentication token. This should be obtained from the Cayenne Dashboard. 
// Your network name and password. 
char ssid[] = ""; 
char password[] = "";    

SoftwareSerial mp3(2,3);         //Grove Serial MP3 Player is connected to Digital Pins 2 and 3 
SoftwareSerial rfid(10,11);        //Grove 125kHz RFID reader is connected to Digital Pins 10 and 11 (Note: lower pins may not work with this board) 

//Global Variables 
int rfidValue = 0;          //rfidValue: used to store the value obtained from the RFID tag 
int vPin6 = 0;           //vPin6: is associated with Virtual Pin 6 on the Cayenne Dashboard - activated when a person is detected by the sensors. 
int vPin7 = 0;           //vPin7: is associated with Virtual Pin 7 on the Cayenne Dashboard - activated when the person fails to identify themselves. 
unsigned long start_Time;         //start_Time: is used in various time related events (logs the start time) 
unsigned long stop_Time;         //stop_Time: is used in various time related events (logs the stop time) 
unsigned long time_Diff = 0;        //time_Diff: is used in various time related events (is used to record the time elapsed from  start time to stop time) 
boolean keyMessagePlayed=false;       //keyMessagePlayed: helps to prevent a message from replaying repeatedly when a person is detected. 
boolean waitingForKey = true;        //waitingForKey: is used to identify if we are still waiting for a person to present a valid key or not 
int songNumber = -1;          //songNumber is a variable used to hold the song number to play (from the playlist on the SD card on the MP3 player) 

/*===============================setup()============================================================================================================================= */ 
void setup(){ 
    delay(2500);           //Delay for 2.5 seconds to allow the Grove MP3 player to initialise 
    mp3.begin(9600);          //Establish a communication link with the Grove MP3 player 
    rfid.begin(9600);          //Establish a communication link with the Grove 125 kHz RFID reader 
    Serial.begin(9600);          //Establish a communication link with my computer (via the Serial monitor) - for debugging purposes. 
    Cayenne.begin(token, ssid, password);          //Establish a communication link with the Cayenne Server - the token must match that in the Cayenne Dashboard 
    setVolume(28);           //Set the volume of the MP3 player to 28 (Range = 0 to 31, whereby 31 is maximum volume) 
    setPlayMode(0x00);          //Configure the MP3 player to play the MP3 file ONCE only (per request). 
} 

/*===============================loop()============================================================================================================================= */ 
void loop(){ 
    Cayenne.run();           //Synchronise with the Cayenne Server 

    if(vPin6&&!vPin7){          //Only progress if Virtual Pin 6 (Person detection) is ACTIVATED but ONLY when Virtual Pin 7 (Intruder alert) has NOT already been activated. 
    if(!keyMessagePlayed){        //This ensures that the message is played only ONCE when a person has been detected. 
    playSong(1);          //Play the following message on the Grove MP3 player: "Place your keys on the mat" 
     keyMessagePlayed=true;        //keyMessagePlayed is changed to TRUE once the message has been played. 
    } 

     /*This section requests the Arduino to look out for the person's keys, and returns TRUE when a successful identification is made. 
    * It returns FALSE if the person fails to put their keys on the Mat within 10 seconds , OR if the person cannot be identified by the Arduino (e.g. Wrong code) 
    * If a person has been successfully identified/verified, it will play a welcome message, and switch off the "Person Detection Trigger", which will then 
    * be used as a method to switch off the Security scanning process. 
    * If a person fails to be identified within 10 seconds, the person is notified that they have been detected, and an Alarm will sound. 
    * Virtual Pin 7 (Intruder Alert) will be ACTIVATED - which will be used as a Trigger within Cayenne to notify me of an Intruder (via Email or SMS). 
    */ 
if(listenForKeys()){         
    //TRUE = Person Identified within 10 seconds 
    playSong(songNumber);        //Play message "Welcome Home - Scott" 
    vPin6 = 0;           //Deactivate the "Person Detection" virtual pin (6). 
    keyMessagePlayed=false;        //Reset the keyMessagePlayed variable for future detections. 
} else { 
    //FALSE = Person not identified within 10 seconds 
    playSong(2);          //Play message on Grove MP3 player : "Your presence has been detected" 
    delay(4000);          //A FOUR second delay is required to allow the message to play, before the alarm sounds. 
    playSong(3);          //Sound the ALARM by playing song 2 on the Grove MP3 player. Song numbers are determined by the order they have been written to the SD card. 
    delay(8000);          //An EIGHT second delay allows the alarm to sound for 8 seconds. 
    playSong(99);          //Playing a non-existing track essentially STOPS the MP3 player. 
    vPin7=1;           //ACTIVATE Virtual Pin 7 - Intruder Detected 
    keyMessagePlayed=false;        //Reset the keyMessagePlayed variable for future detections. 
} 

upDateCayenne();          //Update the Cayenne Dashboard with any changes made to the virtual pins (6 or 7). This method can be found below. 
    } 
}  


/*=writeToMP3 function====================================================================================================================================================== 
    * is used to simplify the process of sending commands to the Grove MP3   */ 
void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){ 
    byte codeMsg[] = {MsgLEN, A,B,C,D,E,F}; 
    mp3.write(0x7E);          //Start Code for every command = 0x7E 
    for(byte i = 0; i<MsgLEN+1; i++){ 
    mp3.write(codeMsg[i]);        //Send the rest of the command to the GROVE MP3 player 
    } 
} 

/*=setVolume function====================================================================================================================================================== 
    * is used to simplify the process of setting the Volume on the Grove MP3 player 
    * Volume range = 00 (muted) to 31 (max volume) 
    */ 
void setVolume(byte Volume){             
    byte tempVol = constrain(Volume, 0, 31);    //Ensure the Volume does not extend beyond the MP3 player's limits 
    writeToMP3(0x03, 0xA7, tempVol, 0x7E, 0x00, 0x00, 0x00); 
} 

/*=setPlayMode function====================================================================================================================================================== 
    * is used to simplify the process of setting up the play Mode on the Grove MP3 player 
    * playMode options: 
     *  0x00 = Single song - played only once ie. not repeated. (default) 
    *  0x01 = Single song - cycled ie. repeats over and over. 
    *  0x02 = All songs - cycled 
    *  0x03 = play songs randomly 
    */ 
void setPlayMode(byte playMode){  
    writeToMP3(0x03, 0xA9, playMode, 0x7E, 0x00, 0x00, 0x00); 
} 

/*=playSong function====================================================================================================================================================== 
    * is used to simplify the process of playing a specific track on the SD card of the Grove MP3 player. 
    * The track number is determined by the order in which the songs were written to the SD card. 
    * Best to name the tracks in sequence, e.g. 0000_Song0.mp3 , 0001_Song1.mp3 etc etc. 
    * And also best to copy them one by one to the SD card from Song 0 to Song x. 
    */ 
void playSong(byte songNum){        
    writeToMP3(0x04, 0xA0, 0x00, songNum, 0x7E, 0x00, 0x00);    
} 

/*=listenForKeys function====================================================================================================================================================== 
    * is used to identify the person detected. 
    * The Arduino will wait a maximum of 10 seconds for the person to place their RFID tag near the Grove RFID reader antenna. 
    * This example shows two RFID tag values that will be accepted. 
    * This method also prints the RFID tag value to the Serial monitor - for debugging purposes. 
    */ 
boolean listenForKeys(){ 
    //reset some variables every time this function is called. 
    songNumber = -1; 
    start_Time = millis(); 
    time_Diff = 0; 
    waitingForKey = true; 
    rfidValue=0; 

    //Wait for a valid RFID tag for a maximum of 10 seconds 
    while(time_Diff<10000 && waitingForKey){ 
     Cayenne.run();          //Make sure to stay in contact with the Cayenne Server while waiting for the RFID tag. 
stop_Time = millis();       
time_Diff = stop_Time - start_Time;     //Measure the time elapsed. 

//If an RFID tag is detected by the Grove RFID reader, it will transmit a series of numbers related to the Tag ID. 
if(rfid.available()){ 
    while(rfid.available()){       //Make sure to read all of the numbers transmitted by the Grove RFID reader 
     rfidValue += rfid.read();      //You could employ a method to extract the exact RFID Tag ID - however just adding each number received - produced a unique number that I could use to identify the person. 
     delay(1);          //A small delay between reads - ensures you get all of the numbers from the RFID reader in one go. 
    } 
    Serial.println("RFID VALUE:"); 
    Serial.println(rfidValue);      //Print the Unique RFID Tag value to the Serial monitor - useful for debugging 


    //If a person has an RFID tag that can be identified in this list, then play a personalised message for that person. 
    switch(rfidValue){ 
     case 628:          //Person #1 has a Tag that generates an rfidValue of 628. 
     songNumber=4;         //File#4 (or message number 4) on the Grove MP3 player will be played when this person is detected. 
     waitingForKey = false;      //setting the "waitingForKey" variable to FALSE - will allow us to break out of the while loop (instead of waiting for a full 10 seconds). 
     break; 
     case 651:          //Person #2 has a Tag that generates an rfidValue of 651. 
     songNumber=5;         //File#5 (or message number 5) on the Grove MP3 player will be played when this person is detected. 
     waitingForKey = false;      //setting the "waitingForKey" variable to FALSE - will allow us to break out of the while loop (instead of waiting for a full 10 seconds). 
     break; 
     case 694:          //Person #3 has a Tag that generates an rfidValue of 694. 
     songNumber=6;         //File#6 (or message number 6) on the Grove MP3 player will be played when this person is detected. 
     waitingForKey = false;      //setting the "waitingForKey" variable to FALSE - will allow us to break out of the while loop (instead of waiting for a full 10 seconds). 
     break; 
     case 658:          //Person #4 has a Tag that generates an rfidValue of 658. 
     songNumber=7;         //File#7 (or message number 7) on the Grove MP3 player will be played when this person is detected. 
     waitingForKey = false;      //setting the "waitingForKey" variable to FALSE - will allow us to break out of the while loop (instead of waiting for a full 10 seconds). 
     break; 
     case 677:          //Person #5 has a Tag that generates an rfidValue of 677. 
     songNumber=8;         //File#8 (or message number 8) on the Grove MP3 player will be played when this person is detected. 
     waitingForKey = false;      //setting the "waitingForKey" variable to FALSE - will allow us to break out of the while loop (instead of waiting for a full 10 seconds). 
     break; 
     default: 
     waitingForKey = true;       //If a person has not been identified, keep waiting for the key/tag until the times runs out. 
     break; 
    } 
} 
    } 

    /* If we are still waiting for an RFID tag (key) at this point, then we were unsuccessful in identifying the person within 10 seconds. 
    * Returning FALSE - will sound the alarm, and will activate Virtual Pin 7 (Intruder Alert). 
    * Returning TRUE - means that we have identified the person who triggered the sensors, and can therefore relax, and turn OFF the Security sensors. 
    */ 
    if(waitingForKey){         
return false; 
    } else { 
    return true; 
    } 
     } 

/*=upDateCayenne function====================================================================================================================================================== 
    * This function will transmit the values of vPin6 and vPin7 to the Cayenne Dashboard's Virtual Pin 6 and 7 (respectively). 
    * This will help the Cayenne Server to keep up to date with the status of each variable. 
    */ 
void upDateCayenne() { 
    Cayenne.virtualWrite(V6, vPin6); 
    delay(50); 
    Cayenne.virtualWrite(V7, vPin7); 
    delay(50); 
} 

/*=CAYENNE_IN(V6) function====================================================================================================================================================== 
    * This will update the Arduino's vPin6 variable to the same value as Virtual Pin 6 on the Cayenne Dashboard. 
    * Which means that you can control the value of vPin6 from the Cayenne app. 
    */ 
CAYENNE_IN(V6){ 
    vPin6 = getValue.asInt(); 
} 

/*=CAYENNE_IN(V7) function====================================================================================================================================================== 
    * This will update the Arduino's vPin7 variable to the same value as Virtual Pin 7 on the Cayenne Dashboard. 
    * Which means that you can control the value of vPin7 from the Cayenne app. 
    */ 
CAYENNE_IN(V7){ 
    vPin7 = getValue.asInt(); 
} 

我也試着修改它,看下面,但沒有成功。

 void loop(){ 
     Cayenne.run();           //Synchronise with the Cayenne Server 


    if(vPin6&&!vPin7){          //Only progress if Virtual Pin 6 (Person detection) is ACTIVATED but ONLY when Virtual Pin 7 (Intruder alert) has NOT already been activated. 
if(!keyMessagePlayed){        //This ensures that the message is played only ONCE when a person has been detected. 
yield(); 
    playSong(1);          //Play the following message on the Grove MP3 player: "Place your keys on the mat" 
    keyMessagePlayed=true;        //keyMessagePlayed is changed to TRUE once the message has been played. 
} 
+0

我不認爲這是一個問題的產量這些電容器。 ESP不能運行多個任務,只要產量適用於任務優先級切換,它不應該是你的問題。 – cagdas

+0

我已經做了一個快速的閱讀,看到ESP也覆蓋了良率。在這裏它說關於屈服: 這是ESP8266和更傳統的Arduino微控制器之間最重要的區別之一。 ESP8266在後臺運行了許多實用功能 - 保持WiFi連接,管理TCP/IP堆棧以及執行其他任務。阻止這些功能運行可能導致ESP8266崩潰並自行重置。爲了避免這些神祕的重置,避免在草圖中出現長時間的循環。 – cagdas

+0

我仍然不相信這是一個收益問題。應該與你的庫,內存,而不是執行。 – cagdas

回答

0

在評論中還沒有提到的是需要一個至少0.1uF的去耦/支持電容器。

我正面臨着最終重置使用微型Python和它的webrepl。在我已經在ESP Vcc和Gnd Pin上直接添加了10uF陶瓷電容後,所有工作都很完美。

記住ESP不僅消耗芯片本身的180mA峯值電流。閃光燈也消耗峯值電流。

這裏還討論:http://internetofhomethings.com/homethings/?p=396

編輯: 這裏是我的ESP-12F的照片。我所有的ESP已連接連ESP-01

ESP12-F with 10uF

+0

我沒有連接任何電容器。在線教程後,我已將每個引腳直接連接到vcc或gnd.so,您是否建議在vcc和gnd之間添加一個電容器?可以給我發送您的連接照片嗎?請確保我會正確地完成所有連接。 – Konstantinos