Showing posts with label ESP8266. Show all posts
Showing posts with label ESP8266. Show all posts

Tuesday, November 28, 2017

IOT- Mongoose OS

In preparing for another IoT project I came across Mongoose OS.  Mongoose OS describes itself as “an operating system for connected products” and is a product of Cesanta. It is open source and dual-licensed under the GPLv2 licenses and a Commercial License which removes the GPLv2 and functionality restrictions.

It supports the ESP32, ESP8266, CC3220, and CC3200.  I have been mostly using it with a nodemcu board but also have gotten several simple examples running on an Olimex mod-wifi-esp822-dev board

I have been working with it off and on for a few weeks and so far am impressed. 

I have put a couple of my examples on github in the hope that they will help others.

More to follow…

Sunday, July 12, 2015

TI SimpleLink SensorTag 2015 - Python

Code for these examples is on GitHub in the dhSensorTag2015 repository.

Having successfully accessed data on the sensor tag I decided to try my hand at programmatically accessing data from the sensor tag.  I started out with the same two examples I had developed in bash: 1) to get the device name; and 2) get the humidty reading from the humidity sensor.

Surprisingly there are not too many options when it comes to Bluetooth Low Engergy APOs for Windows 7 or Linux. One of the few that exists is Ian Harvey’s bluepy for python on Linux.

Python isn’t something I use very much.  I do most of my work in Perl and C with a bit of Java thrown in for Android.  Fortunately there are a set of pretty good set of instructions for setting up bluepy on the Raspberry Pi that can be found at: http://www.elinux.org/RPi_Bluetooth_LE.  The output from these instructions is below:

pi@raspberrypi ~ $ git clone https://github.com/IanHarvey/bluepy.git
Cloning into 'bluepy'...
remote: Counting objects: 459, done.
remote: Total 459 (delta 0), reused 0 (delta 0), pack-reused 459
Receiving objects: 100% (459/459), 1.51 MiB | 556 KiB/s, done.
Resolving deltas: 100% (172/172), done.
pi@raspberrypi ~ $ cd bluepy/bluepy
pi@raspberrypi ~/bluepy/bluepy $ make
gcc -L. -O0 -g -DHAVE_CONFIG_H -I../bluez-5.4/attrib -I../bluez-5.4 -I../bluez-5.4/lib -I../bluez-5.4/src -I../bluez-5.4/gdbus -I../bluez-5.4/btio `pkg-config glib-2.0 dbus-1 --cflags` -o bluepy-helper bluepy-helper.c ../bluez-5.4/lib/bluetooth.c ../bluez-5.4/lib/hci.c ../bluez-5.4/lib/sdp.c ../bluez-5.4/lib/uuid.c ../bluez-5.4/attrib/att.c ../bluez-5.4/attrib/gatt.c ../bluez-5.4/attrib/gattrib.c ../bluez-5.4/attrib/utils.c ../bluez-5.4/btio/btio.c ../bluez-5.4/src/log.c `pkg-config glib-2.0 --libs`
pi@raspberrypi ~/bluepy/bluepy $ python btle.py B0:B4:48:B9:2C:82

Running the script generates the a dump of the services and characteristics.

Connecting to: B0:B4:48:B9:2C:82, address type: public
Service <uuid=Generic Access handleStart=1 handleEnd=7> :
    Characteristic <Device Name>, supports READ
    -> 'SensorTag 2.0'
    Characteristic <Appearance>, supports READ
    -> '\x00\x00'
    Characteristic <Peripheral Preferred Connection Parameters>, supports READ
    -> 'P\x00\xa0\x00\x00\x00\xe8\x03'
Service <uuid=f000aa70-0451-4000-b000-000000000000 handleStart=63 handleEnd=70> :
    Characteristic <f000aa71-0451-4000-b000-000000000000>, supports NOTIFY READ
    -> '\x00\x00'
    Characteristic <f000aa72-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x00'
    Characteristic <f000aa73-0451-4000-b000-000000000000>, supports READ WRITE
    -> 'P'
Service <uuid=f000ac00-0451-4000-b000-000000000000 handleStart=81 handleEnd=88> :
    Characteristic <f000ac01-0451-4000-b000-000000000000>, supports NOTIFY READ WRITE
    -> '\x00\x00'
    Characteristic <f000ac02-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x02\x02\x00\x00\x00'
    Characteristic <f000ac03-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x00D'
Service <uuid=Generic Attribute handleStart=8 handleEnd=11> :
    Characteristic <Service Changed>, supports INDICATE
Service <uuid=ffe0 handleStart=71 handleEnd=75> :
    Characteristic <ffe1>, supports NOTIFY
Service <uuid=f000aa64-0451-4000-b000-000000000000 handleStart=76 handleEnd=80> :
    Characteristic <f000aa65-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x7f'
    Characteristic <f000aa66-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x00'
Service <uuid=f000aa00-0451-4000-b000-000000000000 handleStart=31 handleEnd=38> :
    Characteristic <f000aa01-0451-4000-b000-000000000000>, supports NOTIFY READ
    -> '\x00\x00\x00\x00'
    Characteristic <f000aa02-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x00'
    Characteristic <f000aa03-0451-4000-b000-000000000000>, supports READ WRITE
    -> 'd'
Service <uuid=f000ffc0-0451-4000-b000-000000000000 handleStart=97 handleEnd=65535> :
    Characteristic <f000ffc1-0451-4000-b000-000000000000>, supports NOTIFY WRITE NO RESPONSE WRITE
    Characteristic <f000ffc2-0451-4000-b000-000000000000>, supports NOTIFY WRITE NO RESPONSE WRITE
Service <uuid=f000aa20-0451-4000-b000-000000000000 handleStart=39 handleEnd=46> :
    Characteristic <f000aa21-0451-4000-b000-000000000000>, supports NOTIFY READ
    -> '\x00\x00\x00\x00'
    Characteristic <f000aa22-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x00'
    Characteristic <f000aa23-0451-4000-b000-000000000000>, supports READ WRITE
    -> 'd'
Service <uuid=Device Information handleStart=12 handleEnd=30> :
    Characteristic <System ID>, supports READ
    -> '\x82,\xb9\x00\x00H\xb4\xb0'
    Characteristic <Model Number String>, supports READ
    -> 'CC2650 SensorTag\x00'
    Characteristic <Serial Number String>, supports READ
    -> 'N.A.\x00'
    Characteristic <Firmware Revision String>, supports READ
    -> '1.01 (Mar 13 2015)\x00'
    Characteristic <Hardware Revision String>, supports READ
    -> 'PCB 1.2\x00'
    Characteristic <Software Revision String>, supports READ
    -> 'N.A.\x00'
    Characteristic <Manufacturer Name String>, supports READ
    -> 'Texas Instruments\x00'
    Characteristic <IEEE 11073-20601 Regulatory Cert. Data List>, supports READ
    -> '\xfe\x00experimental'
    Characteristic <PnP ID>, supports READ
    -> '\x01\r\x00\x00\x00\x10\x01'
Service <uuid=f000aa80-0451-4000-b000-000000000000 handleStart=55 handleEnd=62> :
    Characteristic <f000aa81-0451-4000-b000-000000000000>, supports NOTIFY READ
    -> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    Characteristic <f000aa82-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x00\x02'
    Characteristic <f000aa83-0451-4000-b000-000000000000>, supports READ WRITE
    -> 'n'
Service <uuid=f000ccc0-0451-4000-b000-000000000000 handleStart=89 handleEnd=96> :
    Characteristic <f000ccc1-0451-4000-b000-000000000000>, supports NOTIFY READ
    -> '6\x00\x00\x00d\x00'
    Characteristic <f000ccc2-0451-4000-b000-000000000000>, supports WRITE
    Characteristic <f000ccc3-0451-4000-b000-000000000000>, supports WRITE
Service <uuid=f000aa40-0451-4000-b000-000000000000 handleStart=47 handleEnd=54> :
    Characteristic <f000aa41-0451-4000-b000-000000000000>, supports NOTIFY READ
    -> '\x00\x00\x00\x00\x00\x00'
    Characteristic <f000aa42-0451-4000-b000-000000000000>, supports READ WRITE
    -> '\x00'
    Characteristic <f000aa44-0451-4000-b000-000000000000>, supports READ WRITE
    -> 'd'

At this point I created a directory SensorTag2015 off my home directory to work in.  I copied the bluepy python files into a subdirectory bluepy.  This gave me a setup that looked like this.

image

In the SensorTag2015 directory I created and ran my python examples.  I started with a very simple script that expects an address for the sensor tag on the command line.

pi@raspberrypi ~/SensorTag2015 $ python getDeviceName.py 00:00:00:00:00:00
SensorTag 2.0
pi@raspberrypi ~/SensorTag2015 $

The script imports two methods from the bluepy.btle fileand then it defines a UUID for the device name.  It checks to make sure that at least one argument is passed and then establishes a connection to the sensor tag.  If the script fails to connect to the sensor tag check to make sure that the sensor tag is advertising.  If it isn't push the left button and check that the green LED is flashing.  Once connected the script checks to make sure that the device name characteristic is readable, reads it and prints it out.

getDeviceName.py

import sys
from bluepy.btle import UUID, Peripheral

temp_uuid = UUID(0x2A00)

if len(sys.argv) != 2:
  print "Fatal, must pass device address:", sys.argv[0], ""
  quit()

p = Peripheral(sys.argv[1])

try:
    ch = p.getCharacteristics(uuid=temp_uuid)[0]
    if (ch.supportsRead()):
            print ch.read()

finally:
    p.disconnect()

Next I developed a slightly more complicated script for reading the Humidity sensor.  The template I developed with this script is what I used for most of the examples.

The script expects the address of the sensor tag to be passed on the command line as with the script above.  Then it defines TI unique UUIDs to configure the sensor and to get data from the sensor.  Next it sets up the values that get written to the sensor's configure UUID to turn the sensor on or off.  Note you must turn a sensor on before you are able to read it and get values other than zero back.

After these variables are setup the script connects to the sensor tag.  Once connected the script writes the value to turn on the sensor to the configuration UUID.  Then it reads the raw data from the sensor tag, converts it to values and calculates the sensor value.  The formulas for calculating the sensor values were pulled from the Android App source code. After the values are printed out the script turns of the sensor and disconnects from the device.

getHumidity.py

#
# TI SimpleLink SensorTag 2015
# Date: 2015 07 06
#
# Sensor: Humidity Temperature
# Values: Temperature and Humidity
#
import struct, sys, traceback
from bluepy.btle import UUID, Peripheral, BTLEException

def TI_UUID(val):
    return UUID("%08X-0451-4000-b000-000000000000" % (0xF0000000+val))

config_uuid = TI_UUID(0xAA22)
data_uuid = TI_UUID(0xAA21)

sensorOn  = struct.pack("B", 0x01)
sensorOff = struct.pack("B", 0x00)

if len(sys.argv) != 2:
  print "Fatal, must pass device address:", sys.argv[0], ""
  quit()

try:
  print "Info, trying to connect to:", sys.argv[1]
  p = Peripheral(sys.argv[1])

except BTLEException:
  print "Fatal, unable to connect!"
  
except:
  print "Fatal, unexpected error!"
  traceback.print_exc()
  raise

else:

  try:
    print "Info, connected and turning sensor on!"
    ch = p.getCharacteristics(uuid=config_uuid)[0]
    ch.write(sensorOn, withResponse=True)
    
    print "Info, reading values!"
    ch = p.getCharacteristics(uuid=data_uuid)[0]
    
    # IR Temperature sensor returns 4 bytes object(LSB),
    # object(MSB), ambient(LSB) and ambient(MSB).
    # Python unpack using "<" which denotes little-endian format
    # and "hh" which denotes 2 unsigned short (2 byte/16 bit) values.
    rawVals=ch.read()
    #(tempVal, humidVal) = struct.unpack('<HH', ch.read())
    tempVal = (ord(rawVals[1])<<8)+ord(rawVals[0])
    humidVal = (ord(rawVals[3])<<8)+ord(rawVals[2])
    
    #object temp and ambient temp are calculated as shown below
    print "Temp: %.2f F" % float((tempVal / 65536.0 * 165 - 40) * 1.8 + 32)
    print "Humidity: %.2f %%RH" % float(humidVal / 65536.0 * 100)
    
    print "Info, turning sensor off!"
    ch = p.getCharacteristics(uuid=config_uuid)[0]
    ch.write(sensorOff, withResponse=True)
    
  except:
    print "Fatal, unexpected error!"
    traceback.print_exc()
    raise

  finally:
    print "Info, disconnecting!"
    p.disconnect()
    
finally:
  quit()

On GitHub in the dhSensorTag2015 repository you will find these examples as well as additional examples to read the other sensors. 

A couple of things to bear in mind with these examples:

  • These examples were written using python 2.7.10 and were developed using V1.12 (Jun 23 2015) of TI’s firmware.
  • The barometer example calculation in the Andriod App doesn’t appear to be correct and therefore the example uses an updated calculation.
  • Data from all of the sensor examples, except the movement sensor, has been validated.

Tuesday, March 31, 2015

It Worked for Me - An Webserver under FreeRTOS on an ESP8266

If you are looking for a webserver to run on an ESP8266 using the FreeRTOS SDK the httpserver_raw server in the contrib-1.4.1.zip file is worth a look.

It compiles and runs using the Unofficial Development Kit for Espressif ESP8266 under Cygwin64 on Windows 7.

The zip file is located at: http://download.savannah.gnu.org/releases/lwip/.

image

Sunday, March 22, 2015

ESP8266 Program Examples

Back in December I had ordered and received an Olimex MOD-WIFI-ESP8266-DEV.  I posted some overview and setup information.

Now I have developed some examples that demonstrate native programming of the ESP8266 in c. The examples were built on Windows using Cygwin64 and the Unofficial Development Kit for Expressif ESP8266.

The examples contain a good bit of documentation in the hopes that they will help other people with their projects.

The basic flow of an ESP8266 program using the Espressif SDK is an event / callback model.  The user defines a set of callbacks in user_init.c.  The callbacks are triggered when events such as a timer also setup in user_init.c expires. 

Another example is a callback that is triggered when a connection is made to the ESP8266 by a client.  This callback can take action based on the connection and may in turn setup additional callbacks.

The example code is on GitHub at digitalhack / dhESP8266.

Hello Digitalhack Example

The first example is a take on the standard helloworld.  I demonstrates use of the user_init.c function for setting up the program, a timer and timer callback function ands printing output to UART0 and UART1.

UART0 is a full UART with both TX and RX that is used to load your code to the ESP8266.  UART1 only has a TX signal and is used for debug messages.

Screen capture from Cygwin of make and flashing the code to the esp8266.

image

UART0 Output from Example

image

UART1 Output from Example

image

Basic Connect Example

This example shows how to setup the ESP8266 to connect to a wireless access point.  The ssid and password for the wireless network are in user_config.h in the examples include file.

UART1 Output from Example

image

Once you see the ip address the EXP8266 has acquired on the debug line (UART1) you should be able to ping the ip address and get a response.

image

Simple Server Example

This example shows how to setup a very simple server.  When a user telnets to the ESP8266 they are prompted for a name.  Once this is entered it is echoed name back to the user before disconnecting.  It demonstrates the callbacks necessary to for TCP communication.

UART1 Output from Example

image

Cygwin screen capture of a session.

image

The example code is on GitHub at digitalhack / dhESP8266.

Saturday, January 3, 2015

Olimex MOD-WIFI-ESP8266-DEV - Software Setup

Getting to the point where I had a workable software setup for the ESP8266 took me a bit.

ESP8266 Software Setup – Linux

I made use of CNLohr’s excellent instructions and youtube video and the Toolchain wiki page on the ESP8266 Community Forum GitHub repository.

The setup that I ended up with is running on Lubuntu 14.04 with the ESP8266 software under /opt/Espressif based on the ESP8266 Community Forum wiki instructions.

ESP8266 Sample Programs

I have successfully gotten the AT v20 Demo to compile and load and the blinky source code example from the ESP8266 Community Forum following the instructions on the wiki.

For the blinky example you need to connect an LED to GPIO2 which is pin 20 on the MOD-WIFI-ESP8266-DEV.  This was the first program I got running correctly.  I got the basic_example to compile and download but wasn’t ever able to get any output from it.

I had some problems getting the AT v20 Demo to compile.  When I was finished with the wiki instructions I had an include directory in /opt/Espressif and also one in /opt/Espressif/ESP8266_SDK.  The ESP8266 Community Forum template includes /opt/Espressif/include as EXTRA_INCDIR.  However to get the AT V20 Demo to compile I had to combine /opt/Espressif/include into /opt/Espressif/ESP8266_SDK/include.

I also had some problems realizing it was compiling and flashing correctly as I didn’t realize that it was looking for the command line to end with CR LF until I read a post in the ESP8266 Community Forums about needing to press CTRL-J.

ESP8266 Software Setup – Windows (Updated Jan 4, 2015)

For windows I found the very well put together ESP8266 DevKit by CHERTS on the ESP8266 Community Forum.  It comes with an installer and for me with two minor changes “worked right out of the box.”  I was already running CYGWIN64 and am not a bit Eclipse user so I just downloaded the x64 DevKit for windows.

I have to update the COM port used for make flash from COM2 to COM3 and the path to the esptool.py script needed the slashed flipped to work in CYVGWIN.  From the examples directory I ran the following:

$ find . -name '*Makefile' -exec sed -i -e 's/COM2/COM3/g' {} \; 
$ find . -name '*Makefile' -exec sed -i -e 's/c:\\Espressif\\utils\\esptool.exe/c:\/Espressif\/utils\/esptool.exe/g' {} \;

One other thing that I did was to put a shell script in /usr/local/bin for screen.  CYGWIN has the screen command but there doesn’t appear to be a way to change RTS and DTR which creates problems for the ESP8266.  The shell script runs Realterm with DTR and RTS set to 0 which sets the control lines to 3.3v which allows normal operation of the ESP8266.

/usr/local/bin/screen

#!/bin/sh
"c:/Program Files (x86)/BEL/Realterm/realterm.exe" BAUD=9600 RTS=0 DTR=0 PORT=3

My thanks to CHERTS for making this so easy.

Olimex MOD-WIFI-ESP8266-DEV - Hardware Setup

Over the past couple of days I have taken a break from my home server upgrade and have been working with the Olimex MOD-WIFI-ESP8266-DEV board.  It took me a bit but I have come up with the setup below which is working very well for me.

Hardware Setup

The MOD-WIFI-ESP8266-DEV is plugged into the left end of a 400 tie point solderless breadboard.  In the middle of the breadboard is a modified Modern Devices USB BUB I.  On the right side of the breadboard is a v3.3 K$931 low voltage regulator that is plugged into a 5v wall wart power supply.

image

The connections are as listed below.

ESP8266-DEV Connection
1 – 3.3v 3.3v
2 – GND GND
3 – GPIO1 / U0TXD RX1 on the USB BUB I
4 – GPIO3 / U0RXD TX1 on the USB BUB I
13 – RSTB Pull to GND to reset.

RTS on the USB BUB I

To one leg of a momentary switch.  The other leg of the momentary switch is connected to GND.  This provides a reset button.
19 – GPIO15 GND / 3.3vJumper (currently unused)
20 – GPIO2 GND / 3.3vJumper (currently unused)
21 – GPIO0 / MTD0 Pull to GND on boot to enter UART flash upload mode.

DTR on the USB BUB I

This setup allows esptool.py to toggle DTR and RTS to reboot the ESP8266 and put it in UART flash upload mode. 

Serial Terminal Emulator Setup Notes

If you have DTR and RTS from your “serial port” connected to the ESP8266 you need to be careful when you are using a terminal emulation program for output from the ESP8266.  You need to make sure that the terminal emulator doesn’t set RTS low and DTR low for normal operation or the ESP8266 will always boot into UART flash mode. 

  • For linux I am using gtkTerm which has the ability to toggle RTS and DTR.
  • For windows I am using Realterm which allows you to set RTS and DTR

One other note about terminal emulators, for the AT demo programs version 19 and 20 the ESP8266 is looking for a CR and then LF (aka CTRL-M and then a CTRL-J) as a the command line terminator.  This means that with most terminal emulators which generally only send a CR (or only an LF) when you press the enter key you will not get an OK back when you type in “AT ENTER”.  This can be overcome by typing “AT CTRL-M CTRL-J”.

Thursday, December 25, 2014

It Worked for Me - Olimex MOD-WIFI-ESP8266-DEV

I ordered two MOD-WIFI-ESP8266-DEV devices from MicroController Pros LLC and as luck would have it they arrived December 24th just in time for Christmas.

Today I soldered on the pins, plugged one into a breadboard and connected them to a FTDI Basic 3.3V from Sparkfun.  I connected the FTDI Basic to the MOD-WIFI-ESP8266-DEV like this:

FTDI ESP8266-DEV
DTR  
RX1 Pin 3
TX1 Pin 4
V3.3 Pin 1
CTS  
GND Pin 2

Then I connected the FTDI Basic to my computer and set the COM port to 115200 and typed in AT and go an OK back.  Next up was to connect to my wifi network and this took me a bit of time.  I found out that you needed to execute the following commands:

AT+CWMODE=3 – this sets the mode so it will connect to access points.

AT+RST – this will reset the device and in my case the device had to be reset after it was changed to CWMODE=3.  Until I reset it all I got was ERROR responses back.

AT+CWLAP – this will get you a list of the WAPs it sees.

AT+CWJAP=”<ssid>”,”<password>” – this will connect you to your wifi network.

AT+CWJAP? – this will tell you if you are connected.

AT+CIFSR – this will give you the IP address of the card.  You should be able to ping this.

More to follow…