home *** CD-ROM | disk | FTP | other *** search
/ business-86-101-185-173.business.broadband.hu / business-86-101-185-173.business.broadband.hu.zip / business-86-101-185-173.business.broadband.hu / SensorProject.ZIP / SensorClient.pde < prev    next >
Text File  |  2010-10-27  |  7KB  |  277 lines

  1. #include <Ethernet.h>
  2. #include "Dhcp.h"
  3. #include "Dns.h"
  4. #include <WString.h>
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7. #include <stdio.h>
  8. #define ONE_WIRE_BUS 4
  9.  
  10. byte mac[] = { 0xDE, 0xAA, 0xBE, 0xEF, 0xFE, 0xED };
  11. byte myip[] = {0,0,0,0};
  12. byte server[] = { 10,146,104,211 };
  13. byte serverport = 81;
  14. byte buffer[6];
  15. char cbuffer[6];
  16. boolean ipAcquired = false;
  17. boolean serverAcquired = false;
  18. boolean relayon = false;
  19. boolean sending = false;
  20. int isConnected = 0;
  21. byte ledpin = 2;
  22. int sleeptime = 60; // in seconds
  23. int retrytime = 5000; // in milliseconds
  24. DnsClass Dns;
  25. Client client(server, serverport);
  26. char* ID = "000000000000000000000000000000000001";
  27. long timer = 0;
  28. long timeout = 0;
  29. byte seconds = 0;
  30.  
  31. /* DS18S20 Temperature chip i/o */
  32. OneWire  ds(ONE_WIRE_BUS);
  33. DallasTemperature sensors(&ds);
  34. byte maxsensors = 0;
  35. byte sensorcount = 0;
  36.  
  37. void printArray(char* delimiter, byte* data, int len, int base)
  38. {
  39.   char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  40.   for(int i = 0; i < len; i++)
  41.   {
  42.     if(i != 0) Serial.print(delimiter);
  43.     Serial.print(itoa(data[i], buf, base));
  44.   }
  45. }
  46.  
  47. void showip()
  48. {
  49.   Dhcp.getLocalIp(myip);
  50.   Serial.print("IP address:");
  51.   printArray(".", myip, 4, 10);
  52.   Serial.print("/");
  53.   Dhcp.getSubnetMask(buffer);
  54.   printArray(".", buffer, 4, 10);
  55.   Serial.println();
  56. }
  57.  
  58. void showmac()
  59. {
  60.   Dhcp.getMacAddress(buffer);
  61.   Serial.print("MAC: ");
  62.   printArray(":", buffer, 6, 16);
  63.   Serial.println();
  64. }
  65.  
  66. void showgateway()
  67. {
  68.   Dhcp.getGatewayIp(buffer);
  69.   Serial.print("Gateway IP: ");
  70.   printArray(".", buffer, 4, 10);
  71.   Serial.println();
  72. }
  73.  
  74. void showdhcpdns()
  75. {
  76.   Dhcp.getDhcpServerIp(buffer);
  77.   Serial.print("DHCP server: ");
  78.   printArray(".", buffer, 4, 10);
  79.   Serial.println();
  80.   Dhcp.getDnsServerIp(buffer);
  81.   Serial.print("DNS server: ");
  82.   printArray(".", buffer, 4, 10);
  83.   Serial.println(); 
  84. }
  85.  
  86. void startnetwork() {
  87.   Serial.println("Requesting IP...");
  88.   int result = Dhcp.beginWithDHCP(mac);
  89.   Serial.println("Request sent.");
  90.   if(result == 1)
  91.   {
  92.     ipAcquired = true;
  93.     showip();
  94.     showmac();
  95.     showgateway();
  96.     Dhcp.getDnsServerIp(buffer);
  97.     showdhcpdns();
  98.   }
  99.   else Serial.println("Failed!");
  100. }
  101.  
  102. void flashled(void)
  103. {
  104.   digitalWrite(ledpin, HIGH);
  105.   delay(1);
  106.   digitalWrite(ledpin, LOW);
  107. }
  108.  
  109. void scanSensors(void)
  110. {
  111.   byte i;
  112.   byte j = 0;
  113.   byte present = 0;
  114.   byte data[12];
  115.   byte addr[8];
  116.   Serial.println("");
  117.   Serial.println("Scanning temperature sensors...");
  118.   while (ds.search(addr))
  119.   {
  120.     Serial.print("R=");
  121.     for( i = 0; i < 8; i++) {
  122.       Serial.print(addr[i], HEX);
  123.       Serial.print(" ");
  124.     }
  125.   
  126.     if ( OneWire::crc8( addr, 7) != addr[7]) {
  127.         Serial.print("CRC is not valid!\n");
  128.         return;
  129.     }
  130.     
  131.     if ( addr[0] == 0x10) {
  132.         Serial.print("Device is a DS18S20 family device.\n");
  133.         maxsensors++;
  134.     }
  135.     else {
  136.       if (addr[0] == 0x28) {
  137.         Serial.print("Device is a DS18B20 family device.\n");
  138.         maxsensors++;
  139.       }
  140.       else {
  141.         Serial.print("Device is unknown!\n");
  142.         Serial.print("Device ID: ");
  143.         Serial.print(addr[0],HEX);
  144.         Serial.println();
  145.         return;
  146.       }
  147.     }
  148.     // The DallasTemperature library can do all this work for you!
  149.     ds.reset();
  150.     ds.select(addr);
  151.     ds.write(0x44,1);         // start conversion, with parasite power on at the end
  152.     delay(1000);     // maybe 750ms is enough, maybe not
  153.     // we might do a ds.depower() here, but the reset will take care of it.
  154.     present = ds.reset();
  155.     ds.select(addr);    
  156.     ds.write(0xBE);         // Read Scratchpad
  157.     Serial.print("P=");
  158.     Serial.print(present,HEX);
  159.     Serial.print(" ");
  160.     for ( i = 0; i < 9; i++) {           // we need 9 bytes
  161.       data[i] = ds.read();
  162.       Serial.print(data[i], HEX);
  163.       Serial.print(" ");
  164.     }
  165.     Serial.print(" CRC=");
  166.     i = OneWire::crc8( data, 8);
  167.     Serial.print(i, HEX);
  168.     Serial.println();
  169.     flashled();
  170.   }
  171.   Serial.println("No more addresses.");
  172.   ds.reset_search();
  173.   delay(250);
  174.   Serial.print("Number of sensors: ");
  175.   Serial.println(maxsensors, DEC);
  176.   Serial.println();
  177. }
  178.  
  179. void setup()
  180. {
  181.   Serial.begin(9600);
  182.   pinMode(ledpin,OUTPUT);
  183.   sensors.begin();
  184.   scanSensors();
  185.   startnetwork();
  186.   Serial.println("Starting monitoring...");
  187.   timer = millis();
  188. }
  189.  
  190. void sendtemperaturedata()
  191. {
  192.   sensors.requestTemperatures();
  193.   flashled();
  194.   float f = sensors.getTempCByIndex(sensorcount);
  195.   Serial.print("Sensor ");
  196.   Serial.print(sensorcount,DEC);
  197.   Serial.print(": ");
  198.   Serial.print(dtostrf(f, 5, 2, cbuffer));
  199.   Serial.println(" Celsius degree.");
  200.   Serial.println("Connecting to HTTP server...");
  201.   if (client.connect()){
  202.     Serial.println("Sending data...");
  203.     client.print("POST /SensorService.asmx HTTP/1.1\n");
  204.     client.print("Host: 10.146.104.211\n");
  205.     client.print("Content-Type: text/xml; charset=utf-8\n");
  206.     client.print("Content-Length: 418\n");
  207.     client.print("SOAPAction: \"http://10.146.104.211/StoreData\"\n\n");
  208.     client.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  209.     client.print("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n");
  210.     client.print("<soap:Body>\n");
  211.     client.print("<StoreData xmlns=\"http://10.146.104.211/\">\n");
  212.     client.print("<password>xPMp%${,.22</password>\n");
  213.     client.print("<Value>");
  214.     client.print(dtostrf(f, 5, 2, cbuffer));
  215.     client.print("</Value>\n");
  216.     client.print("<SensorID>");
  217.     client.print(ID);
  218.     client.print("</SensorID>\n");
  219.     client.print("</StoreData>\n");
  220.     client.print("</soap:Body>\n");
  221.     client.print("</soap:Envelope>\n\n");
  222.     Serial.println("Ready, waiting for answer...");
  223.   }
  224.   sensorcount++;
  225. }
  226.  
  227. void loop(){
  228.   if (millis()-timer > 1000) {
  229.     timer = millis();
  230.     flashled();
  231.   }
  232.   if (sending){
  233.     if (client.available()&&client.connected()) {
  234.       char c = client.read();
  235.       Serial.print(c);
  236.       timeout = millis();
  237.     } 
  238.     else if (!client.connected()) {
  239.       Serial.println();
  240.       Serial.println("Disconnected from HTTP server.");
  241.       client.flush();
  242.       client.stop();
  243.       sending = false;
  244.       delay(1000);
  245.     } else {
  246.       if (millis()-timeout>5000) {
  247.         client.flush();
  248.         client.stop();
  249.       }
  250.     }
  251.   }
  252.   else {
  253.     if (ipAcquired){
  254.       if (sensorcount>=maxsensors){
  255.         Serial.println();
  256.         Serial.println("Sleeping...");
  257.         seconds = 0;
  258.         while (seconds<sleeptime){
  259.           seconds++;
  260.           delay(1000);
  261.           flashled();
  262.         }
  263.         sensorcount = 0;
  264.       }
  265.       client.stop();
  266.       delay(500);
  267.       sending = true;
  268.       sendtemperaturedata();
  269.     }
  270.     else {
  271.       Serial.println("TCP/IP not configured, retrying later...");
  272.       delay(retrytime);
  273.       startnetwork();
  274.     }
  275.   }
  276.