|
我使用了Arduino UNO去連接ESP2866 ESP-01 Wifi module,然後就傳送了一個php檔的文件去一個自製的伺服器。
我嘗試使用AT指令去連接TCP,雖然在傳輸資料到TCP的時候成功了,但是當我使用Arduino Sketch來寫AT程式時(在下面),在傳送及連接TCP數據時,不知道為什麼它一時成功一時失敗。
我想請問一下各位大大 如果小的想解決這個問題
我需要做什麼? 用不用加什麼設備? > . <
或者要穩定ESP-8266 ESP-01 Wifi module的電壓?
謝謝大家了哦~
這個是我編寫的程式,請各位大大過目一下:
#include <SoftwareSerial.h>
#define RX 2
#define TX 3
int fsrAnalogPin = 0;
String AP = "Test"; // CHANGE ME
String PASS = "testtest"; // CHANGE ME
String HOST = "...";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int fsrReading;
int fsrReading2;
int fsrReading3;
int Seats1 = 0;
int Seats2 = 0;
int Seats3 = 0;
int result = 0;
SoftwareSerial esp8266(RX,TX);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT+RST",5,"OK");
sendCommand("AT+RESTORE",15,"OK");
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP_CUR=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
fsrReading = analogRead(fsrAnalogPin);
if (fsrReading > 1)
{
Seats1 = 0;
}
else
{
Seats1 = 1;
}
if (fsrReading2 > 0)
{
Seats2 = 0;
}
else
{
Seats2 = 1;
}
if (fsrReading3 > 0)
{
Seats3 = 0;
}
else
{
Seats3 = 1;
}
result = Seats1 + Seats2 + Seats3 ;
String stringOne = "GET /~it-comp1/submit_sum.php?a1=" ;
String stringTwo = "&a2=" ;
String stringThree = "&plate=LV8402" ;
String getData = stringOne + result + stringTwo + 0 + stringThree ;
sendCommand("AT+CIPSTART=\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=" +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
result = 0 ;
delay(1000);
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
在這裏我再次多謝大家哦~
|
|