Last active
August 29, 2015 14:19
-
-
Save hsinhoyeh/b560f90492b3f6bccfa0 to your computer and use it in GitHub Desktop.
this gist shows how to communicate a http server via arduino uno + rsp 8299
This file contains 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
// NOTE: the following is just a POC. codes are not purified yet. | |
#include <SoftwareSerial.h> | |
SoftwareSerial esp8266 = SoftwareSerial(3,2); // another serial which binds pix TX:3, RX:2 for comminucating ESP8266 | |
// NOTE: the default serial is used to send debug info back to PC. | |
// the wifi configuration | |
const char wifiName[50] = "bad1", wifiPassword[50] = "illbeback"; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); // setup baao rate: 9600 from pc to arduino | |
esp8266.begin(9600); // setup bauo rate: 9600 from arduino to BSP8299 | |
digitalWrite(13, HIGH); | |
resetWifi(); | |
waitResponse(); | |
useMultiConn(); | |
waitResponse(); | |
changeToStationMode(); | |
waitResponse(); | |
connectToWiFi(); | |
waitResponse(); | |
digitalWrite(13, LOW); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
connectToOtherDevice("192.168.43.193", 8099, ""); | |
} | |
void noWaitResponse() { | |
delay(3000); | |
while (esp8266.available() <= 0) { | |
} | |
while (esp8266.available() > 0) { | |
//drawn all bytes | |
Serial.print(esp8266.read(), DEC); | |
Serial.print(" "); | |
} | |
Serial.println(); | |
} | |
void waitResponse() { | |
delay(3000); | |
while (esp8266.available() <= 0) { | |
} | |
while (esp8266.available() > 0) { | |
//drawn all bytes | |
Serial.print(esp8266.read(), DEC); | |
Serial.print(" "); | |
} | |
Serial.println(); | |
} | |
void resetWifi() { | |
esp8266.println("AT+RST"); | |
while (esp8266.available() <= 0) { | |
} | |
delay(1000); | |
} | |
void useMultiConn() { | |
esp8266.println("AT+CIPMUX=1"); //enable multiple connections | |
} | |
void changeToStationMode() { | |
esp8266.println("AT+CWMODE=1"); | |
while (esp8266.available() <= 0) { | |
} | |
delay(700); | |
} | |
void connectToWiFi() { | |
esp8266.print("AT+CWJAP=\""); | |
esp8266.print(wifiName); | |
esp8266.print("\",\""); | |
esp8266.print(wifiPassword); | |
esp8266.println("\""); | |
delay(10000); // wait for a long delay. | |
} | |
void connectToOtherDevice(const char* ip, int port, const char * commands) { | |
char port_str[4]; | |
char datalen_str[4]; | |
char cmdBuffer[128]; | |
unsigned long ip_long; | |
memset(cmdBuffer, 0, 128); | |
memset(port_str, 0, 4); | |
memset(datalen_str, 0, 4); | |
// convert port | |
itoa(port, port_str, 10); | |
int datalen = 0; | |
// "GET " "/" "\r\n" | |
datalen += (4 + 1 + 2); | |
// " HTTP/1.0\r\nHosts: " + "192.168.43.193:8099 + "\r\n\r\n\r\n" | |
datalen += 17 + 19 + 6; | |
itoa(datalen, datalen_str, 10); | |
// send command | |
// AT+CIPSTART=1,"TCP","192.168.43.193",8099, where 1 indicates multiconnection. | |
// 192.168.43.193 is the server's ip, and 8099 is the service port | |
strcpy(cmdBuffer, "AT+CIPSTART=1,\"TCP\",\""); | |
sprintf(&cmdBuffer[strlen(cmdBuffer)], ip); | |
strcpy(&cmdBuffer[strlen(cmdBuffer)], "\","); | |
strcpy(&cmdBuffer[strlen(cmdBuffer)],port_str); | |
// send command | |
Serial.println(cmdBuffer); | |
esp8266.println(cmdBuffer); | |
waitResponse(); | |
memset(cmdBuffer, 0, 128); | |
// sned comamnd | |
// AT+CIPSEND=1,$byte, where $byte is the total bytes send | |
strcpy(cmdBuffer, "AT+CIPSEND=1,"); | |
strcpy(&cmdBuffer[strlen(cmdBuffer)], datalen_str); | |
// send | |
esp8266.println(cmdBuffer); | |
Serial.println(cmdBuffer); | |
waitResponse(); | |
memset(cmdBuffer, 0, 128); | |
// send command | |
// GET / HTTP/1.0\r\nHost: 192.168.43.193:8099\r\n\r\n\r\n | |
Serial.println(strlen(cmdBuffer)); | |
strcpy(&cmdBuffer[strlen(cmdBuffer)], "GET / HTTP/1.0\r\n"); | |
strcpy(&cmdBuffer[strlen(cmdBuffer)], "Host: 192.168.43.193:8099\r\n\r\n\r\n"); | |
// send | |
esp8266.println(cmdBuffer); | |
Serial.println(cmdBuffer); | |
if (esp8266.available() > 0) { | |
if (esp8266.find("+IPD,")) { | |
noWaitResponse(); | |
} | |
} | |
esp8266.println("AT+CIPCLOSE=1"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment