Controlling the Humidity with an Embedded Linux System
Listing 3. Read Sensor Data
unsigned int readSHT1xData(void)
{
int iLoop;
unsigned int uiBitRead;
unsigned int uiMSB=0;
unsigned int uiLSB=0;
unsigned int uiRetValue;
/* Read MSB from SHT1x */
for (iLoop = 0; iLoop < 8; iLoop++)
{
uiMSB <<= 1;
writeSHT1xOne(SCK_SHT);
uiBitRead = readSHT1x(DATA_SHT);
udelay(2);
writeSHT1xZero(SCK_SHT);
udelay(2);
if (uiBitRead)
uiMSB |= 1;
}
/* Acknowledge sequence; must drive data
* line as it is tri-stated at this point */
driveDataLine(DATA_SHT);
writeSHT1xZero(DATA_SHT);
udelay(2);
writeSHT1xOne(SCK_SHT);
udelay(2);
writeSHT1xZero(SCK_SHT);
tristateDataLine(DATA_SHT);
udelay(2);
/* Read LSB from SHT1x */
for (iLoop = 0; iLoop < 8; iLoop++)
{
uiLSB <<= 1;
writeSHT1xOne(SCK_SHT);
uiBitRead = readSHT1x(DATA_SHT);
udelay(2);
writeSHT1xZero(SCK_SHT);
udelay(2);
if (uiBitRead)
uiLSB |= 1;
}
/* Don't acknowledge last byte so the device
* doesn't transmit the 8-bit CRC as it isn't
* really necessary for this application */
uiRetValue = u8tou16(uiMSB, uiLSB);
return(uiRetValue);
}
The procedures shown in Listings 1, 2 and 3 form the core of the SHT11 two-wire interface device driver code. When the driver receives an ioctl requesting the humidity, the three instructions shown in Listing 4 are all that is needed to read the current humidity from the sensor.
Listing 4. Read Humidity Sequence
writeSHT1xTransmissionStartSequence();
writeSHT1xCommand(SHT1x_MEASURE_HUMIDITY);
uiHumidity = readSHT1xData();
The second device driver controls the relays and switches the 120V AC and neutral lines to the humidifier and dehumidifier. The ioctl interface for the relay driver required the following ioctl functions:
RELAY8_IOC_READ_RELAYS: read the current relay settings.
RELAY8_IOC_WRITE_RELAYS: set the relays to the supplied state.
Reading the relay settings is used to ensure that the relays are in the desired position. The relay hardware actually includes eight relays, and all eight relay values are written in one shot. The data register used to control and report the relay positions consists of one 8-bit register. This register either is read to report the current relay settings or written to change the relay settings. Unlike the SHT11 driver, the relay driver can affect a change in a relay state with one writeb Linux driver C instruction. Listing 5 shows the relay read and write procedures, along with an excerpt from the ioctl processing that differentiates between read and write. It doesn't get much simpler than this!
Listing 5. Read/Write Relays
unsigned char readRelay8(int iRelay8Address)
{
/* Read Relay8 register and return the value */
return(readb(iRelay8Address));
}
void writeRelay8(int iRelay8Address, unsigned char ucValues)
{
/* Write Relay8 register with the values */
writeb(ucValues, iRelay8Address);
}
// Excerpt from ioctl function:
switch(cmd) {
case RELAY8_IOC_READ_RELAYS:
/* Read Relay8 relay values */
ucRelayValues = readRelay8(relay8_base + RELAY8_CONTROL);
if (copy_to_user((typeof(relay8_relayValues)) arg,
&ucRelayValues,
sizeof(relay8_relayValues))) {
ret = -EFAULT;
}
break;
case RELAY8_IOC_WRITE_RELAYS:
/* Write Relay8 relay values */
writeRelay8(relay8_base + RELAY8_CONTROL,
*(typeof(relay8_relayValues)) arg);
break;
default:
ret = -ENOTTY;
}
I wrote a user-mode application that periodically polls the SHT11 device driver for the current humidity and temperature using the ioctl SHT1X_IOC_READ_HUMIDITY and SHT1X_IOC_READ_TEMPERATURE, respectively. Depending on the desired humidity setting, the application determines whether the current humidity is either too high or too low, taking into account the tolerance of plus or minus 3.5% rH. If an actionable event is determined, the specific relays are turned either on or off using the relay device driver RELAY8_IOC_WRITE_RELAYS ioctl function. For example, when the user-mode application reads the humidity and determines that the humidifier must be turned on, it issues an ioctl RELAY8_IOC_WRITE_RELAYS function to switch on both relays that are dedicated to the 120V A/C and neutral lines of the humidifier. At the same time, the application also ensures that the two relays associated with the 120V A/C and neutral lines of the dehumidifier are switched off. Relay control can be one of three options: 1) the humidifier is turned on, and the dehumidifier is turned off; 2) the dehumidifier is turned on, and the humidifier is turned off; or 3) both the humidifier and dehumidifier are turned off. The application never turns both the humidifier and dehumidifier on at the same time. The application is loaded at Linux boot time and, like most embedded applications, runs perpetually.
Along with controlling the humidifier and dehumidifier relays, the application accumulates and saves statistics. In this control system, the actual data that is acted upon is required to be persistent—that is, the humidity data must be saved somewhere for later use. The user-mode application is responsible for saving the data for later use by a browser, and it does so with the use of the SNMP (Simple Network Management Protocol) support provided by the net-snmp Linux package.
SNMP is a standard set of protocols and policies for managing networks and devices. The net-snmp implementation of SNMP consists of an agent, which runs as a Linux dæmon snmpd, and a database called a Management Information Base, or MIB. A MIB is structured as a tree, with branches grouping together similar items. I extended the standard Linux MIB that is shipped with the net-snmp package and added a new branch off of the “enterprises” node, which includes all the humidity controller items that I need (Figure 4). The snmpd agent acts on the MIB at the request of SNMP clients—that is, the agent reads/writes data from/to the MIB on behalf of client get and set requests. In this architecture, there are two clients: the user-mode application and the Web browser.
In order to adapt SNMP to any application, a MIB must be defined in a standard MIB ASN.1 format. I defined a MIB for my humidity controller and called it HUMIDITYCONTROLLER-MIB, which gets loaded when the snmpd dæmon runs during the Linux boot process. The MIB contains data items that are represented by object identifiers, or OIDs. An example of an OID definition from my MIB for the humidity controller targetHumidity variable is shown below:
targetHumidity OBJECT-TYPE
SYNTAX Integer32(0..2147483647)
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"Target humidity."
::= { humidityEntry 3 }
The previous ANS.1 MIB definition phrase generates an OID with the value .1.3.6.1.4.1.2200.2.3. This rather cryptic-looking sequence of numbers is a scheme used to identify a leaf in the MIB tree. The branch that I added to the enterprises node is identified by the integer 2200. Under the 2200 node is the node identified by a 2, which contains all of the overall humidity controller items that the system needs. The leaf node identified by a 3 is the targetHumidity.
The Linux SNMP package contains a very useful tool called mib2c. mib2c takes a MIB definition, such as HUMIDITYCONTROLLER-MIB, and generates C code that can be used to extend the standard Linux snmpd agent. Several options exist when generating code with mib2c. I used the more general option for generating C code from a MIB with the mib2c.scalar.conf configuration, which causes code to be generated for general-purpose scalar OIDs, as opposed to table-based OIDs. The generated C code is used by the snmpd dæmon. Listing 6 is a distilled example of the generated C code from mib2c for the targetHumidity OID that shows the code framework needed to support the SNMP get (MODE_GET) and SNMP put (MODE_SET_RESERVE1 and MODE_SET_COMMIT) operations.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- RSS Feeds
- Validate an E-Mail Address with PHP, the Right Way
- Readers' Choice Awards
- Tech Tip: Really Simple HTTP Server with Python
- DynDNS
54 min 27 sec ago - Reply to comment | Linux Journal
1 hour 26 min ago - All the articles you talked
3 hours 50 min ago - All the articles you talked
3 hours 53 min ago - All the articles you talked
3 hours 54 min ago - myip
8 hours 19 min ago - Keeping track of IP address
10 hours 10 min ago - Roll your own dynamic dns
15 hours 24 min ago - Please correct the URL for Salt Stack's web site
18 hours 35 min ago - Android is Linux -- why no better inter-operation
20 hours 50 min ago




Comments
I really couldn't understand
I really couldn't understand what a amancaes is?? what does if do anyways?? i could only understand that it indicates a very much greater degree of humidity, than at a corresponding height at Iquique, but how does it do that. Is an Amancaes also similar to Humidifier Filters. If this is so then why not keep a humidifier instead of a Amancaes. doesn't the both work same way.
explanation
Yes an amancaes is an ingenious device that can automatically control humidity - not only local humidity, but humidity in any other part of the world, remotely. It does this using the Iquique height factor to convert local humidity control (i.e. Linux kernel version/distribution) into remote humidifier filter command words. It is frankly one of the keys to Darwin's entire theory of natural selection, based on humidity and moss observation.