Advertisement
blfuentes

Untitled

Apr 8th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial esp8266(3, 2); // RX | TX
  3.  
  4. #define DEBUG true
  5.  
  6. int ERROR_PIN = 7;
  7. int OK_PIN = 6;
  8.  
  9. char serialbuffer[600];//serial buffer for request url
  10. const String ssid = "heimnetzlannet";
  11. const String pw = "77041084";
  12.  
  13. int state = 0;
  14.  
  15. void setup()
  16. {
  17.   delay(1000);
  18.   /**
  19.   // init leds
  20.   pinMode(ERROR_PIN, OUTPUT);
  21.   pinMode(OK_PIN, OUTPUT);
  22.  
  23.   state = 0;
  24.  
  25.   digitalWrite(ERROR_PIN, HIGH);
  26.   digitalWrite(OK_PIN, LOW);
  27.   /**/
  28.   // init ports
  29.   Serial.begin(9600);
  30.   Serial.println("initializing esp8266 port...");
  31.   esp8266.begin(9600);
  32.   delay(400);
  33.   // init WIFI
  34.   /**/
  35.   while(!esp8266.available())
  36.   {
  37.     Serial.print("...");
  38.     delay(300);
  39.   }
  40.   Serial.println();
  41.   Serial.println("FINISH esp8266 initializing!");
  42.   //
  43.   /**
  44.   digitalWrite(ERROR_PIN, LOW);
  45.   digitalWrite(OK_PIN, HIGH);
  46.   state = 1;
  47.   /**/
  48.   /**/
  49.   // Setup connection
  50.   sendData("print(wifi.sta.getip())\r\n",2000,DEBUG);
  51.   sendData("wifi.setmode(wifi.STATION)\r\n",1000,DEBUG);
  52.   //sendData("AT+CWMODE=1\r\n",2000,DEBUG);
  53.   //sendData("AT+RST\r\n",3000,DEBUG);
  54.   //sendData("AT+CWLAP\r\n",6000,DEBUG);
  55.   sendData("wifi.sta.config(\"" + ssid + "\",\""+ pw +"\")\r\n",4000,DEBUG);
  56.   sendData("print(wifi.sta.getip())\r\n",4000,DEBUG);
  57.  
  58.   delay(2000);
  59.   // test output
  60.   Createconnection("");
  61.   /**/
  62.   /**/
  63. }
  64.  
  65. void loop()
  66. {
  67.   if (esp8266.available())
  68.   {
  69.     char c = esp8266.read() ;
  70.     Serial.print(c);
  71.     /**
  72.     if(state == 0)
  73.     {
  74.       state = 1;
  75.       digitalWrite(ERROR_PIN, LOW);
  76.       digitalWrite(OK_PIN, HIGH);
  77.     }
  78.     /**/
  79.   }
  80.   else
  81.   {
  82.     /**
  83.     if(state > 0)
  84.     {
  85.       state = 0;
  86.       digitalWrite(ERROR_PIN, HIGH);
  87.       digitalWrite(OK_PIN, LOW);
  88.     }
  89.     /**/
  90.   }
  91.   if (Serial.available())
  92.   {  
  93.     char c = Serial.read();
  94.     esp8266.print(c);
  95.   }
  96. }
  97.  
  98. //////////////////////////////////////////////////////////////////////////////
  99. char* sendData(String command, const int timeout, boolean debug)
  100. {
  101.   String response = "";
  102.   int idx = 0;
  103.   esp8266.print(command); // send the read character to the esp8266
  104.   long int time = millis();
  105.   while( (time+timeout) > millis())
  106.   {
  107.     while(esp8266.available())
  108.     {
  109.       // The esp has data so display its output to the serial window
  110.       char c = esp8266.read(); // read the next character.
  111.       serialbuffer[idx] = c;
  112.       //response+=c;
  113.       idx++;
  114.     }
  115.   }
  116.   serialbuffer[idx] = '\0';
  117.   if(debug)
  118.   {
  119.     Serial.print(serialbuffer);
  120.   }
  121.   return serialbuffer;
  122. }
  123. //////////////////////////////////////////////////////////////////////////////////
  124. String Createconnection(String url)
  125. {
  126.   String tmpCreateConnection = "conn=net.createConnection(net.TCP, false)\r\n";
  127.   String tmpReceive = "conn:on(\"receive\", function(conn, pl) print(pl) end)\r\n";
  128.   String tmpConnect = "conn:connect(80,\"121.41.33.127\")\r\n";
  129.   String tmpSend = "conn:send(\"GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n\")";
  130.   sendData(tmpCreateConnection, 4000, DEBUG);
  131.   sendData(tmpReceive, 4000, DEBUG);
  132.   sendData(tmpConnect, 4000, DEBUG);
  133.   sendData(tmpSend, 15000, DEBUG);
  134.   WebRequest("");
  135. }
  136. //////////////////////////////////////////////////////////////////////////////////
  137. String WebRequest(String url)
  138. {
  139.   //conn:send("GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  140.   String tmpGET = "conn:send(\"GET / HTTP/1.1\r\n";
  141.   String tmpHost = "Host: www.nodemcu.com\r\n";
  142.   String tmpConnection = "Connection: keep-alive\r\n";
  143.   String tmpAccept = "Accept: */*\r\n\r\n\")\r\n";
  144.   sendData(tmpGET, 1500, DEBUG);
  145.   sendData(tmpHost, 1500, DEBUG);
  146.   sendData(tmpConnection, 1500, DEBUG);
  147.   sendData(tmpAccept, 6000, DEBUG);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement