So I’m not sure I totally understand how the TCP code works and I’ve been having a little trouble getting two photons communication back and forth with each other using client.write(buff,len)
and server.write(buff,len)
.
So I have a “console” photon connecting to a “dome” photon (the names are relevant to the project we’re working on). The console connects as a client to the dome and then booth should then send their names to each other. This is all I’m trying to get working right now. I have it functioning one way with server.write(buff,len)
and client.read()
in a loop taking out one byte at a time. The console prints the correct name “DOME” to the serial monitor.
The server code can’t seem to read the console’s name however, and I think this is just simple user error. I don’t really understand why there’s a client.write()
function with no server.read()
function to pair with it, so I’m just using client.read()
in both cases. This worked for a single character but not for the buffers. The two files I’m using are below, can anyone point me in the right direction here?
console.ino
SYSTEM_THREAD(ENABLED)
//Here we declare our server and our client objects
TCPServer server = TCPServer(78);
TCPClient client;
//Here we have the IP address for the other photon we're communication with and
//it's port number.
byte dome_IP[4] = {192,168,11,101};
byte pod_IP[4] = {192,168,11,102};
int dome_Port = 79;
int pod_Port = 80;
//This is a value unique to this code to send out
const unsigned char name[7] = {'C','O','N','S','O','L','E'};
char other
Name[4] = {0};
//Here's the pin for the button we're using
int buttonPin = D0;
//User LED pin
int userLED = D7;
int i = 0;
//Array used for debouncing the switch
uint8_t debounce = 0b00000000;
//Holds the state of the userLED
bool userLED_state = false;
bool buttonState = false;
void setup() {
//Turn off wifi so we can set up our static ip
WiFi.off();
//Set static ip info
IPAddress myAddress(192,168,11,100);
IPAddress netmask(255,255,255,0);
IPAddress gateway(192,168,11,1);
IPAddress dns(192,168,11,1);
//Actually set the static ip
WiFi.setStaticIP(myAddress, netmask, gateway, dns);
//Tell the photon to use the static ip
WiFi.useStaticIP();
//Reconnect to wifi
WiFi.on();
WiFi.connect();
//Start listening for clients
server.begin();
//Start up serial so we can communicate various commands
Serial.begin(9600);
//Hold still and check wifi until we're connected over serial
while(!Serial.available()) Particle.process();
Serial.println("Sup guys! I'm the console!");
//Print out wifi credentials, just to make sure everything is ok
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
//Some pinMode calls to initialize our GPIO
pinMode(buttonPin, INPUT);
pinMode(userLED, OUTPUT);
digitalWrite(userLED, LOW);
}
void loop() {
//Runs for maintenance
/*Particle.process();*/
//If we're connected to a client, which we should be most of the time, send
//a value to the client to make sure we're connected. If not try to connect
//to a client.
/*if(client.connected())
{
Serial.println("Oh hey there!");
server.write(buttonState);
}
else
{
Serial.println("And nobody came....");
client = server.available();
}*/
if(client.connected())
{
Serial.println("Hey dome!");
if(client.available())
{
client.write(name,7);
Serial.print("From the dome... ");
while(client.available())
{
otherName[i] = client.read();
i++;
}
i = 0;
for(i=0;i<4;i++)
{
Serial.print(otherName[i]);
}
Serial.print("\t");
}
}
else
{
client.connect(dome_IP,dome_Port);
/*Serial.println("dome?");*/
}
//Here we read
debounce = debounce<<1;
debounce |= digitalRead(buttonPin);
/*Serial.print("debounce: ");
Serial.print(debounce,BIN);*/
if(debounce == 0b10000000)
{
buttonState = true;
}
else if(debounce == 0b01111111)
{
buttonState = false;
}
/*Serial.print("\tUser LED state: ");
Serial.println(userLED_state);*/
digitalWrite(userLED, buttonState);
delay(100);
}
And here’s dome.ino
SYSTEM_THREAD(ENABLED)
//Here we declare our server and our client objects
TCPServer server = TCPServer(79);
TCPClient client;
//Here we have the IP address for the other photon we're communication with and
//it's port number.
byte console_IP[4] = {192,168,11,100};
byte Pod_IP[4] = {192,168,11,102};
int console_Port = 78;
int pod_Port = 80;
//This is a value unique to this code to send out
const unsigned char name[4] = {'D','O','M','E'};
char otherName[7] = {0};
//Here's the pin for the button we're using
int buttonPin = D0;
//User LED pin
int userLED = D7;
int i = 0;
//Array used for debouncing the switch
uint8_t debounce = 0b00000000;
//Holds the state of the userLED
bool userLED_state = false;
bool buttonState = false;
void setup() {
//Turn off wifi so we can set up our static ip
WiFi.off();
//Set static ip info
IPAddress myAddress(192,168,11,101);
IPAddress netmask(255,255,255,0);
IPAddress gateway(192,168,11,1);
IPAddress dns(192,168,11,1);
//Actually set the static ip
WiFi.setStaticIP(myAddress, netmask, gateway, dns);
//Tell the photon to use the static ip
WiFi.useStaticIP();
//Reconnect to wifi
WiFi.on();
WiFi.connect();
//Start listening for clients
server.begin();
//Start up serial so we can communicate various commands
Serial.begin(9600);
//Hold still and check wifi until we're connected over serial
while(!Serial.available()) Particle.process();
Serial.println("Sup guys! I'm the dome!");
//Print out wifi credentials, just to make sure everything is ok
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
//Some pinMode calls to initialize our GPIO
pinMode(buttonPin, INPUT);
pinMode(userLED, OUTPUT);
digitalWrite(userLED, LOW);
}
void loop() {
//Runs for maintenance
/*Particle.process();*/
//If we're connected to a client, which we should be most of the time, send
//a value to the client to make sure we're connected. If not try to connect
//to a client.
if(client.connected())
{
Serial.println("Oh hey there!");
server.write(name,4);
while(client.available());
{
otherName[i] = client.read();
i++;
}
i = 0;
Serial.print("And I got a...");
for(i=0;i<7;i++)
{
Serial.print(otherName[i]);
}
Serial.print("\t");
}
else
{
/*Serial.println("And nobody came....");*/
client = server.available();
}
//Here we read
debounce = debounce<<1;
debounce |= digitalRead(buttonPin);
/*Serial.print("debounce: ");
Serial.print(debounce,BIN);*/
if(debounce == 0b10000000)
{
buttonState = true;
}
else if(debounce == 0b01111111)
{
buttonState = false;
}
/*Serial.print("\tUser LED state: ");
Serial.println(userLED_state);*/
digitalWrite(userLED, buttonState);
delay(100);
}