Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- SoftwareSerial esp8266(3, 2); // RX | TX
- #define DEBUG true
- int ERROR_PIN = 7;
- int OK_PIN = 6;
- char serialbuffer[600];//serial buffer for request url
- const String ssid = "heimnetzlannet";
- const String pw = "77041084";
- int state = 0;
- void setup()
- {
- delay(1000);
- /**
- // init leds
- pinMode(ERROR_PIN, OUTPUT);
- pinMode(OK_PIN, OUTPUT);
- state = 0;
- digitalWrite(ERROR_PIN, HIGH);
- digitalWrite(OK_PIN, LOW);
- /**/
- // init ports
- Serial.begin(9600);
- Serial.println("initializing esp8266 port...");
- esp8266.begin(9600);
- delay(400);
- // init WIFI
- /**/
- while(!esp8266.available())
- {
- Serial.print("...");
- delay(300);
- }
- Serial.println();
- Serial.println("FINISH esp8266 initializing!");
- //
- /**
- digitalWrite(ERROR_PIN, LOW);
- digitalWrite(OK_PIN, HIGH);
- state = 1;
- /**/
- /**/
- // Setup connection
- sendData("print(wifi.sta.getip())\r\n",2000,DEBUG);
- sendData("wifi.setmode(wifi.STATION)\r\n",1000,DEBUG);
- //sendData("AT+CWMODE=1\r\n",2000,DEBUG);
- //sendData("AT+RST\r\n",3000,DEBUG);
- //sendData("AT+CWLAP\r\n",6000,DEBUG);
- sendData("wifi.sta.config(\"" + ssid + "\",\""+ pw +"\")\r\n",4000,DEBUG);
- sendData("print(wifi.sta.getip())\r\n",4000,DEBUG);
- delay(2000);
- // test output
- Createconnection("");
- /**/
- /**/
- }
- void loop()
- {
- if (esp8266.available())
- {
- char c = esp8266.read() ;
- Serial.print(c);
- /**
- if(state == 0)
- {
- state = 1;
- digitalWrite(ERROR_PIN, LOW);
- digitalWrite(OK_PIN, HIGH);
- }
- /**/
- }
- else
- {
- /**
- if(state > 0)
- {
- state = 0;
- digitalWrite(ERROR_PIN, HIGH);
- digitalWrite(OK_PIN, LOW);
- }
- /**/
- }
- if (Serial.available())
- {
- char c = Serial.read();
- esp8266.print(c);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- char* sendData(String command, const int timeout, boolean debug)
- {
- String response = "";
- int idx = 0;
- 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.
- serialbuffer[idx] = c;
- //response+=c;
- idx++;
- }
- }
- serialbuffer[idx] = '\0';
- if(debug)
- {
- Serial.print(serialbuffer);
- }
- return serialbuffer;
- }
- //////////////////////////////////////////////////////////////////////////////////
- String Createconnection(String url)
- {
- String tmpCreateConnection = "conn=net.createConnection(net.TCP, false)\r\n";
- String tmpReceive = "conn:on(\"receive\", function(conn, pl) print(pl) end)\r\n";
- String tmpConnect = "conn:connect(80,\"121.41.33.127\")\r\n";
- String tmpSend = "conn:send(\"GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n\")";
- sendData(tmpCreateConnection, 4000, DEBUG);
- sendData(tmpReceive, 4000, DEBUG);
- sendData(tmpConnect, 4000, DEBUG);
- sendData(tmpSend, 15000, DEBUG);
- WebRequest("");
- }
- //////////////////////////////////////////////////////////////////////////////////
- String WebRequest(String url)
- {
- //conn:send("GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
- String tmpGET = "conn:send(\"GET / HTTP/1.1\r\n";
- String tmpHost = "Host: www.nodemcu.com\r\n";
- String tmpConnection = "Connection: keep-alive\r\n";
- String tmpAccept = "Accept: */*\r\n\r\n\")\r\n";
- sendData(tmpGET, 1500, DEBUG);
- sendData(tmpHost, 1500, DEBUG);
- sendData(tmpConnection, 1500, DEBUG);
- sendData(tmpAccept, 6000, DEBUG);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement