Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################################ESP8266 WIFI Communication with Python:
- ###################################################Arduino Code:
- /*
- * Connect LAPTOP to 192.168.4.1. Send packets using Packet sender
- */
- #include <SoftwareSerial.h>
- #define DEBUG true
- SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
- // This means that you need to connect the TX line from the esp to the Arduino's pin 2
- // and the RX line from the esp to the Arduino's pin 3
- void setup()
- {
- Serial.begin(9600);
- esp8266.begin(9600); // your esp's baud rate might be different
- pinMode(11,OUTPUT);
- digitalWrite(11,LOW);
- pinMode(12,OUTPUT);
- digitalWrite(12,LOW);
- pinMode(13,OUTPUT);
- digitalWrite(13,LOW);
- sendData("AT+RST\r\n",2000,DEBUG); // reset module
- sendData("AT+CWMODE=2\r\n",2000,DEBUG); // configure as access point
- sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
- sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
- sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80*/
- Serial.println("Ready");
- }
- void loop()
- {
- if (esp8266.available())
- {
- if(esp8266.find("+IPD,"))
- {
- delay(10);
- esp8266.find("pin=");
- int pN = (esp8266.read()-38);
- Serial.println(pN);
- pinMode(pN,OUTPUT);
- digitalWrite(pN,!digitalRead(pN));
- }
- }
- }
- /*
- * Name: sendData
- * Description: Function used to send data to ESP8266.
- * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
- * Returns: The response from the esp8266 (if there is a response)
- */
- String sendData(String command, const int timeout, boolean debug)
- {
- String response = "";
- esp8266.print(command); // send the read character to the esp8266
- long int time = millis();
- while( (time+timeout) > millis())
- {
- while(esp8266.available())
- {
- // The esp has data so display its output to the serial window
- char c = esp8266.read(); // read the next character.
- response+=c;
- }
- }
- if(debug)
- {
- Serial.print(response);
- }
- return response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement