Hi guys,
Not really sure if this is Hardware or firmware but my problem is with the TCP libraries. I’ve been using them to try and set up a server on one core with a thermistor attached, read data from the thermistor and transmit it to a core running TCP client which then prints it over serial. So far I’ve managed to get working thermistor code here:
#include <math.h>
float vcc = 3.29; // only used for display purposes, if used
// set to the measured Vcc.
float pad = 9940; // balance/pad resistor value, set this to
// the measured resistance of your pad resistor
float thermr = 10000; // thermistor nominal resistance
float Thermistor(int RawADC) {
long Resistance;
float Temp; // Dual-Purpose variable to save space.
Resistance=pad*((4095.0 / RawADC) - 1);
Serial.println(Resistance);
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later
Serial.println(Temp);
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Serial.println(Temp);
Temp = Temp - 273.15; // Convert Kelvin to Celsius
Serial.println(Temp);
return Temp;
}
/*
double Thermistor(int RawADC) {
double Temp;
Temp = log(10000.0*((1024.0/RawADC-1)));
// =log(10000.0/(1024.0/RawADC-1)) // for pull-up configuration
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}
*/
int aRead = A0;
int val = 0; // variable to store the read value
// EXAMPLE USAGE
// telnet defaults to port 23
TCPServer server = TCPServer(80);
TCPClient client;
void setup()
{
Serial.begin(9600);
// start listening for clients
// server.begin();
while(!Serial.available()) SPARK_WLAN_Loop();
// Make sure your Serial Terminal app is closed before powering your Core
// Now open your Serial Terminal, and hit any key to continue!
}
void loop()
{
float temp;
val = analogRead(aRead);
Serial.print(val);
temp=Thermistor(val); // read ADC and convert it to Celsius
Serial.print("Celsius: ");
Serial.print(temp); // display Celsius
//temp = (temp * 9.0)/ 5.0 + 32.0; // converts to Fahrenheit
//Serial.print(", Fahrenheit: ");
//Serial.print(temp,1); // display Fahrenheit
Serial.println("");
delay(5000); // Delay a bit...
}
And I have some TCP code posted below. The problem is this seems to be increibly tempromental it works some days and other days it refuses too. Here is the code:
Server
// EXAMPLE USAGE
// telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;
void setup()
{
Serial.begin(9600);
// start listening for clients
server.begin();
while(!Serial.available()) SPARK_WLAN_Loop();
// Make sure your Serial Terminal app is closed before powering your Core
// Now open your Serial Terminal, and hit any key to continue!
}
void loop()
{
int c;
if (client.connected()) {
while (client.available()) {
Serial.print("It's working, promise: ");
c = client.read();
}
} else {
// if no client is yet connected, check for a new connection
Serial.print(" No connection here ");
client = server.available();
}
Serial.print(c);
delay (2000);
}
Client
byte serv[] = { 172, 16, 0, 222 }; //dos
//TCPServer server = TCPServer(23);
TCPClient client;
void setup()
{
Serial.begin(9600);
// start listening for clients
while(!Serial.available()) SPARK_WLAN_Loop();
// client.connect(serv,23);
if (client.connect(serv, 23))
{
Serial.println("connected");
client.println("We are all connected");
}
else
{
Serial.println("connection failed");
}
}
void loop()
{
if (client.connected())
{
int c = client.read();
Serial.print(c);
}else{
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;){
Spark process;
}
}
}
Here’s the test data over the last two days. My two cores both have assigned IP’s on a open wifi network at my office. The Ip’s are as follows:
Sparky Uno 172, 16, 0, 221
Sparky Duo 172, 16, 0, 222
Here’s the test data:
Testing Cores with different code.TCPOLDC on UNO / TCPOLDS on DUO ---- DIDN’T WORK
TCPOLDS on UNO / TCPOLDC on DUO ---- DIDN’T WORK
TCPCPP Client on UNO / TCPCPP Server on DUO ---- DIDN’T WORK
TCPCPP Server on UNO / TCPCPP Client on DUO ---- WORKED! But, shouldn’t work… IP is wrong… Still worked after reset.Changed “Int” to “Float” and added in thermistor stuff. No luck.
New day:
Changed “Float” to “Long”. No luck.
Re-test day after TCPCPP Server on UNO / TCPCPP Client on DUO (with thermistor code there
but not called) Didn’t work!?!?Re-test again(With therm code commented out) DIDN’T WORK
TCPOLDS on UNO / TCPOLDC on DUO ---- DIDN’T WORK
TCPOLDC on UNO / TCPOLDS on DUO ---- DIDN’T WORK
TCPCPP Client on UNO / TCPCPP Server on DUO ---- DIDN’T WORK
TCPCPP Server on UNO / TCPCPP Client on DUO ---- DIDN’T WORK
I don’t understand why my code works one day but not another. Is this the fault of the wifi at work? To clear up-
TCPOLDS = Old server code (Known good code)
TCPOLDC = Old Client code (Known good code)
TCPCPPServer = Server code with therm
TCPCPPClient = client code with therm
Sorry for the lecture, hope someone can help, thanks in advance