using adafruit CC3000 wifi as serial monitor

I want to use the adafruit CC3000 wifi to send command to controller (UNO board) using any host(computer or phone) on the network, and the host (computer or phone) will return the command output for me).

for example:
command to be send:
get log
result returned:
ID:132456789, event: In, Time: 12:59:59
ID:987654321, event: out Time: 23:59:59
...

what do you plan to use on your computer to send the command and get the command output?

I did such a project using C# with sockets

yes, it is c# software

codingKnight:
I did such a project using C# with sockets

can you advice me with the functions you used in your software in controller side and C# side

sorry for the late reply

#include <Adafruit_CC3000.h>
#include <Adafruit_CC3000_Server.h>
#include <ccspi.h>

#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
#define WLAN_SSID “network_name” // cannot be longer than 32 characters!
#define WLAN_PASS “network_password”
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,SPI_CLOCK_DIV2);
#define WLAN_SECURITY WLAN_SEC_WPA2
uint32_t ip = cc3000.IP2U32(190,168,100,100);//ip address of your computer
int port = 6000;//port that you will use to communicate with computer
Adafruit_CC3000_Client client;
char commandChar;
String commandStr;

//function that connects to network and to my C# server
void loadNetwork(){
if (!cc3000.begin())
{
Serial.println(“Couldn’t begin()! Check your wiring?”);
}
Serial.print("Attempting to connect to ");
Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(“Failed!”);
} else {
connectedToServer = true;
Serial.println(“Connected!”);
Serial.println(“Request DHCP…”);
cc3000.checkDHCP();
delay(1000);

}
if(!client.connected()){
Serial.println(“CONNECTING”);
client = cc3000.connectTCP(ip, port);
delay(1000);
}
}

void setup(void)
{
Serial.begin(9600);
loadNetwork();
}

void loop(void){
//sends data to server
if(client.connected()){
client.print(data);
}
//accepts instructions
if(client.available())
{
commandStr = “”;
while(client.available()){
commandChar = client.read();
commandStr += commandChar;
}
}

}

I removed parts of my code just showing the code you need for read, write, start a server and accept a connection

for c# you need these

Int32 port = 6000;//port should be the same with arduino
IPAddress localAddr = IPAddress.Parse("190.168.100.100");//ip address of your computer
NetworkStream myNetworkStream;
byte myWriteBuffer;
TcpListener server;
Socket s;
Boolean streamOpen = false;

//just call this to start server
public void serverMain()
{
try
{
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
}
catch (Exception e)
{
}
}

//checking for connections
private void timer1_Tick(object sender, EventArgs e)
{
if (server.Pending())
{
//some code here
}
else
{
//some code here
}
}

//I placed it in a button to accept the connection of the arduino board
private void button1_Click(object sender, EventArgs e)
{
if (server.Pending())
{
try
{
s = server.AcceptSocket();
accepted = true;
myNetworkStream = new NetworkStream(s);
streamOpen = true;
}
catch (Exception)
{
}
}
else
{
//some code here
}
}

//read data send by arduino
private void networkMonitorTimer_Tick(object sender, EventArgs e)
{
if (accepted)
{
byte myReadBuffer = new byte[4096];
s.Receive(myReadBuffer);
String catData = Encoding.UTF8.GetString(myReadBuffer);
label3.Text += catData;
}
}

//send command to robot
private void commandRobot(String command)
{
if (s != null)
{
if (myNetworkStream.CanWrite)
{
myWriteBuffer = Encoding.UTF8.GetBytes(command);
myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
}
}
}

thank you Mr. codingKnight
it is very appreciated to share your experience with me

I do a tutorial using the functions you sent (files are attached)
I face 2 problems with this method:
1- you can use only the computer IP, so you need to change the sketch for each user to put its computer IP.
2- on each time you want to connect, you should restart the sketch to re-request the connection.

is there a way to solve this two problems, or I should use another method?!

IOT_tutorial.zip (60.9 KB)

  1. well my setup was a bunch of robots are connected to my pc so I really needed to use a static IP. So I don't have any suggestions for this one.
  2. you don't really have to re upload the sketch just let the connection die. my previous setup was using a usb wifi dongle and it preserves the connection for 20 mins or so I just adjusted that to a shorter time so that the previous connection dies immediately. or you can disconnect the socket connection from your pc server so when you restart your arduino it immediately connects.

--this is based on my experience with the project I did hope all positive ends happen the same to you

codingKnight:
--this is based on my experience with the project I did hope all positive ends happen the same to you

thank you Mr. codingKnight

@codingKnight please use the code button </> so your code looks like this and is easy to copy to a text editor

…R

Robin2:
@codingKnight please use the code button </> so your code looks like this and is easy to copy to a text editor

…R

sorry my bad

noted

after some reasearch, i see that the telnet chatserver is achieve my need.
I test the chatserever example of adafruit library, this example is only receive byte from the telnet client. I updated to receive string instead of one byte, and i try to also send string. Here i face a problem, the cc3000 is keep on the readString proccess till it get a string, so it can't send anything till it receive string.
below is my sketch loop, the other code is the same as the one on the example with out any change:

void loop(void)
{
	// Try to get a client which is connected.
	Adafruit_CC3000_ClientRef client = chatServer.available();
	client.setTimeout(100);
	if (client) {
		// Check if there is data available to read.
		Serial.println("CLient Connected");
		if (client.available() > 0) {
			String s = client.readStringUntil('\r');   // Read a string
			Serial.print("receive: ");
			Serial.println(s);
		}

		if (Serial.available()) {
			String line = Serial.readString();
			client.print(line);
			Serial.print("send: ");
			Serial.println(line);
		}
	}
	delay(100);
}

so, I need a way to complete my code if there is nothing to read like the serial connection?

mm you got stuck here?

if (client.available() > 0) {
			String line = Serial.readString();

mm tried giving the string some time to come in before reading it?
and remove the "> 0"?

//check string if available
if(client.available())
{
        //give string time to come in
        delay(400);
	String s = client.readStringUntil('\r');   // Read a string
	Serial.print("receive: ");
	Serial.println(s);
}

the same,
the code still waiting the string

I tried to receive byte instead of string, but in this case I failed to send anything. so what is the problem?!!!