February 12, 2017

Find the MQTT broker without an IP adress

If you, like me, like to built IoT devices based on ESP8266, you may use MQTT to send and receive messsages. One problem is to equip each device with the IP number of the MQTT broker. One solution is to add a simple management web server to the device and make it open a Wifi access point serving an admin page, when it is started the first time.

A better option is to use a service discovery protocol. The most popular protocol is known under the name mDNS, Avahi, Bonjour or Zeroconf. This protocol let you advertise the MQTT service on the local network and have your IoT devices find it without using an IP name or number.

Say that you run the MQTT broker Mosquitto at port 1883 on a Raspberry Pi.

Use nano to create a service description file:

sudo nano /etc/avahi/services/mosquitto.service

and paste the following text:

<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
 <name replace-wildcards="yes">Mosquitto MQTT server on %h</name>
  <service>
   <type>_mqtt._tcp</type>
   <port>1883</port>
   <txt-record>info=Publish, Publish! Read all about it! mqtt.org</txt-record>
  </service>

</service-group>

Save and exit with Ctrl-O <ENTER> Ctrl-X.

Now the Avahi daemon immediately advertises the mqtt service on your local network.
To see that it works, just open any service browser - I used Bonjour Browser from http://www.tildesoft.com/ on my Mac.

On the ESP8266 side, the following code example shows how to find the MQTT server ip and port.


#include <ESP8266mDNS.h>

...

if (!MDNS.begin("ESP")) { 
  Serial.println("Error setting up mDNS");
} else {
  Serial.println("mDNS setup finished");

  Serial.println("Sending mDNS Query");
  int n = MDNS.queryService("mqtt", "tcp");

  if (n == 0) {
    Serial.println("No service found");
  } else {
    // at least one MQTT service is found
    // ip no and port of the first one is MDNS.IP(0) and MDNS.port(0)
}

January 20, 2017

How to use the DOIT ESP32 DEVKIT

ESP32

I have successfully used the WeMos D1 mini ESP8266 dev board for a while. This is a high quality, well documented board available for $2.60 here.

Now that the ESP32 is released, a number of dev boards have arrived. They are a lot more expensive than the ESP8266 boards. Recently I found the DOIT ESP32 DEVKIT for a reasonable price, on Ebay. I payed GBP19 for ESP-32 ESP-32S Development Board 2.4GHz Dual-Mode WiFi+Bluetooth Antenna Module.


This board is not as well documented and there is not much information at all from the seller.
But I got it working after a lot of work and here's how. I used a Mac, but it should work with linux and Windows too.

Steps

  1. Download and install Arduino IDE
  2. Open web page  https://github.com/espressif/arduino-esp32 and follow the instructions under Installation Instructions/Using through Arduino IDE, for your operating system
  3. Install driver for the serial chip used to connect the board to your PC by USB, from the page http://www.silabs.com/products/mcu/pages/usbtouartbridgevcpdrivers.aspx
  4. Connect the board to your PC with a USB to micro USB cable
  5. In Arduino IDE, use File/New and paste the test program below
  6. Select Tools/Board ESP32 Dev Module
  7. Select Tools/Port /dev/cu.SLAB_USBtoUART (on Windows and Linux the name is different)
  8. Press the small BOOT button and click the Upload button in Arduino IDE, and wait until alot has happened in the log in the bottom of the window - it takes about 15 seconds (I kept the BOOT button pressed during the whole upload, but that may not be neccessary) 
  9. Open the Tools/Serial Monitor and select 115200 baud
  10. Now you should see the word Hello printed every second (you may push the EN button to reset the board, in order to see this)

Test program

void setup() {
  Serial.begin(115200);
  delay(200);
}
void loop() {
  Serial.write("Hello!\n");
  delay(1000);
}

Not achieved yet

  • I haven't found how to control the on-board LED yet
  • I haven't found any schematics for the board

PlatformIO

I prefer to use PlatformIO when developing for Arduino, EPS etc. But according to https://github.com/platformio/platform-espressif32/issues/5, the Arduino IDE has to be used the first time, to get the DOIT ESP32 properly initialized.

In PlatformIO, I select the board Espressif/Espressif ESP32 Dev Module.

August 12, 2011

Install rbclipper in Sketchup 8

Sketchup doesn't support ruby gems. In some cases it is possible to move the content of a gem into a Sketchup installation to use it. I installed the excellent Clipper library by Angus Johnson. Mike Owens have made a ruby binding rbclipper, that I installed like this:
  • install ruby and devkit as in Compile C++ plug-in for Sketchup.
  • open a shell window with DevKit/msys.bat
  • run the command
    • gem install clipper
  • copy the file clipper.so to the sketchup plugins directory - in my case i copied C:\Ruby187\lib\ruby\gems\1.8\gems\clipper-2.9.0\lib\clipper.so to C:\Program Files\Google\Google SketchUp 8\Plugins
  • test by running the following ruby commands in the sketchup ruby console

    require 'clipper'
    a = [[0, 0], [0, 100], [100, 100], [100, 0]]
    b = [[-5, 50], [200, 50], [100, 5]]
    c = Clipper::Clipper.new
    c.add_subject_polygon(a)
    c.add_clip_polygon(b)
    c.union(:non_zero, :non_zero).inspect

  • the result should be 
    • [[[100.0, 0.0], [100.0, 5.0], [200.0, 50.0], [100.0, 50.0], [100.0, 100.0], [0.0, 100.0], [0.0, 50.0], [-4.999999, 50.0], [0.0, 47.8571432653061], [0.0, 0.0]]]

Compile C++ plug-in for Sketchup

Sketchup crashes when loading a C++ ruby extension. As Dan Rathbun says in for example http://forums.sketchucation.com/viewtopic.php?f=180&t=34735#p306377, you have to compile the extension using ruby 1.8. This solved my problem - I had used ruby 1.9 when I experienced my crashes.

I failed to find a complete how-to for successfully compiling an extension for Sketchup, so here is how I did:
  • now you should have mytest.so in the current directory
  • copy mytest.so to the sketchup Plugins folder - in my case C:\Program Files\Google\Google SketchUp 8\Plugins
  • start sketchup
  • use menu Window/Ruby Console
  • in the ruby console edit field do as the rubyinside.com/how-to... page says:
    • require 'mytest'
    • include MyTest
    • puts test1
  • the last command should print 10