Feed on
Posts
Comments

Check my new blog post on the ESP8266 Serial-to-WiFi module


As the Internet-of-Things (IoT) are becoming more and more popular, there is an increasing demand for low-cost and easy-to-use WiFi modules. For Raspberry Pi and BeagleBone, you can plug in a cheap USB WiFi dongle (like the popular Edimax 7811). These dongles are generally less than $10. But for microcontroller-based devices, the options are much fewer and generally more costly. For example, an Arduino WiFi shield can easily cost $40 to $50; even the more recent CC3000 module costs about $35. If you are puzzled by the big price difference: those cheap WiFi dongles require a USB host system, which is beyond the capability of Arduino. Still, it’s unsettling to realize that a WiFi module or shield would cost more than an off-the-shelf WiFi router, even though the router is significantly more powerful.

Recently I saw a post on Hack A Day alerting us about a new Serial-to-WiFi chip. I immediately ordered two of those. I honestly don’t know what I should expect from these $3 parts, but perhaps it’s a good push towards a new wave competitions on low-cost WiFi solutions. Anyways, while waiting for the shipment to arrive, it reminded me that a while back I actually bought something similar: a Hi-Link HLK-RM04 Serial-to-WiFi module. It’s about $10, so still pretty low-cost. I am glad I didn’t lose it, so I took it out of a pile of electronics and started experimenting with it.

IMG_0222IMG_0221

As you can see, the module is pretty small (about 40mm x 30mm). The picture on the right above shows the components underneath the metal shield. The chip (RT5350F) is a 360MHz MIPS core with built-in WiFi support. The module is quite powerful — at factory default settings it functions as a normal WiFi router. Now, in order to get it to talk to a microcontroller like Arduino, I need to use its Serial-to-WiFi capability. What is that? Well it means using the serial (TX/RX) interface to send and receive Ethernet buffers, and similarly using serial to send commands to the module and query or change its current status. This is quite convenient because first, it only takes two wires (TX/RX) of the microcontroller to talk to the module, second, it moves WiFI-related tasks to the module allowing the Arduino code to be very much light-weighted.


Check my new blog post on the ESP8266 Serial-to-WiFi module


Power-Up. To start the module you just need to provide +5V power. At the first use, the module boots up into a WiFi AP (access point) named Hi-Link-xxxx so you can actually log on to the WiFi network and change settings there. Here is what the homepage looks like (default IP: 192.168.16.254, default log-in: admin/admin):

hlk_rm04_1

Change Settings. For my purpose I need to set the module as a WiFi client so that it can log on to my home network, and allow the Arduino to communicate wirelessly through the module. So I changed the NetMode to WiFi Client – Serial, and put in my home network’s SSID and password. I also changed the Serial baud rate to 57600 (the default is 115200): the lower baud rate can help Arduino to communicate with it more reliably especially if I am going to use software serial. Make sure that the Local/Remote port number is set to 8080 or anything other than 80 (because 80 is reserved for the module’s homepage).

hlk_rm04_2

Receiving Data. Now after the module boots up, it appears as a device on my home network (for example, 192.168.1.147). If I type in 192.168.1.147 in a browser, I will still see the same homepage as above. However, now I can communicate with the module’s serial interface through port 8080. To see what’s going on, I used a PL2303 USB-serial converter: connect the serial converter’s +5V, GND, TXD, RXD to the WiFi module’s +5V, GND, RX, TX (note TXD->RX and RXD->TX), then I opened a serial monitor at 57600 baud rate (putty in Windows or gtkterms in Linux). Now type in http://192.168.1.147:8080 in a browser, I get the following output on the serial monitor:

GET / HTTP/1.1
Host: 192.168.1.147:8080
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4

Cool, this means the Ethernet buffer has been received through the serial RX pin. If I can then send something back through the serial TX pin, like acknowledgement plus HTML text, the data will be transferred back to the browser. This is basically how HTTP GET command works.

A Simple Arduino Example. To send data back through the serial TX pin, I used an Arduino to set up a simple example. To get reliable communication, I used Arduino’s hardware RX/TX (0/1) pins. The drawback of this is that when you program the Arduino you need to temporarily disconnect the WiFi module from these two pins, otherwise they will interfere with the uploading process. Here is a picture of the setup:
hlk_rm04_3

Next, I wrote an Arduino program to print out the analog values of A0 to A5. This is actually modified from the WebServer example in the Arduino Ethernet Shield library:

void setup() {                
  Serial.begin(57600);
}

void loop() {
  boolean has_request = false;
  if (Serial.available()) {
    while(Serial.available()) {char c = Serial.read();}
    has_request = true;
  }
  if (has_request) {
    Serial.println("HTTP/1.1 200 OK");
    Serial.println("Content-Type: text/html");
    Serial.println("Connection: close");  // the connection will be closed after completion of the response
    Serial.println("Refresh: 5");  // refresh the page automatically every 5 sec
    
    String sr = "\n";
    sr += "

Basically receiving/sending Ethernet buffers is done through reading/writing into serial. Now open a browser and type in http://192.168.1.147:8080, I see the following:

analog input 0 is 521
analog input 1 is 503
analog input 2 is 498
analog input 3 is 510
analog input 4 is 525
analog input 5 is 548

Pretty cool, isn’t it! 🙂 If I have a SD card connected to Arduino, I can also serve files, such as Javascripts or logging data, from the SD card.

Blinking LEDs. By analyzing the received buffer, I can also implement various HTTP GET commands. For example, the program below sets the LED blinking speed by using http://192.168.1.147:8080/blink?f=5 where f sets the frequency.

void setup() {     
  Serial.begin(57600);  
  pinMode(13, OUTPUT);
}

int f = 0, pos;
void loop() {
  boolean has_request = false;
  String in = "";
  if (Serial.available()) {
    in = "";
    while (true) {  // should add time out here
      while (Serial.available() == false) {}
      in += (char)(Serial.read());
      if (in.endsWith("\r\n\r\n")) {
        has_request = true;  break;
      }
    }
  }
  if (has_request) {
    int i1 = in.indexOf("GET /blink?f="), i2;
    if (i1 != -1) {
      i2 = in.indexOf(" ", i1+13);
      f = in.substring(i1+13, i2).toInt();
    }
    Serial.println("HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close");
    String sr = "\n";
    sr += "\n";
    sr += "LED frequency: ";
    sr += f;
    sr += "Hz.";
    Serial.print("Content-Length: ");
    Serial.print(sr.length());
    Serial.print("\r\n\r\n");
    Serial.print(sr);
    has_request = false;
  }
  if (f>0) {
    static unsigned long t = millis();
    if (millis() > t + 1000/f) {
      digitalWrite(13, 1-digitalRead(13));
      t = millis();
    }
  }
}

More on Changing Settings. Instead of using the WiFi module’s homepage to configure settings, you can also change settings by sending serial commands to the module directly. This can be done by pulling the RST pin on the module low for a couple of seconds, which switches the chip in AT command mode. Then you can send (through serial) any of the listed AT commands to change parameters such as SSID and password. This is a nice way to add some automation to the configuration process. Details can be found in the module’s datasheet.

Drawbacks. The main drawback of a Serial-to-WiFi module is that the data transfer speed is quite low. I tested transferring a file stored on SD card and am only getting 5 to 7 KB/s. This means to transfer a 1MB file it will take 3 minutes. Clearly not a good solution for bulk data. Another drawback is that if you need to restart the WiFi module it generally takes 35 to 50 seconds to boot up, that can be an issue for the impatient. Fortunately you likely don’t need to reboot the WiFi module frequently.

Overall the HLK-RM04 Serial-to-WiFi module is a reasonable and low-cost solution for adding WiFi to Arduino and similar microcontroller boards. At the price of $10, there is no much more you can ask for 🙂

Note: check out our OpenSprinkler DC-powered version, which uses an innovative circuit design that drives sprinkler solenoids using DC-only voltage.

I often get questions about sprinkler valves, so in this post I will explain the basic electric properties of sprinkler valves. If you are designing a sprinkler controller circuit, understanding these properties can be helpful. It’s a common mistake to assume sprinkler valves work with DC voltage. While most valves indeed CAN be powered by DC voltage (see below), they are designed to work with AC voltage in the range of 22VAC to 28VAC. That’s why if you look at a standard sprinkler transformer, the output is usually AC.

The electric part of a sprinkler valve is the solenoid — it’s a cylindrical-shaped thing screwed into the valve. At the center of the solenoid is a rod supported by a spring. The solenoid has two wires connected to its internal coil. Applying 24VAC on the two wires energizes the coil, and causes the rod to contract into the solenoid. This releases the internal water pressure thus opening the valve, allowing water to flow through the valve. Removing the voltage causes the rod to revert back to its original position. This allows the water pressure to build up internally hence stopping the flow. Because closing the valve relies on internal pressure build-up, it usually takes a few seconds to completely stop the water flow. This also means if your water pressure is too low you may not be able to completely stop the water flow.

IMG_0205IMG_0206

Let’s start by measuring the resistance of the sprinkler solenoid. I have two example solenoids, one made by Orbit and one made by Hunter. According to the multimeter, one measures 32.3 ohm, and the other measures 24.1 ohm. So the resistances are pretty low. If you think about it for a while, you might realize something is not quite right here: if we apply 24V on the solenoid, wouldn’t that produce a 24 V / 24.1 ohm = 1 amp current draw? That’s quite steep. In fact, my sprinkler transformer is only rated 750 mA output current, so it can’t provide enough current to drive even one solenoid?!

The catch is exactly in the fact that sprinkler solenoids are powered by AC voltage. Because the solenoid is made of a coil, it not only has coil resistance but also inductance. When operated on AC power, the inductance produces significant reactance which cannot be ignored. You can read the Wikipage to find out how reactance is calculated, but basically it has to do with the frequency of the input voltage, and the inductance of the coil. Because inductors ‘prevent’ current from changing rapidly, it behaves like a ‘resistor’ under changing current (i.e. AC). The higher the frequency, the higher the ‘resistance’ (i.e. reactance).

With an LCR meter, I measured the inductance of the two solenoids:
IMG_0207IMG_0209
One reads 63.57 mH, the other 132.45 mH. So if we power the solenoids by 24VAC, 60Hz, which is the standard output of a sprinkler transformer, we will get a reactance of:

This, plus the resistance, gives a total impedance of:

Note that the reactance counts into the imaginary part of the impedance. Using complex numbers is just a convenient way of denoting not only the magnitude but also the phase. For example, when you apply a sinusoid voltage on the inductor, the corresponding current is also a sinusoid wave, but with a different phase. Using complex numbers, the calculation can be carried out quite easily.

Now we can calculate the operating current under 24VAC (rms). We only care about the magnitude, so the current (rms) would be:

OK, so this is getting closer to the reality. But 0.6 amp current still sounds high. What is missing? Well, remember that when the solenoid is activated, the rod will be attracted into the solenoid, and that can change the inductance significantly. So let’s re-measure the inductance with the rod pushed in:

IMG_0208IMG_0210

Indeed the inductance jumped from 63.57 mH and 132.45 mH previously to 194.4 mH and 199.6 mH respectively. OK, now if we redo the calculations, we will find out that the correct reactance is:

and current (rms) os:

The resulting current is about the same on the Hunter solenoid. So this roughly matches the electric specification of a typical sprinkler valve. It’s actually still a bit off: if we measure the actual AC current flowing through the solenoid:
IMG_0211IMG_0212

The readings are about 0.2 amp. I suspect the difference comes from the measurement of the inductance. On my LCR meter, which can measure inductance at two frequency levels: 120 Hz and 1 kHz, I find that the inductance is measured differently under the two frequencies. Because the actual operating frequency of the solenoid is 60 Hz, and my meter cannot measure 60 Hz, the inductance I am getting is probably somewhat off. That should explain the difference between the calculation and the actual current reading.

The 0.6 amp current we calculated above probably explains the inrush current: when the solenoid is just energized, there is an impulse current that’s typically higher than the holding current. This is because the rod is still out, and hence the reactance is lower, causing a higher current than when the rod is attracted in.

Operating Sprinkler Valves Under DC

Note: check out our OpenSprinkler DC-powered version, which uses an innovative circuit design that drives sprinkler solenoids using DC-only voltage.

From the calculations above, it’s obvious that the coil inductance is important at limiting the operating current when the valve is powered under AC. What about if we power the valve under DC? Obviously we shouldn’t use 24VDC, because that would draw too much current (0.75 to 1 amp). If you search online, you will find plenty of posts talking about powering sprinkler valves using 12VDC. This actually works well in general. Using 12VDC has advantages in that 12VDC power adapters are cheaper and much easier to find; the circuit design is simpler, and you can use the same circuit to interface with other DC devices like relays and motors. In contrast, 24VAC power circuits are more complex and you can’t use the same circuit to directly interface with DC devices.

However, you should be aware that because the sprinkler solenoid’s resistance is pretty low, the operating current under 12VDC will be relatively high, around 400 to 500 mA. This more than doubles the 200 mA operating current (rms) under 24VAC. Also, the coil will heat up more, and this potentially shortens its life. For example, under 12VDC, the Orbit valve above will dissipate 12 * 12 / 32.3 = 4.5 Watt; whereas under 24VAC, the same valve only dissipates 0.2 * 0.2 * 32.3 = 1.3 Watt (note that only the resistive portion dissipate power, inductive portion does’t).

Again, the issue is that under DC there is no reactance, so the coil’s inductance plays no effect at limiting the current. What if we reduce the voltage further to 9VDC, in order to reduce the operating current? After all, the solenoid only needs 200mA holding current to remain activated. Unfortunately that won’t work: I’ve tried powering solenoids with 9V, and I can’t get the valve to reliably energize. The problem is that 9V is not sufficient to provide the required inrush current, so the rod cannot get fully attracted in. However, if the rod is already in, 9V is sufficient to hold the solenoid activated. So if you really want to make it work with 9V, you need a circuit that can provide a high impulse voltage; then once the solenoid is activated, you can lower the voltage to reduce the current (hence power) consumption. A possible solution is to use a boot converter (very much similar to circuits for latching solenoids) to provide an impulse high voltage, but this comes at the cost of increased circuit and software complexity.

At the Maker Faire this year I got lots of questions about soil moisture sensors, which I knew little about. So I started seriously researching the subject. I found a few different soil sensors, learned about their principles, and also learned about how to make my own. In this blog post, I will talk about a cheap wireless soil moisture sensor I found on Amazon.com for about $10, and how to use an Arduino or Raspberry Pi to decode the signal from the sensor, so you can use it directly in your own garden projects.

IMG_0183

What is this?
A soil moisture sensor (or meter) measures the water content in soil. With it, you can easily tell when the soil needs more water or when it’s over-watered. The simplest soil sensor doesn’t even need battery. For example, this Rapitest Soil Meter, which I bought a few years ago, consists of simply a probe and a volt meter panel. The way it works is by using the Galvanic cell principle — essentially how a lemon battery or potato battery works. The probe is made of two electrodes of different metals. In the left picture below, the tip (dark silver color) is made of one type of metal (likely zinc), and the rest of the probe is made of another type of metal (likely copper, steel, or aluminum). When the probe is inserted into soil, it generates a small amount of voltage (typically a few hundred milli-volts to a couple of volts). The more water in the soil, the higher the generated voltage. This meter is pretty easy to use manually; but to automate the reading you need a microcontroller to read the value.

IMG_0180IMG_0182

Resistive Soil Moisture Sensor
Another type of simple soil sensor is a resistive sensor (picture on the right above). It’s made of two exposed electrodes, and uses the fact that the more water the soil contains, the lower the resistance between the two electrodes. The resistance can be measured using a simple voltage dividier and an analog pin. While it’s very simple to construct, resistive sensors are not extremely reliable, because the exposed electrodes can degrade and get oxidized over time.

Capacitive Soil Moisture Sensor
Capativie soil sensors are also made of two electrodes, but insulated (i.e. not exposed). The two electrodes, together with the soil as a dielectric material, form a capacitor. The higher the water content, the higher the capacitance. So by measuring the capacitance, we can infer the water content in soil. There are many ways to measure capacitance, for example, by using the capacitor’s reactance to form a voltage divider, similar to the resistor counterpart. Another way is to create an RC oscillator where the frequency is determined by the capacitance. By counting the oscillation frequency, we can calculate the capacitance. You can also measure the capacitance by charging the capacitor and detecting the charge time. The faster it charges, the smaller the capacitance, and vice versa. The Chirp (picture below), which is an open-source capacitive soil sensor, works by sending a square wave to the RC filter, and detecting the peak voltage. The higher the capacitance, the lower the peak voltage. Capacitive sensors are not too difficult to make, and are more reliable than resistive ones, so they are quite popular.

chirp

More Complex Soil Sensors
There are other, more complex soil sensors, such as Frequency Domain Reflectometry (FDR), Time Domain Reflectometry (TDR), and neutron sensors. These are more accurate but also will cost a fortunate to make.

Wireless Soil Moisture Sensor
Because soil sensor is usually left outdoors, it’s ideal to have it transmit signals wirelessly. In addition, because soil moisture can vary from spot to spot, it’s a probably good idea to use multiple sensors distributed at different locations to get a good average reading. Wireless would make it more convenient to set up multiple sensors.

Recently I found this 433MHz wireless soil sensor from Amazon, for only $10, very cheap. It comes with a transmitter unit and a receiver display unit. The transmitter unit has a soil probe. The receiver unit has a LCD — it displays soil moisture level (10 bars) and additionally indoor / outdoor temperature. Let me open up the transmitter to see what’s inside:

IMG_0177IMG_0178IMG_0179IMG_0181

There is a soil probe, a 433MHz transmitter, a microcontroller at the center, a thermistor, and a SGM358 op-amp. Pretty straightforward. The soil probe looks quite similar to the battery-free soil meter probe that I mentioned above. So I am pretty sure this is not a resistive or capacitive probe, but rather a Galvanic probe. Again, the way it works is by outputting a variable voltage depending on the water content in soil. By checking the PCB traces, it looks like the op-amp is configured as a voltage follower, which allows the microcontroller to reliably read the voltage generated by the Galvanic probe.

Now we understand the basic principle of the sensor, let’s take a look at the RF signal from the sensor. I’ve done quite a few similar experiments before, so I will just follow the same procedure as described in this post.

Raw Waveform. To begin, I use a RF sniffing circuit to capture a raw waveform, which looks like this:

soil_waveform

Encoding. Each transmission consists of 8 repetitions. The above shows one repetition: it starts with a sync signal (9000us low); a logic 1 is a impulse (475us) high followed by a 4000us low; a logic 0 is the same impulse high followed by a 2000us low. So the above signal translates to:

11110011 01100000 11111111 00111001 1111

The signal encodes both temperature and soil humidity values. By varying temperature and soil moisture, and observing how the signals change, it’s pretty easy to figure out that the 12 bits colored blue correspond to temperature (10 times Celcius), and the 8 bits colored red corespond to the soil moisture value. The first 12 bits are device signature, which is quite typical in this type of wireless sensors; the last four bits are unclear, but likely some sort of parity checking bits for the preceding four bytes). So the above signal translates to 25.5°C and a soil moisture value of 57.

The display unit shows soil moisture level in 10 bars — 1 to 3 bars are classifed as ‘dry’, 4 to 7 bars are classified as ‘damp’, and above 7 bars are classified as ‘wet’. How does this translate to the soil moisture value? Well empirically (from the data I observed) the dry-damp boundary is around 60, and damp-wet boundary is around 100.

Arduino Program. I next wrote an Arduino program to listen to the sensor and display the soil moisture value and temperature to the serial monitor. For this you will need a 433MHz receiver, and the program below assumes the receiver’s data pin is connected to Arduino digital pin 3. Because the encoding scheme is very similar to a wireless temperature sensor that I’ve analyzed before, I took that program and made very minimal changes and it worked instantly.

IMG_0051soil_display_arduino

Raspberry Pi Program. By using the wiringPi library, the Arduino code can be easily adapted to Raspberry Pi. The following program uses wiringPi GPIO 2 (P1.13) for data pin.

IMG_0185IMG_0184

I haven’t done much tests about the transmission range. I’ve put the sensor at various locations on my lawn, and I’ve had no problem receiving signals inside the house. But my lawn is only a quarter acre in size, so it’s not a great test case.

One thing I really liked about using off-the-shelf sensors is that they are cheap and have ready-made waterproof casing. That combined with an Arduino or RPi can enable a lot of home automation projects at low cost.

This is a quick update that AASaver has been upgraded to version 2.1. It inherits all the capabilities of version 2.0, including built-in 5V voltage booster, flashlight LEDs, breadboard power pin headers, USB port (for charging USB devices), LiPo charger (with adjustable charging current). On top of those, version 2.1 adds a 3.3V LDO and a switch you can use to choose between 5V or 3.3V output voltage. This has been the main requested feature that was missing on version 2.0. Using an LDO (instead of changing the feedback resistors) has the advantages that the voltage booster and USB port always output 5V, while the 3.3V output is only effective on the LEDs and the breadboard pin headers. Here are some pictures:

IMG_0173IMG_0158IMG_0161

IMG_0162IMG_0159IMG_0171

For those who are curious what AASaver is: it’s a multi-purpose voltage booster for AA/AAA batteries. I came up with this idea initially when I was cleaning up a box of ‘dead’ AA batteries one day, and was surprised that many of them actually have pretty good output voltage, like 1.2 to 1.3V. Many AA batteries rejected from gadgets (e.g. remote controls and smoke alarms) still have plenty of juice, but these gadgets don’t have built-in booster circuit to bump the voltage up, so a lot of batteries are wasted this way. I was learning voltage booster at that point and had the idea of designing a multi-purpose circuit for AA batteries, so that I can harvest the remaining energy in ‘dead’ batteries for a variety of things, like lighting up LEDs, powering breadboard circuits, charging LiPo batteries etc. That’s how AASaver was invented. It’s not restricted to used or ‘dead’ batteries — if you plug in a fresh pair of batteries, you can also use it to charge your phone or other USB gadgets. It’s a really neat, useful, and inexpensive tool that everyone should have a few of these!

Below is the original video I made for AASaver. Keep in mind that the current version has a lot more features than shown in the video, including USB charging, LiPo charging. You can even modify it to become a solar charger.

With this new version, I’ve also prepared a more detailed User Manual, with assembly and usage instructions. We have just fulfilled an order from Micro Center so in the near future you may even find AASaver in the Micro Center retail stores 🙂

Back in April, I blogged about WAi (Worthington Assembly Inc.), a circuit assembly company located only 15 minutes away from where I live. They’ve got great reputation in the maker community, and have helped projects such as Tessel, RGB-123 LED matrices to take off. I am glad to announce that we’ve now partnered with WAi to manufacture OpenSprinkler Pi (OSPi), and the first batch of 200 boards just came out from a late-evening production yesterday. Check out these awesome videos Chris Denney took during the production:

Full-speed version

Slow-motion version

Some static pictures (taken with an outdated phone, sorry about the quality!). Picture captions: 1) tapes loaded onto pick-and-place machine; 2) first board came out of the reflow oven; 3) first board after AOI inspection; 4) me holding a stack of 25 panels.
0612141917a0612142026

06121420390612142235

Compared to manufacturing in China, using a local facility has the unbeatable advantage of quick turn-around time. The cost may be higher than made in China, but for an open-source hardware business like us, it’s critical to remain agile and be quick about making changes. I’ve been there — waiting for months for an order to ship from China, which was painful and stressful. By the time the shipment arrived, I already wanted to move on to the next version. With WAi, we may have found just the perfect solution for our need, and this is supporting local manufacturing too, win-win! 🙂

« Newer Posts - Older Posts »