String from arduino input

Hello Everyone

I am looking at making an interface to a project I currently have. What I am trying to do is use an arduino with an ethernet shield, to output a text string to my pc.

Basically upon a digital input (on the arduino) it would send a text string to a specific port on the pc to command the software (on the pc) to do something.

From what I understand the arduino would be a client since the pc is the server just awaiting a command on a port.

I am wondering if anyone knows of a good place to start? Or maybe a link to something that reflects what I am trying to do?

Thanks

Look at any of the Ethernet client examples.

I'd start with File->Examples->Ethernet->TelnetClient. It takes characters from Serial and sends them to the server and takes characters from the server and sends them to Serial.

Basic event based client test code. When an e is received via the serial monitor (the event), the client code sends a GET request to a test page on my web account, which sends back a response viewable in the serial monitor.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println("Host: web.comporium.net");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Hello Everyone,

Ok, dont laugh, but I did this working somewhat. Here is my current issue. I can get the pc to do what it needs to by entering a “1” in the serial monitor or by shorting pin 2 to 5 volts. I also have pin 2 pulled down so it isnt floating.

When I do short pin 2 to 5 volts it sends alot of ones instead of just one number. Any suggestions on my code or ideas?
Yes I am new but trying hard to figure this out. The pc will accept a number or a command like “in”

Here is my code:

Telnet client
 
 This sketch connects to a a telnet server (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.  You'll need a telnet server 
 to test this with.
 Processing's ChatServer example (part of the network library) works well, 
 running on port 10002. It can be found as part of the examples
 in the Processing application, available at 
 http://processing.org/
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 14 Sep 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 */
//  Variables

int button;                 // button value, high or low/////////////////modified
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,231);

// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,1,112); 

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use  port 10002):
EthernetClient client;

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
pinMode(2, INPUT);  // sets the digital pin 2 as input///////////////modified

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 5150)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}


void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  
  {
    button = digitalRead(2);   // check if the pushbutton is pressed/////////////modified
    
  if (button == HIGH) {     // if it is, the buttonState is HIGH: ///////////////////modified  
    
     client.println("1"); //send signal to command pc/////////////////modified
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar); 
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}
}

Thanks!

When I do short pin 2 to 5 volts it sends alot of ones instead of just one number.

The technique used to eliminate your troubles is called "denouncing.". Search for numerous examples. Then, only send switch state changes to the PC.

mrburnette:

When I do short pin 2 to 5 volts it sends alot of ones instead of just one number.

The technique used to eliminate your troubles is called “denouncing.”. Search for numerous examples. Then, only send switch state changes to the PC.

I am confused I guess… Isnt this just a software way of doing a pull up or pull down resistor? Or is button state a better way of sending out a string?

I am not trying to argue or discount any advice, I just don’t understand what is different.

Thanks

jerkykid: I am confused I guess.... Isnt this just a software way of doing a pull up or pull down resistor? Or is button state a better way of sending out a string?

I am not trying to argue or discount any advice, I just don't understand what is different.

Thanks

Probably because mrburnette was mistaken. It's not an issue of debouncing (yet). Your code doesn't test when a button goes from being not pushed to pushed, it simply tests if it's down. Because loop() runs very fast (on the order of microseconds), it will send it repeatedly until the button is released. What you need to is to only send the data on the signal edge (when the button goes from LOW to HIGH). This concept is demonstrated in the StateChangeDetection example.

There are three parts you need to get right.

You need to design the circuit so that the input state matches the switch state. The best way to do that is with a pull-up resistor (with the switch connecting to ground) or a pull-down resistor (with the switch connecting to Vcc). Since the Arduino has internal pull-up resistors, this is the recommended approach. There are example sketches showing how to implement that.

When the switch is operated the input state will typically change back and forth many times rather than just change cleanly, due to the mechanical characteristics of the switch. To deal with that you need to wait until the input has settled in the new state rather than handle every momentary change. This is referred to as debouncing. You can do it in software or hardware. Software is recommended because it is cheaper and easier than adding external hardware. There are example sketches showing how to implement that.

If you only want to take action when the (debounced) input state changes, then you need to perform edge detection. In other words you need to compare the current state with the previous state and only take action when these are different. There are example sketches showing how to implement that.