งานปฏิบัติ 3
Code
#include <SoftwareSerial.h>
#define TIMEOUT 5000 // mS
#define LED 5
SoftwareSerial mySerial(7, 6); // RX, TX
void setup()
{
pinMode(LED,OUTPUT);
Serial.begin(115200);
mySerial.begin(115200);
SendCommand("AT+RST", "Ready");
delay(5000);
SendCommand("AT+CWMODE=1","OK");
SendCommand("AT+CIFSR", "OK");
SendCommand("AT+CIPMUX=1","OK");
SendCommand("AT+CIPSERVER=1,80","OK");
}
void loop(){
String IncomingString="";
boolean StringReady = false;
while (mySerial.available()){
IncomingString=mySerial.readString();
StringReady= true;
}
if (StringReady){
Serial.println("Received String: " + IncomingString);
if (IncomingString.indexOf("LED=ON") != -1) {
digitalWrite(LED,HIGH);
}
if (IncomingString.indexOf("LED=OFF") != -1) {
digitalWrite(LED,LOW);
}
}
}
boolean SendCommand(String cmd, String ack){
mySerial.println(cmd); // Send "AT+" command to module
if (!echoFind(ack)) // timed out waiting for ack string
return true; // ack blank or ack found
}
boolean echoFind(String keyword){
byte current_char = 0;
byte keyword_length = keyword.length();
long deadline = millis() + TIMEOUT;
while(millis() < deadline){
if (mySerial.available()){
char ch = mySerial.read();
Serial.write(ch);
if (ch == keyword[current_char])
if (++current_char == keyword_length){
Serial.println();
return true;
}
}
}
return false; // Timed out
}
ความคิดเห็น
แสดงความคิดเห็น