通过WiFi连接,实现小灯点亮,然后通过手机app上的语音控制实现改变灯的颜色以及开关等简单操作。具体的代码如下。
分工情况:李晓康 写代码 ,江灵杰 组装硬件,合作测试等。
#include
#include
#include
String var_variable;
ColorLED strip_A0 = ColorLED(16, A0);
#if defined(__AVR_ATmega1284P__) || defined (__AVR_ATmega644P__) || defined(__AVR_ATmega128RFA1__)
#define EspSerial Serial1
#define UARTSPEED 115200
#endif
#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
#include
SoftwareSerial mySerial(2, 3);
#define EspSerial mySerial
#define UARTSPEED 9600
#endif
ESP8266 wifi(&EspSerial);
bool wifimCottonbegin(const char* ssid, const char* pass, const char* id, const char* token)
{
WifiInit(EspSerial, UARTSPEED);
Serial.print(F("Start Connection mCotton...Waiting for 5S..."));
while (!wifi.setOprToStation());
while (!wifi.joinAP(ssid, pass));
wifi.setWiFiconnected(true);
while (!wifi.mqttSetServer("mCotton.microduino.cn", (1883)));
while (!wifi.mqttConnect(id, id, token));
wifi.setMqttConnected(true);
while (!wifi.mqttSetDiveceIDToken(id, token));
char SUBSCRIBTOPICA[30] = "";
strcat(SUBSCRIBTOPICA, "ca/");
strcat(SUBSCRIBTOPICA, id);
while (!wifi.mqttSetSubscrib(SUBSCRIBTOPICA));
char SUBSCRIBTOPICP[30] = "";
strcat(SUBSCRIBTOPICP, "cp/");
strcat(SUBSCRIBTOPICP, id);
while (!wifi.mqttSetSubscrib(SUBSCRIBTOPICP));
return true;
}
String uploadData(char* _st, int _data)
{
String send_data = "";
send_data = "{\"";
send_data += _st;
send_data += "\":\"";
send_data += _data;
send_data += "\"}";
return send_data;
}
String uploadData(char* _st, char* _data)
{
String send_data = "";
send_data = "{\"";
send_data += _st;
send_data += "\":\"";
send_data += _data;
send_data += "\"}";
return send_data;
}
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;
}
}
String PassageReceive;
void setup()
{
strip_A0.begin();
strip_A0.setPixelColor(0,0x000000);
strip_A0.show();
if(wifimCottonbegin("iPhone XS Max" , "qwertyui" , "5c95c3a430a51f0016df8c2e" , "2lF3LoLQNe6P"))
{
strip_A0.setPixelColor(0,0x19eee2);
strip_A0.show();
}
}
void loop()
{
PassageReceive = wifi.getMqttJson();
if (PassageReceive != "")
{
PassageReceive.trim();
if (wifi.isMqttConnected())
{
var_variable=solution(PassageReceive, "show_S1");
if(((var_variable.toInt()) == 0))
{
strip_A0.setPixelColor(0,0x000000);
strip_A0.show();
}
if(((var_variable.toInt()) == 1))
{
strip_A0.setPixelColor(0,0xee3900);
strip_A0.show();
}
if(((var_variable.toInt()) == 2))
{
strip_A0.setPixelColor(0,0x9aee35);
strip_A0.show();
}
}
}
}
立即注册