#include <Arduino.h>
#include <Microduino_ColorLED.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4,5);
#define BLESerial mySerial
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;
}
}
ColorLED strip_A0 = ColorLED(16, A0);
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");
}
}
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 != "")
{
Serial.println(solution(BLE_Receive, "show_S1"));
if(((solution(BLE_Receive, "show_S1")) == "orange"))
{
strip_A0.setPixelColor(1,0xf78300);
strip_A0.show();
BLESenddata("control_C1", String("mCookie"));
delay(30);
}
if(((solution(BLE_Receive, "show_S1")) == "red"))
{
strip_A0.setPixelColor(2,0xf70000);
strip_A0.show();
BLESenddata("control_C1", String("mCookie"));
delay(30);
}
if(((solution(BLE_Receive, "show_S1")) == "green"))
{
strip_A0.setPixelColor(0,0xa8e000);
strip_A0.show();
BLESenddata("control_C1", String("mCookie"));
delay(30);
}
if(((solution(BLE_Receive, "show_S1")) == "yellow"))
{
strip_A0.setPixelColor(3,0xf2f700);
strip_A0.show();
BLESenddata("control_C1", String("mCookie"));
delay(30);
}
if(((solution(BLE_Receive, "show_S1")) == "purple"))
{
strip_A0.setPixelColor(0,0xa500c7);
strip_A0.show();
BLESenddata("control_C1", String("mCookie"));
delay(30);
}
if(((solution(BLE_Receive, "show_S1")) == 50))
{
strip_A0.setPixelColor(0,0x0066f7);
strip_A0.show();
BLESenddata("control_C1", String("mCookie"));
delay(30);
}
if(((solution(BLE_Receive, "show_S1")) == "pink"))
{
strip_A0.setPixelColor(0,0xf70090);
strip_A0.show();
BLESenddata("control_C1", String("mCookie"));
delay(30);
}
}
delay(10);
}
创建时间:2018/12/17
作者: |
立即注册