Pages: [1]   Go Down
Author Topic: Network Script to accept User Input  (Read 1265 times)
0 Members and 1 Guest are viewing this topic.
Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been looking days and trying for even more, so I'm sorry if this comes off as a really stupid 'nube' question, but..

How to do I input user commands into a network connected script?  Let me explain my ignorance,  if it isn't already too obvious):

I wrote (copied) a little sketch that uses the usual serial input call for some Int values such as:

    // if there's any serial available, read it:
    while (Serial.available() > 0) {
  
    // look for the next valid integer in the incoming serial.  Input as h,m,s :
     int hip= Serial.parseInt();
    // do it again:
     int mip= Serial.parseInt();
    // do it again:
     int sip= Serial.parseInt();
     // do it again:
     int Mode= Serial.parseInt();
        
     if (Serial.read() == '\n') // look for the newline. That's the end of your time input.
      {

Then I started working with my Ethernet shield and got that connected.  I tried modifying the user input/output and quickly found ways to send the output of my script back to the ip client/user, but I can't find an easy way to replace my Serial Terminal script above to allow the ip client to replace the Serial.available input.

I've looked at lots of Network Shield scripts but I haven't found one that does what I need to do, (either that or I don't know enough to see what I need when it's starring me in the face).

Anyone have a simple script example that will get me going in the right direction?

Thanks
  
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because the EthernetClient object inherits the Print class methods you could use almost the same interface, just replace the "Serial" with your client object. Keep in mind this is for a TCP client only, a web client has a different interface.

Quote
Anyone have a simple script example that will get me going in the right direction?

I don't know what you mean with this question, C++ is not a script language so you have to specify what script example you mean.

Show us the code you already have, we might be able to help you further then.
Logged

Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As you can tell have don't really understand too much of this yet, so when I said "script" I really meant an Arduino sketch.   As for my code it's too much of a mess right now to show you. What I do have is a fairly lengthy sketch that interfaces to a commercially available Timer Display that uses  RS422 like input.  The Timer gets its display data in the form of Pulse Width Coded bits driven by and received with RS422 chips.  My Arduino sketch works  to send it countdown timer info.  The sketch gets user input from comma deliminated "Int" via the Serial Terminal. When I modified it to take TCP data from my Network Shield I fairly easily got the return data to be sent over the IP, but no amount of fudging my sketch could get the user input data to replace the Serial.available data.  Each time I tried using one of the network commands to replace the Serial Input I got any number of compiling errors.  So what I would love to see is a simple sketch that would accept IP  comma deliminated data and when it sees a New Line, echo the input back to the user via the return ip path.  If I could see this, I'd easily be able to modify my sketch to work my Network Shield.

Thanks again.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you really have an Arduino Ethernet Shield (with WizNet5100 chip)? Do you use the Ethernet.h library that comes with the Arduino IDE?

As you see, without your code we're just guessing and we have better things to do. Show us at least the code that is giving you the errors and post the exact error messages you get.
Logged

Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes I have a new Arduino Ethernet Shiled with the Wiznet 5100.  I did include the Ethernet.h library.  Here's my pre-ethernet code:
Code:
/*

 
 Countdown sketch using Ascii input and Ascii output
 */
 int pw_out = 12;
 int sip = 0;         // seconds user input
 int mip = 0;         // minutes user input
 int hip = 0;         // hours user input
 int secs = 0;        // Calculated Seconds for countdown output
 int mins = 0;        // Calculated Minutes for countdown output
 int hours = 0;       // Calculated Hours for countdown output
 unsigned long T =0;            // Current Count Down Time in Seconds
 unsigned long TR = 0;          // Time Run since count down started
 unsigned long intmillis = 0;   // initial "millis" reading used to calculate TR
 int t = 0;    // variable
 int s = 0;    // variable
 int S = 0;    // variable
 int m = 0;    // variable
 int M = 0;    // variable
 int h = 0;    // variable
 int Mode = 0;    // Mode Variable
 //int incomingByte =0;
 
 
void setup() {
   Serial.begin(9600); // open the serial port at 9600 bps:
   pinMode(pw_out, OUTPUT);      
 }
 void loop() {

sip=0;
mip=0;
hip=0;
Mode=0;

//incomingByte = 0;

 // if there's any serial available, read it:
 while (Serial.available() > 0) {
    
    // look for the next valid integer in the incoming serial.  Input as h,m,s :
     int hip= Serial.parseInt();
    // do it again:
     int mip= Serial.parseInt();
    // do it again:
     int sip= Serial.parseInt();
      // do it again:
     int Mode= Serial.parseInt();
    
    
     if (Serial.read() == '\n') // look for the newline. That's the end of your time input.
      {  
     T = (sip) + (mip*60) + (hip*3600);  // Total user input Time in seconds.
     intmillis = millis();          // reinitalizes a new time reference at the start of a new count down loop.
     TR =0;                         // reinitalizes TR (TimeRun) for next user input countdown.
  
    
    while (secs >=0)      // If countdown is greater than "0" seconds keep generating code.
     {
    if (Mode==0)  TR = (millis() - intmillis)/1000;
    
    // time Count Down loop has been running since start (in seconds).
    secs = T - TR;        //Calculate seconds
    mins=secs/60;         //convert seconds to minutes
    hours=mins/60;        //convert minutes to hours
    secs=secs-(mins*60);  //subtract the coverted seconds to minutes in order to display 59 secs max
    mins=mins-(hours*60); //subtract the coverted minutes to hours in order to display 59 minutes max
  
    
   if (secs < 0)  // Forces Dispaly to 'loop' 0:00:00 when time has rundown to zero.
    {
   secs = 0;
   mins = 0;
   hours = 0;     //
   }
   else
 
   M= (mins/10);  // changes the 2-digit mins and secs characters into M,m & S,s format.
   m= (mins%10);
   S= (secs/10);
   s= (secs%10);
  
  
     Serial.print(hours);  //writes hours value to serial monitor
     Serial.print(":");    //writes :
     Serial.print(M);      //writes Minutes Decade value to serial monitor
     Serial.print(m);      //writes Minutes Unit value to serial monitor//writes :
     Serial.print(":");    //writes :
     Serial.print(S);      //writes Seconds Decade value to serial monitor
     Serial.print(s);      //writes Seconds Unit value to serial monitor//writes :
     Serial.println();     //prints line return
    
  
  // the following code writes a serial bit stream to pin 12 that is Pulse Width Coded.  A "1" bit is 556us ON and 277us OFF.  A "0" bit is the opposite, 277us ON and 566us OFF.
  
  //loads "0" into the display's 10ths digit.
  
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  
  
  
   switch (s) {

  case 0:
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  digitalWrite(pw_out, HIGH);   // LOW
  delayMicroseconds(277);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(556);
  break;
  
  case 1:
  digitalWrite(pw_out, HIGH);   // HIGH
  
// I've removed the rest of the CASE statements to allow my code to fit the size limit in this POST    
  
  digitalWrite(pw_out, HIGH);   // Unused Bit.  Always "1"
  delayMicroseconds(556);
  digitalWrite(pw_out, LOW);
  delayMicroseconds(277);

  delayMicroseconds(1500);      //1.5ms Short Interword Gap.
  
  
 
  // if there's any serial available, read it:
   if (Serial.available() > 0) {
  // incomingByte = Serial.read();
  
  break;
  
 
               }    
 
           }
 
       }
 
    }  
 
 }


I modified this so that my Network Shield would echo back the serial input:

Code:
#include <SPI.h>
#include <Ethernet.h>
/*

 
 Countdown sketch using Ascii input and Ascii output
 */
 // network configuration.  gateway and subnet are optional.
 
 // the media access control (ethernet hardware) address for the shield:
 byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x32, 0x36 };  
 //the IP address for the shield:
 byte ip[] = { 10, 0, 0, 55 };    
 // the router's gateway address:
 byte gateway[] = { 10, 0, 0, 1 };
 // the subnet:
 byte subnet[] = { 255, 255, 0, 0 };
 
// telnet defaults to port 23
 EthernetServer server = EthernetServer(23);
 
 
 int pw_out = 12;
 int sip = 0;         // seconds user input
  
   Serial.begin(9600); // open the serial port at 9600 bps:
   pinMode(pw_out, OUTPUT);      
 }
 void loop() {

 

Again I had to remove most of the code to fit the 9500 character limit.  It does show how I added the Network stuff.
I added this code to the orig sketch to echo the serial output.  It does send the data to the IP client.

Code:

 client.print(hours);  //writes hours value to serial monitor
     client.print(":");    //writes :
     client.print(M);      //writes Minutes Decade value to serial monitor
     client.print(m);      //writes Minutes Unit value to serial monitor//writes :
     client.print(":");    //writes :
     client.print(S);      //writes Seconds Decade value to serial monitor
     client.print(s);      //writes Seconds Unit value to serial monitor//writes :
     client.println();     //prints line return
    

So you see it's a bit of a mess.  I warned you.  Seeing my working serial code, how do I add the network stuff to allow ip data entery?


Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at the ChatServer example of the Ethernet library. More or less you can insert your "while (Serial.available() > 0)" loop into the example at the "while (client.available() > 0)" loop and replace the "Serial" word by "client".

You have a principal problem in your code. You're checking for available characters in the while statement. If there is at least one character you're doing the whole while block but there you read much more than one character. The read() method is non-blocking, so if no character is available in the buffer it returns -1 and is NOT waiting for a character to arrive.
Logged

Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks pylon for your input.  I'm pretty sure I tried that already and got one of the many compiling errors I had talked about.  Obviously I had other problems in my code, which by the way for the most part is a cut and paste of someone else's work. I'm really stumbling around trying to figure things out.  It's amazing to me my code even works as well as it does.

I think what I'll do is go back to the drawing board and write a really simple sketch in an attempt to 'hone' my networking skills.  I'll try to figure out the code to accept user input via TCPIP and echo it back to the user's session.  Again if anyone already has this sort of basic sketch written, I'd love to see it.  Something like this:  User enters coma deliminated characters like A,B,C (return/newline)  and then gets back to the client session "ABC".

Thanks
Logged

Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I looked at the ChatServer sketch.  This looks great. Thankyou.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Keep in mind that the ChatServer sketch uses server.write() (writing the same content to all connected clients) while you probably need client.write() (writing the content only to this particular client) for your application.
Logged

Pages: [1]   Go Up
Jump to: