Go Down

Topic: Network Script to accept User Input (Read 1 time) previous topic - next topic

mfriesen

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
 

pylon

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.

mfriesen

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.

pylon

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.

mfriesen

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: [Select]

/*


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: [Select]

#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: [Select]


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?



Go Up