Sunday, February 5, 2017

NodeMCU and OSX

During my adventure with NodeMCU, ESP9266 and DHT22 I had to switch from Windows machine to Macbook. Looks like having it running from OSX is as easy as at windows.

Step 1: Download and install CP210x USB to UART Bridge VCP Drivers
Step 2: Connect your NodeMCU and verify if you have USB serial port available


Step 3: Connect to this virtual COM using your favorite terminal emu. Remember to adjust settings (Default: 9600/8/N/1). Screen is the one you probably already have. If you didn't change default configuration then you are ready to go. Just type `screen /dev/tty.SLAB_USBtoUART`


Works like a charm.

Flashing firmware is also easy. You have to have python and pip installed. Python is already there. All you have to do is `sudo easy_install pip`. 
After pip is installed you can use it to install esp tool by using command `pip install esptool`:


Now we can try to install some software. Little bit more complicated, but can survive. In order to enable flashing of device, GPIO0 must be set to low. It can be done by pressing "FLASH" button during powering on (just keep it pressed as you plug it into USB port). Also remember to disconnect your terminal from serial port. I was able to reflash my NodeMCU using this method easily.


After that just replug your device, open back terminal emulator and reconnect to serial interface. You are back in the business. What is also quite interesting, after flashing on Windows I had to upload init.lua again, this time it was not removed.

So yea, one last thing to learn would be how to up(down)load files. Need another cool tool to be installed - nodemcu-uploader. Procedure exactly the same as before. Note, disconnect your terminal and according to instructions, as we use old firmware, change baudrate to 9600.


Now we can fully work with NodeMCU from OSX directly.
You can continue with this guide. Only difference is - you will have to enter all commands manually.




Getting more of LUA and NodeMCU

In my first and second article about making home smart, we managed to build the sensor, connect it, install firmware and do some basic things like lighting LED or reading temperature and humidity data from sensor. Now it's time to do some serious coding. This is the place where I spent most of my time with NodeMCU so far. Not because it's so complex - LUA is quite friendly and simple. If you did some coding with Perl or Python, this would be very easy for you.


Let's start properly - there's LUA book available on-line, called Programming in Lua. First edition is available on-line and even though you are probably already familiar with scripting, it's good to review it on a fast forward just to know what's going on.

Second very importand resource is this page. It tells you everything that you have to know in order to be succesfull with various devices you might want to connect to your unit.

Okay, let's get started. We already have some experience and we know how to light up the diode and how to read the sensor data. Now it's time to write down code, which will run during module bootup, start WiFi, connect to the network and read our sensor periodically. Let's keep things simple for the time being and just display the data. Sending it somewhere will be our next step.

As you might have noticed, after MCU starts, it displays message "cannot open init.lua".


This is the file that firmware use to start whatever script we want to execute. 
Let's try with some basic code (most of it we already got from Lua Loader by just clicking! :))

print("Connecting to wifi SSID wifi-name")
wifi.setmode(wifi.STATION)
wifi.sta.config("wifi-name","wifi-password")

status=wifi.sta.status()
if status == 5 then
print(string.format("Wifi Connected succesfully (%d)",status))
sid,pwd,bssid = wifi.sta.getconfig()
print("Wifi SSID: ", ssid)
print("Wifi IP: ",wifi.sta.getip())
print("Assuming normal operations.")
else
print(string.format("Wifi Not Connected (%d).",status))
end

pin=2
gpio.mode(4,gpio.OUTPUT)
gpio.write(4,0)
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
gpio.write(4,1)

if status == dht.OK then
print(string.format("DHT Temperature: %.1f;Humidity: %.1f%%\r\n",temp,humi ))
else
print("Fatal: Invalid Read from DHT")
end

Okay, cool. We have something that works. By the way, I light up blue LED while data is read from the sensor. This act as a gadget as well as visual keep-alive. In the future when it will be installed it will be very easy to verify if it works by looking at it.

Normally our approach would be to have it in the loop with some delay in order to have it done periodically. At least sensor part. What if connection to wifi takes longer than execution of code? We also have to include some delay here and retry in case of failure.

With my little experience with this platform, seems that introducing delay may be quite difficult and this is supposed to be event driven. So I found two useful ways to do this

Just like with LuaLoader, I went for timers just because I found them first.

So here's the idea:
- turn WiFi verification and sensor read into functions
- start WiFi Association
- start reoccuring timer for 1 second, every time it fires verification of WiFi happens
- if negative, do nothing, just keep it running every second
- if positive, remove timer and start another timer which will trigger sensor function periodically.

-- 1. Initiate connection to WiFi:
print("Connecting to wifi SSID wifi-name")
wifi.setmode(wifi.STATION)
wifi.sta.config("wifi-name","wifi-password")

-- 2. Turn wifi verification to a function
function checkwifi()
  status=wifi.sta.status()
  if status == 5 then
    print(string.format("Wifi Connected succesfully (%d)",status))
    ssid,pwd,bssid = wifi.sta.getconfig()
    print("Wifi SSID: ", ssid)
    print("Wifi IP: ",wifi.sta.getip())
    print("Assuming normal operations.")
-- 6. If we detect that Wifi Connection is successful
-- terminlate timer ID 0 and start new timer ID 1
-- which will run every 15 seconds and trigger getdht()
    tmr.unregister(0)
    tmr.alarm(1,15000,1,function() getdht() end)
  else
    print(string.format("Wifi Not Connected (%d). Retrying in 1 second...",status))
  end
end

-- 3. Turn sensor read into function
function getdht()
  pin=2
  gpio.mode(4,gpio.OUTPUT)
  gpio.write(4,0)
  status, temp, humi, temp_dec, humi_dec = dht.read(pin)
  gpio.write(4,1)

  if status == dht.OK then
    print(string.format("DHT Temperature: %.1f;Humidity: %.1f%%\r\n",temp,humi ))
  else
    print("Fatal: Invalid Read from DHT")
  end
end

-- 4. After everything is set up print a message:
print("Waiting 1 second for WiFi init...")
-- 5. Start 1-second (1000) reoccuring (tmr.ALARM_AUTO) timer
-- with ID 0 (0).
-- Once triggered it will run checkwifi() and start over.
tmr.alarm(0,1000,tmr.ALARM_AUTO,function() checkwifi() end)

Now you can copy-paste this to your notepad, remove comments, adjust to whatever wifi name or timer values you need and copy it to your clipboard. Go to your Lua Loader and click "Paste Text" button. It will paste your clipboard directly to the console. You probably want to hit "Restart" button before you do that and remove whitespaces from the beginning of the lines as Lua Loader doesn't like them.


Let's make sure this code will be executed automatically every time our device boots. Save it as init.lua and copy to the device using Lua Loader.

Okay, script loaded, let's reset once again and see results.
In the next part we will see how to send this data to another device through WiFi and use them.




First steps with ESP8266, DHT22 and LUA

If you get here, hopefully you already have your  board connected. If not, please refer to my previous article as it explains this proces step by step.
First interesting feature of Lua Loader is ability to generate some Lua code for us by just hitting buttons. It is also useful to learn as code generated based on our input is displayed on the main window.
Enter your SSID and password and click "Set AP". In main window you should see commands required to establish WiFi connectivity:

You can try to hit "Get IP" "Survey" and this green icon in the middle - it provides WiFi status. You can also terminate this connection if you wish by hitting Disconnect buton.

Great stuff, lets try something else. There is LED diode localized near to WiFi Antenna. It is connected to GPIO pin 04 and we can light it. On the right hand side you can see blue "GPIO" panel.
Set it to the following values and click "Set" then click "0".
Led should be now shining blue. Additionally, two lines of Lua code required to achieve this result are generated and displayed in the main window:
If you want to turn it off, click on "1".

Okay, great, so far so good. We finally get to the point where have board running, tested with some basic features. It's time to understand pinouts and connect DHT sensor. (Please google for nodemcu pinout)

And let's take a closer look on our sensor. It comes with two version - sensor only which has four pins and sensor mounted on PCB which I have. For the first one you gonna need additional resistor in order to connect things properly. With my version it's all about connecting right pins together. They are described as "GND", "ACC" and "DAT". GND is quite descriptive,  must be connected to "GND" on ESP board. ACC is +3,3V and we connect it to "3V3" pin. last one is Data and this should be connected to any of GPIO pinouts.

On ESP itself you will see multiple pins with GND or 3V3 descriptions. It doesn't matter which one you use, they are multiplied for covenience. This is how I connected things:


I used pin described as D2 (PIN2) which is in fact GPIO04 pin.

Let's make this thing finally useful and see current temperature and humidity then.

Great! We are now able to read temperature and humidity values from the sensor. In the next section we will try to make it automatic and somehow transmitted to HomeKit software.







Smart Home: HomeKit, MQTT, DHT22, ESP8266, LUA and NodeMCU.

Inspired by my friend and humidity issues in my bathroom I decided to have some fun and start making my home more inteligent.

First attempt - make humidity and temperature sensor which will present data on my iPhone "Home" App. While it looks not complicated when we split it into pieces we need the following components:


- Temperature/Humidity Sensor //DHT22
- Wireless module, capable of reading sensor data and sending it somewhere // ESP8266/NodeMCU
- Some software smart enough to receive and understand data from sensor, identify from which potential sensor this data comes and forward it into HomeKit // MQTT
- Homekit software to talk with phone
- Phone who will read and present data from HomeKit software

I ordered by ESP and DHT on most popular chinesee portal and I aimed for cheapest, so both cost me together a couple of bucks. DHT was delivered quite on time and in good shape, however ESP was more then 3 months in transit and looks like crap. Surprisingly, it works just fine :-)


In order to get started with ESP we need couple of things:
- USB cable
- ESP Flasher
- ESP firmware
- IDE

Start here if you use OSX or Linux machine.

Once you connect your ESP to windows machine with USB you should already see first signs of life from your module, blue led near WiFi antenna should blink randomly. Go to Settings -> Devices -> Connected Devices and look for something like this:

If you see it - great, your USB to Serial driver works just fine and your virtual COM port is whatever the numer is (my case - COM3)

Next step - flash your ESP with new software. Optional, but you will definitely have to flash your ESP multiple times and it's a piece of cake so let's make sure we have newest firmware on it. (And, by the way, if you are already here, it's just about time to at least  browse this resource - you will need it all the time while playing around with your ESP).

You can try to build  your own firmware here but I didn't bother. For the 1st time I just used last pre-compiled image available here. There are two flavors of image, float and integer. I took float, why not?

Yea, it was build almost year ago, but again, for the starter, we don't need fireworks.
Flasher sits here along with short instruction how to use it. Just download 32bit or 64bit exec file and run. Go to config tab and click on gearwheel icon, select your firmware, leave offset at 0x0. Then get back to operation, make sure your COM port is set properly and click FLASH button. Progress bar should start becoming green and your firmware should be present on ESP in a couple of minutes.
Now it's time to see what this little baby can do for us. By the way, if flashing does not work for some reason, just disconnect everything you might have already connected to the unit.

There is a couple of different IDEs to use with this board, I picked up LuaLoader for no good reason While bit annoying if you want to talk to your device interactively, it works fine.

All you need to do is to download executable and run it. Go to Settings -> Comm Port Settings.
Then configure your serial port as presented.
Then, hit Connect (Right to Settings). Then red "Restart" buton on right hand side. You should see something like this in main window.

If you see this - Excellent. Now it's time to do something useful with your dev board.