-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
117 lines (104 loc) · 2.58 KB
/
main.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "main.h"
#include "VoltageMonitor.h"
#include "CurrentMonitor.h"
VoltageMonitor volt;
CurrentMonitor curr;
bool _isEnabled;
unsigned long lastTime = 0UL;
void setup() {
pinMode(refPin, INPUT);
pinMode(vsetPin, OUTPUT);
pinMode(bsensePin,INPUT);
pinMode(isensePin, INPUT);
pinMode(vsensePin, INPUT);
pinMode(isetPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(heartbeatLedPin, OUTPUT);
// D0 - SDA, D1 - SCL
pinMode(ilimPin, INPUT);
pinMode(boostPin, OUTPUT);
// Initialize Serial communication
Serial.begin(9600);
Serial.println(WiFi.localIP());
Time.zone(-7);
Spark.syncTime();
// Initialize I2C Communication
Wire.begin();
volt.SetDesiredVoltage(0);
curr.SetDesiredCurrent(0);
tone(buzzerPin,1000,400);
delay (400);
tone(buzzerPin, 1200, 400);
// Declare the Spark function for cloud connectivity.
Spark.function("webset",WebSet);
_isEnabled = false;
}
void loop()
{
unsigned long now = millis();
if (now-lastTime>1000UL) {
digitalWrite(heartbeatLedPin, !digitalRead(heartbeatLedPin));
lastTime = now;
float currentCurrent = curr.ReadSenseCurrent();
int currentVoltage = volt.ReadSenseVoltage();
// now is in milliseconds
char publishString[100];
sprintf(publishString,"{\"v\":%d,\"c\":%f, \"b\":%d}", currentVoltage, currentCurrent, volt.ReadBatteryPercentage());
Spark.publish("Stats",publishString);
#if DEBUG_SERIAL
Serial.print("Current: ");
Serial.print(currentCurrent);
Serial.print(" Voltage: ");
Serial.print(currentVoltage);
Serial.println("");
#endif
}
}
int WebSet(String command) {
int c, v = 0;
bool enable = false;
// Copy the command string to an array of chars, split based on "-" characters
char * params = new char[command.length() + 1];
strcpy(params, command.c_str());
char * p = strtok(params, "-");
// Step through each of the command settings.
int commandStep = 0;
while (p != NULL)
{
Serial.println(p);
if (commandStep == 0)
{
v = atoi(p); // Voltage setting in mV
}
if (commandStep == 1)
{
c = atoi(p); // Current setting in mA
}
if (commandStep == 2)
{
if (strcmp(p,"true") == 0)
{
enable = true;
}
else
enable = false;
}
p = strtok(NULL, "-");
commandStep++;
}
if (!enable)
{
_isEnabled = false;
curr.SetDesiredCurrent(0);
volt.SetDesiredVoltage(0);
return 0;
}
else
{
_isEnabled = true;
curr.SetDesiredCurrent(c);
volt.SetDesiredVoltage(v);
return 1;
}
return 1;
}