Hi everyone,
I’m using the Photon device for my IoT project.
I’m trying to send a color value in hex format (like FF0000) to Photon over a TCP connection on port 6000 from a Java application.
I’m getting strange debugging situation. Here are the codes:
// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"
TCPServer server = TCPServer(6000);
TCPClient client;
// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"
/*
* This is a minimal example, see extra-examples.cpp for a version
* with more explantory documentation, example routines, how to
* hook up your pixels and all of the pixel types that are supported.
*
*/
#include "application.h"
SYSTEM_MODE(AUTOMATIC);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 60
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup()
{
// start listening for clients
server.begin();
// Make sure your Serial Terminal app is closed before powering your device
Serial.begin(9600);
// Now open your Serial Terminal, and hit any key to continue!
while(!Serial.available()) Particle.process();
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.SSID());
}
void loop()
{
if (client.connected()) {
String color;
color.reserve(10);
Serial.println(client.available());
while (client.available()) {
char c = client.read();
color+=c;
// server.println(client.read());
}
Serial.println(color);
/*String color = "FF00FF";
long number = (long) strtol( &color[1], NULL, 16);
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;*/
client.stop();
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}
}
And the Java’s one:
String sentence = "FF0000";
String modifiedSentence;
Socket clientSocket = new Socket("10.0.0.17", 6000);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(sentence);
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
From the console of Atom software I got just an F, which is the first char. If I try to print also the value of the client.available(), I’m getting one, why? As I explained before, the idea is to store the entire color value in the String value color in the Photon software. Thank you in advance for your help!