SoftwareSerial mySerial(4,5);
String solution(String _sta, char *c)
{
String data;
if (strstr(_sta.c_str(), c) == NULL)
return "";
else
{
if (_sta.startsWith("{") && _sta.endsWith("}"))
{
_sta = _sta.substring(1, _sta.length() - 1);
_sta.replace("\"", "");
uint8_t _length = _sta.length();
char buf[_length];
char c_all[30] = "";
char data1[] = ":%s";
strcat(c_all, c);
strcat(c_all, data1);
sscanf(_sta.c_str(), c_all, &buf);
data = String(buf);
}
if (data != NULL)
return data;
}
}
void BLESenddata(String _st, String _data)
{
String send_data;
int8_t number;
send_data = "{\"";
send_data += _st;
send_data += "\":\"";
send_data += _data;
send_data += "\"}";
number = send_data.length() / 17;
if (number == 0)
{
BLESerial.println(send_data);
delay(30);
}
else
{
while (number >= 0)
{
BLESerial.print(send_data.substring(0, 17));
send_data = send_data.substring(17, send_data.length());
delay(30);
number--;
}
BLESerial.print("\n");
}
}
ColorLED strip_A0 = ColorLED(16, A0);
String uartMsg;
uint8_t uartStep = 0;
String readserail()
{
char inByte = BLESerial.read();
switch (uartStep)
{
case 0:
uartMsg = "";
if (inByte == '{')
{
uartMsg += inByte;
uartStep = 1;
}
break;
case 1:
uartMsg += inByte;
if (inByte == '}')
{
uartStep = 0;
return uartMsg;
}
break;
default:
break;
}
return "";
}
void setup()
{
Serial.begin(9600);
strip_A0.begin();
BLESerial.begin(9600);
}
void loop()
{
String BLE_Receive = readserail();
if (BLE_Receive != "")
{
delay(0.1*1000);
Serial.println(solution(BLE_Receive, "show_S1"));
if(((solution(BLE_Receive, "show_S1")) == "red"))
{
BLESenddata("control_C1", String("1"));
delay(30);
strip_A0.setPixelColor(0,0xfd0000);
strip_A0.show();
}
if(((solution(BLE_Receive, "show_S1")) == "green"))
{
strip_A0.setPixelColor(0,0x10ae05);
strip_A0.show();
BLESenddata("control_C1", String("2"));
delay(30);
}
if(((solution(BLE_Receive, "show_S1")) == "blue"))
{
strip_A0.setPixelColor(0,0x0523ae);
strip_A0.show();
BLESenddata("control_C1", String("3"));
delay(30);
}
}
delay(10);
}
创建时间:2018/12/06
作者: |
立即注册