Things used in this project

Hardware components:
Iotopia box rs
AllThingsTalk IOTOPIA Rapid Development kit
×1
SeeedStudio Grove RGB lcd display
×1
LEGO
brick list above
×1
Software apps and online services:
Wzfnsjw4zdiosfhfhejx
AllThingsTalk Maker

Schematics

How to connect
House schematic
Connecting the LED
To fit the LED nicely in the roof, we have to extend the legs a bit. Just solder some wires to them and attach them on the backside of the Grove connector.
Ch led

Code

legoHouseGeneric.inoArduino
Sketch for one house. You need a second and link them together through the SmartLiving cloud platform.
/*

Arduino UART Demo Sketch. This Sketch is made for an Genuino 101 IoT board with a Grove
UART WiFi module based on the popular ESP8266 IoT SoC to communicate to the AllThingsTalk
IoT developer cloud.
The Grove UART WiFi module has firmware installed which includes the ATT_IOT library. It
communicates through Serial1 of the Genuino 101 board.

### Instructions
1. Setup the Arduino hardware
  - Use an Arduino Genuino 101 IoT board
  - Connect the Arduino Grove shield, make sure the switch is set to nullV
  - Connect USB cable to your computer
  - Connect a Grove LCD RGB display to pin I2C of the Arduino shield
  - Connect a Grove Button to pin D2 of the Arduino shield
  - Connect a Grove LED socket to pin 3D of the Arduino shield
  - Grove UART wifi to pin UART (D0,D1)
2. Add 'ATT_IOT_UART' library to your Arduino Environment
     https://github.com/allthingstalk/arduino-UART-client
3. Fill in the missing strings (deviceId, clientId and clientKey) in the keys.h file
4. Upload the sketch

*/

#include "ATT_IOT_UART.h"      // AllThingsTalk Arduino UART IoT library
#include <SPI.h>               // Required to have support for signed/unsigned long type
#include "keys.h"              // Keep all your personal account information in a seperate file
#include <Wire.h>
#include "rgb_lcd.h"

ATTDevice Device(&Serial1);
char httpServer[] = "api.smartliving.io";          // HTTP API Server host
char mqttServer[] = "broker.smartliving.io";       // MQTT Server Address

rgb_lcd lcd;

// Define default values
#define red 30
#define green 30
#define blue 30

#define messageId 1
#define displayId 0   // I2C
#define buttonId 2
#define ledId 3

bool sensorVal = false;
bool buttonVal = false;
boolean hasMessage = false;

#define scrollSpeed 240    // Milliseconds delay for scrolling on lcd display
int xOffset = 16;
long ms;
String msg;
int msgLength;

void setup()
{
  Serial.begin(57600);                                 // Init serial link for debugging
  while(!Serial && millis() < 1000);                   // Make sure you see all output on the monitor. After 1 sec, it will skip this step, so that the board can also work without being connected to a pc  Serial.println("Starting sketch");
  Serial1.begin(115200);                               // Init serial link for WiFi module
  while(!Serial1);

  while(!Device.StartWifi())
    Serial.println("Retrying...");
  while(!Device.Init(DEVICEID, CLIENTID, CLIENTKEY))   // If we can't succeed to initialize and set the device credentials, there is no point to continue
    Serial.println("Retrying...");
  while(!Device.Connect(httpServer))                   // Connect the device with the AllThingsTalk IOT developer cloud. No point to continue if we can't succeed at this
    Serial.println("Retrying");

  Device.AddAsset(buttonId, "LED", "button", true, "boolean");
  Device.AddAsset(displayId, "Display", "display", true, "string");
  Device.AddAsset(messageId, "Send message", "send", "true", "string");

  delay(1000);                                         // Give the WiFi some time to finish everything
  while(!Device.Subscribe(mqttServer, callback))       // Make sure that we can receive message from the AllThingsTalk IOT developer cloud (MQTT). This stops the http connection
    Serial.println("Retrying");

  lcd.begin(16, 2);

  digitalWrite(ledId, LOW);
  Device.Send("false", buttonId);
  turnOnDisplay(red, green, blue, "House now ready for use!");

  Serial.println("House now ready for use!");
}

void loop() 
{
  bool sensorRead = digitalRead(buttonId);             // Read status Digital Sensor
  if (sensorVal != sensorRead)                         // Verify if value has changed
  {
    sensorVal = sensorRead;
    if(sensorVal) {            // Only send the value when pressed down.
      buttonVal = !buttonVal;
      if(buttonVal)
      {
        digitalWrite(ledId, HIGH);
        Device.Send("true", buttonId);
      }
      else
      {
        digitalWrite(ledId, LOW);
        Device.Send("false", buttonId);
      }
    }
  }

  if(hasMessage)    // Nicely scroll the message
  {
    scrollMessage();
  }
  
  Device.Process();
}

void scrollMessage()
{
  if(millis() - ms > scrollSpeed)
  {
    ms = millis();
    xOffset--;
    printLine(0, 0, "                ");
    if(xOffset >= 0) printLine(0, xOffset, msg.substring(0, 16 - xOffset));
    else if(msgLength + xOffset >= 0) printLine(0, 0, msg.substring(-xOffset, min(-xOffset + 16, msgLength)));
    else {
      xOffset = 16;   // Reset text scroll loop
    }
  }
}

/***********************************************************************************************/

void turnOnDisplay(int r, int g, int b, String message)
{
  hasMessage = true;
  msg = message;
  msgLength = message.length();
  lcd.display();
  lcd.setRGB(r, g, b);

  xOffset = 16;
  ms = millis();
}

void turnOffDisplay()
{
  hasMessage = false;
  lcd.noDisplay();
  lcd.setRGB(0, 0, 0);
}

void printLine(int r, int c, String line)
{
  lcd.setCursor(c, r);
  lcd.print(line);
}

void toggleDisplay(bool val)
{
  if(val)      // turn on status LED
  {
    buttonVal = true;
    digitalWrite(ledId, HIGH);
    Device.Send("true", buttonId);
  }
  else        // turn off status LED
  {
    buttonVal = false;
    digitalWrite(ledId, LOW);
    Device.Send("false", buttonId);
  }
}

/***********************************************************************************************/

void callback(int pin, String& value) 
{ 
  // Toggle display of connected house
  if(pin == buttonId)
  {
    Serial.print("Received button: ");
    Serial.println(value);
    if(value.equals("true"))
      toggleDisplay(true);
    else
      toggleDisplay(false);
  }

  // Turn on display of connected house with custom message
  if(pin == messageId)
  {
    toggleDisplay(true);
    Device.Send(value, messageId);
  }

  // Set message on our display
  if(pin == displayId)
  {
    Device.Send(value, displayId);
    if(value.equals("%off%"))
      turnOffDisplay();
    else
      turnOnDisplay(red, green, blue, value);
  }
}
keys.hArduino
Device credentials from SmartLiving. Fill in the device credentials of the house device you are uploading to.
#ifndef SETTINGS
#define SETTINGS

#define DEVICEID ""        // Your device id comes here
#define CLIENTID ""        // Your client id comes here
#define CLIENTKEY ""       // Your client key comes here

#endif
AllThingsTalk Arduino UART client
Client library which supports the Genuino 101 with UART WiFi module. First time you run it, you'll need to enter your WiFi credentials. Check the serial monitor!

Credits

Mathboy
Sander Hendrickx

I love mathematics, programming, MacGuyver, electronics, robotics, artificial intelligence, hiking, Rubik's cubes, Star Wars, astrophysics and finding Waldo

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Simple Dark Sensor
Easy
  • 49
  • 1

A simple dark sensor with or without Arduino.

LED With Arduino 101
Easy
  • 260
  • 5

Protip

Learn how to light a LEDs using Digital Write and AnalogWrite on Arduino 101!

My First Arduino 101 Car Controlled by BLE
Easy
  • 560
  • 4

Full instructions

Recently I received an Arduino 101 kit from Hackster. My joy was great because it had a lot to do new projects!

Upgrade Firmware & Library of Arduino 101
Easy
  • 85
  • 2

Protip

This tutorial explains how to upgrade firmware and library of Arduino 101.

Grove starter kit for arduino --- Buzzer
Easy
  • 77
  • 2

Protip

Teaches you how to use the buzzer in the Arduino Grove starter kit.

Grove starter kit for arduino --- Sound sensor
Easy
  • 83
  • 2

Protip

Teaches you how to use the sound sensor in the Arduino Grove starter kit.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login