Hi all,
I am trying to write an Arduino sketch (that will be uploaded to an Arduino Uno combined with a CC3000 wireless module by Sparkfun).
This sketch should connect to a server PC, and send information via TCP/IP in the form of “1” or “0”.
The problem is that the Server program (run separately on a PC with an IP of 192.168.2.13 [not static]),
does not seem to identify that the CC3000 (the client), is trying to establish a connection.
The current Arduino sketch (Client) is as below.
Of course I changed the “SSID” and “PASSWORD” with the actual SSID and PASSWORD… The CC3000 initializes and connects to the local WiFi with no problem.
Client Code (Arduino UNO + CC3000 WiFi Side):
//////////////// WIFI INITIALIZATION //////////////////
/****************************************************************/
#include <SPI.h>
#include <SFE_CC3000.h>
#include <SFE_CC3000_Client.h>
// Pins
#define CC3000_INT 2 // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN 7 // Can be any digital pin
#define CC3000_CS 10 // Preferred is pin 10 on Uno
// Connection info data lengths
#define IP_ADDR_LEN 4 // Length of IP address in bytes
// Constants
char ap_ssid[] = "SSID"; // SSID of network
char ap_password[] = "PASSWORD"; // Password of network
unsigned int ap_security = WLAN_SEC_WPA2; // Security of network
// ap_security can be any of: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
// WLAN_SEC_WPA, or WLAN_SEC_WPA2
unsigned int timeout = 10000; // Milliseconds
char server[] = "192.168.2.13"; // Remote server
// Global Variables
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);
int ConnectionIndicator;
/****************************************************************/
//////////////// LDR INITIALIZATION //////////////////
/****************************************************************/
int LDR = 0; //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0; //that’s a variable to store LDR values
int light_sensitivity = 500; //This is the approx value of light surrounding your LDR
boolean Toilet_Stat;
/****************************************************************/
void setup()
{
//////////////// WIFI SETUP //////////////////
/****************************************************************/
ConnectionInfo connection_info;
int i;
// Initialize Serial port
Serial.begin(115200);
Serial.println();
Serial.println("---------------------------");
Serial.println("SparkFun CC3000 - WebClient");
Serial.println("---------------------------");
// Initialize CC3000 (configure SPI communications)
if (wifi.init())
{
Serial.println("CC3000 initialization complete");
}
else
{
Serial.println("Something went wrong during CC3000 init!");
}
// Connect using DHCP
Serial.print("Connecting to SSID: ");
Serial.println(ap_ssid);
if (!wifi.connect(ap_ssid, ap_security, ap_password, timeout))
{
Serial.println("Error: Could not connect to AP");
}
// Gather connection details and print IP address
if (!wifi.getConnectionInfo(connection_info))
{
Serial.println("Error: Could not obtain connection details");
}
else
{
Serial.print("IP Address: ");
for (i = 0; i < IP_ADDR_LEN; i++)
{
Serial.print(connection_info.ip_address[i]);
if (i < IP_ADDR_LEN - 1)
{
Serial.print(".");
}
}
Serial.println();
}
// Make a TCP connection to remote host
Serial.print("Connecting with Server: ");
Serial.println(server);
ConnectionIndicator = client.connect(server, 80);
Serial.println(ConnectionIndicator); //Returns an int (1, -1, -2, -3, -4) indicating connection status:
//SUCCESS (1)
//TIMED_OUT (-1)
//INVALID_SERVER (-2)
//TRUNCATED (-3)
//INVALID_RESPONSE (-4)
if (ConnectionIndicator==1)
{
Serial.println("Success: TCP connection Established!");
}
else if (ConnectionIndicator <= -1)
{
Serial.println("Error: Could not make a TCP connection");
}
/****************************************************************/
//////////////// LDR SETUP //////////////////
/****************************************************************/
pinMode(13, OUTPUT); //Use 13 because there is already a built in yellow LED in Arduino which shows output when pin 13 is enabled.
}
void loop()
{
LDRValue = analogRead(LDR);//reads the ldr’s value through LDR
if (LDRValue>500)
{
Toilet_Stat = 0;
Serial.println(LDRValue);//prints the LDR values to serial monitor
Serial.println(Toilet_Stat);
client.write(Toilet_Stat);
}
else
{
Toilet_Stat = 1;
Serial.println(LDRValue);//prints the LDR values to serial monitor
Serial.println(Toilet_Stat);
client.write(Toilet_Stat);
}
if (LDRValue > light_sensitivity)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}
delay(1000);
//This is the speed by which LDR sends value to arduino
/****************************************************************/
}
Server Code (Computer Side):
Written in C#
//
/* Server Program */
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
public class serv
{
public static void Main()
{
try
{
IPAddress ipAd = IPAddress.Parse("192.168.2.13");
// use local m/c IP address, and
// use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 80);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 80...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
I guess the problematic section with the Arduino code, is this:
I can’t seem to understand what is wrong with it, maybe there is something missing…
// Make a TCP connection to remote host (server computer)
Serial.print("Connecting to Server: ");
Serial.println(server);
ConnectionIndicator = client.connect(server, 80);
Serial.println(ConnectionIndicator); // "client.connect" function returns an int (1, -1, -2, -3, -4) indicating connection status:
//SUCCESS (1)
//TIMED_OUT (-1)
//INVALID_SERVER (-2)
//TRUNCATED (-3)
//INVALID_RESPONSE (-4)
if (ConnectionIndicator==1)
{
Serial.println("Success: TCP connection Established!");
}
else if (ConnectionIndicator <= -1)
{
Serial.println("Error: Could not make a TCP connection");
}
This code line:
client.connect(server, 80);
Doesn’t seem to return one of the values (1, -1, -2, -3, -4)
instead it always returns “0”
which is not even in the options for this function as I understand…
By the way, the C# code (Server part) was taken from here:
The server+client code written in this page seems to work perfectly together.
I am puzzled…
Help would be appreciated a lot!
Thanks in Advance.](Introduction to TCP client server in C# - CodeProject)