Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #define BUFFER_SIZE 512
  2.  
  3. #define SSID "BRAGA" // change this to match your WiFi SSID
  4. #define PASS "BRAGA1155224433665511" // change this to match your WiFi password
  5. #define PORT "8080" // using port 8080 by default
  6.  
  7. char buffer[BUFFER_SIZE];
  8.  
  9.  
  10. // If using Software Serial for debug
  11. // Use the definitions below
  12. #include <SoftwareSerial.h>
  13. SoftwareSerial esp(9,11); // use pins 7, 8 for software serial
  14. #define dbg Serial
  15.  
  16. // If your MCU has dual USARTs (e.g. ATmega644)
  17. // Use the definitions below
  18. //#define dbg Serial // use Serial to talk to esp8266
  19.  
  20. // By default we are looking for OK\r\n
  21. char OKrn[] = "OK\r\n";
  22. byte wait_for_esp_response(int timeout, char* term=OKrn) {
  23. unsigned long t=millis();
  24. bool found=false;
  25. int i=0;
  26. int len=strlen(term);
  27. // wait for at most timeout milliseconds
  28. // or if OK\r\n is found
  29. while(millis()<t+timeout) {
  30. if(esp.available()) {
  31. buffer[i++]=esp.read();
  32. if(i>=len) {
  33. if(strncmp(buffer+i-len, term, len)==0) {
  34. found=true;
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. buffer[i]=0;
  41.  
  42. return found;
  43. }
  44.  
  45. void setup() {
  46.  
  47. // assume esp8266 operates at 115200 baud rate
  48. // change if necessary to match your modules' baud rate
  49. esp.begin(9600);
  50.  
  51.  
  52.  
  53.  
  54. setupWiFi();
  55.  
  56. // print device IP address
  57.  
  58. esp.println("AT+CIFSR");
  59. wait_for_esp_response(1000);
  60. }
  61.  
  62. bool read_till_eol() {
  63. static int i=0;
  64. if(esp.available()) {
  65. buffer[i++]=esp.read();
  66. if(i==BUFFER_SIZE) i=0;
  67. if(i>1 && buffer[i-2]==13 && buffer[i-1]==10) {
  68. buffer[i]=0;
  69. i=0;
  70.  
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76.  
  77. void loop() {
  78. int ch_id, packet_len;
  79. char *pb;
  80. if(read_till_eol()) {
  81. if(strncmp(buffer, "+IPD,", 5)==0) {
  82. // request: +IPD,ch,len:data
  83. sscanf(buffer+5, "%d,%d", &ch_id, &packet_len);
  84. if (packet_len > 0) {
  85. // read serial until packet_len character received
  86. // start from :
  87. pb = buffer+5;
  88. while(*pb!=':') pb++;
  89. pb++;
  90. if (strncmp(pb, "GET /", 5) == 0) {
  91. wait_for_esp_response(1000);
  92.  
  93. serve_homepage(ch_id);
  94. }
  95. }
  96. }
  97. }
  98. }
  99.  
  100. void serve_homepage(int ch_id) {
  101. String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\nRefresh: 5\r\n";
  102.  
  103. String content="";
  104. // output the value of each analog input pin
  105. for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  106. int sensorReading = analogRead(analogChannel);
  107. content += "analog input ";
  108. content += analogChannel;
  109. content += " is ";
  110. content += sensorReading;
  111. content += "<br />\n";
  112. }
  113.  
  114. header += "Content-Length:";
  115. header += (int)(content.length());
  116. header += "\r\n\r\n";
  117. esp.print("AT+CIPSEND=");
  118. esp.print(ch_id);
  119. esp.print(",");
  120. esp.println(header.length()+content.length());
  121. if(wait_for_esp_response(2000, "> ")) {
  122. esp.print(header);
  123. esp.print(content);
  124. } else {
  125. esp.print("AT+CIPCLOSE=");
  126. esp.println(ch_id);
  127. }
  128. }
  129.  
  130.  
  131. void setupWiFi() {
  132. // try empty AT command
  133. esp.println("AT");
  134. wait_for_esp_response(1000);
  135.  
  136. // set mode 1 (client)
  137. esp.println("AT+CWMODE=1");
  138. wait_for_esp_response(1000);
  139.  
  140. // reset WiFi module
  141. esp.print("AT+RST\r\n");
  142. wait_for_esp_response(1500);
  143. delay(3000);
  144.  
  145. // join AP
  146. esp.print("AT+CWJAP=\"");
  147. esp.print(SSID);
  148. esp.print("\",\"");
  149. esp.print(PASS);
  150. esp.println("\"");
  151. // this may take a while, so wait for 5 seconds
  152. wait_for_esp_response(5000);
  153.  
  154. esp.println("AT+CIPSTO=30");
  155. wait_for_esp_response(1000);
  156.  
  157. // start server
  158. esp.println("AT+CIPMUX=1");
  159. wait_for_esp_response(1000);
  160.  
  161. esp.print("AT+CIPSERVER=1,"); // turn on TCP service
  162. esp.println(PORT);
  163. wait_for_esp_response(1000);
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement