作者:书友32368660 | 来源:互联网 | 2023-09-06 09:26
我想创建一个LED灯条,可以从PC上控制Wifi。要获取数据,我使用Arduino MKR wifi1010。当我从PC向Arduino发送消息时,它只读取了前2个字节/字符。
首先,LED灯条的最后几个LED会以随机的颜色发光,但是我可以通过从计算机发送比所需更多的字节来解决此问题。如果每个LED均为白色,则该指示灯有效。同样,第一个LED可以发红光。其他所有东西都不起作用。
其原因不是LED灯带,因为我可以从Arduino正常控制LED灯带。问题是我的PC和Arduino之间的传输。
这是Arduino的代码。
#include
#include
#include
#include "FastLED.h"
#define NUM_LEDS 300
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
//#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "WifiSSID"; // your network SSID (name)
char pass[] = "WifiPassword"; // your network password (use for WPA,or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wifi radio's status
int LED_Num = 300;
WiFiServer server(56393);
void setup() {
Serial.begin(9600); // initialize serial communication
FastLED.addLeds(leds,NUM_LEDS);
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareversion();
if (fv Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid,pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now,so print out the status
}
char str[4];
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
int NoData = 0;
while (client.connected()) { // loop while the client's connected
if (client.available() ) { // if there's bytes to read from the client,NoData = 0;
char c = client.read(); // read a byte,then
currentLine += c; // add it to the end of the currentLine
} else {
NoData++;
if (NoData > 1000) {
char message[currentLine.length()];
currentLine.toCharArray(message,currentLine.length());
if (message[0] == (char)0) {
Serial.println("init");
byte high = highByte(LED_Num);
byte low = lowByte(LED_Num);
byte intBytes[] = {low,high};
client.print(byteArrayToString(intBytes));
break;
} else if (message[0] == (char)1) {
if (sizeof(message) >= NUM_LEDS * 3 + 1) {
Serial.println("set");
char data[sizeof(message) - 1];
RemoveFirstElement(message,data);
for (int i = 0; i leds[i] = CRGB((int)data[i * 3 + 0],(int)data[i * 3 + 1],(int)data[i * 3 + 2]);
}
byte ToReturn[1];
client.print(byteArrayToString(ToReturn));
FastLED.show();
break;
}
} else if (message[0] == (char)2) {
Serial.println("get");
break;
}
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
Serial.println("---------------");
}
}
String byteArrayToString(byte * bytes) {
return String((char *)bytes);
}
void RemoveFirstElement(char* bytes,char* newBytes) {
for (int i = 0; i newBytes[i] = bytes[i + 1];
}
bytes = newBytes;
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
Serial.println(ip);
}
我在C#中创建了一个库,用于从我的计算机控制LED,库的代码为:
public class Device {
public Color[] LEDs = null;
string host = "";
int port;
public Device( string host,int port ) {
this.host = host;
this.port = port;
}
public void init( ) {
byte[] SendData = new byte[1];
SendData[0] = 0;
byte[] GetData = Send( SendData );
LEDs = new Color[BitConverter.ToInt32( GetData,0 )];
}
public void set( ) {
byte[] SendData = new byte[1];
SendData[0] = 1;
SendData = concat( SendData,ColorArrayToByteArray( LEDs ) );
SendData = concat( SendData,new byte[500] );
Send( SendData );
}
public void get( ) {
byte[] SendData = new byte[1];
SendData[0] = 2;
byte[] GetData = Send( SendData );
LEDs = ByteArrayToColorArray( GetData );
}
byte[] Send( byte[] data ) {
TcpClient client = new TcpClient( host,port );
NetworkStream stream = client.GetStream( );
stream.Write( data,data.Length );
if ( data[0] == 0 )
data = new byte[4];
else if ( data[0] == 1 )
data = new byte[1];
else if ( data[0] == 2 )
data = new byte[LEDs.Length * 3];
Int32 bytes = stream.Read( data,data.Length );
return data;
}
byte[] ColorArrayToByteArray( Color[] colors ) {
byte[] bytes = new byte[colors.Length * 3];
for ( int i = 0; i bytes[i * 3 + 0] = (byte) colors[i].R;
bytes[i * 3 + 1] = (byte) colors[i].G;
bytes[i * 3 + 2] = (byte) colors[i].B;
}
return bytes;
}
Color[] ByteArrayToColorArray( byte[] bytes ) {
Color[] colors = new Color[bytes.Length / 3];
for ( int i = 0; i int R = bytes[i + 0];
int G = bytes[i + 0];
int B = bytes[i + 0];
colors[i / 3] = Color.FromArgb( R,G,B );
}
return colors;
}
public static byte[] concat( byte[] x,byte[] y ) {
if ( x == null )
throw new ArgumentNullException( "x" );
if ( y == null )
throw new ArgumentNullException( "y" );
int oldLen = x.Length;
Array.Resize( ref x,x.Length + y.Length );
Array.Copy( y,x,oldLen,y.Length );
return x;
}
}
在此示例中,只有第一个LED闪烁红色:
Device d = new Device( "192.168.178.53",56393 );
d.init( );
for ( int i = 0; i d.LEDs[i] = Color.Red;
}
d.set( );