Arduino MKR1000 -> thinger.io

I’ve been working on the weekend, and this is the result:

The thing is now connected to a DHT11 (temperature and humidity sensors), a light sensor, soil moisture sensor, and a Relay module, that activates an acuarium pump. looks pretty ugly, but works… that is the first step.

I cant believe how easy was to integrate everything thanks to thinger.io library. I hope I can get access soon to the web api part, since there are some things that I’d like to work on, (like keeping a history of the readings, being able to configure a refresh rate on a donut chart, different than the refresh rate of the history chart, for the same device reading type.. etc.

Anyway, I’m really happy with the speed I was able to get this working thanks to the library, and how simple and clean the code ended up looking.

#define _DEBUG_

#include <SPI.h>
#include <WiFi101.h>
#include <ThingerWifi.h>
#include <DHT.h>

#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"

ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

const int LedPin = 6;

// PIN configuration
const int DhtSensorPin = 2;
const int WaterRelayPin = 3;
const int SoilMoistureSensorPin = A5;
const int LightSensorPin = A6;

int ledState = LOW;
int waterRelayState = LOW;

const int DhtType = DHT11;

DHT dht(DhtSensorPin, DhtType);

void setup() {
	Serial.begin(9600);
	dht.begin();

	// configure wifi network
	thing.add_wifi(SSID, SSID_PASSWORD);

	pinMode(LedPin, OUTPUT);
	pinMode(WaterRelayPin, OUTPUT);

	// Configure the LED
	// Need to track the state separatelly from the real pin, since MKR1000 does not respond the correct value when reading an output pin
	thing["led"] << [](pson & in) {
		if (in.is_empty()) {
			in = ledState;
		}
		else {
			ledState = in ? HIGH : LOW;
			digitalWrite(LedPin, ledState);
		}
	};

	// Configure the Water Relay
	// Need to track the state separatelly from the real pin, since MKR1000 does not respond the correct value when reading an output pin
	thing["water"] << [](pson & in) {
		if (in.is_empty()) {
			in = waterRelayState;
		}
		else {
			waterRelayState = in ? HIGH : LOW;
			digitalWrite(WaterRelayPin, waterRelayState);
		}
	};

	thing["dht11"] >> [](pson & out) {
		out["humidity"] = dht.readHumidity();
		out["celsius"] = dht.readTemperature();
		out["fahrenheit"] = dht.readTemperature(true);
	};

	thing["light"] >> [](pson & out) {
		out = map(analogRead(LightSensorPin), 0, 1023, 0, 100);
	};

	thing["moisture"] >> [](pson & out) {
		out = map(analogRead(SoilMoistureSensorPin), 0, 1023, 0, 100);
	};
}

void loop() {
	thing.handle();
}
1 Like