Tuesday, April 28, 2015

Esp8266 - Easy Programming - NOT! (or, at least not yet)

UPDATED: see below

I have been playing with my Esp8266 (see previous post). I received these a few weeks ago, but before use, I had to wait to receive a 3.3 Volt USB to Serial Programmer.

Esp8266 Wired and Ready
To work easily with a Protoboard, I removed the original angled header from the Programmer, and replaced it with a "straight down" header. Unsoldering the header was a pain, because it was assembled with Lead-Free Solder, which does NOT melt very easy - I hate that stuff ! - (A note to the Manufacture, the EPA, and OSHA, I was not planning on eating or licking the board, normal 63/37 solder would have be fine  :-)


WIFI Analyzer
Once Wired, the Fun Starts.

Out of the box (the bag actually) this little board seem easy and is interesting to set up. With the help of online resources I was able to send and receive messages from the serial interface.

The supplied "AT+" commands allow you to change the WIFI mode and connect to a local WIFI Access Point.

I use a Cellphone App; "Wifi Analyzer" to see the Esp8266's RF output, along with other local WIFI AP's. 

And, another App; "Net Scan", was used to check for the IP Address as seen from my network.

The ElectroDragon.com web page provides some good instructions and examples, but the way the pages is written as an Image, you can NOT Cut-n-Paste from it, now how dumb is that!

The following is an example of a normal initial dialogue with a Esp8266, recorded here for my future reference (Cut-n-Paste works here). Note: to make the listing shorter, some blank lines have be removed.




AT
OK

AT+RST
OK
c_�� #��FjS�fJ;��
[Vendor:www.ai-thinker.com Version:0.9.2.4]
ready

AT+CWMODE=3
OK

AT+CWLAP
+CWLAP:(4,"EBCON",-41,"00:16:01:84:c4:a8",4)
OK

AT+CWJAP="EBCON","mypasswd"
OK

AT+CWJAP?
+CWJAP:"EBCON"
OK

AT+CIPMUX=1
OK

AT+CIFSR
192.168.4.1
192.168.2.164
OK




But, NOT all is well in Paradise.

I have managed to "Brick" the first two of my Esp8266's (only 3 more left).

After the initial set up and tests, my desire was to install an application onto the Esp8266, I chose a simple example "Hello World - Web Server" from the specially configured Arduino IDE 1.6.1.

The download (actually Uploading) went as expected, with a status of "Done Uploading". But, that is when the troubles began. I can no longer see the RF output on the WIFI Analyzer, the IP Address does not show up on Net Scan, and the RESET status text sent to the Terminal Window is just Binary Trash, regardless of the Baud Rate or Terminal Settings that I choose.
Binary Trash

Update: With help from Jeff - KO7M, and his Digital Analyser we have determined the "Trash" is a "Reset Message" sent at 74115 Baud (or 74880 baud nominal), most of my usual programming tools do not have that baud rate as an option. But even when forced, no joy:




$ ./esptool.py --baud 74880 -p /dev/ttyUSB* write_flash 0x000000 foo.bin
Connecting...
Erasing flash...
Writing at 0x00000000... (0 %)
Traceback (most recent call last):
  File "./esptool.py", line 538, in <module>    
  esp.flash_block(block, seq)
  File "./esptool.py", line 197, in flash_block
    struct.pack('<IIII', len(data), seq, 0, 0)+data, ESPROM.checksum(data))[1] != "\0\0":
  File "./esptool.py", line 106, in command
    raise Exception('Invalid head of packet')
Exception: Invalid head of packet



We were able to extract binary from a known "good Esp8266", but was unable to flash a "bad Esp8266" with that binary (see above).

Currently I am, "still" Lost.

There is a lot of conflicting information regarding; "How to UnBrick a Esp8266", the suggested programs do not work well with Linux, and the Firmware files were very hard to find. The most helpful online resource seems to be from this blog.

But, so far, I have NOT been able to "UnBrick" my two Esp8266's, they are cheap and I could just toss them, but I want to figure this out.

Here is what I see on a Workstation terminal, note: I am in group "dialout" and have normal write access to /dev/ttyUSB0. I thought that maybe the Serial Data Rate was wrong (see above), but then I can repeat the Arduino re-program just fine, with the proper finished "Done Uploading" report.


$ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Apr 28 17:03 /dev/ttyUSB0

$ ./esptool.py -p /dev/ttyUSB0 write_flash 0x000000 AI-v0.9.5.0_AT_Firmware.bin
Connecting...
Traceback (most recent call last):
  File "./esptool.py", line 473, in <module>
    esp.connect()
  File "./esptool.py", line 151, in connect
    raise Exception('Failed to connect')
Exception: Failed to connect




I am hopeful, . . . and when I do find the answer, I will update this post with additional information.


UPDATE: It works, see post.


-- Home Page: https://WA0UWH.blogspot.com

Sunday, April 12, 2015

More Playing with MQTT

With updated software, mosquitto version 1.4.1, it is very easy to connect two Brokers (see problem as suggested on previous post). A few new line in the configuration files is all that is needed. To publish (upload-only) my Broker's Topics to the public Broker at iot.eclipse.org, I used the following.

Content of: /etc/mosquitto/conf.d/bridge.conf


connection ebcon.com
address iot.eclipse.org
topic # out 0 "" ebcon.com/



Out-bound topics have "ebcon.com/" pre-pended, currently I do not allow or receive data from iot.eclipse.org directly by my Broker. If I want a topic, I subscribe directly with an application (mosquitto_sub).

My own local Brokers (in three different physical locations) exchange data from the two lesser Brokers in bidirectional fashion. Brokers do not have to be hierarchical, they can be peers. In my case I chose to configure the "bridge" on what I consider the lesser Brokers.



connection remote1
address <remote broker IPA>
topic # both 0



As a test, I created a "JoyStick" control publisher, when the joysticks are moved, all Brokers receive the data, much faster than I expected. Performance is very good. Eventually, I will control remote servos to direct (point) Raspberry PI Cameras.

You can subscribe to my testing with:



$ mosquitto_sub -v -h iot.eclipse.org -t 'ebcon.com/#'




Note: Mosquitto MQTT Brokers can publish any kind of data, including Web Pages and Photos.



I am just learning and having FUN with MQTT !


-- Home Page: https://WA0UWH.blogspot.com

Esp8266 With 0.1 Inch Header

In preparation for programming my Esp8266 and the Internet of Things (IoT), I have installed a 0.1 inch header. The connector pad on the Esp8266 are 2mm and therefore a lot of lead bending was necessary.

I think this was my first soldering project that required full 15X Microscope magnification. The Esp8266-Esp11 board is much smaller than implied from the ebay photos (a previous post). The on-line diagrams and documentation are correct, but in your hand it is very small.

Esp8266-Esp11 with
0.1 Inch Header Installed
I have on order the programming interface, which should be received soon. I will use the Esp8266 with the installed header for use on Protoboards, actual projects will use direct solder to matting "Castellated Pads" on custom PCB's.
Esp8266-Esp11 Pin Layout

For my later reference, I have included the pin diagram.


-- Home Page: https://WA0UWH.blogspot.com

Tuesday, April 7, 2015

MQTT - Internet of Things (IoT) TimeService

I am still playing with the Internet of Things (IoT) and MQTT.

I have implemented a simple TimeService, so each of my (future) devices can subscribe to-and-know the correct current time. This script is more complex than needed, but shown here as an example of what can easily be done with a simple script.




#! /bin/bash
# A mosquitto Time Server
# Author: Eldon Brown - eldonb@ebcon.com

    # Usage: progname [-v] [-p TopicPrefix] [-h BrokerHostAddress] [other mosquitto options]

    # Check for Verbose Debug Option
    case "$1" in
        (-v):; set -xv; shift 1 ;;
    esac

    # Set Topic and Service and then add "Prefix" to Topic if desired
    TOPIC="Time/`date +%Z`"
    SERVICE="client/${TOPIC}"
    case "$1" in
        (-p):; TOPIC="$2/${TOPIC}"; SERVICE="$2/${SERVICE}"; shift 2 ;;
    esac

    HOST='' # Assume Local Host
    case "$1" in
        (-h):; HOST="$1 $2"; shift 2 ;;
    esac


    mosquitto_pub -r ${HOST} -q 1 -t "${SERVICE}" -m 1 $*
    trap "mosquitto_pub -r ${HOST} -q 1 -t \"${SERVICE}\" -m 0 $*; exit 0" SIGHUP SIGINT SIGTERM

    while true
    do

        sleep `date '+(60-%S) %% 10' | bc` # Provide on 10 sec intervals

        date '+%s %Y/%m/%d %H:%M:%S'

        sleep 2          # To avoid multiple reports for the same second

    done |

    mosquitto_pub ${HOST} -t "${TOPIC}" -l $*

# End



For a while I will "publish" this time, as a service on a the public MQTT Broker, at: iot.eclipse.org

You can connect and see the results by executing:



$ mosquitto_sub -h iot.eclipse.org -v -t 'ebcon.com/client/#' -t 'ebcon.com/Time/#' 



The -t 'client/#' topic is retained (-r) by the Broker and will report that the TimeService is active (1), or not (0).

Just for fun, You can subscribe to everything on the iot.eclipse.org MQTT Broker, but be prepared to be Over Whelmed with data!



$ mosquitto_sub -h iot.eclipse.org -v -t '#' 




So, you ask: Just how does MQTT fit into my Ham Radio plans?

I plan to implement "remote control" of radios (among many other things) using MQTT.

More information to follow.


-- Home Page: https://WA0UWH.blogspot.com

Saturday, April 4, 2015

MQTT - Bridge Work-a-Round

 UPDATE:
I was using a very OLD version of "mosquitto" which did not support Bridges well, I am hoping the current version will work better.

-

To experiment with the Internet-of-Things (IoT), while waiting to receive my Esp8266's (see previous post), I have enlisted my Raspberry PI's into playing the part of a remote data collection device. My Desktop Workstation is the Control Point and the Master MQTT Message Broker.

Originally I had planned to use a Master/Slave Bridged Broker configuration, where the Workstation would be the Master, and the Raspberry PI's would be a Bridged Slave Brokers, and they would collect data and messages from the Esp8266's.

But, I have not been able to get the "mosquitto" Bridged Broker configuration to work as documented :-(

I have a work-a-round; I will temporary use an "ssh tunnel" to directly collect messages from the Raspberry PI's.

At each Raspberry PI's, an "ssh tunnel" is created for "port 1883" (the MQTT port), and therefore the Master Broker is directly available to the Raspberry PI's. A side benefit of is; the "tunnel" is compressed and encrypted.

On Raspberry PI:


$ ssh -CnNTf -L 1883:localhost:1883 <master workstation IPA>




With this setup, I can conduct; tests for camera servo control, send SMS messages to the Cell Phone, and other experiments (see previous posts).



-- Home Page: https://WA0UWH.blogspot.com

Friday, April 3, 2015

The Internet-of-Things and MQTT

UPDATED: 2015/04/07
To include mosquitto2sms status via mqtt

The Internet Hobby Electronics world is ABLAZE with Esp8266 excitement.  These Ebay $3.00 modules are being used to create some very interesting Internet-of-Things (IoT) projects. Search Google and Youtube for Esp8266.

To make interesting projects, code can be downloaded into the processor on the Esp8266, or it can be connected to a processor like the Arduino. In either case, access to the Internet for a Hobby Project is very exciting.

One way to provide status and/or control to/from the Esp8266 is via a (little known) protocol called "MQTT", which provides simple and small processor code footprint. Actually, it is not unknown, Facebook uses it !

For Raspberry PI and Linux people, there are downloadable commands that implement MQTT, the package that contain these are "mosquitto" and "mosquitto-clients"




$ sudo apt-get install mosquitto mosquitto-clients




With "mosquitto" and the "curl" command (see previous post), I implemented a mosquitto2sms gateway, as:

mosquitto2sms


#! /bin/bash
# Author: Eldon R Brown - WA0UWH

 # Usage: mosquitto2sms [-h BrokerHostAddress] []


 # set -xv   # For Debug

 mosquitto_sub -v -t 'ToSMS/#' $* |

 sed -u -n 's/^ToSMS\/\([0-9][0-9]\+\) /\1 /p' |         # Extract just the phone Number and Message

 while read phone mesg
 do

  # echo -e "\n\n${phone} ${mesg}"  # For Debug
  curl http://textbelt.com/text -d "number=${phone}" -d "message=${mesg}" 2>&1 |
  grep -q true &&
  mosquitto_pub -t "ToSMS/${phone}/Stat" -m 1 $* ||
  mosquitto_pub -t "ToSMS/${phone}/Stat" -m 0 $*

 done

# End




On another system, or any MQTT device, a simple message can be easily sent to a Cell Phone. Example as shown is from Raspberry PI.




mosquitto_pub -q 1 -t ToSMS/2025551212 -m 'Test Message'




To watch (or monitor) all of the activity on the messaging system, execute the following (i.e., subscribe to all):




mosquitto_sub -v -t '+/#'




 I am anxiously awaiting the arrival of my ebay Esp8266's, . . . only a few more days !


-- Home Page: https://WA0UWH.blogspot.com

Wednesday, April 1, 2015

Sending SMS from the Raspberry PI

Yesterday my friend Larry - KB7KMO was in my Shop, we both have need for Status and/or Alarm Messaging from the Raspberry PI, a casual google searches did not provide much easy help. There is/was much information on how to do it with very long and complex configurations, but we wanted something simple.

After Larry left, and after several hours (actually more than a few) of research, I have found a free service that can be used to send a "SMS" messages from the Raspberry PI to a Cell Phone, using the "curl" command.

Obviously, An Internet connection is needed for the PI, and the "curl" command must be installed (if not already).



$ sudo apt-get install curl



The free SMS service is provided by "textbelt.com" (thanks, textbelt !).

There are limitations to the "textbelt.com" service; no more than 75 messages sent per day from a single IP Address, and no more than 3 messages per 3 minutes can be sent to a single Cell Phone number. For most typical use, this should not be a problem.

Note: the SMS messages received by the Cell Phone will be delivered from "txt@textbelt.com".

Usage: on the command line or shell script:



$ curl http://textbelt.com/text -d "number=AAAPPPNNNN" -d "message=This is a test message"



Where "AAAPPPNNNN" is the: Cell Phone Number, AreaCode, Prefix, and Number. Also, dashes ("-") can be used within the phone number, as: "number=202-555-1212"

Note: the words "number=" and "message=" must be used as shown.

For Canada and International use see: Web page.


- Have Fun with PI Messaging !


-- Home Page: https://WA0UWH.blogspot.com