Potato-Powered IoT

Power consumption benchmark using Zolertia's Re-Motes, Spark Core, Ubidots, and potatoes.

AdvancedFull instructions provided3,766
Potato-Powered IoT

Things used in this project

Story

Read more

Code

Spark Core low power demo

C/C++
Spark Core source code used to benchmark power consumption, requires to install DNSClient.h library at https://github.com/Hootie81/DNSClient
// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"
#include "application.h"
#include "DNSClient.h"

/**
* Declaring the variables.
*/
HttpClient http;

// Ubidots specific
#define VARIABLE_TEST ""
#define TOKEN ""

#define LED D7
#define AWAKE_BEFORE_SLEEP 1000 // was 20000
#define SLEEP_SECONDS 60

// Enable to allow sending data to Ubidots
#define SEND_DATA 1

int var_test = 0;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
      { "Content-Type",     "application/json" },
      { "X-Auth-Token" ,    TOKEN },
      { "host", "things.ubidots.com" },
      { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

IPAddress dnsServerIP(8,8,8,8);
IPAddress remote_addr;
DNSClient dns;

char serverName[] = "things.ubidots.com";

bool checkConn(void){
   int ping;
   Serial.println("\n*****************************");
   
   if (WiFi.ready()){
       Serial.print("Local IP: ");
       Serial.println(WiFi.localIP());
       Serial.print("Local GW: ");
       Serial.println(WiFi.gatewayIP());
       ping = WiFi.ping(dnsServerIP, 3);
       Serial.print("Ping to google.com: ");
       Serial.println(ping);
       Serial.println("*****************************\n");
     
       if (ping > 0) return TRUE;
       return FALSE;
    }

   Serial.println("Connection not ready !!!");
   Serial.println("*****************************\n");
   return FALSE;
}

void blink_led(uint16_t t){
    digitalWrite(LED, HIGH);
    delay(t);
    digitalWrite(LED, LOW);   
}

void getData(void){
    var_test = analogRead(A0);
}

void postData(void){
    // Send to Ubidots
    request.path = "/api/v1.6/variables/"VARIABLE_TEST"/values";
    request.body = "{\"value\":" + String(var_test) + "}";
    http.post(request, response, headers);

    // Serial.println(response.status);
    // Serial.println(response.body);
}

void setup() {
    // Initialize pins
    pinMode(A0, INPUT);
    
    // Initialize serial console
    Serial.begin(9600);

    // Initialize server information, get IP via DNS resolv
    request.port = 80;    
    dns.begin(dnsServerIP);
    dns.getHostByName(serverName, remote_addr);
    request.ip = remote_addr;
}

void loop() {
    
    // Read data from the sensors
    // TODO: check for errors, change return from void to uint8_t/bool
    getData();
    
    // Send data to Ubidots
    #ifdef SEND_DATA
        postData();
    #endif
    
    // Short blink to indicate we have finished posted
    blink_led(500);

    // Stay awake enough time to allow being reprogramed        
    delay(AWAKE_BEFORE_SLEEP);
        
    // Put the core back to sleep
    Spark.sleep(SLEEP_MODE_DEEP, SLEEP_SECONDS);
}

SimpleWindowGUI.py

Python
Python script to create the serial connection to the RE-Mote and display the data, based on TKinter.
#!/usr/bin/env python
#  -*- coding: utf-8 -*-

# Simple and stupid text box
#
# Copyright (C) 2015, Antonio Lian,
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  3. The names of its contributors may not be used to endorse or promote
#     products derived from this software without specific prior written
#     permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

__author__ = "Antonio Lignan"

import Tkinter as tk
import tkMessageBox
from PIL import Image, ImageTk

# To read mmap.xml file
import os
import sys
import platform
import collections
import serial
import threading
import Queue
import datetime
from time import sleep


class SerialThread(threading.Thread):
    def __init__(self, queue, port, baud):
        threading.Thread.__init__(self)
        self.queue = queue
        self.s = None
        self.port = port
        self.baud = baud
        self.connected = False

    def run(self):
        self.connected = False

        try:
          self.s = serial.Serial(self.port, self.baud)
        except:
          print "Error connecting to mote"

        self.connected = True

        while self.connected:
            queue = self.s.inWaiting()
            if queue > 0:
                text = self.s.readline(self.s.inWaiting())
                self.queue.put(text)
            sleep(0.1)

    def stop(self):
        self.connected = False
        self.s.close()
        self._Thread__stop()


# Class to build a simple GUI window
class SimpleWindowGUI(object):
    def __init__(self):
        self.master = tk.Tk()
        self.master.title("IoT Potato")
        self.frame = tk.Frame(self.master, borderwidth=5, bg='white')

        # Create a text entry variables for the connection module
        self.entry_port = tk.StringVar()
        self.entry_parity = tk.StringVar()
        self.entry_stopbits = tk.StringVar()
        self.entry_baudrate = tk.StringVar()
        self.entry_bytesize = tk.StringVar()

        # Create a text box widgets to store the text input into the text entry variable
        self.entry_box_port = tk.Entry(self.frame, textvariable=self.entry_port, justify="center")
        self.entry_box_parity = tk.Entry(self.frame, textvariable=self.entry_parity, justify="center")
        self.entry_box_stopbits = tk.Entry(self.frame, textvariable=self.entry_stopbits, justify="center")
        self.entry_box_baudrate = tk.Entry(self.frame, textvariable=self.entry_baudrate, justify="center")
        self.entry_box_bytesize = tk.Entry(self.frame, textvariable=self.entry_bytesize, justify="center")

        # Create a label tag for the serial port connection, port, parity, etc
        self.label_connection_text = tk.StringVar()
        self.label_port_text = tk.StringVar()
        self.label_parity_text = tk.StringVar()
        self.label_baudrate_text = tk.StringVar()
        self.label_stopbits_text = tk.StringVar()
        self.label_bytesize_text = tk.StringVar()
        self.button_connect_text = tk.StringVar()

        # Create a label and print box for the received data over the serial port
        self.label_serial_data_text = tk.StringVar()
        self.entry_serial_data = tk.StringVar()
        self.label_serial_timestamp = tk.StringVar()
        self.entry_box_serial_data = tk.Entry(self.frame, textvariable=self.entry_serial_data, justify="left",
                                              font=("Helvetica", 20))

        # A connection status
        self.connected = False

        # An image
        self.img = ImageTk.PhotoImage(Image.open("RemotePotato.png"))
        self.panel = tk.Label(self.frame, image=self.img)

        # Initialize all windows class components
        self.initialize()

        # Geometric specific
        self.master.resizable(False, False)
        self.master.update()
        self.master.geometry(self.master.geometry())

        # Initialize serial thread
        self.queue = Queue.Queue()

        # And loop
        self.master.mainloop()

    def initialize(self):
        # Create a grid layout manager
        self.frame.grid()

        # Add the text box widget to the layout manager, stick to the East/West edges of the cell,
        # bind to an event when press enter to emulate the connect button, but bind also to mouse click to clear cells
        self.entry_box_port.grid(column=2, row=0, columnspan=3, sticky='EW', padx=10)
        self.entry_box_port.bind("<Button-1>", self.on_click_text_entry_box_port)
        self.entry_box_port.bind("<Return>", self.call_on_button_connect)
        self.entry_box_parity.grid(column=2, row=1, columnspan=3, sticky='EW', padx=10)
        self.entry_box_parity.bind("<Button-1>", self.on_click_text_entry_box_parity)
        self.entry_box_parity.bind("<Return>", self.call_on_button_connect)
        self.entry_box_baudrate.grid(column=2, row=2, columnspan=3, sticky='EW', padx=10)
        self.entry_box_baudrate.bind("<Button-1>", self.on_click_text_entry_box_baudrate)
        self.entry_box_baudrate.bind("<Return>", self.call_on_button_connect)
        self.entry_box_stopbits.grid(column=2, row=3, columnspan=3, sticky='EW', padx=10)
        self.entry_box_stopbits.bind("<Button-1>", self.on_click_text_entry_box_stopbits)
        self.entry_box_stopbits.bind("<Return>", self.call_on_button_connect)
        self.entry_box_bytesize.grid(column=2, row=4, columnspan=3, sticky='EW', padx=10)
        self.entry_box_bytesize.bind("<Button-1>", self.on_click_text_entry_box_bytesize)
        self.entry_box_bytesize.bind("<Return>", self.call_on_button_connect)

        # Create a simple button bind to an event handler
        button_connect = tk.Button(self.frame, textvariable=self.button_connect_text, command=self.on_button_connect)
        button_connect.grid(column=0, row=0, columnspan=2, sticky='EW')
        self.button_connect_text.set(u"CONNECT")

        # Create labels, black font and white background, text left-aligned ("w"),
        # expand the label across 2 cells in the grid layout manager (columnspan),
        # bind the variable tag to the widget

        label_port = tk.Label(self.frame, textvariable=self.label_port_text, anchor="w", fg="black", bg="white")
        label_port.grid(column=5, row=0, columnspan=4, sticky='EW')
        self.label_port_text.set(u"Serial Port")
        label_parity = tk.Label(self.frame, textvariable=self.label_parity_text, anchor="w", fg="black", bg="white")
        label_parity.grid(column=5, row=1, columnspan=4, sticky='EW')
        self.label_parity_text.set(u"(E)ven, (O)dd, (N)one")
        label_baudrate = tk.Label(self.frame, textvariable=self.label_baudrate_text, anchor="w", fg="black", bg="white")
        label_baudrate.grid(column=5, row=2, columnspan=4, sticky='EW')
        self.label_baudrate_text.set(u"Baud Rate")
        label_stopbits = tk.Label(self.frame, textvariable=self.label_stopbits_text, anchor="w", fg="black", bg="white")
        label_stopbits.grid(column=5, row=3, columnspan=4, sticky='EW')
        self.label_stopbits_text.set(u"Stop bits")
        label_bytesize = tk.Label(self.frame, textvariable=self.label_bytesize_text, anchor="w", fg="black", bg="white")
        label_bytesize.grid(column=5, row=4, columnspan=4, sticky='EW')
        self.label_bytesize_text.set(u"Byte size")

        # Append the image
        self.panel.grid(column=0, row=8, columnspan=4, sticky='W')

        # Serial received data
        self.entry_box_serial_data.grid(column=4, row=8, columnspan=4, sticky='EW', padx=10)

        label_serial_data = tk.Label(self.frame, textvariable=self.label_serial_data_text, anchor="w", fg="black", bg="white")
        label_serial_data.grid(column=0, row=6, columnspan=2, sticky='EW')
        self.label_serial_data_text.set(u"Received: ")

        # This is the connection status label
        label_connection = tk.Label(self.frame, textvariable=self.label_connection_text, anchor="w",
                                    fg="black", bg="grey")
        label_connection.grid(column=0, row=5, columnspan=10, sticky='EW')
        self.label_connection_text.set(u"Awaiting connection!")

        label_serial_timestamp = tk.Label(self.frame, textvariable=self.label_serial_timestamp, anchor="w", fg="black", bg="white")
        label_serial_timestamp.grid(column=5, row=9, columnspan=2, sticky='EW')
        self.label_serial_timestamp.set(u"Last data not available")

        # Print instructions on the text box
        self.entry_port.set(u"/dev/ttyUSB0")
        self.entry_parity.set(u"N")
        self.entry_baudrate.set(u"115200")
        self.entry_stopbits.set(u"1")
        self.entry_bytesize.set(u"8")
        self.entry_serial_data.set(u"None")

    # Wrapper to call on_button_connect from triggers with associated events (like entry boxes)
    def call_on_button_connect(self, event):
        if not self.connected:
            self.on_button_connect()

    # Auxiliary function to avoid repeating code
    def set_connected_state(self):
        self.label_connection_text.set(u"CONNECTED !")
        self.connected = True
        self.button_connect_text.set(u"DISCONNECT")

    # Default behaviour when pressing the CONNECT/DISCONNECT button to create a serial connection
    def on_button_connect(self):
        if self.connected is False:
              # Check if any of the cells are empty
              if self.entry_box_port.get() == "" or \
                  self.entry_box_parity.get() == "" or \
                  self.entry_box_baudrate.get() == "" or \
                  self.entry_box_bytesize.get() == "" or \
                  self.entry_box_stopbits.get() == "":
                  self.label_connection_text.set(u"Invalid: cell missing!")
                  return

              self.connected = True
              self.button_connect_text.set(u"DISCONNECT")
              self.label_connection_text.set(u"Awaiting data!")
              self.thread = SerialThread(self.queue, self.entry_box_port.get(), self.entry_box_baudrate.get())
              self.thread.start()
              self.process_serial()

        else:
            # Wrap in a try/catch block in case use tangles with offline mode and leaves serial connection open
            self.connected = False
            self.button_connect_text.set(u"CONNECT")
            self.label_connection_text.set(u"Awaiting connection!")
            self.thread.stop()


    # The functions below are meant only for clearing the text box content on a mouse click,
    # this could be optimized into a single function, this has to be done eventually :)

    def on_click_text_entry_box_port(self, event):
        self.entry_box_port.delete(0, tk.END)

    def on_click_text_entry_box_parity(self, event):
        self.entry_box_parity.delete(0, tk.END)

    def on_click_text_entry_box_baudrate(self, event):
        self.entry_box_baudrate.delete(0, tk.END)

    def on_click_text_entry_box_stopbits(self, event):
        self.entry_box_stopbits.delete(0, tk.END)

    def on_click_text_entry_box_bytesize(self, event):
        self.entry_box_bytesize.delete(0, tk.END)

    def process_serial(self):
        while self.queue.qsize() and self.connected == True:
            try:
                text = self.queue.get()
                self.entry_serial_data.set(text)
                self.label_serial_timestamp.set(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
            except Queue.Empty:
                pass
        self.master.after(100, self.process_serial)

# The main code starts here
if __name__ == "__main__":
    # Create a GUI object and set title, it will loop from the constructor
    app = SimpleWindowGUI()

CoP_potato.ino

C/C++
ATTiny co-processor low-power
/* Re-Mote's Co-Processor
 * with AtTiny85 @ 1Mhz
 * Antonio Lignan <alinan@zolertia.com>
 * Javier Sanchez <jsanchez@zolertia.com>
 *
 *Program that sends to sleep for 1 minute 
 *across the board exept the Attiny1634 
 *turn on the Re-Mote, and wait for the signal 
 *to CC2538 for go back to sleep
 *
 */

#include <i2c.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

#define WAKE_UP_BY_CHANGE_PIN 0
#define BOOT_DELAY 128
#define I2C_ENABLE 0
//#define ACTIVE_WAIT 1


/* List of pins driven on behalf of the Re-Mote */
const byte RM_CC1120            = PIN_B0;  // 14
const byte RM_MICROSD           = PIN_C0;  // 13
const byte RM_BOOTLOADER_RESET  = PIN_A3;  // 5
const byte RM_BOOTLOADER_ENABLE = PIN_A2;  // 6
const byte RM_ENABLE            = PIN_C5;  // 9
const byte RM_SYSOFF            = PIN_C2;  // 11
const byte RM_DEBUG             = PIN_B2;  // 15
const byte RM_BOOTRTS           = PIN_A0;   
const byte RM_BOOTTTR           = PIN_A1;
const byte RM_SLEEP             = PIN_A5;

/* Debug variables */

/* Global flag to arbitrate the I2C calls and sleep */
int wait_more = 0;

/*
 * Operation
 * Upon setup the MCU should disable all Re-Mote's peripherals
 * before going to sleep
 * The MCU should enter into Power Down mode
 * If an I2C command arrives, it should wake up the MCU and take 
 * care of the command and wait 2 seconds for the next command, 
 * else fall to sleep again
 */

void setup(){ 
  /* Initialize the Re-Mote peripherals and other pins */
  pinMode(RM_CC1120, OUTPUT);            // Enable/Disable CC1120
  pinMode(RM_MICROSD, INPUT);            // Detect if MicroSD is in
  pinMode(RM_BOOTLOADER_RESET, INPUT);  // At the moment disabled
  pinMode(RM_BOOTLOADER_ENABLE, OUTPUT); // At the moment disabled
  pinMode(RM_ENABLE, INPUT);             // CC2538 core enabled by external PU resistor
  pinMode(RM_SYSOFF, OUTPUT);             // Not enabled, HW coded
  pinMode(RM_DEBUG, OUTPUT);             // Used as debug
  pinMode(RM_BOOTRTS, INPUT);            // Input from Jtag RTS
  pinMode(RM_BOOTTTR, INPUT);            //Input from Jtag DTR
  pinMode(RM_SLEEP, INPUT);

  digitalWrite(RM_CC1120, LOW);
  digitalWrite(RM_DEBUG, LOW);
  digitalWrite(RM_BOOTLOADER_RESET, HIGH);  // Keep ON
  digitalWrite(RM_BOOTLOADER_ENABLE, HIGH); // ??
  digitalWrite(RM_DEBUG,HIGH);
  /* Initialize TWI interrupts, assign slave address and map callback,
   * PC1 and PB1 used as SCL/SDA respectively */
    
  #if I2C_ENABLE
   beginTWI(0x69);
   //eventFromMaster(receiveEvent);
  #endif

}

void loop(){
  // if (!wait_more){
    /* Wait a bit to give time to the Re-Mote to initialize its own stuff */
    // delay(BOOT_DELAY);
   static int i;
    /* Go to sleep */
    pinMode(RM_BOOTLOADER_RESET, INPUT);  // At the moment disabled
    pinMode(RM_BOOTLOADER_RESET, INPUT);  // At the moment disabled
    // digitalWrite(RM_BOOTLOADER_RESET,HIGH); // deberia ser el sysoff
    //delay(100);
    
    for(i=0;i<45;i++){
      //i<15 1 minute
      //i<45 3 minute
      //i<75 5 minutes
      //i<150 10 minutes
      WDTCSR = 0b01101000;
      system_sleep();
    }
   
    //do {
    //  digitalWrite(RM_BOOTLOADER_RESET,LOW);  //salida del soc para poner en sleep
    //}while(bitRead(PORTA,3) == HIGH);
    pinMode(RM_BOOTLOADER_RESET, OUTPUT);  // At the moment disabled    
    pinMode(RM_BOOTLOADER_RESET, OUTPUT);  // At the moment disabled
    digitalWrite(RM_BOOTLOADER_RESET,LOW);  //salida del soc para poner en sleep
    digitalWrite(RM_BOOTLOADER_RESET,LOW);  //salida del soc para poner en sleep
    digitalWrite(RM_BOOTLOADER_RESET,LOW);  //salida del soc para poner en sleep
    digitalWrite(RM_BOOTLOADER_RESET,LOW);  //salida del soc para poner en sleep    

#ifdef ACTIVE_WAIT
 // use with a single wait.
  WDTCSR = 0b01001100;//1seg
  system_sleep();
  WDTCSR = 0b01001100;//1seg
  system_sleep();

#else
  // Waiting until the microcontroller has done, detecting the pin fitted to 0
  while(digitalRead(RM_SLEEP)==0);   // Check if the pin RM_SLEEP is 0 and wait.

#endif



}

// check this from here http://gammon.com.au/interrupts adapt to ATTiny
void system_sleep() {
  digitalWrite(RM_DEBUG,LOW);
#if WAKE_UP_BY_CHANGE_PIN
  PCMSK0  |= bit(PCINT4); // want pin D4 / pin 3
  GIFR   |= bit(PCIF0);   // clear any outstanding interrupts  CHECK !!!
  GIMSK  |= bit(PCIE0);   // enable pin change interrupts CHECK !!!!
#endif

  ADCSRA = 0;                          // Disable ADC
  power_all_disable ();                // Power off ADC, Timer 0/1, serial interface  
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Sleep mode: Power Down
 // noInterrupts();                      // Timed sequence
  sleep_enable();                      // Enable the selected sleep mode
  // MCUCR = bit (BODS) | bit (BODSE); // Disables Brown-Out detection (done on target)
  interrupts();                        // Enable interrupts
  sleep_cpu();                         // Enters sleep mode

  /* System will likely resume from here */
  wait_more = 1;
  
 // noInterrupts();                      // Disable interrupts
#if WAKE_UP_BY_CHANGE_PIN
  PCMSK0  &= ~bit(PCINT4);             // Disable D4 interrupt
#endif
  // sleep_disable();                     // System continues execution here...
  // power_all_enable();                  // Enable all again
  // interrupts();                        // Re-enable interrupts
}

/* Callback event from a ReMote I2C write */
void receiveEvent(char recv){
  uint8_t readStatus;
  switch(recv){

    /* CC1120 basic commands */
    case 0x01: // Enable
      digitalWrite(RM_CC1120, HIGH);
      break;
    case 0x02: // Disable
      digitalWrite(RM_CC1120, LOW);
      break;
    case 0x03: // Get current status
      readStatus = digitalRead(RM_CC1120);
      break;

    /* MicroSD basic commands */
    case 0x04:
      readStatus = digitalRead(RM_MICROSD);
      break;

    /* Debug call, just toggle the RM_DEBUG pin */
    case 0x42:
      digitalWrite(RM_DEBUG, !digitalRead(RM_DEBUG));
      break;
  }
  /* Let the MCU go back to sleep */
  wait_more = 0;
}

#if WAKE_UP_BY_CHANGE_PIN
ISR (PCINT0_vect){
  // Not used
}
#endif

ISR(WDT_vect){
  wdt_disable();
}

Ubidots demo

Credits

Toni Lozano Fernández

Toni Lozano Fernández

2 projects • 10 followers
Geek developer, guitar enthusiastic
Antonio Lignan

Antonio Lignan

14 projects • 56 followers
Maker, R+D engineer
Aitor Mejias

Aitor Mejias

3 projects • 9 followers
I'm a hardware designer, and I like very much anime and roleplaying games.
Javier Sanchez Garrido

Javier Sanchez Garrido

5 projects • 14 followers

Comments