Advertisement
Guest User

ESP8266-01 ARDUINO AT COMMANDS LED

a guest
Jan 25th, 2019
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #########################################ESP8266 WIFI Communication with Python:
  2. ###################################################Arduino Code:
  3. /*
  4. * Connect LAPTOP to 192.168.4.1. Send packets using Packet sender
  5. */
  6. #include <SoftwareSerial.h>
  7.      
  8. #define DEBUG true
  9.      
  10. SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
  11.                                  // This means that you need to connect the TX line from the esp to the Arduino's pin 2
  12.                                  // and the RX line from the esp to the Arduino's pin 3
  13.  
  14. void setup()
  15.     {
  16.       Serial.begin(9600);
  17.       esp8266.begin(9600); // your esp's baud rate might be different
  18.      
  19.       pinMode(11,OUTPUT);
  20.       digitalWrite(11,LOW);
  21.      
  22.       pinMode(12,OUTPUT);
  23.       digitalWrite(12,LOW);
  24.      
  25.       pinMode(13,OUTPUT);
  26.       digitalWrite(13,LOW);
  27.      
  28.       sendData("AT+RST\r\n",2000,DEBUG); // reset module
  29.       sendData("AT+CWMODE=2\r\n",2000,DEBUG); // configure as access point
  30.       sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  31.       sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  32.       sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80*/
  33.       Serial.println("Ready");
  34.      
  35.     }
  36.      
  37. void loop()
  38. {
  39.         if (esp8266.available())
  40.         {
  41.           if(esp8266.find("+IPD,"))
  42.           {
  43.             delay(10);
  44.             esp8266.find("pin=");
  45.  
  46.             int pN = (esp8266.read()-38);
  47.          
  48.             Serial.println(pN);
  49.            
  50.             pinMode(pN,OUTPUT);
  51.             digitalWrite(pN,!digitalRead(pN));
  52.           }
  53.         }
  54.  
  55. }
  56.  
  57.      
  58. /*
  59. * Name: sendData
  60. * Description: Function used to send data to ESP8266.
  61. * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
  62. * Returns: The response from the esp8266 (if there is a response)
  63. */
  64. String sendData(String command, const int timeout, boolean debug)
  65.     {
  66.         String response = "";
  67.        
  68.         esp8266.print(command); // send the read character to the esp8266
  69.        
  70.         long int time = millis();
  71.        
  72.         while( (time+timeout) > millis())
  73.         {
  74.           while(esp8266.available())
  75.           {
  76.            
  77.             // The esp has data so display its output to the serial window
  78.             char c = esp8266.read(); // read the next character.
  79.             response+=c;
  80.           }  
  81.         }
  82.        
  83.         if(debug)
  84.         {
  85.           Serial.print(response);
  86.         }
  87.        
  88.         return response;
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement