Monday, December 26, 2016

Getting the ESP-13 Arduino WiFi shield to work - part 1, sending data from Arduino over Wifi

I thought it would be fun to make some little WiFi enabled gadgets in my house, so I bought a cheap ($19.99) WiFi solution on Amazon.com : the "Gowoops ESP8266 Web Server Serial Port WiFi Shield Expansion Board ESP-13 and UNO R3 ATmega328P Development Board For Arduino with USB cable"

But it turned out that I, like many people, couldn't simply "turn it on and expect it to work".

So I spent a few hours clicking around various sites, including this one that was probably the closest to being helpful - but since it expected me to have a Windows machine, and some software that I could not use, I was still stuck.

So - for Mac users among you, here is what it took for me to get data that the Arduino generates to be sent over WiFi to a receiving program (in my case I used Python - because it's built in to the Mac, I am familiar with it, and networking is trivial. But it's really just there to prove that data is making it from the shield to the computer, over the wireless link).

Step 1: configure the shield as a serial port server

Plug the shield into your Arduino, and power them up (I just used the USB cable for this).
Press the “key” button on the ESP-13 (see picture)

for at least one second to make sure it goes back to default mode. It takes a few seconds to “come to life” in the default state.

Make sure the two DIP switches are OFF at this stage - this keeps the Arduino from talking to the shield – necessary for programming the Arduino. Note, in the above diagram the DIP switches are ON.

On your computer or phone, turn on WiFi and select DoitWiFi_Config from the list of possible access points. Make sure you have DHCP enabled, so your computer will be given an IP address by the ESP-13.  Once you are connected, open a browser, and go to 192.168.4.1 (this is the default address of the shield)

In the resulting configuration page, change the AP name to DoitWiFi_Ser2Net
Leave encryption OPEN (for now)

Press Submit (green) button at the bottom of the page (you may need to scroll to find it - the below is a composite of screenshots from my phone).



Step 2: write a short Arduino program to send data over the serial link.
Here is a sample program that prints a sequence of numbers – one per second:

int ii;
void setup()
{
  Serial.begin(9600);
  ii=0;
}
void loop()
{
  delay(1000);
  Serial.print("packet # ");
  Serial.print(ii++,DEC);
  Serial.println(" - hello ESP8266 WiFi"); //output the serial data
}

Enter this code into the Arduino development environment as a new sketch, and check that it works. Because I used the Arduino that came with the WiFi shield (they came as a package on Amazon) I used the board "Arduino/Genuino Uno" under "Tools/Board", and the USB port that showed up (since I connected my Mac with USB).


Step 3: upload the program to the Arduino.
This is done in the usual way, but remember – THE DIP SWITCHES MUST BE OFF because otherwise the upload will fail.
You can test that the program works by opening the serial monitor; you should see a series of messages (one per second) like this:



Step 4: make a program that can “receive” data.
The following Python program works on e.g. a Mac. Enter the text into a file called tcptest.py

import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('192.168.4.1', 9000)
print >>sys.stderr, 'connecting to %s port %s'% server_address
sock.connect(server_address)
print >>sys.stderr, 'Connection made!'

try:
 while 1:
  data = sock.recv(16)
  print >>sys.stderr, '%s' % data,

finally:
  print >>sys.stderr, 'closing socket'
  sock.close()


Note that the server address (192.168.4.1) and port (9000) are the default parameters that were set in step 1 (you didn’t actually have to do anything to get these values, they are the default). This is just about the shortest program I can think of to capture TCP/IP packets and display them - if you know Python it should be easy to think of ways to make it do more, but that's not the point of this post...

Step 5: connect your computer’s WiFi to the shield
Find the DoitWiFi_Ser2Net wireless access point, and connect your computer to it. For this to work, you need to have DHCP enabled – this will ensure that your computer gets given a wireless address by the device that it will be able to talk to. Many in-house wireless systems have IP addresses starting with 192.168.1. – you will know you have succeeded when your IP address is something like 192.168.4.2

Step 6: connect the Arduino serial port to the WiFi shield
You now have to set the two DIP switches to “ON” so the Arduino will send serial data to the ESP-13 - at this point you must not try to upload programs to the Arduino; if you are still using the USB cable for power that's OK.

Step 7: start the Python program to capture the output from the shield
At the command line in the Terminal, run the above Python program by typing

python tcptest.py

You will see an output like this (stop it by typing Ctrl-C):


python tcptest.py 
connecting to 192.168.4.1 port 9000
Connection made!
packet # 467 - hello ESP8266 WiFi
packet # 468 - hello ESP8266 WiFi
packet # 469 - hello ESP8266 WiFi
packet # 470 - hello ESP8266 WiFi
packet # 471 - hello ESP8266 WiFi
packet # 472 - hello ESP8266 WiFi
packet # 473 - hello ESP8266 WiFi
packet # 474 - hello ESP8266 WiFi
packet # 475 - hello ESP8266 WiFi
packet # 476 - hello ESP8266 WiFi
packet # 477 - hello ESP8266 WiFi

As you can see, you are getting packets every second with the current value of the counter in the Arduino program - in other words, you have your first WiFi link running!

I hope to figure out how to send data in the other direction... so I can instruct the Arduino to "do things" - and then, figure out how to make the shield "play nice" on my home network so I don't have to fiddle with network settings on my computer to be able to talk to it. All of that will be the subject of a follow-up blog, if I figure it out.

Saturday, August 7, 2010

Burning calories running uphill

The other day I ran up a really big hill - Mount Carmel in Israel, in fact - wearing my Garmin Forerunner 305.
It keeps track of your speed and heart rate and is supposed to tell you how many calories your burned (it knows my weight, age and gender). It's pretty good on the flats, but completely wrong in the climb!

Here are the pertinent numbers (just the first few from a longer climb):

mile...av speed....climb...calories
1........5.7 mph......55 ft....162
2........4.4 mph....325 ft....128
3........3.8 mph....450 ft....115
4........4.3 mph....337 ft....133

The actual effort expended in miles 2 - 4 was very high: with a 10-15% grade you're really working (and it was a hot July day) yet the calories were very low (especially for a 200 pound guy).

I'm a physicist and I decided to do a bit of "back of the envelope math".

You recall from high school that an object with mass m being raised by a distance h increases its potential energy by m x g x h

Using round numbers (g=10 m/s^2), a 100 kg person climbing 100 meters increases his potential energy by 100 kJ.

The body is roughly 25% efficient in converting calories to "work done", which conveniently means that 1 kJ of "net work" costs about 1 kcal of "food work". (1 cal is approx 4.2 J).

This suggests a simple modification to the conventional "calorie calculators" you find online. To get a more accurate estimate of your work done you can add the following term:

Calories due to climbing = (body mass in kg) x (height climbed in meters) / 100

You add this correction to whatever your conventional "running on the flat" calculator comes up with.

After I had come up with this equation I found a blog
http://thismanruns.blogspot.com/2008/01/calories-burned-running-hills.html

which talked about the same problem and which included a reference to a scientific paper from the Journal of Applied Physiology
which describes measurements done on athletes to come up with accurate estimates of the calories burned as they run on a treadmill. They found pretty much the same relationship (although you have to wade through the descriptions and the graphs to get to the essence) - that when you climb, you expend about 40 J / kg / m (see figure 3), with only a weak dependence on the slope (once it's a "real" slope - more than a few % grade).

I look forward to hearing people's thoughts on this - and perhaps one day Garmin will include a better equation in their running GPS.

Saturday, April 4, 2009

Fixing a Wacom Bamboo pen

My daughter's Bamboo graphics pad stopped working. Oddly enough the eraser side of the pen was still OK - but the stylus side was dead. This suggested it was the pen - not the pad or the connection - that was at fault.
I found a brilliant blog at http://abiamy.com/abiyasablogs/2008/04/06/fixing-a-broken-wacom-pen/
which described my symptoms, and had pictures for a similar pen. But the Bamboo pen has a slightly different design so I thought I would document the disassembly (including what went wrong for me...).

The tip of the pen contains a coil with a ferromagnetic core. This core breaks when you drop the pen - this means it doesn't make as strong of a magnetic field and that means the pad can't sense the pen (or vice versa). To get to the core, you have to take the pen apart (carefully - there is a lot of sensitive electronics inside!)

First step: remove the plastic tab next to the buttons:



Note where I put the screwdriver. Gently ease it under the tab and nudge until you get something like this:



This is where I went wrong and forced the tab off. With a snap something broke, and when I looked closely I saw I should probably have slid the tab (moved in in the direction of my fingers in this picture) rather than pull up and away from the pen - see this picture:



Even better - I could have just left it alone. I later found that the top and bottom half of the stylus are only held together at one point - and so just lifting the tab at one end is enough to free the two halves. With the tab off, careful jiggling allowed me to separate the top and bottom of the pen (it takes quite a bit of force: be careful not to bend it as the two parts separate, or you will break the circuit board inside):



As the two parts separated I could see the little blue tab that I had broken off:



Next, I needed to liberate the electronics board which was still being held by the buttons. I was a bit apprehensive now, but it turns out that this time you can just use a small screwdriver and nudge the buttons straight up and off:



After carefully easing the circuit board out of the barrel I could see the broken coil (it didn't look broken, but when I pulled very gently on the end it came apart, showing a crack):



I pulled out the plastic tip (I needed to turn it to free it - not sure if I could have pulled but with the coil looking quite fragile I decided to play it safe - you don't want to break the little wires that hold it in place). Now, using some Loctite superglue gel (the kind that doesn't go everywhere but still sticks like crazy), and with my daughter's help to hold the crack open as I applied the glue, I was able to put a couple of small dabs in the crack and push it tightly shut - in the process pushing most of the glue out of the crack:



Two more cautions before putting it all back together.
First, make sure there is no glue on the inside - or you won't be able to put the plastic tip back in. Holding the assembly vertical, I moved the tip in and out a few times - it did come out sticky so I kept wiping it clean, until it went in and out without getting sticky.
Second - you want to make sure the glue has time to dry properly, and with the smallest possible gap in the ferrite core. To so this, I held the assembly vertically and let it sit for about an hour - long enough for the superglue to really set. A shot glass and some tissue paper did the trick:



When everything was dry, I put it all back together again. But for the little blue tab that had broken off and the white mark on the blue tab where I had bent it when I should have slid it, it was "good as new". And more importantly, it worked again!



A Great Big ThankYou to Abiyasa - the blogger who inspired me to do this. Let me know if it worked for you!