├── ESP8266.ino └── README.md /ESP8266.ino: -------------------------------------------------------------------------------- 1 | char serialbuffer[1000];//serial buffer for request url 2 | 3 | void setup() 4 | { 5 | Serial1.begin(115200);//connection to ESP8266 6 | Serial.begin(115200); //serial debug 7 | 8 | 9 | //set mode needed for new boards 10 | Serial1.println("AT+RST"); 11 | Serial1.println("AT+CWMODE=1"); 12 | delay(500);//delay after mode change 13 | Serial1.println("AT+RST"); 14 | 15 | //connect to wifi network 16 | Serial1.println("AT+CWJAP=\"YOUR_WIFI_NETWORK\",\"YOUR_WIFI_PASSWORD\""); 17 | } 18 | 19 | void loop() 20 | { 21 | //output everything from ESP8266 to the Arduino Micro Serial output 22 | while (Serial1.available() > 0) { 23 | Serial.write(Serial1.read()); 24 | } 25 | 26 | if (Serial.available() > 0) { 27 | //read from serial until terminating character 28 | int len = Serial.readBytesUntil('\n', serialbuffer, sizeof(serialbuffer)); 29 | 30 | //trim buffer to length of the actual message 31 | String message = String(serialbuffer).substring(0,len-1); 32 | Serial.println("message: " + message); 33 | 34 | //check to see if the incoming serial message is a url or an AT command 35 | if(message.substring(0,2)=="AT"){ 36 | //make command request 37 | Serial.println("COMMAND REQUEST"); 38 | Serial1.println(message); 39 | }else{ 40 | //make webrequest 41 | Serial.println("WEB REQUEST"); 42 | WebRequest(message); 43 | } 44 | } 45 | } 46 | 47 | //web request needs to be sent without the http for now, https still needs some working 48 | void WebRequest(String request){ 49 | //find the dividing marker between domain and path 50 | int slash = request.indexOf('/'); 51 | 52 | //grab the domain 53 | String domain; 54 | if(slash>0){ 55 | domain = request.substring(0,slash); 56 | }else{ 57 | domain = request; 58 | } 59 | 60 | //get the path 61 | String path; 62 | if(slash>0){ 63 | path = request.substring(slash); 64 | }else{ 65 | path = "/"; 66 | } 67 | 68 | //output domain and path to verify 69 | Serial.println("domain: |" + domain + "|"); 70 | Serial.println("path: |" + path + "|"); 71 | 72 | //create start command 73 | String startcommand = "AT+CIPSTART=\"TCP\",\"" + domain + "\", 80"; //443 is HTTPS, still to do 74 | 75 | Serial1.println(startcommand); 76 | Serial.println(startcommand); 77 | 78 | 79 | //test for a start error 80 | if(Serial1.find("Error")){ 81 | Serial.println("error on start"); 82 | return; 83 | } 84 | 85 | //create the request command 86 | String sendcommand = "GET http://"+ domain + path + " HTTP/1.0\r\n\r\n\r\n";//works for most cases 87 | 88 | Serial.print(sendcommand); 89 | 90 | //send 91 | Serial1.print("AT+CIPSEND="); 92 | Serial1.println(sendcommand.length()); 93 | 94 | //debug the command 95 | Serial.print("AT+CIPSEND="); 96 | Serial.println(sendcommand.length()); 97 | 98 | //delay(5000); 99 | if(Serial1.find(">")) 100 | { 101 | Serial.println(">"); 102 | }else 103 | { 104 | Serial1.println("AT+CIPCLOSE"); 105 | Serial.println("connect timeout"); 106 | delay(1000); 107 | return; 108 | } 109 | 110 | //Serial.print(getcommand); 111 | Serial1.print(sendcommand); 112 | } 113 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ESP8266 2 | ======= 3 | This is code to run on an Arduino Micro used to communicate with the ESP8266 Wifi board using the serial debugging console. The Arduino Micro has 2 UART connections, one using the USB for communicating with a computer and the other (in this case) is connected to the TX/RX pins on the ESP8266. The ESP8266 is also powered using the 3.3v and GND pins on the Arduino Micro. 4 | 5 | To use, start the sketch and open the Serial Monitor in the Arduino IDE (after setting the wifi network name and password in the code). To test that the ESP8266 is running correctly just put "AT" into the Serial Monitor and hit Send (make sure Carriage Return is set in the Seial Monitor). If it has the pins sent up correctly you should see an "OK". 6 | 7 | The Arduino code is waiting for a serial command that is either a URL (such as "www.signalvehicle.com", note the lack of "http") or an AT command (such as "AT+CIFSR" which will show the IP address if it connected to a Wifi network). The commands will always begin with "AT...", if it doesnt see AT as the firt 2 characters it will assume that the input is a URL. The URL can also contain a full path, such as "www.signalvehicle.com/index.asp" (again without the "http://"). If a valid URL is given, the module will output the HTML from the requested file to the debug window. 8 | 9 | I am still working of the HTTPS (secure) request, but as of now this will only work on HTTP GET requests. The same code should work on any Arduinos that have more than one UART, such as the MEGA boards. My full writeup on how I have the hardware connected (with images and video) is located here: 10 | http://contractorwolf.com/esp8266-wifi-arduino-micro/ 11 | --------------------------------------------------------------------------------