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 >
Wrap
Text File
|
2010-10-27
|
7KB
|
277 lines
#include <Ethernet.h>
#include "Dhcp.h"
#include "Dns.h"
#include <WString.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <stdio.h>
#define ONE_WIRE_BUS 4
byte mac[] = { 0xDE, 0xAA, 0xBE, 0xEF, 0xFE, 0xED };
byte myip[] = {0,0,0,0};
byte server[] = { 10,146,104,211 };
byte serverport = 81;
byte buffer[6];
char cbuffer[6];
boolean ipAcquired = false;
boolean serverAcquired = false;
boolean relayon = false;
boolean sending = false;
int isConnected = 0;
byte ledpin = 2;
int sleeptime = 60; // in seconds
int retrytime = 5000; // in milliseconds
DnsClass Dns;
Client client(server, serverport);
char* ID = "000000000000000000000000000000000001";
long timer = 0;
long timeout = 0;
byte seconds = 0;
/* DS18S20 Temperature chip i/o */
OneWire ds(ONE_WIRE_BUS);
DallasTemperature sensors(&ds);
byte maxsensors = 0;
byte sensorcount = 0;
void printArray(char* delimiter, byte* data, int len, int base)
{
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i = 0; i < len; i++)
{
if(i != 0) Serial.print(delimiter);
Serial.print(itoa(data[i], buf, base));
}
}
void showip()
{
Dhcp.getLocalIp(myip);
Serial.print("IP address:");
printArray(".", myip, 4, 10);
Serial.print("/");
Dhcp.getSubnetMask(buffer);
printArray(".", buffer, 4, 10);
Serial.println();
}
void showmac()
{
Dhcp.getMacAddress(buffer);
Serial.print("MAC: ");
printArray(":", buffer, 6, 16);
Serial.println();
}
void showgateway()
{
Dhcp.getGatewayIp(buffer);
Serial.print("Gateway IP: ");
printArray(".", buffer, 4, 10);
Serial.println();
}
void showdhcpdns()
{
Dhcp.getDhcpServerIp(buffer);
Serial.print("DHCP server: ");
printArray(".", buffer, 4, 10);
Serial.println();
Dhcp.getDnsServerIp(buffer);
Serial.print("DNS server: ");
printArray(".", buffer, 4, 10);
Serial.println();
}
void startnetwork() {
Serial.println("Requesting IP...");
int result = Dhcp.beginWithDHCP(mac);
Serial.println("Request sent.");
if(result == 1)
{
ipAcquired = true;
showip();
showmac();
showgateway();
Dhcp.getDnsServerIp(buffer);
showdhcpdns();
}
else Serial.println("Failed!");
}
void flashled(void)
{
digitalWrite(ledpin, HIGH);
delay(1);
digitalWrite(ledpin, LOW);
}
void scanSensors(void)
{
byte i;
byte j = 0;
byte present = 0;
byte data[12];
byte addr[8];
Serial.println("");
Serial.println("Scanning temperature sensors...");
while (ds.search(addr))
{
Serial.print("R=");
for( i = 0; i < 8; i++) {
Serial.print(addr[i], HEX);
Serial.print(" ");
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
if ( addr[0] == 0x10) {
Serial.print("Device is a DS18S20 family device.\n");
maxsensors++;
}
else {
if (addr[0] == 0x28) {
Serial.print("Device is a DS18B20 family device.\n");
maxsensors++;
}
else {
Serial.print("Device is unknown!\n");
Serial.print("Device ID: ");
Serial.print(addr[0],HEX);
Serial.println();
return;
}
}
// The DallasTemperature library can do all this work for you!
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=");
i = OneWire::crc8( data, 8);
Serial.print(i, HEX);
Serial.println();
flashled();
}
Serial.println("No more addresses.");
ds.reset_search();
delay(250);
Serial.print("Number of sensors: ");
Serial.println(maxsensors, DEC);
Serial.println();
}
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
sensors.begin();
scanSensors();
startnetwork();
Serial.println("Starting monitoring...");
timer = millis();
}
void sendtemperaturedata()
{
sensors.requestTemperatures();
flashled();
float f = sensors.getTempCByIndex(sensorcount);
Serial.print("Sensor ");
Serial.print(sensorcount,DEC);
Serial.print(": ");
Serial.print(dtostrf(f, 5, 2, cbuffer));
Serial.println(" Celsius degree.");
Serial.println("Connecting to HTTP server...");
if (client.connect()){
Serial.println("Sending data...");
client.print("POST /SensorService.asmx HTTP/1.1\n");
client.print("Host: 10.146.104.211\n");
client.print("Content-Type: text/xml; charset=utf-8\n");
client.print("Content-Length: 418\n");
client.print("SOAPAction: \"http://10.146.104.211/StoreData\"\n\n");
client.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
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");
client.print("<soap:Body>\n");
client.print("<StoreData xmlns=\"http://10.146.104.211/\">\n");
client.print("<password>xPMp%${,.22</password>\n");
client.print("<Value>");
client.print(dtostrf(f, 5, 2, cbuffer));
client.print("</Value>\n");
client.print("<SensorID>");
client.print(ID);
client.print("</SensorID>\n");
client.print("</StoreData>\n");
client.print("</soap:Body>\n");
client.print("</soap:Envelope>\n\n");
Serial.println("Ready, waiting for answer...");
}
sensorcount++;
}
void loop(){
if (millis()-timer > 1000) {
timer = millis();
flashled();
}
if (sending){
if (client.available()&&client.connected()) {
char c = client.read();
Serial.print(c);
timeout = millis();
}
else if (!client.connected()) {
Serial.println();
Serial.println("Disconnected from HTTP server.");
client.flush();
client.stop();
sending = false;
delay(1000);
} else {
if (millis()-timeout>5000) {
client.flush();
client.stop();
}
}
}
else {
if (ipAcquired){
if (sensorcount>=maxsensors){
Serial.println();
Serial.println("Sleeping...");
seconds = 0;
while (seconds<sleeptime){
seconds++;
delay(1000);
flashled();
}
sensorcount = 0;
}
client.stop();
delay(500);
sending = true;
sendtemperaturedata();
}
else {
Serial.println("TCP/IP not configured, retrying later...");
delay(retrytime);
startnetwork();
}
}
}