Things used in this project

Hardware components:
Iotopia box rs
AllThingsTalk IOTOPIA Rapid Development kit
Relay (generic)
Grove button
Grove rotary angle sensor
FONA 800 Cellular Board
Software apps and online services:
AllThingsTalk Maker


What does the Electracker do?
Img 20170313 132044 7wmudfbf9i
Electricity usage
Verbruikgraph odrxq37jli


#include <SoftwareSerial.h>
#include "ATT_IOT_FONA.h"
#include "ATT_MQTT.h"

#include "ATT_IOT_GPRS.h"
#include <SPI.h>                //required to have support for signed/unsigned long type.

#define deviceId "OGdYamJWEgVbr52y8CPywuNJ"
#define clientId "ensorTomWillaert_rZr0KPzV"
#define clientKey "tcRglRdK"

#define FONA_APN       ""
#define FONA_USERNAME  ""
#define FONA_PASSWORD  ""

#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 5
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);

ATTDevice Device(deviceId, clientId, clientKey);            	//create the object that provides the connection to the cloud to manager the device.
#define httpServer ""                  		// HTTP API Server host                  
#define mqttServer httpServer                               	// MQTT Server Address 

//define pins on ARDUINO
 int lamp1 = 5;
const int lamp2 = 6;
const int button1 = 7;
const int button2 = 8;
#define rotar1 A0
#define rotar2 A1

//Variables to look if button is on or off
bool lamp1On, lamp2On = false;
int val1, val2 = 0;
int old_val1, old_val2 = 0;
int state1, state2 = 0;

//Variables to measure usages
float usageLamp1, usageLamp2, usageTotal;

//Variable to know when to send data to the cloud
float lastSendTime;
float timeBetweenSend = 7; //converting to seconds later

//Variables for the refresh rate
float lastUpdate;
float checkRefreshRate = 0.1;

//int just to send electricity to server
const int pinElectricity = 4;

//required for the device
void callback(const char* topic, const char* payload, unsigned int length);
// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details.

void setup()
  //Sets the extensions plugged into ARDUINO into the right "state"
  pinMode(lamp1, OUTPUT);  
  pinMode(lamp2, OUTPUT);  
  pinMode(button1, INPUT);  
  pinMode(button2, INPUT);
  pinMode(rotar1, INPUT);
  pinMode(rotar2, INPUT);
  timeBetweenSend *= 1000;
    fonaSS.begin(19200); 										// if you're using software serial
    Serial.begin(57600);                                        // init serial link for debugging
	while(!Serial && millis() < 2000);                   		// Make sure you see all output on the monitor. After 2 sec, it will skip this step, so that the board can also work without being connected to a pc  
    while (! Device.InitGPRS(fonaSS, FONA_RST, F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) {
        Serial.println("Retrying FONA");
    Serial.println(F("Connected to Cellular!"));
    delay(2000);                                        		// wait a few seconds to stabilize connection
    while(!Device.Connect(httpServer))            				// connect the device with the IOT platform.                                                                                

    //Create Assets so it displays in ATT UI
    Device.AddAsset(pinElectricity, "Electricity", "Watt" ,false, "{\"type\": \"number\"}");
    Device.AddAsset(lamp1On, "Lamp 1", "light emitting diode", "boolean", false, "boolean");
    Device.AddAsset(lamp2On, "Lamp 2", "light emitting diode", true, "boolean");
    while(!Device.Subscribe(callback, mqttServer, 1883))                          // make certain that we can receive message from the iot platform (activate mqtt)
  //Set all things to the right state in ATT UI
  Device.Send("false", lamp1);
  Device.Send("false", lamp2);
  Device.Send("0", pinElectricity);

void loop()
  if(lastSendTime + timeBetweenSend < millis() ) {
    lastSendTime = millis();

// Callback function: handles messages that were sent from the iot platform to this device.
void callback(const char* topic, const char* payload, unsigned int length) 
  String msgString; 
  {                                                           //put this in a sub block, so any unused memory can be freed as soon as possible, required to save mem while sending data
    char message_buff[length + 1];                        
    strncpy(message_buff, (char*)payload, length);      
    message_buff[length] = '\0';                                
    msgString = String(message_buff);
  Serial.print("Payload: ");                           
  Serial.print("topic: ");
  int pinNr = Device.GetPinNr(topic, strlen(topic));
  if(pinNr == lamp1On)
  if(msgString == "true")
      lamp1On = true;
      lamp1On = false;
    Device.Send(msgString, lamp1On);         

//Function to check if buttons are in an on or off state.
void CheckButtons() {
  //Read if button is pressed at the moment
  val1 = digitalRead(button1);
  val2 = digitalRead(button2);

  //Check if button is TRUE or FALSE
  if((val1 == HIGH) && (old_val1 == LOW)) {
    state1 = 1 - state1;
  old_val1 = val1;

  if((val2 == HIGH) && (old_val2 == LOW)) {
    state2 = 1 - state2;
  old_val2 = val2;

  //Enable or disable lamps
  if(state1) {
    digitalWrite(lamp1, HIGH); lamp1On = true;
  else {
    digitalWrite(lamp1, LOW); lamp1On = false;
  if(state2) {
    digitalWrite(lamp2, HIGH); lamp2On = true;
  else {
    digitalWrite(lamp2, LOW); lamp2On = false;

  //Measure usage
void CheckEnergy() {

  if(millis() > lastUpdate + lastUpdate * checkRefreshRate) { //Makes it so you can choose refesh rate
    //Measure usage by lamp 1
    if(lamp1On) {
    usageLamp1 = analogRead(rotar1);
    else {
      usageLamp1 = 0;
    //Measure usage by lamp 2
    if (lamp2On) {
    usageLamp2 = analogRead(rotar2);
   else {
    usageLamp2 = 0;

   //add up usages 
   usageTotal = usageLamp1 + usageLamp2;
   //var thats needed for refresh rate
   lastUpdate = millis();

//Convert the total usage to a string and send it to the ATT interface
void SendValuesToCloud() {
  //Electricity used
  String data;
  data = String(usageTotal);

  Device.Send(data, pinElectricity);

  //Lamps state
  if(lamp1On) {
    data = "true";
  else {
    data = "false";
  Device.Send(data, lamp1);

  if(lamp2On) {
    data = "true";
  else {
    data = "false";
  Device.Send(data, lamp2);



Engineer Electronics. Love software development


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


Similar projects you might like

The composting friend
  • 597
  • 34

Full instructions

Our goal is to make composting easier to use and to maintain.

Travel Trainer
  • 76
  • 3

Work in progress

A mobile VR application that helps people with learning disabilities practice travelling by using an Intel Edison to gather behaviour data.

Travel Trainer

Team Independence VR

Smart Room Slack Bot
  • 188
  • 2

Full instructions

A Slack bot that manages each room in a venue. The bot takes bookings and monitors the environment (temp, light, power) and occupancy (PIR).

  • 905
  • 42

Work in progress

Heating ventilation and air-conditioning for a dome.

  • 404
  • 14

Full instructions

It's the sorting machine that you've been waiting for. Just throw all your junk in it and let it sort for you.


Team The Avengers

Home Automation Orchestrator
  • 447
  • 6

Full instructions

A device that gathers information from various home automation agents and that dispatch orders to those devices based on rules

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project