Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
UbiMaker
Published © GPL3+

Linkit One Wifi - Battery Test

How many sensor measurements can you stream with a single battery charge?

BeginnerProtip4,040
Linkit One Wifi - Battery Test

Things used in this project

Software apps and online services

Ubidots
Ubidots

Story

Read more

Code

battery_tests_linkit.ino

C/C++
A firmware for Arduino IDE 1.5.7 with the Linkit One library.
/*
 Ubidots WiFi client - Battery test

 This sketch sends an incremental value and the battery level to Ubidot

 Change the macro WIFI_AP, WIFI_PASSWORD, WIFI_AUTH, URL, TOKEN and VARIABLE_IDs accordingly.

 based on the Web Client example created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe
 modified 20 Aug 2014
 by MediaTek Inc.
 modified 6 Jun 2015
 by Ubidots, Inc.
 */

#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <LBattery.h>

#define WIFI_AP "Abriles_LTE"
#define WIFI_PASSWORD "12345678"
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.

// Ubidots information

#define URL        "things.ubidots.com"
#define TOKEN      "sdagdgklafdsjlkdg32lk"          // replace with your Ubidots token generated in your profile tab
#define VARIABLEID1 "5576280e76254212a7d0ece9"                // create a variable in Ubidots and put its ID here (http://app.ubidots.com/ubi/datasources/)
#define VARIABLEID2 "55762817762542133c9726ba"                // create a variable in Ubidots and put its ID here (http://app.ubidots.com/ubi/datasources/)

int counter = 0;

void setup()
{
  LTask.begin();
  LWiFi.begin();
  Serial.begin(9600);

  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }
}

void loop()
{ 
  save_value(String(counter++), VARIABLEID1);
  save_value(String(LBattery.level()), VARIABLEID2);
}


void save_value(String value, String VARIABLEID){
 
  Serial.println("Sending value to Ubidots...");
  LWiFiClient c;
  while (!c.connect(URL, 80))
  {
    Serial.println("Retrying to connect...");
    delay(100);
  }

  String data = "{\"value\":"+ value + "}";
  String thisLength = String(data.length());
  
  // Build HTTP POST request
  c.print("POST /api/v1.6/variables/");
  c.print(VARIABLEID);
  c.print("/values?token=");
  c.print(TOKEN);
  c.println(" HTTP/1.1");
  c.println("Content-Type: application/json");
  c.println("Content-Length: " + thisLength);
  c.print("Host: ");
  c.println(URL);
  c.print("\n" + data);
  c.print(char(26));

  // read server response
  
  while (c){
    Serial.print((char)c.read());
  }
  
  c.stop();
  
}

Credits

UbiMaker
53 projects • 231 followers
Maker @ ubidots.com

Comments