Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Sander Hendrickx
Published

Connected Lego desk houses

Stay connected with long distance relations in a fun way. Put your little house on your work desk or nightstand =)

BeginnerShowcase (no instructions)5 hours1,865
Connected Lego desk houses

Things used in this project

Hardware components

IOTOPIA Rapid Development kit
AllThingsTalk IOTOPIA Rapid Development kit
×1
Seeed Studio Grove RGB lcd display
×1
LEGO
brick list above
×1

Software apps and online services

AllThingsTalk Maker
AllThingsTalk Maker

Story

Read more

Schematics

How to connect

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.

Code

legoHouseGeneric.ino

Arduino
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.h

Arduino
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

Sander Hendrickx

Sander Hendrickx

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

Comments