OVERALL HOME    Other material for programmers, not Arduino specific
ARDUINO: Top Page    Branch "A": Arduino Course    Branch "B": Arduino "How To"s and Projects
ArduServer and AdruSimpSrv: HOME
-d- Bookmark this on Delicious   Recommend to StumbleUpon

Arduino as TCP/IP server, AdruSimpSrv: The Sourcecode

With input and output

Locally... or across the internet

This page is browser friendly, by the way. Make your browser's window less wide than your whole screen and you will find the narrower columns much easier to read. For more tips, see my Power Browsing hints.

This page is not(?) enough

I strongly recommend that you at least skim through the information on my page about how this code was developed, but if you want just "the answer", here it is. At least here is the Arduino half of things. You also need "bespoke" client software to access the server which is created by the following. (Free client software is available. See the development page, link above.)

Client Software

Remember: To access the ArduSimpSrv, you need "bespoke" client code. One program which can send requests to an ArduSimpSrv is my free Windows program, TCP006. I hope someone will write an equivalent program for Linux, and let me post a link to it here.

Fancier client programs... one to record the data being seen by the ArduSimpSrv, perhaps?... is certainly possible. One server can service many needs.

STOP PRESS: It seems that maybe there's an element of bad advice in some of the pages this leads to.

It may be a bad idea to use any of the following at the same time as a W5100 based shield....

... and D13, but this last one only if you "care" that on the shield it goes to an LED, and from there through a resistor to ground. (This merely parallels the same thing on many Arduino and clone boards... BUT, to digress from the digression, BE AWARE that the software behind "setup()" and "loop()" pulse D13 high briefly before executing whatever you specified in "setup()".)

Of course, I unwittingly used some of those lines in my designs. The designs SEEM to work, but it would be a good idea to take what I've put on those lines and move them elsewhere. I will gradually be revising my designs as follows....

In every case, to modify the software is easy... but you will need to look carefully to see whether what is currently online has been modified or not.

To change the line used for, say, the phototransistor watching the LED in, say, ArduServer2, the line....

client.print("<br>Analog input 0 reads: <b>");
client.print(analogRead(0));

... must be changed to...

client.print("<br>Analog input 1 reads: <b>");
client.print(analogRead(1));

... and you are done! (You'll have to move one wire on your breadboard, too, if you had it wired for the original design.)

(Digression: Is nothing every easy? In 99.0% of my code, such a change would merely have been a matter of changing something like the "const byte bLEDpin=8;" at the start of the program. But in this case, to avoid fighting with... not hard... getting the prompt to identify the channel, I took a shortcut and specified the channel explicitly instead of a global variable. Sigh.

Sorry! I could have done without this, too! When will the Official Arduino Site have proper documentation of the ethernet shield and library??


The Arduino Sourcecode for ArduSimpSrv....

//
// TCP005: A simple server... not a web server... for Arduino..
//    An expanded TCP004, which illustrates the core elements.
//    Requires purpose written client software.... see below.

// See http://sheepdogguides.com/arduino/art5simpsrv.htm for details

//Version: 11 Oct 2011 (with some tweaks done on webpage)
//N.B. THE FOLLOWING ALSO "define" the version. And they can
//   be read by the client, with command $7E.
//These MUST be set to printable ASCII, e.g 'A' or 65, as
//   the client software expects this.
//The checksum can be in the range 0-255
static uint8_t bVerMostSB='0';
static uint8_t bVerMidSB='0';
static uint8_t bVerLSB='1';

//    Requires purpose-crafted client software.
//    TCP002.exe in....
//    http://sheepdogguides.com/zips/TCP001.zip
//    ... is suitable. for accessing the BB 82
//        command and (static) response.
//    If you know of, or have written, Linux
//    equivalents, please contact http://sheepdogguides.com/
//
// This code is in the public domain.
//
// It requires a W5100 based ethernet interface, although converting
//    the software to run with a Microchip ENC28J60 should not be hard.

//To do: Add "read 1-Wire"... and make client s/w dongleable via 1-Wire chip?

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

//The following determine the MAC address and IP address
//   that will be in the ethernet shield on your Arduino.
//Both must be unique on your LAN

uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
uint8_t ip[] = { 192,168,0,242 };

//The following determines what port the server will listen on.
int iSPort = 5202;

Server server(iSPort);

boolean boLEDOn;//To do with non-essential LED on/off stuff
uint8_t bLEDpin=8;//To do with non-essential LED on/off stuff
  //Software assumes an LED-with-resistor on this pin. See LEDon,
  //LEDoff subroutines.
  //(Pin 13 unsuitable, for some reason, when using Ethernet Pro hardware)
uint8_t bBeepPin=7;//Pin a sounder will be connected to. Pin
  //high: Beep on, low: off.
uint8_t bBeepDur=10;
  //Length of beep, in milliseconds. Do not make this longer
  //  than you can help. The system is blocked while the beep
  //  is on. (Too short, and the sounder can't sound, though.)
//N.B.The pins defined by bLEDpin and bBeepPin will be configured as an output...
  //be careful to match your program with your hardware!

int iCommandsAccepted;//Where we count how many commands have come in,
// or things that we thought might be commands...

uint8_t response[68];
// N.B. Size of response limited by what you put in prior line.
// Be careful not to overrun the buffer.
uint8_t bRespLen;

boolean boValidCommand;

void Beep(){
digitalWrite(bBeepPin,HIGH);
delay(bBeepDur);
digitalWrite(bBeepPin,LOW);
};

void LEDOn(){
//Note that this makes the assumption that the LED+resistor
//  go to GND from the output pin controlling them
//Just change "HIGH" to "LOW" if your LED is wired the other
//  way.
    digitalWrite(bLEDpin,HIGH);
    boLEDOn=true;
}

void LEDOff(){
//Note that this makes the assumption that the LED+resistor
//  go to GND from the output pin controlling them
//Just change "LOW" to "HIGH" if your LED is wired the other
//  way.
    digitalWrite(bLEDpin,LOW);
    boLEDOn=false;
}

void ProcessBB82(){
      uint8_t *r = response;
      *r++ = 0xBB;
      *r++ = 0x82;
      *r++ = 0x01;
      *r++ = 0x01;
      *r++ = 0xFF;
      *r++ = 0xFF;
      bRespLen=6;
      boValidCommand=true;
}

void Process7E81(){
// Report Version information to client.
//   This routine as yet untested, as a way to call
//   it does not exist in the client software

      uint8_t *r = response;
      *r++ = 0x7E;
      *r++ = 0x54;
      *r++ = bVerMostSB;
      *r++ = bVerMidSB;
      *r++ = bVerLSB;
      *r++ = bVerMidSB ^ bVerLSB;//XOR the bytes (checksum)
      bRespLen=6;
      boValidCommand=true;
}

void Process7F80(){
      uint8_t bMS=iCommandsAccepted >> 8;//MSB of iCommandsAccepted
      uint8_t bLS=iCommandsAccepted & 0xFF;//LSB of iCommandsAccepted

      uint8_t *r = response;
      *r++ = 0x7F;
      *r++ = 0x55;
      *r++ = bMS;
      *r++ = bLS;
      *r++ = bMS ^ bLS;//XOR the bytes (checksum)
      bRespLen=5;
      boValidCommand=true;
}

void Process50AF(){
      LEDOff();
      uint8_t *r = response;
      *r++ = 0x50;
      *r++ = 0x00;
      *r++ = 0x05;
      bRespLen=3;
      boValidCommand=true;
}

void Process51AE(){
      LEDOn();
      uint8_t *r = response;
      *r++ = 0x51;
      *r++ = 0x00;
      *r++ = 0x04;
      bRespLen=3;
      boValidCommand=true;
}

void Process718E(){
      uint16_t iAn1=analogRead(1);
      uint8_t bMS=iAn1 >> 8;//MSB of iAn1
      uint8_t bLS=iAn1 & 0xFF;//LSB of iAn1

      uint8_t *r = response;
      *r++ = 0x71;
      *r++ = 0x51;
      *r++ = bMS;
      *r++ = bLS;
      *r++ = bMS ^ bLS;//XOR of data bytes, "checksum"
      bRespLen=5;
      boValidCommand=true;
}


void setup(){
  iCommandsAccepted=0;
  pinMode(bLEDpin,OUTPUT);
  LEDOn();//Also makes boLEDOn true.
  pinMode(bBeepPin,OUTPUT);
  digitalWrite(bBeepPin,LOW);

  // Initialize the W5100 ethernet chip
  Ethernet.begin(mac, ip);

  // Start the serial library (for debug output)
  // The serial monitor is not required for the server's operation.
  Serial.begin(9600);

  // Start the TCP server
  Serial.print("Opening server...\n");
  server.begin();
}

void loop(){
  Client c = server.available();
  if (c.connected()) {
    Serial.print("Incoming connection!\n");
    // Read two bytes from client
    uint8_t b1 = c.read();
    uint8_t b2 = c.read();
    ++iCommandsAccepted;

    // Debugging: Show me what the client sent
    Serial.print("The client said 0x");
    Serial.print(b1,HEX);
    Serial.print(" 0x");
    Serial.print(b2,HEX);

    Serial.print("   ");
    Serial.print(iCommandsAccepted);

    Serial.print("\n");

    // Deal with the command...
    boValidCommand=false;//Set true within a command processor, if one found
    if ((b1 == 0x50) and (b2==0xAF)) Process50AF();
    if ((b1 == 0x51) and (b2==0xAE)) Process51AE();
    if ((b1 == 0x71) and (b2==0x8E)) Process718E();
    if ((b1 == 0x7E) and (b2==0x81)) Process7E81();
    if ((b1 == 0x7F) and (b2==0x80)) Process7F80();
    if ((b1 == 0xBB) and (b2==0x82)) ProcessBB82();

    // Send response
    if (boValidCommand) {
          c.write(response,bRespLen);
          Beep();//It would be possible to move this
            //to within the handlers of selected
            //commands, if you did not want a beep
            //for EVERY command processed.
            //Or, elsewhere, it would report all
            //requests for service, whether valid or not.
          }

    };//End of if c.connected (then...) block
    // Close the connection... TKB: close connection or stop server? Don't want latter!
    //TKB REMMED OUT, as CONNECTION REPEATEDLY CLOSED...
    //c.stop();
    //seems to work! (Didn't (properly) with c.stop... server shut down?
 }//end of Loop()



I hope that was useful...

Remember: The code is explained for you at my page explaining the ArduSimpSrv creation.




Search for other things...


Please note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.


Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers, but yes, they do pay me if you use this link! As do the Google advertisers, about whom I know nothing, of course.)



Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

CSS behind the page checked, at least once upon a time!, with http://jigsaw.w3.org/css-validator/
Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.


Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....