Guest User

Untitled

a guest
Aug 30th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. #include "etherShield.h"
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4.  
  5. OneWire ds(3); // on pin 3
  6. #define ONE_WIRE_BUS 3 // Data wire is plugged into digital pin 3 on the Arduino
  7. #define TEMPERATURE_PRECISION 12
  8. #define NumberOfDevices 5 // Set maximum number of devices in order to dimension
  9. // Array holding all Device Address arrays.
  10. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
  11.  
  12. DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
  13.  
  14. byte allAddress [NumberOfDevices][8]; // Device Addresses are 8-element byte arrays.
  15. // we need one for each of our DS18B20 sensors.
  16.  
  17. byte totalDevices; // Declare variable to store number of One Wire devices
  18. // that are actually discovered.
  19.  
  20. DeviceAddress Sensor1 = { 0x28, 0xF2, 0xBE, 0x43, 0x03, 0x00, 0x00, 0x63 };
  21. DeviceAddress Sensor2 = { 0x28, 0xC9, 0x7E, 0x43, 0x03, 0x00, 0x00, 0x54 };
  22. DeviceAddress Sensor3 = { 0x28, 0xE9, 0x9E, 0x43, 0x03, 0x00, 0x00, 0x38 };
  23.  
  24. static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
  25. static uint8_t myip[4] = {192,168,1,15};
  26. static char baseurl[]="http://192.168.1.15/";
  27. static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)
  28.  
  29. #define BUFFER_SIZE 500
  30. static uint8_t buf[BUFFER_SIZE+1];
  31. #define STR_BUFFER_SIZE 22
  32. static char strbuf[STR_BUFFER_SIZE+1];
  33.  
  34. EtherShield es=EtherShield();
  35.  
  36. // prepare the webpage by writing the data to the tcp send buffer
  37. uint16_t print_webpage(uint8_t *buf);
  38. int8_t analyse_cmd(char *str);
  39. #define TEMP_PIN 3
  40. void getCurrentTemp( int *sign, int *whole, int *fract);
  41.  
  42. void setup(){
  43.  
  44. Serial.begin(9600);
  45. sensors.begin();
  46.  
  47. // locate devices on the bus
  48. Serial.print("Locating devices...");
  49. Serial.print("Found ");
  50. Serial.print(sensors.getDeviceCount(), DEC);
  51. Serial.println(" devices.");
  52.  
  53. totalDevices = discoverOneWireDevices(); // get addresses of our one wire devices into allAddress array
  54. for (byte i=0; i < totalDevices; i++)
  55. sensors.setResolution(allAddress[i], TEMPERATURE_PRECISION); // and set the a to d conversion resolution of each.
  56.  
  57. // report parasite power requirements
  58. Serial.print("Parasite power is: ");
  59. if (sensors.isParasitePowerMode()) Serial.println("ON");
  60. else Serial.println("OFF");
  61.  
  62. Serial.print("Device 0 Resolution: ");
  63. Serial.print(sensors.getResolution(Sensor1), DEC);
  64. Serial.println();
  65.  
  66. Serial.print("Device 1 Resolution: ");
  67. Serial.print(sensors.getResolution(Sensor2), DEC);
  68. Serial.println();
  69.  
  70.  
  71. /*initialize enc28j60*/
  72. es.ES_enc28j60Init(mymac);
  73. es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
  74. delay(10);
  75.  
  76. /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
  77. // LEDA=greed LEDB=yellow
  78. //
  79. // 0x880 is PHLCON LEDB=on, LEDA=on
  80. // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
  81. es.ES_enc28j60PhyWrite(PHLCON,0x880);
  82. delay(500);
  83. //
  84. // 0x990 is PHLCON LEDB=off, LEDA=off
  85. // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
  86. es.ES_enc28j60PhyWrite(PHLCON,0x990);
  87. delay(500);
  88. //
  89. // 0x880 is PHLCON LEDB=on, LEDA=on
  90. // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
  91. es.ES_enc28j60PhyWrite(PHLCON,0x880);
  92. delay(500);
  93. //
  94. // 0x990 is PHLCON LEDB=off, LEDA=off
  95. // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
  96. es.ES_enc28j60PhyWrite(PHLCON,0x990);
  97. delay(500);
  98. //
  99. // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  100. // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
  101. es.ES_enc28j60PhyWrite(PHLCON,0x476);
  102. delay(100);
  103.  
  104. //init the ethernet/ip layer:
  105. es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
  106.  
  107. // initialize DS18B20 datapin
  108. digitalWrite(TEMP_PIN, LOW);
  109. pinMode(TEMP_PIN, INPUT); // sets the digital pin as input (logic 1)
  110.  
  111.  
  112. }
  113.  
  114.  
  115. byte discoverOneWireDevices() {
  116. byte j=0; // search for one wire devices and
  117. // copy to device address arrays.
  118. while ((j < NumberOfDevices) && (oneWire.search(allAddress[j]))) {
  119. j++;
  120. }
  121. int i=0;
  122. for (byte i=0; i < j; i++) {
  123. Serial.print("Device ");
  124. Serial.print(i, DEC);
  125. Serial.print(": ");
  126. printAddress(allAddress[i]); // print address from each device address arry.
  127. }
  128. Serial.print("\r\n");
  129. return j ; // return total number of devices found.
  130. }
  131.  
  132.  
  133.  
  134. void printAddress(DeviceAddress addr) {
  135. byte i;
  136. for( i=0; i < 8; i++) { // prefix the printout with 0x
  137. Serial.print("0x");
  138. if (addr[i] < 16) {
  139. Serial.print('0'); // add a leading '0' if required.
  140. }
  141. Serial.print(addr[i], HEX); // print the actual value in HEX
  142. if (i < 7) {
  143. Serial.print(", ");
  144. }
  145. }
  146. //tesztelésre
  147. Serial.println("*****");
  148. for( i=0; i < 8; i++) { // prefix the printout with 0x
  149. Serial.print(addr[i], HEX); // print the actual value in HEX;
  150. }
  151. Serial.println("*****");
  152.  
  153. Serial.print("\r\n");
  154. }
  155.  
  156.  
  157.  
  158. float printTemperature(DeviceAddress addr) {
  159. float tempC = sensors.getTempC(addr); // read the device at addr.
  160. if (tempC == -127.00) {
  161. Serial.print("Error getting temperature");
  162. } else {
  163. Serial.println(tempC); // and print its value.
  164. return tempC;
  165. }
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. void loop(){
  173. uint16_t plen, dat_p;
  174. int8_t cmd;
  175.  
  176. plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
  177.  
  178. /*plen will ne unequal to zero if there is a valid packet (without crc error) */
  179. if(plen!=0){
  180.  
  181. // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.
  182. if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){
  183. es.ES_make_arp_answer_from_request(buf);
  184. return;
  185. }
  186.  
  187. // check if ip packets are for us:
  188. if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
  189. return;
  190. }
  191.  
  192. if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
  193. es.ES_make_echo_reply_from_request(buf,plen);
  194. return;
  195. }
  196.  
  197. // tcp port www start, compare only the lower byte
  198. if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport){
  199. if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
  200. es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack
  201. return;
  202. }
  203. if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
  204. es.ES_init_len_info(buf); // init some data structures
  205. dat_p=es.ES_get_tcp_data_pointer();
  206. if (dat_p==0){ // we can possibly have no data, just ack:
  207. if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
  208. es.ES_make_tcp_ack_from_any(buf);
  209. }
  210. return;
  211. }
  212. if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
  213. // head, post and other methods for possible status codes see:
  214. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  215. plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
  216. goto SENDTCP;
  217. }
  218. if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
  219. plen=print_webpage(buf);
  220. goto SENDTCP;
  221. }
  222. cmd=analyse_cmd((char *)&(buf[dat_p+5]));
  223. if (cmd==1){
  224. plen=print_webpage(buf);
  225. }
  226. SENDTCP: es.ES_make_tcp_ack_from_any(buf); // send ack for http get
  227. es.ES_make_tcp_ack_with_data(buf,plen); // send data
  228. }
  229. }
  230. }
  231.  
  232. }
  233. // The returned value is stored in the global var strbuf
  234. uint8_t find_key_val(char *str,char *key)
  235. {
  236. uint8_t found=0;
  237. uint8_t i=0;
  238. char *kp;
  239. kp=key;
  240. while(*str && *str!=' ' && found==0){
  241. if (*str == *kp){
  242. kp++;
  243. if (*kp == '\0'){
  244. str++;
  245. kp=key;
  246. if (*str == '='){
  247. found=1;
  248. }
  249. }
  250. }else{
  251. kp=key;
  252. }
  253. str++;
  254. }
  255. if (found==1){
  256. // copy the value to a buffer and terminate it with '\0'
  257. while(*str && *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){
  258. strbuf[i]=*str;
  259. i++;
  260. str++;
  261. }
  262. strbuf[i]='\0';
  263. }
  264. return(found);
  265. }
  266.  
  267. int8_t analyse_cmd(char *str)
  268. {
  269. int8_t r=-1;
  270.  
  271. if (find_key_val(str,"cmd")){
  272. if (*strbuf < 0x3a && *strbuf > 0x2f){
  273. // is a ASCII number, return it
  274. r=(*strbuf-0x30);
  275. }
  276. }
  277. return r;
  278. }
  279.  
  280.  
  281. uint16_t print_webpage(uint8_t *buf)
  282. {
  283. char temp_string[10];
  284. int i=0;
  285. int k=0;
  286. float tempC;
  287. //char *temp_string="100";
  288.  
  289. uint16_t plen;
  290.  
  291. plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
  292. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<meta http-equiv=refresh content=5>"));
  293.  
  294. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<form METHOD=get action=\""));
  295. plen=es.ES_fill_tcp_data(buf,plen,baseurl);
  296. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));
  297.  
  298. i=0;
  299. byte present = 0;
  300. byte type_s;
  301. byte data[12];
  302. byte addr[8];
  303. float celsius, fahrenheit;
  304. float garageTempf = 0.0;
  305. char garageTempc[7];
  306.  
  307. sensors.requestTemperatures();
  308.  
  309.  
  310. char z=0;
  311. for (byte k=0; k < totalDevices; k++) {
  312. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("Sensor: ") );
  313.  
  314. celsius=printTemperature(allAddress[k]);
  315. dtostrf(celsius, 3, 2, garageTempc);
  316. Serial.print("Sensor");
  317. Serial.print(k);
  318. Serial.print(": ");
  319. Serial.println(celsius);
  320. i=0;
  321. while (garageTempc[i]) {
  322. buf[TCP_CHECKSUM_L_P+3+plen]=garageTempc[i++];
  323. plen++;
  324. }
  325. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(" &#176C<br>") );
  326. }
  327.  
  328.  
  329. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=1>"));
  330. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Get Temperature\"></form>"));
  331. plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</center>"));
  332.  
  333. return(plen);
  334. }
Advertisement
Add Comment
Please, Sign In to add comment