Skip to content

Instantly share code, notes, and snippets.

@technobly
Created January 6, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technobly/957ef725f8397d592c8c7269691771be to your computer and use it in GitHub Desktop.
Save technobly/957ef725f8397d592c8c7269691771be to your computer and use it in GitHub Desktop.
Electron Ubidots Example
// Author: RWB <https://community.particle.io/users/rwb/activity>
SYSTEM_MODE(SEMI_AUTOMATIC);
//SYSTEM_THREAD(ENABLED);
// This #include statement was automatically added by the Particle IDE.
#include "Ubidots/Ubidots.h"
#define TOKEN "token" // Put here your Ubidots TOKEN
#define DATA_SOURCE_NAME "ElectronSleepNew"
//This serial prints system process via USB incase you need to debug any problems you may be having with the system.
SerialLogHandler logHandler(LOG_LEVEL_ALL);
Ubidots ubidots(TOKEN); // A data source with particle name will be created in your Ubidots account
int button = D0; // Connect a Button to Pin D0 to Wake the Electron when in System Sleep mode.
int ledPin = D7; // LED connected to D1
int sleepInterval = 60; // This is used below for sleep times and is equal to 60 seconds of time.
void setup() {
//Serial.begin(115200);
pinMode(button, INPUT_PULLDOWN); // Sets pin as input
pinMode(ledPin, OUTPUT); // Sets pin as output
ubidots.setDatasourceName(DATA_SOURCE_NAME); //This name will automatically show up in Ubidots the first time you post data.
// Initalize the PMIC class so you can call the Power Management functions below.
PMIC pmic;
// Set charging current to 1024mA (512 + 512 offset)
pmic.setChargeCurrent(0, 0, 1, 0, 0, 0);
// Set the lowest input voltage to 4.84 volts. This keeps my 5v solar panel from operating below 4.84 volts.
pmic.setInputVoltageLimit(4840);
} // end setup()
void loop() {
FuelGauge fuel; // Initalize the Fuel Gauge so we can call the fuel gauge functions below.
// If the battery SOC is above 20% then we will turn on the modem and then send the sensor data.
if (fuel.getSoC() > 20)
{
float value1 = fuel.getVCell();
float value2 = fuel.getSoC();
ubidots.add("Volts", value1); // Change for your variable name
ubidots.add("SOC", value2);
// This command turns on the Cellular Modem and tells it to connect to the cellular network.
Cellular.connect();
// If the cellular modem does not successfuly connect to the cellular network
// in 10 mins then go back to sleep via the sleep command below. After 5 mins
// of not successfuly connecting the modem will reset.
if (!waitFor(Cellular.ready, 600000)) {
// Put the Electron into Sleep Mode for 2 Mins + leave the Modem in
// Sleep Standby mode so when you wake up the modem is ready to send
// data vs a full reconnection process.
System.sleep(D0, RISING, sleepInterval * 2, SLEEP_NETWORK_STANDBY);
} // Cellular.ready
ubidots.sendAll(); // Send fuel gauge data to your Ubidots account.
digitalWrite(ledPin, HIGH); // Sets the LED on
delay(250); // waits for a second
digitalWrite(ledPin, LOW); // Sets the LED off
delay(250); // waits for a second
digitalWrite(ledPin, HIGH); // Sets the LED on
delay(250); // waits for a second
digitalWrite(ledPin, LOW); // Sets the LED off
// Put the Electron into Sleep Mode for 2 Mins + leave the Modem in Sleep Standby
// mode so when you wake up the modem is ready to send data vs a full reconnection process.
System.sleep(D0, RISING, sleepInterval * 2, SLEEP_NETWORK_STANDBY);
} // if (fuel.getSoC() > 20)
// Else the battery SOC is below 20% then we will flash the LED 4 times so we know.
// Then put the device into deep sleep for 1 hour and check SOC again.
else
{
/* The 6 lines of code below are needed to turn off the Modem before sleeping
* if your using SYSTEM_THREAD(ENABLED); with the current 0.6.0 firmware.
* It's a AT Command problem currently.
*/
//Cellular.on();
//delay(10000);
//Cellular.command("AT+CPWROFF\r\n");
//delay(2000);
//FuelGauge().sleep();
//delay(2000);
digitalWrite(ledPin, HIGH); // Sets the LED on
delay(150); // Waits for a second
digitalWrite(ledPin, LOW); // Sets the LED off
delay(150); // Waits for a second
digitalWrite(ledPin, HIGH); // Sets the LED on
delay(150); // Waits for a second
digitalWrite(ledPin, LOW); // Sets the LED off
delay(150); // Waits for a second
digitalWrite(ledPin, HIGH); // Sets the LED on
delay(150); // Waits for a second
digitalWrite(ledPin, LOW); // Sets the LED off
delay(150); // Waits for a second
digitalWrite(ledPin, HIGH); // Sets the LED on
delay(150); // Waits for a second
digitalWrite(ledPin, LOW); // Sets the LED off
System.sleep(SLEEP_MODE_DEEP, 3600); //Put the Electron into Deep Sleep for 1 Hour.
} // end else
} // end loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment