Last active
January 30, 2018 16:19
-
-
Save xseignard/a650f241b434eb1b296a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <SoftwareSerial.h> | |
| // ESP8266 rx, tx | |
| SoftwareSerial esp8266(11, 12); | |
| void setup() { | |
| //connection to ESP8266 | |
| esp8266.begin(9600); | |
| esp8266.setTimeout(2000); | |
| //serial debug | |
| Serial.begin(9600); | |
| // enable wifi | |
| pinMode(13, OUTPUT); | |
| digitalWrite(13, HIGH); | |
| delay(1000); | |
| if (check()) { | |
| resetESP8266(); | |
| if (connectWifi("ssid", "password")) { | |
| String response=""; | |
| boolean ok = httpPOST("test=42", "192.168.1.24", 3000, "/event", &response); | |
| if (ok) { | |
| Serial.println(response); | |
| } | |
| else { | |
| Serial.println("Something went wrong while posting data"); | |
| } | |
| } | |
| } | |
| } | |
| void loop() { | |
| } | |
| boolean check() { | |
| esp8266.println("AT"); | |
| Serial.println("checking.."); | |
| boolean ok = false; | |
| if (esp8266.find("OK")) { | |
| Serial.println("ESP8266 available"); | |
| ok = true; | |
| } | |
| return ok; | |
| } | |
| void resetESP8266() { | |
| // reset ESP8266 | |
| esp8266.println("AT+RST"); | |
| delay(3000); | |
| // set station mode | |
| esp8266.println("AT+CWMODE=1"); | |
| delay(300); | |
| // reset ESP8266 | |
| esp8266.println("AT+RST"); | |
| delay(500); | |
| esp8266.println("AT+CIPMUX=0"); | |
| delay(500); | |
| } | |
| boolean connectWifi(String ssid, String password) { | |
| String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\""; | |
| esp8266.println(cmd); | |
| boolean connected = false; | |
| for(int i = 0; i < 20; i++) { | |
| Serial.print("."); | |
| if(esp8266.find("OK")) { | |
| Serial.println("Connected!"); | |
| connected = true; | |
| break; | |
| } | |
| delay(50); | |
| } | |
| if(!connected) { | |
| Serial.println("Cannot connect to wifi"); | |
| } | |
| return connected; | |
| } | |
| boolean httpPOST(String data, String server, int port, String uri, String* response) { | |
| // initiate TCP connection | |
| String start = "AT+CIPSTART=\"TCP\",\"" + server + "\"," + port; | |
| // prepare the data to be posted | |
| String postRequest = | |
| "POST " + uri + " HTTP/1.1\r\n" + | |
| "Host: " + server + ":" + port + "\r\n" + | |
| "Accept: *" + "/" + "*\r\n" + | |
| "Content-Length: " + data.length() + "\r\n" + | |
| "Content-Type: application/x-www-form-urlencoded\r\n" + | |
| "\r\n" + | |
| data; | |
| // notify ESP8266 about the lenght of TCP packet | |
| String sendCmd = "AT+CIPSEND=" + postRequest.length(); | |
| esp8266.println(start); | |
| if (esp8266.find("OK")) { | |
| Serial.println("TCP connection OK"); | |
| esp8266.println(sendCmd); | |
| if (esp8266.find("SEND OK")) { | |
| Serial.println("Packet sent"); | |
| while (esp8266.available()) { | |
| String tmpResp = esp8266.readString(); | |
| response = &tmpResp; | |
| } | |
| // close the connection | |
| esp8266.println("AT+CIPCLOSE"); | |
| return true; | |
| } | |
| else { | |
| Serial.println("Cannot send packet"); | |
| return false; | |
| } | |
| } | |
| else { | |
| Serial.println("Cannot initiate TCP connection"); | |
| return false; | |
| } | |
| } |
I added these lines after line 57 then everything works.
esp8266.println("AT+CIPMUX=0");
delay(500);
Great! I'll try this asap. But i think i bricked a cactus micro... I'll try with my other one and I'll ask you for my brick problem.
Thanks for investigating!
So I tried and still cannot initiate the TCP connection.
I never receive the "OK" from line 100
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The line 87 should change to:
Arduino IDE think it's a comment mark.