Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Sunday, 8 August 2021

Connecting a LoLin NodeMcu v3 IoT to the Azure IoT Hub using the Arduino IDE

An overview of how to connect a LoLin NodeMCU v3 IoT device to the Azure IoT Hub using the Arduino IDE.

The Azure IoT Hub provides a cloud-hosted solution back-end to connect backend applications with IoT devices.

The LoLin NodeMCU v3 device is a wireless IoT device based on the ESP8266 microchip. For more info see First Impressions of the LoLin MCUv3 

The high-level steps to connect the LoLin NodeMCU IoT to the Azure IoT cloud are

  1. Create an Azure IoT Hub service (Azure offers a basic free version)
  2. Manually register a device with your Azure IoT Hub service (easily done in the Azure portal)
  3. Install the Arduino IDE and install the Azure IoT libraries via the Arduino IDE library manager
  4. Use the Azure IoT device SDK for C to write a program for the device to connect to your Azure IoT Hub service or use the Microsoft provided sample program.

For steps 1 to 3 refer to the following guide for step by step instructions to setup an Azure IoT Hub, register a LoLin device, setup the Arduino libraries and run your first IoT program

 

Microsoft Azure IoT C SDK

Microsoft publish a C SDK that supports a wide range of IoT devices including the LoLin NodeMCU. Developers can use this C SDK to develop programs to connect devices to the Azure IoT Hub.

The LoLin NodeMCU is based on the ESP8266 Microcontroller (MCU) and is what Microsoft call a constrained device in that it has comparatively limited resources, such as memory, compared to devices based on a CPU.

The Azure IoT C SDK includes a version that is optimised to run on constrained devices. For example, it is single threaded whereas some of the functions in the unconstrained version will spin up seperate threads to complete jobs in the background.

These C libraries need to be installed in the Arduino IDE environment using its Library Manager as explained in the guide above. Once installed we can include these libraries in our programs with

#include <AzureIoTHub.h>

Azure IoT SDK for C developer documentation 

Microsoft publish a detailed developer how-to guide for the Azure IoT device SDK which explains how to use the API with examples. Find it here Dev guide for the Azure IoT device SDK for C     

The Azure IoT SDK reference provides detailed information on the C library functions and data structures and can be found on GitHub here Github for Azure IoT C SDKs and Libraries

Sample program

Microsoft provide a sample program 'iothub_ll_telemetry_sample' to demonstrate use of the Azure IoT SDK on the ESP8266. 

The key functions called by the demo program are

esp8266_sample_init(const char* ssid, const char* password)
Connect to wLAN
 
Initialise the IoTHub client system
 
device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol)
Creates a IoT Hub client for communication with an existing IoT Hub using the specified connection string parameter.
 
Set the Azure Server certificate used to validate TLS connection to iothub
 
Creates a new IoT hub message from a null terminated string. The type of the message will be set to IOTHUBMESSAGE_STRING
 
Asynchronous call to send the message specified by eventMessageHandle
The call back mesage 'send_confirm_callback' is called with the results of the message transmission. 
 
This function MUST be called by the user so work (sending/receiving data on the wire, computing and enforcing timeout controls, managing the connection to the IoT Hub) can be done by the IoTHubClient. The recommended call frequency is at least once every 100 milliseconds.
 
In the demo program it attempts to send several messages and once sent counts the number of times the call_back function is called. Once the number of call backs equals the total number of messages sent it exits. Throughout it is calling the Do Work function.

Sunday, 26 May 2019

Dragino LoRa Shield for Arduino


The Dragino LoRa Shield for Ardunio enables your Arduino to transmit and receive data packets wirelessly many kilometers using the LoRaWAN network for free. There is no need for wifi or 3/4/G network connections.


Arduino Dragino Shield (red) plugged into a MEGA2560 Arduino

LoRaWAN

LoRaWAN is a wireless protocol for wide area networks which uses the unlicenced 863-870 MHz frequency band in Europe. Other parts of world use other unlicenced frequencey bands.

So that many devices can all share the same frequency band the European frequency regulations impose specific duty-cycles on devices for each sub-band. These apply to each device that transmits on a certain frequency and devices have to respect these duty-cycles. Most channels used by LoRaWAN have a duty-cycle as low as 1% or even 0.1% which means in pratice you can only transmit or receive small amounts of data.


LoRaWAN is implemented using the proprietary LoRa modulation system sold by Semtech corporation.
The LoRaWAN protocols are defined by the LoRa Alliance and formalized in the LoRaWAN Specification which can be downloaded on the LoRa Alliance website.


Dragino LoRa Shield

The Dragino LoRa Shield is a LoRaWAN transceiver in a Arduino shield form factor that uses the Semtech SX1276/SX1278 chip and costs around £25.

http://www.dragino.com/products/module/item/102-lora-shield.html


The Things Network

The Things Network is a project dedicated to building a network for the Internet of Things based on LoRaWAN.

LoRaWAN Gateways connect to the The Things Network and forward received data packets from LoRaWAN Devices to The Things Network cloud service which in turn forwards them on to your Internet connected application. It can also send data packets from your application to devices.


There are plenty of gateways available to connect to - if you'd like to find a gateway in your area, check the Gateway Map. If there are no gateways available locally it is simple to setup your own and connect it to the The Things Network. 

First transmission attempt

To set up your Arduino as an The Things Network device and transmit a message you need to
  1. Regsiter with The Things Network, create an application and register your device with the application. In settings set the registered device to use ABP activiation and make a note of the Network Sesion Key, application session key and Device address.
  2. Using Arduino IDE > Library Manager - install the IBM LMIC framework library
  3. Using the Arduino IDE - use the ttn-abp example from the LMIC library. Modify the source code to use the NWKSKEY, APPSKEY and DEVADDR as noted above and flash to your arduino.
  4. Your arduino will tranmist a message "hello world" once a minute. If this message is received by a Things Network Gateway you can see it appear in the data tab as shown below. 

Test packets received



The details of one of the test packets is shown below. You can see it was received by two Gateways which are about about 1km and 1.5km away. This seemed impressive given then the Arduino was indoors.  

Test packet details





Monday, 22 April 2019

AM2302 Humidity and Temperature Sensor Module

The AM2302 (DHT22) is a calibrated relative humidity and temperature sensor module with a single data line for data exchange.  It has a small micro controller which uses a proprietary protocol.

AM2302 sensor module


Pin Out:
1. VDD 3.3V to 6V
2. DATA
3. N/C (not connected)
4. GND

Measurement:
  • Humidity: 0 to 100% 
  • Temperature: -40 to 125 degrees Celsius

Accuracy:
  • humidity+-2%RH(Max+-5%RH)
  • temperature+-0.2Celsius

In applications, an external pull-up resistor, about 10 - 5kΩ, is often required. Sometimes this resistor is already integrated in the module, sometimes its necessary to add it.


Arduino IDE

Numereous Arduiono IDE libraries exist for the DHTxx family of modules.  For ESP8266 try https://github.com/beegee-tokyo/DHTesp which disables task switching during reads to ensure timings are not affected.






Soil Moisture Sensor - MH-Sensor Series, Flying-Fish


The Soil Moisture Sensor from MH-Sensor Series, Flying-Fish, provides both a Digital (High or Low) and an Analogue output.

The module has four pins
  •     VCC: For power (2V to 36V)
  •     A0: Analog output
  •     D0: Digital output
  •     GND: Ground
The module also contains a potentiometer which will set the threshold value and then this threshold value will be compared by the LM393 comparator. The output LED will light up and down according to this threshold value.

Soil Humidity Sensor (MH-Sensor Series Flying-Fish)
 Connected to a 5V supply the analogue output measured as follows
  • In air - 5V
  • Wet soil - 2V
  • Water - 1.9V


Saturday, 20 April 2019

Programming ESP8266 Arduino Shield using Arduino IDE


The ESP8266 Arduino Shield can be programmed using the Arduino IDE.

Install the ESP8266 board Managers

Go "To File > Preferences", and set Additional Boards Manager URLs: to include http://arduino.esp8266.com/stable/package_esp8266com_index.json 

NB multiple board manager URLs can be seperated with a comma

Go to "Tools -> Board -> Boards Manager", type "ESP8266" and install it.

Go to "Tools -> Board" and select "Generic ESP8266 Module".

Connect ESP8266 to PC 

Connect ESP8266 Shield debug port to PC via USB to TTL Serial board.
  • RX to TX
  • TX to RX
  • GND to GND
  • 3V3 to 3V3

Set DSP8266 to flash mode

Set DIP switches 3 (flash mode) and 4 (DFU) ON

Upload sketch program

Open sketch program and select upload

Set DSP8266 to normal operation and run sketch program

Set DIP switches 3 (flash mode) and 4 (DFU) OFF


Sunday, 14 April 2019

Connecting to an Arduino ESP8266 Shiald v1.0



The Arduino ESP8266 Shiald v1.0 is an Arduino UNO format shield for an ESP8266 WiFi module.

The ESP8266 WiFi module is a self contained SOC from Espressif Systems with TCP/IP stack and WiFi capability enabling any microcontroller to access a WiFi network. Out of the box the ESP8266 Shiald module is capable of operating independently without an Arduino Uno being attached

There are many different Arduino ESP8266 Shialds out there that look almost identical but come with different default software installed and variances in circuitry.


This article is about two such Shialds and describes initial findings when connecting to the board debug port from a PC and the default Access Point.

The vendor of the first Shiald is AI-THINKER and the second DOIT.AM. The names are marked on the ESP8266 module case.
AI-THINKER and DOIT.AM ESP8266 shialds

Shiald DIP settings

The Shiald has 4 DIP switches as follows (when ON):
  1.  Connect ESP8266 TX to pin D0
  2.  Connect ESP8266 RX to pin D1
  3.  Enable ESP8266 bootloader mode
  4.  Enable DFU led (purpose?)
To run the programmed software ensure DIP 3 is OFF.  To flash new software to the ESP8266 ensure DIP 3 is ON.

NB the debug TX and RX pins are permenantly connected to the ESP8266 TX and RX.

Connecting Shiald to PC via USB to TTL serial adapter

To connect to the Shiald debug port from a PC use a USB to TTL serial adapter.

USB to TTL serial adapter

Ensure voltages are matched e.g both USB to TTL serial adapter set to 5V

Connect as follows:

  • Shiald Debug RXD <-> TX

  • Shiald Debug TXD <-> RX

  • Shiald Debug GND <-> GND

  • Shiald Debug 5 V <-> VCC

PuTTY connection to Shiald Debug port

To interact with the Shiald Debug port from your PC use software like PuTTY.

Use the following serial settings
  • COM port to the USB to TTL serial adapter COM port
  • baud 115200

Shiald AI-Thinker


Connecting to Shiald Board debug

Pressing reset on Shiald results in the following output

Debug output from THINKER-AI Shiald

In this case the Shiald is running SDK version 0.9.2. It is using the network 192.168.4.0/24 for the wLAN, the server itself is using the IP address 192.168.4.1 and providing a DHCP service for wLAN client devices.

Many web pages state the AI-THINKER software is capable of processing AT commands. For example, sending the command 'AT' the shiald will respond with an 'OK' message.

The AT commands must end with "\r\n".  By default, PuTTY sends CR (\r) when the Enter Key is pressed therefore press Ctrl+M to send CR (\r) and Ctrl+J to send LF (\n).

Unfortunately, I was unable to get my AI-THINKER shiald to response to any AT commands. I checked the wiring and confirmed the commands were being sent but the board was unresponsive.

Connecting to AI-Thinker Shiald  Access Point wLAN

By default the board acted as an Access Point with SSID AI-THINKER and default password ai-thinker. In the debug window you see output indicating when a client wLAN deviceconnects. 

iPhone connected to Shiald wLAN

The AI-Thinker shiald web server

The AI-Thinker Shiald runs a web browser on port 80  (this is the only open port on the shiald)

To test wifi connection from your wifi device go to  http://192.168.4.1/client?command=0

Web page http://192.168.4.1/client?command=0

I could not find any other web pages supported by the web server.

DOIT.AM Shield

There are some boards out there with the wrong components installed preventing the D0 and D1 pins operating correctly as transistors marked J3Y have been installed instead of the required J1Y 3.3V/5V logic convertors.

The DOIT.AM I have is an example of a board with this problem. Testing with a multimeter for continuity between the TX / RX pins and D0 and D1 pins confirmed there was no connection. For testing purposes I used the debug TX / RX pins.

Connecting to DOIT.AM Shiald Board debug 

Connecting to the debug board and pressing reset on Shiald results in the following output. The default software on this board responds to AT commands. 

Debug output from DOIT.AM Shiald

Connecting to DOIT.AM Shiald  wLAN

The SSID is ESP_2834A6.No password required. 

The DOIT.AM Shiald web server

The DOIT.AM Shiald has no open ports which is a problem that needs further investigation (to be continued).




Sunday, 27 March 2016

Arduino TEA 5767 FM radio

An FM radio using a TEA5767 FM radio module and an Arduino

Main parts

The main parts used are
  • TEA 5767 FM Stereo Radio Module for Arduino 76-108MHZ (http://www.aliexpress.com)
  • TOOGOO(R) Keypad Shield Board  (amazon)
  • Arduino
  • FM aerial
  • Headphones / speakers

The TEA5767 FM module provides the stereo FM radio recevier and headphone outputs. The receiver is controlled by the Arduino via I2C. 

The TOOGOO keypad shield provides a two line display for displaying the current frequency and signal strength etc. It also provides the buttons that are used to control the radio.

TEA5767 FM Stereo Radio Module

 The datasheet for the TEA5767 can be found here datasheet

Source code

My source code for the project can be found at the Git hub repository source code

The software radio supports 5 modes which are cycled through using the up and down keys

  1. Signal mode - the display shows the current frequency and signal strength. Volume is controlled with the left and right keys
  2. Manual mode - left and right keys change the tuned frequency
  3. Scan mode - left and right keys scan for the next radio station
  4. Preset mode - recall a saved preset radio station. Use left and right keys to select the preset and the select key to recall it
  5. Programme mode - save the current radio station as a preset. Use the left and right keys to select the preset and the select key save

Presets are saved in the Arduino EEPORM so they are not lost when the power is turned off. 

The software includes a library of functions for controlling the TEA5767. This library works with the TEA5767 in I2C mode therefore the BUSMODE pin needs to be connected to ground to select ITC mode. The IC address is 11000001.

The most fiddly operation provided for by the library is setFrequency(double freq)which sets the radio frequency. The radio uses a Phased Lock Loop (PLL) and to tune the radio to a wanted frequency the corresponding PLL word must be calculated. This is a 14bit word that is sent to the programmable divider. 

The PLL word for high side injection mode is calculated as follows

    // calculate PPL word for high side injection
    // PPL = 4 * freq(Hz) + 225kHz / clock(Hz)
    frequencyB=round( ( 4*( (freq*1000000)+225000) ) /32768);

My library only uses the high side injection mode when tuning the radio. According to the datasheet the function should tune the radio to both the high side and low side and then select the one with the strongest signal but in practice I've found using only the high side seems works fine for demo purposes. 

Wiring

The wiring should be connected as follows
 
TEA5767 wiring
 * SCA to Arduino SDA
 * SDL to Arduino SDL
 * VCC to 5V
 * GND to GND

LCD wiring
 * R/W (pin 1) to GND
 * VCC (pin 2) to 5V
 * V0 (pin 3) to variable resistor (contrast)
 * RS (pin 4) to Arduino digital pin
 * E  (pin 6) to Arduino digital pin 9
 * D4 (pin 11) to Arduino digital pin 4
 * D5 (pin 12) to Arduino digital pin 5
 * D6 (pin 13) to Arduino digital pin 6
 * D7 (pin 14) to Arduino digital pin 7

Tuesday, 29 October 2013

Arduino library shiftIn function bug

Found a bug in the Arduino library ShiftIn() function whilst testing an ALU board.

In order to test the ALU board they are connected to an Arduino via an IO board which provides 16 input and output pins using 74HC165 shift register ICs.

 
The test program generates the two 4-bit inputs for the ALU and eight control signals to select the mathematical operation to be performed. The test values are then serially written to the two 74HC595 SIPO shift registers on the IO board, the parallel outputs are wired to the ALU board.

The ALU output is then serially read from the 74HC165 PISO shift register and the result displayed in the Arduino IDE.

The test program cycles through a large numbers of tests to test the ALU thoroughly.
 
Initially the ALU did not work and I spent two hours debugging it. It became apparent that the problem was with the IO board as manually probing the ALU board showed it was working fine. Initially, I suspected a floating input on one of the logic pins but after extensive probing could not find the problem.

Eventually my suspicion turned to the Arduino library and the ShiftIn() function and I decided to look at the source code to check what it was doing.

Whilst Googling for the source code I came across a bug report (issue 467) within the source code repository for the Arduino library shiftIn function. The bug report described a problem with the way the clock line is driven causing the 8th bit to be missed. Eureka!

Quickly replacing the ShiftIn() function with my own code and retesting the problem went away.
 
 

Monday, 29 July 2013

Arduino - kit for testing and dev support

I have purchased a Arduino development kit to act as a test bed and toolkit to support the development of Juno.

I plan to use the Arduino to provide a slow clock signal and a clock signal that can be manually stepped one pulse at a time. 

The Arduino will also be useful as a test bed to simulate signals to individual modules to test them before integration.

It will also be useful for programming the EEPROM and simulating parts that have not been built yet in software.

The Arduino kit I ordered is from SainSmart and includes a lot of additional pieces that look very useful such as an LCD display. 

http://www.amazon.co.uk/gp/r.html?R=7KIXKDF9L81B&C=11P1MGQVK7Z7I&H=SHXMN6VNFVPEUYUVMJO6RSYVZQ4A&T=C&U=http%3A%2F%2Fwww.amazon.co.uk%2Fdp%2FB00BWL1744%2Fref%3Dpe_385721_37986871_pe_364681_36330151_item