Sample : Arduino TCP

#include <SPI.h>

#include <Ethernet.h>

#include <OneWire.h>

 

// TCP Server

// http://forum.arduino.cc/index.php?topic=123756.0

 

// the media access control (ethernet hardware) address for the shield:

byte mac[] = {

 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//the IP address for the shield:

byte ip[] = {

 192, 168, 30, 177 };  

// the subnet:

byte subnet[] = {

 255, 255, 255, 0 };

 

// Use TCP Port 12345 as Default

EthernetServer server = EthernetServer(12345);

 

// DS1820 Variablen

byte i;

byte present = 0;

byte data[12];

byte addr[8];

 

boolean headersend = false;

 

// DS18S20 Temperature chip I/O

OneWire ds(2);  // on pin 2

 

void setup()

{

 Serial.begin(57600);

 

 Serial.println("LogView Studio - OpenFormat Zero Demo (TCP Server for Data sending)");

 Serial.println("2013 by Dominik Schmidt\n");

 Serial.println("This demo uses a DS1820 temperature sensor on Pin 2 for data.");

 

 Serial.println("Init DS1820 Sensor");

 if ( !ds.search(addr)) {

   Serial.println("No more addresses.\n");

   ds.reset_search();

   return;

 }

 

 Serial.print("R=");

 for( i = 0; i < 8; i++) {

   Serial.print(addr[i], HEX);

   Serial.print(" ");

 }

 Serial.println("");

 

 if ( OneWire::crc8( addr, 7) != addr[7]) {

   Serial.println("CRC is not valid!\n");

   return;

 }

 

 if ( addr[0] == 0x10) {

   Serial.println("Device is a DS18S20 family device.\n");

 }

 else if ( addr[0] == 0x28) {

   Serial.println("Device is a DS18B20 family device.\n");

 }

 else {

   Serial.print("Device family is not recognized: 0x");

   Serial.println(addr[0],HEX);

   return;

 }

 

 // initialize the ethernet device

 Ethernet.begin(mac, ip, subnet);

 

 // start listening for clients

 server.begin();

}

 

void loop()

{

 float temp;

 String tempst;

 

 // Check if a client is available

 // The Server only starts if LogView Studio sends some data.

 // So check für the 02h as startbyte.

 EthernetClient client = server.available();

 

 if (client == true)

 {

   Serial.println("Client Connected");

 

   // Check if the client is still connected

   while (client.connected())

   {

     // Has the client send Data ?

     if (client.available())

     {

       char c = client.read();

       switch (c)      

       {

       case 83: // "S" -> Start

         Serial.println("Start detected");

         // Send Header information

         tempst = "$N$;Room Temp Logging\r\n";

         server.print(tempst);

         tempst = "$C$;Temp 1 [°C,T];Temp 2\r\n";

         server.print(tempst);

         headersend = true;

         break;

       case 69: // "E" -> End

         Serial.println("End detected");

         headersend = false;

         break;

       }

     }

   

     // Send data if header was send

     if (headersend){

       temp = GetTemp();

       char StrBuffer [10];

       tempst = dtostrf(temp, 6, 2, StrBuffer);

       Serial.println(tempst);

       tempst = "$" + tempst + ";2.0\r\n";

       server.print(tempst);

     }

   }

   Serial.println("Client Disconnected");

 }

}

 

// Read Temperature from DS1820

float GetTemp()

{

 int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;

 float value;

 

 ds.reset();

 ds.select(addr);

 ds.write(0x44,1);         // start conversion, with parasite power on at the end

 

 delay(1000);     // maybe 750ms is enough, maybe not

 // we might do a ds.depower() here, but the reset will take care of it.

 

 present = ds.reset();

 ds.select(addr);  

 ds.write(0xBE);         // Read Scratchpad

 

 //   Serial.print("P=");

 //   Serial.print(present,HEX);

 //   Serial.print(" ");

 for ( i = 0; i < 9; i++) {           // we need 9 bytes

   data[i] = ds.read();

   //     Serial.print(data[i], HEX);

   //     Serial.print(" ");

 }

 //   Serial.print(" CRC=");

 //   Serial.print( OneWire::crc8( data, 8), HEX);

 //   Serial.println();

 

 LowByte = data[0];

 HighByte = data[1];

 TReading = (HighByte << 8) + LowByte;

 SignBit = TReading & 0x8000;  // test most sig bit

 if (SignBit) // negative

 {

   TReading = (TReading ^ 0xffff) + 1; // 2's comp

 }

 

 Tc_100 = (TReading*100/2);

 

 value = Tc_100 / 100;

 if (Tc_100 % 100 == 50)

 {

   value += 0.5;

 }  

 if (SignBit)

 {

   value = value * -1;

 }

 

 return value;

}


Send feedback on this topic
Copyright (C) 2013-2014 Dominik Schmidt / Holger Hemmecke. All rights reserved.