esp8266 restarting.maybe需要产量

时间:2016-09-25 15:32:44

标签: arduino esp8266

我在esp8266 201上遇到了一些问题。我用一个(绰绰有余的)电源供电,它不断重启。

即使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.
}

1 个答案:

答案 0 :(得分:0)

评论中尚未提及需要至少0.1uF的去耦/支持电容。

我最终使用micro python及其webrepl进行重置。在ESP Vcc和Gnd Pin上直接添加一个10uF陶瓷电容后,一切都运行良好。

请记住,ESP不仅消耗芯片本身的180mA峰值电流。闪光灯在峰值时也会消耗大电流。

此处还讨论了:http://internetofhomethings.com/homethings/?p=396

编辑: 这是我的ESP-12F的照片。我所有的ESP都连接了这些电容器,即使是ESP-01

也是如此

ESP12-F with 10uF