Documente Academic
Documente Profesional
Documente Cultură
A Data Acquisition Sytem with Internet connectivity for better Connectivity and Controllability on a Move
Index .........
1.Introduction.................................................................................. 1
Embedded systems Data Acquisition System Internet Enabled Embedded Systems Internet Data Acquisition System
Benefits Limitations
RJ45 MagJack LM35 Temperature Sensor Power Supply Motor Driver L293D
5. Methods of Building a Data Acquisition System............................................16 6. Using the ENC28J60 Ethernet Controller Node connected to a router to upload the sensor value .................................................................... 17
2
8. A Full Duplex communication over the internet , between the third party server (Pachube) and an Embedded device connected to PC directly............30
Methodology Programs structure 9. Conclusion .................................................................................................................................... 34 10. Reference ....................................................................................................................................34
Introduction
Embedded Systems
Embedded systems is a computer system designed for specific control functions within a larger system often with real time computing constraints .It is embedded as part of a complete device often including hardware and mechanical parts whereas a desktop PC is designed to be a generalpurpose computer, such as a personal computer (PC), is designed to be flexible and to meet a wide range of end-user needs but are quite expensive . Embedded systems control many devices in common use today and are usually build dedicated to perform a specific task . Design engineers can optimize it to reduce the size and cost of the product and increase the reliability and performance. Some embedded systems are mass-produced and reducing the cost still further. Embedded systems are especially suited for use in transportation, fire safety, military, security, medical applications in form of alarm system monitoring abnormal activity or Data acquisition system monitoring a patients heart beat for further analysing of the patients health. Other consumer electronics like handheld computer , Personal Digital Assistant (PDA) , GPS , Home automation system ,Temperature monitoring system in homes etc
Processing may consist of a large variety of operations , ranging from simple comparison to complicated mathematical manipulations. It can be for such a purpose as collection of information(averages,satistics) ,converting the data into useful forms (eg calculations of efficiency of motor speed , torque and power input developed), using data for controlling a process ,performing repeated calculation to separate signals buried in the noise , generating information for display ,and various other purpose. Data may be transmitted over long distances or short distance . Both wired or wireless transmission is used for transmission of data from test centre to a nearby PC , display panel or CRO .In wireless transmission the data is send from test centre to the display unit either through infrared , Bluetooth or Zigbee protocol . Wireless transmission tends to be more flexible , but expensive and complex as there is a need for connecting external transmission/receiver modules between the test centre and display unit and then to provide necessary calibration between them.
the problem and could even set it right .The information can easily be accesed and controlled by PC or PDA with internet access , which in turn can be connected to a local low cost local area network (LAN) to transfer sensor values directly to a data logger or computer. The accessibility of this information is significantly curtailed by this need for proximity. However, developments in the Internet protocol, TCP/IP, which is the universal communication standard, looks set to change all of this. In order to allow communications to be directed, every physical location on the Internet (server or client) requires an IP address. Softwares have been designed to run in PDA or PC that passes messages received on an IP network to the logger hardware via a serial port or USB. Or through GPRS , Bluetooth , Zigbee etc. Benefits : The Benefits of an internet based DAS are Display information instantly in condensed , understandable and legible manner so that it can be easily assimilated. Provide a series of recorded event saved in the server , to the operators PDA on demand Send an e-mail or SMS to the operators PC or mobile phone in case an emergency occurs Provide the operator with necessary guidance message and options to perform secure operation of control systems remotely in case an emergency occurs eg switching on a cooler in case the temperature rises to an alarming level , switch on a pump to water the plants when the moisture in soil has dropped to a very low value etc Produce a hard copy record of all plant operationg events and various plant logs on demand. Internet based DAS provide the flexibility for handling embedded systems which requires real time demands like traffic monitoring , precision agriculture , water resources management etc Limitation: Although few of the biggest drawback in building Internet based DAS (IDAS) are The biggest limitation is Security , An IDAS with internet connectivity and an external server acting as primary memory , Crucial information of plant operation and real-time sensor reading might fall in wrong hands (Hackers), which could led to disastrous result both financially and to Human life. Power Management: Current internet enabled embedded devices (IEED)employ high end peripherals and heavy software stacks leading to the increased consumption of power and consequent reduction in battery life. Full usage of Channel: Most of the Internet enabled embedded devices (IEED) available are capable of supporting communication over fast Ethernet or even gigabit Ethernet , Though the communication is over the internet , they are not capable of using the channels to the full extent. On an average , most of the IEED are capable of transferring only a few bytes of data . Current Internet technologies do not feature predictable delays in data delivery .Most IEED are real time and hence predictable time delay is a basic requirement . eg In case of A/D conversion an IDAS need some time delay (conversion time), any packets of data through internet to the IDAS during this time interval demanding the real-time sensor reading from IDAS could disturb the entire apparatus set-up.
The System consists of two nodes one is the microcontroller board which consists of microcontroller ATmega168 , Temperature sensor LM35 , Potentiometer (for testing purpose) ,Motor Driver L293D (for operating actuators like pump, fan) 3v3 volt regulator for powering the Ethernet module and The second node is the Ethernet controller which consists of Ethernet Controller ENC28j60 from Microchip and RJ45 Jack with Integrated magnetic and integrated LEDS (LEDA and LEDB : Depicting Transmission and Reception of Packets through the cable) The Microcontroller board node communicates with the Ethernet controller node using the SPI (Serial peripheral Interface) protocol. Hence , Four cables are used for Chip Select (CS), Shift Clock(SCLK) , SDI (Din) and SDO (Dout) to maintain SPI communication between the two nodes.
Microcontroller Atmega168
Fig 2 : Pin Diagram of Microcontroller ATMEGA168 Microcontroller ATMEGA 168 is a 8 bit 28 pin microcontroller from ATMEL corporation , Some of the prime feature of this Microcontroller because of which this microcontroller has been selected for this project are :Memory Details 1K bytes of internal SRAM 512 bytes of EEPROM 9
32x8 general purpose resistors (GPRs) Write/ Erase Cycles : 10,000 flash / 100,000 EEPROM
Peripheral Features 8 channel 10 bit ADC 6 PWM channels Two 8 bit timer/counter with separate prescaler and compare mode Dual programmable Serial USART On chip analog Comparator Master Slave SPI Serial Interface Programmable Watch Dog Timer with separate On chip Oscillator All these features are sufficient , for building the IDAS . Pheripheral features like 8 channel ADC can be used for reading the sensor values , PWM channels are used for operating external peripherals like servo motors , motor etc , Serial USART is used to serially transfer data like sensor reading , Unexpected errors etc , On chip Analog comparator is used for comparing the sensor reading with a reference voltage (and then to take necessary operation) , SPI Serial interface : to communicate with the ENC28j60 ethernet controller and Watch Dog Timer to reset itself in case of unexpected error in absence of the operator, Small size and low cost gives an extra advantage.
10
The Ethernet controller may be a separate, standalone device, or it may be integrated with the host microcontroller (MCU). The Ethernet controller transmits and receives data from the Ethernet cable and manages Ethernet protocol. Ethernet controllers are often separated into two pieces, the Media Access Control (MAC) and the Physical Layer (PHY).
The ENC28J60 is designed to operate at 25 MHz with a crystal connected to the OSC1 and OSC2 pins. The ENC28J60 design requires the use of a parallel cut crystal. Use of a series cut crystal may give a frequency out of the crystal manufacturer specifications. Thus the crystal, has to be 25.000 MHz, not 24 not 26, 25 MHz no more or no less and as specified in the datasheet . This has to do with the timing specifications of the IEEE 802.3 (Ethernet) standard, if the ENC28J60 oscillator is running at a different frequency, it will have a very hard time to communicate with other IEEE 802.3 compliant devices.
11
Fig 5 : RJ45 Magjack The Yellow and the Green LEDs in the figure above indicates the Reception and Transmission of Packets respectively.
12
Power Supply
The apparatus requires dual power supply of 5V and 3.3V for powering Microcontroller and ENC28J60 respectively , The microcontroller is powered by a 5V voltage regulator LM7805 and a Zener diode is used to regulate power provided to the ENC28J60.
Fig 8: The Microcontroller Boards schemetic with serial port, motor driver and voltage regulator
14
15
Fig 10 (a): The Microcontroller module with sensor(potentiometer) , motor driver and power regulator
16
Software used
The System requires two different set of different computer Programs one for generating a .hex file which is to be burned inside the microcontroller ROM And the second is a interactive program with Graphical User interface (GUI) running in PC /PDA. The program built for this project would only run on PC. The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (GUI program running in PC : Processing).
Arduino
Arduino is an open-source single-board AVR microcontroller programming software , descendant of the open-source Wiring platform, designed to make the process of using electronics in multidisciplinary projects more accessible. The hardware consists of a simple open hardware design for the Arduino board with an Atmel AVR processor and on-board input/output support. The software consists of a standard programming language compiler and the boot loader that runs on the board. Arduino (or Wiring) programming instructions are much like that of C++ , it even supports object oriented programming and have several pre defined library functions including for interfacing with Ethernet modulus like Wiznet and ENC28j60 , The arduino library for ENC28J60 used in this project was developed by Andy Lindsay and was downloaded from his blog. Arduino has a different view of the AVR microcontroller ATMEGA168 as shown
Fig 11 : Pins function of AVR ATMEGA168 when used with Arduino software
Processing
Processing too is an open source programming language and integrated development environment (IDE) built for the electronic arts and visual design communities with the purpose of teaching the basics of computer programming in a visual context, and to serve as the foundation for electronic sketchbooks. The project was initiated in 2001 by Casey Reas and Benjamin Fry, both formerly of the Aesthetics and Computation Group at the MIT Media Lab. One of the stated aims of 17
Processing is to act as a tool to get non-programmers started with programming, through the instant gratification of visual feedback. The language builds on the Java programming language, but uses a simplified syntax and supports rich graphics. Other features of processing like its platform independent and takes low memory space makes it an ideal programming tool to built GUI for interfacing with microcontroller (sending sensor value ) , PC and external server (where the sensor values are being recorded) A mobile version of processing is available called Mobile Processing allows software written using the Processing language and environment to run on Java powered mobile devices. Which can be used to build IDAS software for mobile devices also. The Processing serial library allows for easily reading and writing data to and from external machines. It allows two computers to send and receive data and gives the flexibility to communicate with custom microcontroller devices, using them as the input or output to Processing programs.
Firmata
Firmata is a generic protocol for communicating with microcontrollers from software on a host computer. It is intended to work with any host computer software package. Right now there is a matching object in a number of languages. It is easy to add objects for other software to use this protocol. Basically, this firmware establishes a protocol for talking to the Arduino from the host software , Firmata can also be used with other microcontroller like PIC microcontroller series of Microchip . The aim is to allow people to completely control the Embedded device from software running on the host computer. The Firmata acts as a bridge between the AVR ATMEGA168 microcontroller and the host software running on PC or PDA.
18
USART protocol would be uploaded to the Google Spreadsheet feature of the user or operators Google Account directly.
Pachube
Pachube is an on-line database service provider especially designed for allowing embedded system developers to connect sensor data (energy and environment data from objects, devices & buildings) to the Web[1] and to build their own applications on it. It was created in 2007 by architect Usman Haque Pachube is a realtime data infrastructure platform for the , Internet of Things (A network of Internet enabled embedded devices )managing millions of datapoints per day from thousands of individuals, organisations & companies around the world. Pachube's powerful and scalable infrastructure enables you to build 'Internet of Things' products and services, and store, share & discover realtime sensor, energy and environment data from objects, devices & buildings around the world. Pachube enables things to "plug-in" to other things in real time so that, for example, buildings, weather stations, interactive environments, air quality monitors, networked energy monitors, virtual worlds and mobile sensor devices can all "talk" and "respond" to each other in realtime Pachube's scalable infrastructure helps you...
Manage realtime sensor & environment data: Pachube is a data brokerage platform for the IEED, managing millions of datapoints per day from thousands of individuals, organisations & companies around the world. Convenient, secure & economical, Pachube stores, converts & serves data in multiple data formats, which makes it interoperable with both established web protocols & existing construction industry standards. All feeds include contextual metadata (timestamps, geolocation, units, tags) that actually make datastreams valuable. Graph, monitor & control remote environments: Embed realtime graphs & widgets in your own website. Analyse & process historical data pulled from any public Pachube feed. Send realtime alerts from any datastream to control scripts, devices & environments. Out-of-the-box configurable tools include a zoomable graph, a mapping/tracking widget, anaugmented reality viewer, SMS alerts & apps for various smartphones. As soon as something is plugged into Pachube, you're ready to monitor & control it.
Build mobile & web apps that create value: With a rapid development cycle & dozens of code examples & libraries, Pachube's simple yet sophisticated'physical-to-virtual' API makes it quick & easy to build applications that add value to networked objects & environments. That's because real value-creation comes from the applications that are built on top of sensor systems. Pachube handles the scalability & highavailability required for complex data management, so as to develop applications that make decision-making more sophisticated.
Use a Internet Enabled Desktop PC (running a Processing code) to communicate with a embedded device through USART protocol and uploading the sensor values to Google Spreadsheet
Maintain a Full Duplex communication over the internet , between the third party server (Pachube) and the Embedded device connected to PC directly. To drive actuators like cooler and pump ; After sending the request to do so by the operator through the server.
20
Using the ENC28J60 Ethernet Controller Node connected to a router to upload the sensor value
As already mentioned the entire hardware is divided into two parts the microcontroller Sensor board and the Ethernet node with the Ethernet router and MAGJACK. The Microcontroller and the Ethernet node communicates through SPI protocol. And Ethernet node sends over those values received from microcontroller to the internet by using the Ethernet (IEEE 802.3).
Fig 13: Mode 1: Ethernet controller and Router Setup Methodology In this method the Microcontroller works as a simple web server that responds to web requests with a page containing the current sensor readings. The program can be modified to change the format of the web page that is sent, but before getting into the details of the code there are a few limitations of this method . Because this program is based on the etherShield library , it doesnt implement a full TCP/IP stack. One of the major limitations is that the etherShield library doesnt support packet fragmentation, which means the size of the response that can be sent is strictly limited. A full TCP/IP implementation breaks up large messages into packets, each of which is transmitted independently across the network and reassembled at the other end to reconstruct the complete message. Packets are, as a major generalization, limited to between 64 and 1518 bytes in size including all the headers. Because the TCP/IP header and the checksum eat into that you actually end up with somewhat less than 1518 bytes as the upper size. Another and the biggest limitation of this method is The other limitation is that the etherShield library doesnt have any provision for specifying a gateway or netmask, so it has no concept of routing. You cant configure a gateway address in your program and have it initiate a TCP/IP connection to a host outside your network because it simply wont understand where to send the packet. Note, however, that you can have it respond to requests from outside your network if your gateway does source-NAT (network address translation).
Programs structure
In the C program written in Arduino Programming tool for microcontroller ATMEGA168, First we need to download the EtherSheild library from the developers website (The EtherSheild library is
21
not available by default in the Pre defined arduino library). After which we have to call the library from the program using the header file
#include "etherShield.h"
We then have to define a MAC address which has to be an unique in your network. Usually the MAC address is hard programmed into the equipments firmware. However with the Arduino programming tool we can define the MAC address ourselves. Likewise, the IP address needs to be unique on your network, and you need to enter it twice: once as the four elements in the myIp array and then as a string in baseurl. These values are different because the TCP/IP stack needs to use the actual local IP address in the headers, but the web page that is created also needs to display a form that submits to itself, and the address used in your browser may be different to the actual IP address if your device is behind a NAT (network address translation) device such as as firewall.
#include "etherShield.h" // configuration. The MAC and IP address have to be unique on your LAN: static uint8_t myMac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; static uint8_t myIp[4] = {192,168,1,15}; static char baseurl[] = "http://192.168.0.77/"; static uint16_t myPort = 80; // Listen to values coming from port 80 by default
where unit8_t is an unsigned integer of length 8 bits and unit16_t is an unsigned integer of length 16 bits. The myPort value can be changed if you want your device to listen on a port other than the default port 80. However, if you change it make sure you also change the baseurl variable to include the port number or the form wont work. For example, if you wanted to listen on port 81, you could change those lines to the following:
static char baseurl[] = "http://192.168.0.77:81/"; static uint16_t myPort = 81; // Listen port for tcp/www The program then creates two buffers used in creating the TCP/IP packet, and creates an instance of the EtherShield object called es as follows: // Set up variables for the TCP/IP buffer #define BUFFER_SIZE 500 static uint8_t buf[BUFFER_SIZE+1]; #define STR_BUFFER_SIZE 22 static char strbuf[STR_BUFFER_SIZE+1]; // Create an instance of the EtherShield object named "es" //EtherShield es=EtherShield(); // Prepare the web page by writing the data to the TCP send buffer uint16_t print_webpage(uint8_t *buf); int8_t analyse_cmd(char *str);
After which we define the pin to which the Sensor is connected which is pin 26 of the controller and which is pin 3 if viewed from Arduino environment. Thus we define -> #define SENSOR 3. The setup function does some setup of the Ethernet connection, and then sets the sensor data pins to be inputs. SFRs of ENC28J60 used for initialization the Ethernet connections are
void setup() { /*initialize enc28j60*/
22
es.ES_enc28j60Init(myMac); // Change clkout from 6.25MHz to 12.5MHz es.ES_enc28j60clkout(2); delay(10); // Magjack leds configuration // LEDA=green LEDB=yellow // 0x880 is PHLCON LEDB=on, LEDA=on es.ES_enc28j60PhyWrite(PHLCON, 0x880); // Values taken from the ENC28J60 datasheet delay(500); // 0x990 is PHLCON LEDB=off, LEDA=off es.ES_enc28j60PhyWrite(PHLCON, 0x990); delay(500); // 0x880 is PHLCON LEDB=on, LEDA=on es.ES_enc28j60PhyWrite(PHLCON, 0x880); delay(500); // 0x990 is PHLCON LEDB=off, LEDA=off es.ES_enc28j60PhyWrite(PHLCON, 0x990); delay(500); // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit es.ES_enc28j60PhyWrite(PHLCON, 0x476); delay(100); //init the ethernet/ip layer: es.ES_init_ip_arp_udp_tcp(myMac, myIp, myPort); //init the ethernet/ip layer: // Set up the data pins from LM35 sensor digitalWrite(SENSOR, LOW); pinMode(SENSOR , INPUT);
After which the ENC28j60 would monitor the RJ45 cable to check for valid packets , using the instruction
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
After which we need to check the plen contains a valid packet, by using the instructions
if(plen!=0) { // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address( the sending of messages to a single network destination identified by a unique address. ) if (es.ES_eth_type_is_arp_and_my_ip (buf,plen)) { es.ES_make_arp_answer_from_request (buf); return; }
After these instructions we need to check where the received wether the ip address are for us , By comparing the destination ip address in the packet with our ip address defined in the beginning of the program which is 198.168.0.77
if (es.ES_eth_type_is_ip_and_my_ip (buf,plen) == 0) and if (buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
if both the ip address match either one of the above instructions would be a valid one . Hence the complete code to check if the ip address match :// check if ip packets are for us: if (es.ES_eth_type_is_ip_and_my_ip (buf,plen) == 0) { return;
23
The main program loop is quite complex because it checks the Ethernet receive buffer each time through and manages the appropriate responses.
// tcp port www start, compare only the lower byte if (buf[IP_PROTO_P] == IP_PROTO_TCP_V && buf[TCP_DST_PORT_H_P] == 0 && buf[TCP_DST_PORT_L_P] == myPort) { if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V) { es.ES_make_tcp_synack_from_syn (buf); // make_tcp_synack_from_syn does already send the syn,ack return; } if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V) { es.ES_init_len_info (buf); // init some data structures dat_p = es.ES_get_tcp_data_pointer(); if (dat_p==0) { // we can possibly have no data, just ack: if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V) { es.ES_make_tcp_ack_from_any (buf); }
checks are both done using a call to analyse_cmd(), which processes the request header to find the value of the cmd (content management database) variable passed through in the request. If the value is 1 it responds with the default page , if the cmd value is 2 it goes into the About page and like wise. This check is not strictly necessary because if you only want your program to return the page with the sensor data on it. This is one of the two most important functions in the program here we can analyse the value of cmd and point to the web page we want to display in the browser of the local client. if (cmd==1){ .... became if ((cmd==1)||(cmd==2)){ if (cmd==1) { Goto Webpage 1 //Run Function uint16_t print_webpage (uint8_t *buf) } else { Goto Webpage 2 // Run Function uint16_t print_webpage_2(uint8_t *buf) };
} if (strncmp ("GET ", (char *) & (buf[dat_p]), 4) != 0) { // head, post for possible status codes 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>")); goto SENDTCP; }
24
if (strncmp("/ ", (char *) & (buf[dat_p+4]), 2) == 0){ plen = print_webpage (buf); goto SENDTCP; } cmd = analyse_cmd ((char *) & (buf[dat_p+5])); if (cmd == 1){ plen = print_webpage (buf); // Send the "Data" page } if (cmd == 2){ plen = print_webpage_2 (buf); // Send a different page (if Needed ) } SENDTCP: es.ES_make_tcp_ack_from_any(buf); // send acknowledge for http es.ES_make_tcp_ack_with_data(buf,plen); // send data } } }
The next function is a subroutine program , used to find the memory address of the The returned value which is stored in the global var strbuf
uint8_t find_key_val(char *str,char *key) { uint8_t found=0; uint8_t i=0; char *kp; kp=key; while(*str && *str!=' ' && found==0){ if (*str == *kp){ kp++; if (*kp == '\0'){ str++; kp=key; if (*str == '='){ found=1; } } }else{ kp=key; } str++; } if (found==1){ // copy the value to a buffer and terminate it with '\0' while(*str && *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){ strbuf[i]=*str; i++; str++; } strbuf[i]='\0'; } return(found); }
The analyse_cmd() function looks specifically for a request argument called cmd, and returns the value if its a number.
int8_t analyse_cmd (char *str) { int8_t r = -1;
25
if (find_key_val (str,"cmd")) { if (*strbuf < 0x3a && *strbuf > 0x2f) { // is a ASCII number, return it r = (*strbuf-0x30); } } return r; }
The next function is the main function which is to be used by the arduino code to make a HTML web page in the local clients browser
uint16_t print_webpage (uint8_t *buf) { // Arrays to hold the temperature reading from each sensor char temp_sensor[10]; float tempC; tempC=AnalogRead(sensor); tempC=(5.0 * tempC*100.0)/1024.0 // this equation converts the analog value from lm35 into the temperature reading // and store the value in variable TEMPC plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<html><head><title>Arduino ENC28J60 Ethernet Shield : Made by Saptarshi Nag : Mem no:193965</title></head><body>")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<center><h1> ENC28j60 Ethernet controller </h1>")); plen = es.ES_fill_tcp_data_p (buf, plen, PSTR ("Temprature value is")); for (i=0;i<10;i++) { buf[TCP_CHECKSUM_L_P+3+plen]=temp_sensor[i++]; plen++; } return plen; }
There is a need to convert your data to a string, and then use of instruction
buf[TCP_CHECKSUM_L_P+3+plen]=temp_sensor[i++];
to add those characters on to the buffer. The main program to be burned in the microcontroller for interface with the Ethernet controller connected to a Router :/* Web based Temperature monitoring system Created by Saptarshi Dec 2011 */ #include "etherShield.h"
26
#define SENSOR 0 // pin 28 of ATMEGA is pin 0 when programmed in Arduino //Programming Environment static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; static uint8_t myip[4] = {192,168,0,77}; static char baseurl[] = "http://192.168.0.77/"; static uint16_t mywwwport = 80; // listen port 80 by default for tcp/www (max range 1-254) #define BUFFER_SIZE 500 static uint8_t buf[BUFFER_SIZE+1]; #define STR_BUFFER_SIZE 22 static char strbuf[STR_BUFFER_SIZE+1]; EtherShield es=EtherShield(); // prepare the webpage by writing the data to the tcp send buffer uint16_t print_webpage(uint8_t *buf, byte* on_off); int8_t analyse_cmd(char *str);
void setup() { /*initialize enc28j60*/ es.ES_enc28j60Init(mymac); es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz delay(10); /* Magjack leds configuration, from enc28j60 datasheet // LEDA=greed LEDB=yellow // 0x880 is PHLCON LEDB=on, LEDA=on // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x880); delay(500); // // 0x990 is PHLCON LEDB=off, LEDA=off // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x990); delay(500); // // 0x880 is PHLCON LEDB=on, LEDA=on // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x880); delay(500); // // 0x990 is PHLCON LEDB=off, LEDA=off // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00); es.ES_enc28j60PhyWrite(PHLCON,0x990); delay(500); // // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10); es.ES_enc28j60PhyWrite(PHLCON,0x476); delay(100); //init the ethernet/ip layer: es.ES_init_ip_arp_udp_tcp(mymac,myip,80); uint16_t plen, dat_p; int8_t cmd; char buft[64]; char dbg[64];
27
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf); /*plen will ne unequal to zero if there is a valid packet (without crc error) */ if(plen!=0){ // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address. if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){ es.ES_make_arp_answer_from_request(buf); return; } // check if ip packets are for us: if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){ return; } if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ es.ES_make_echo_reply_from_request(buf,plen); return; } // tcp port www start, compare only the lower byte if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_POR T_L_P]==mywwwport){ if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){ es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack return; } if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){ es.ES_init_len_info(buf); // init some data structures dat_p=es.ES_get_tcp_data_pointer(); if (dat_p==0){ // we can possibly have no data, just ack: if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){ es.ES_make_tcp_ack_from_any(buf); } return; } if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ // head, post and other methods for possible status codes
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>")); goto SENDTCP; } if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){ plen=print_webpage(buf, on_off); goto SENDTCP; }
cmd = analyse_cmd ((char *) & (buf[dat_p+5])); if (cmd == 1){ plen = print_webpage (buf); // Send the "Data" page } if (cmd == 2){ plen = print_webpage_2 (buf); // Send a different page (if Needed ) } SEND TCP: es.ES_make_tcp_ack_from_any(buf); // send ack for http es.ES_make_tcp_ack_with_data(buf,plen); // send data } }
28
// The returned value is stored in the global var strbuf uint8_t find_key_val(char *str,char *key) { uint8_t found=0; uint8_t i=0; char *kp; kp=key; while(*str && *str!=' ' && found==0){ if (*str == *kp){ kp++; if (*kp == '\0'){ str++; kp=key; if (*str == '='){ found=1; } } }else{ kp=key; } str++; } if (found==1){ // copy the value to a buffer and terminate it with '\0' while(*str && *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){ strbuf[i]=*str; i++; str++; } strbuf[i]='\0'; } return(found); } int8_t analyse_cmd(char *str) { int8_t r=-1; if (find_key_val(str,"cmd")){ if (*strbuf < 0x3a && *strbuf > 0x2f){ // is a ASCII number, return it r=(*strbuf-0x30); } } return r; }
uint16_t print_webpage (uint8_t *buf) { // Arrays to hold the temperature reading from each sensor char temp_sensor[10]; float tempC; tempC=AnalogRead(sensor); tempC=(5.0 * tempC*100.0)/1024.0 // this equation converts the analog value from lm35 into the temperature reading // and store the value in variable TEMPC
29
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<html><head><title>Arduino ENC28J60 Ethernet Shield : Made by Saptarshi Nag : Mem no:193965</title></head><body>")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<center><h1> ENC28j60 Ethernet controller </h1>")); plen = es.ES_fill_tcp_data_p (buf, plen, PSTR ("Temprature value is")); for (i=0;i<10;i++) { buf[TCP_CHECKSUM_L_P+3+plen]=temp_sensor[i++]; plen++; } return plen; }
The program above has been made to build a simple webserver to put up the sensor values up on the internet. This is a Half Duplex communication.
30
We first download the Java client library and Google Collection Library nicknamed Guava to the Processing library. After downloading the required libraries. We built the Processing code which reads the serial port for sensor values and upload the code to the spreadsheet service of the Google account assigned in the code . In the beginning we need to import the library whose functions( predefined ) we would be using in our program
import import import import com.google.gdata.client.spreadsheet.*; com.google.gdata.data.*; com.google.gdata.data.spreadsheet.*; com.google.gdata.util.*;
The four instructions above imports from the Google API client library depending on the application we are working on (Google Spreadsheet). The above four imported packages also have been recommended for spreadsheet related application in Google Developers Guide (v3.0). the last imported package gdata.util is the most important among all the four gdata.util contains a wide assortments of classes and interfaces throughout the core Google Api client library .
import processing.serial.*;
This instruction is used for importing the Processings serial library. The Processing serial library allows for easily reading and writing data to and from external machines. In this case the ATMEGA168 microcontroller. The next few instructions are used to uniquely identify your Google Account and the spreadsheet we want to record our temperature sensor data.
// Variables structures for google spreadsheet API SpreadsheetService service; //Holds link to all my spreadsheets WorksheetEntry worksheet; //Holds link to the sensor log spreadsheet String uname = "[Saptarshi.nag99]";//My Google user name String pwd = "[--- My Google Password ---]"; String spreadsheet_name = "sensor log"; to want to write the sensor data to. //Name of the spreadsheet we want
Next we define the variables we need for writing sensor data ...
Serial port; // Create object from Serial class int oldTime; //timer variable int reportingInterval = 2000; //Number of miliiseconds between when sensor data is recorded ; This is the Sampling rate which may differ for diffent sensors
After this declaration, our main program starts, We use the predefined library of the Google API clent within the user defined function transmitData(<value to be written>) which passes values to the Google spreadsheet.
void transmitData(float val) {
31
String date = day() + "/" + month() + "/" + year(); //Build the current date,from Google server String time = hour() + ":" + minute() + ":" + second(); //Build the current time,from Google server try { //Create a new row with the name value pairs ListEntry newEntry = new ListEntry(); //Creates an object newEntry newEntry.getCustomElements().setValueLocal("date", date); newEntry.getCustomElements().setValueLocal("time", time); newEntry.getCustomElements().setValueLocal("reading",Float.toString(val)); //Write it out to the google doc URL listFeedUrl = worksheet.getListFeedUrl(); ListEntry insertedRow = service.insert(listFeedUrl, newEntry); } catch (Exception e) { println(e.getStackTrace()); } }
Are the ones which actually upload values up on the Google Server. transmitdata() is where most of the work happens. It starts by creating a number of strings that hold the current date and the time, and then creates a new ListEntry, which is the primary data class used to interact with worksheets. The class method getCustomElements().setValueLocal() is used to map the Processing variables to the column names in the worksheet.
The codes have been enclosed between the try catch block which is a Java programming instruction for exceptional handling. An exception is an event, which occurs during the execution
of a program, that disrupts the normal flow of the program's instructions. The catch block contains code that is executed if and when the exception handler is invoked. The next codes set up the processing to start receiving serial data
Println(Serial.list); String arduinoPort = Serial.list()[<the port number controller is connected>]; port = new Serial(this, arduinoPort, 9600);
The first instruction , list the number of available serial ports in your PC , In the second instruction we assign the name of the port arduinoPort and in the third line of the instruction we define the baudrate in this case by the rate of 9600 baud rate data would be transmitted/Received between PC and microcontroller The Main Processing Program:import com.google.gdata.client.spreadsheet.*;
32
import com.google.gdata.data.*; import com.google.gdata.data.spreadsheet.*; import com.google.gdata.util.*; import processing.serial.*; // Variables structures for google spreadsheet API SpreadsheetService service; //Holds link to all your spreadsheets WorksheetEntry worksheet; //Holds link to the sensor log spreadsheet String uname = "[Saptarshi.nag99]"; //Your google account user name String pwd = "[--- My Google Password ---]"; String spreadsheet_name = "sensor log"; int spreadsheet_idx = 0; //Index for the "sensor log spreadsheet //Variables for writing sensor data Serial port; // Create object from Serial class int oldTime; //timer variable int reportingInterval = 2000; //Number of miliiseconds between when sensor data is recorded // Sends the data to the spreadsheet void transmitData(float val) { String date = day() + "/" + month() + "/" + year(); //Build the current date String time = hour() + ":" + minute() + ":" + second(); //Build the current time try { //Create a new row with the name value pairs ListEntry newEntry = new ListEntry(); newEntry.getCustomElements().setValueLocal("date", date); newEntry.getCustomElements().setValueLocal("time", time); newEntry.getCustomElements().setValueLocal("reading", Float.toString(val)); //Write it out to the google doc URL listFeedUrl = worksheet.getListFeedUrl();//URL of the resource is set ListEntry insertedRow = service.insert(listFeedUrl, newEntry); } catch (Exception e) { println(e.getStackTrace()); } } void setup() { //Set up the serial port to read data Println(Serial.list) String arduinoPort = Serial.list()[3]; port = new Serial(this, arduinoPort, 9600); oldTime = millis(); //Set up the google spreadsheet try { service.setUserCredentials(uname, pwd); // Search for the spreadsheet named we're looking for // this code pulls a list of all the Spreadheets inthe acocunt and searches for the // one with the matching name. URL feedURL = new URL("http://spreadsheets.google.com/feeds/spreadsheets/private/full/");
33
SpreadsheetFeed feed = service.getFeed(feedURL,SpreadsheetFeed.class); for (SpreadsheetEntry entry: feed.getEntries()) { if (entry.getTitle().getPlainText().equals(spreadsheet_name) ) { break; } spreadsheet_idx += 1; } //Fetch the correct spreadsheet SpreadsheetEntry se = feed.getEntries().get(spreadsheet_idx); //Fetch the spreadsheet we want worksheet = se.getWorksheets().get(0); //Fetch the first worksheet from that spreadsheet println("Found worksheet " + se.getTitle().getPlainText()); } catch (Exception e) { println(e.toString()); } } //Reads the port every few seconds and sends the data back to Google void draw() { float val = 0; if (port.available() > 0) { // If data is available, val = port.read(); // read it and store it in val transmitData(val); // Send the val as argument in the function //transmitData(float) } }
Mode 3: A Full Duplex communication over the internet , between the third party server (Pachube) and an Embedded device connected to PC directly.
Pachube is an on-line database service provider allowing developers to connect sensor data (energy and environment data from objects, devices & buildings) to the Web and to build their own applications on it. This method uses a internet enabled PC connected through a serial port to the embedded device. The microcontroller sends the sensor values to the PC through the serial port . A processing code is used in this method too to run on the host PC collect the data being send by the microcontroller through serial port and this time uses Pachube services to host the sensor data in a graphical and legible order. Pachube also provides application of dial and push button to send data to the embedded device.
Methodology
Create an account in Pachube
34
First we need to make an account in Pachube.com by filling in a form and select a username and password. Then go to Homepage > My feeds and The browser will redirect you to another page ,where you have to fill in an another form where you mention the: Title of the Feed Description of the Feed Select Tags (optional) Geographical Location name Select your location in the Map or mention your latitude/longitude Select the Domain your sensors are working at (Indoor or outdoor), (Fixed/Mobile) Select the privacy settings (Public or Private) Add a Datastream (variable whose values will be displayed and stored)
Immediately after filling up the form , We request the Pachube service to generate an API key for the Datastream ( Which we would be using in our Processing program ) After we set up the following configuration details in our Pachube account. Its time to start sending data . We connect the Microcontroller board with the host PC. In Processing we have to install New libraries of Pachuino Library (v 005), EEML library ( Extended Environments Markup Language (EEML)) for link processing codes to Pachube. 35
After which we compile the processing code (given below), in the host PC : import processing.serial.*; import cc.arduino.*; import eeml.*; import pachuino.*;
void setup(){ p = new Pachuino(this, Arduino.list()[2],9600); // where 9600 is the baud rate //and [2] is the serial port were the microcontroller is connected p.setPort(5230);
p.setKey("cI2AMoDhJj1RDuiTXi9kTsmLMegU90G8Gw8hZaXjlOvuMROxqHh9fezUT6sBR5sZWT jDe4KyKAQgYEybQrHMyKnBDL5HKdWYOPQ4yMmntWWhgdUFw0Nyhy2i9Q6fDfMH"); // MY API KEY ; Provided to me by Pachube.com // local sensors p.addLocalSensor("analog", 0,"Temperature");
void draw(){ float tempVal1 = p.localSensor[0].value; p.digitalWrite(15, 1); // Make the enable pin of l293D high
} 36
OutPut Screenshot
On Entering the URL https://pachube.com/feeds/44513 in a web browser (Google Chrome, in my case) the present sensor reading along with other information like Geographical location and other details will be displayed in a graphical and legible manner.
37
Pachube.com also provides option on how to display the data in other various graphical formats like dial clock , graphs with preferred color settings etc.
38
Conclusion This is a prototype of an Internet Enabled Data Acquisition system , which demonstrates how to upload the sensor value to a remote server through internet. It demonstrates the application of ENC28j60 Ethernet module developed by Microchip It demonstrates how to use the service of third-party external server of Pachube to collect and categorise data in legible and graphical order The complexity of the program increases with functionality , Protocols and Embedded systems are designed to minimise the complexity so that even a non technical operator can use the system with ease. Internet based data acquisition system could be widely used in industries and home for monitoring sensor data remotely wirelessly without the need of expensive wireless receiver/transmission module. Reference 1. Pactical Arduino : A book on Arduino programming environment written by Jonathan Oxan 2. Getting Started with Processing : Book written by Casy Reans and Ben Fry on the Processing programming language 3. Micrchip.com: Documentary notes of ENC28J60 Ethernet controller module 4. Atmel.com: Documentary notes on ATMEGA168 microcontroller 5. Arduino.cc: Official arduino programming website 6. Pachube.com 7. Mr Andy D linslays blog: http://blog.thiseldo.co.uk 8. Nuelectronics.com: developers of ENC28J60 ethernet modules C library for Arduino 9. Makezine.com: online magazine for electronics hobbyist Projects 10. Code.google.com: to read the documentary of the Google API library for Java client.
39