Carey Payette
Published © GPL3+

Azure Sphere MT3620 – I2C Temperature and Humidity

Read an I2C temperature and humidity sensor with the MT3620 Grove Shield.

BeginnerProtip1 hour4,285
Azure Sphere MT3620 – I2C Temperature and Humidity

Things used in this project

Hardware components

Seeed Studio MT3620 Development Board for Azure Sphere
Seeed Studio MT3620 Development Board for Azure Sphere
×1
Seeed Studio MT3620 Grove Starter Kit
×1
Seeed Studio Grove - Blue LED Button
×1
Seeed Studio Grove - Temperature and Humidity Sensor (SHT31)
×1

Software apps and online services

Microsoft Azure
Microsoft Azure
Microsoft Visual Studio 2017

Story

Read more

Schematics

Hardware Setup

Code

app_manifest.json snippet

JSON
Capabilities property
  "Capabilities": {
    "AllowedConnections": [],
    "Gpio": [ 8, 9, 10, 15, 16, 17, 18, 19, 20, 12, 13, 0, 1, 4, 5, 57, 58, 11, 14, 48 ],
    "Uart": [ "ISU0", "ISU3" ],
    "WifiConfig": false
  }

main.cs listing

C/C++
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "applibs_versions.h"
#include <applibs/log.h>

#include "mt3620_rdb.h"

// 1. include the Grove, Grove LED Button, and Grove Temp Humidity Headers from the MT3620_Grove_Shield_Libary
#include "Grove.h"
#include "Sensors/GroveLEDButton.h"
#include "Sensors/GroveTempHumiSHT31.h"

static volatile sig_atomic_t terminationRequested = false;

static void TerminationHandler(int signalNumber)
{
	terminationRequested = true;
}

int main(int argc, char *argv[])
{
	// 2. define variables for button and initial state
	static GPIO_Value_Type buttonState, lastButtonState;
	void *ledButton = GroveLEDButton_Init(1, 0);
	lastButtonState = GroveLEDButton_GetBtnState(ledButton);

	// 3. initialize i2c feed with baud rate and temp humidity sensor
	int i2cFeed;
	GroveShield_Initialize(&i2cFeed, 115200);
	void* tempHumiditySensor = GroveTempHumiSHT31_Open(i2cFeed);

	struct sigaction action;
	memset(&action, 0, sizeof(struct sigaction));
	action.sa_handler = TerminationHandler;
	sigaction(SIGTERM, &action, NULL);

	// FYI - loop is every 1000 nanoseconds
	const struct timespec sleepTime = { 0, 1000 };
	while (!terminationRequested) {

		// 4. determine button state and if it has changed, toggle LED accordingly
		buttonState = GroveLEDButton_GetBtnState(ledButton);
		if (buttonState != lastButtonState) {
			if (buttonState == 0) {
				GroveLEDButton_LedOn(ledButton);
				Log_Debug("Button pressed.\n");
				// 5. take a temperature and humidity reading every time button is pressed
				GroveTempHumiSHT31_Read(tempHumiditySensor);
				float temperature = GroveTempHumiSHT31_GetTemperature(tempHumiditySensor);
				float humidity = GroveTempHumiSHT31_GetHumidity(tempHumiditySensor);
				Log_Debug("Temperature: %.1fC\n", temperature);
				Log_Debug("Humidity: %.1f\%c\n", humidity, 0x25);
			}
			else {
				GroveLEDButton_LedOff(ledButton);
				Log_Debug("Button released.\n");
			}
		}
		lastButtonState = buttonState;

		nanosleep(&sleepTime, NULL);
	}
	return 0;
}

Github

https://github.com/Seeed-Studio/MT3620_Grove_Shield

Credits

Carey Payette

Carey Payette

13 projects • 134 followers
Sr. Software Engineer at Independent

Comments