I have been playing around with the spark for creating a simple energy meter. I created something that will trigger on a pulse(1 pulse per watt-hour), sample the current time, and store this in flash. Then when I connect with TCP(once a day or so), i retrieve the array of data, to display it as a graph.
but I currently seem to run into a small issue regarding millis(). the code posted below provides an output that leaves me to think that millis() is resetting and seems to be affected by the tcpserver.
// Define the pins we're going to call pinMode on
int sensor = D0;
unsigned long pulsearray[100];
unsigned int pulsearrayindex = 0;
unsigned int storearrayindex = 0;
unsigned int flashindex = 0;
unsigned int printlist = 0;
unsigned int timst = 0;
bool printthelist = false;
TCPServer server = TCPServer(23);
TCPClient client;
void blink();
int SparkFlash_read(int address);
int SparkFlash_write(int address, uint16_t value);
// This routine runs only once upon reset
void setup() {
// It's important you do this here, inside the setup() function rather than outside it or in the loop function.
server.begin();// start listening for clients
pinMode(sensor, INPUT_PULLUP);
attachInterrupt(sensor, blink, FALLING);
}
// This routine gets called repeatedly, like once every 5-15 milliseconds.
// Spark firmware interleaves background CPU activity associated with WiFi + Cloud activity with your code.
// Make sure none of your code delays or blocks for too long (like more than 5 seconds), or weird things can happen.
void loop()
{
if(storearrayindex != pulsearrayindex)
{
SparkFlash_write(flashindex, (pulsearray[storearrayindex] >> 16) & 0x0000ffff);
SparkFlash_write(flashindex + 2, pulsearray[storearrayindex] & 0x0000ffff);
flashindex += 4;
if(flashindex > 0x1FFFFF)
{
flashindex = 0;
}
storearrayindex++;
if(storearrayindex > 99)
{
storearrayindex = 0;
}
}
if (client.connected())
{
if (client.available())
{
if(client.read() == 'g')
{
server.println("list");
printthelist = true;
printlist = 0;
}
}
if(printthelist == true)
{
if(flashindex != printlist)
{
timst = SparkFlash_read(printlist);
timst <<= 16;
timst |= SparkFlash_read(printlist + 2);
server.print(timst);
//debug
server.print(",");
server.print(pulsearrayindex);
server.print(",");
server.print(storearrayindex);
server.print(",");
server.print(flashindex);
server.print(",");
server.print(pulsearray[pulsearrayindex]);
// end debug
server.print(",");
server.println(printlist);
printlist += 4;
if(printlist > 0x1FFFFF)
{
printlist = 0;
}
}
else
{
printthelist = false;
client.flush();
}
}
}
else
{
// if no client is yet connected, check for a new connection
client = server.available();
}
}
// This is a circular buffer of 100 items to store when a pulse happened
void blink()
{
pulsearray[pulsearrayindex] = millis();
pulsearrayindex++;
if(pulsearrayindex > 99)
{
pulsearrayindex = 0;
}
}
int SparkFlash_read(int address)
{
if (address & 1)
return -1; // error, can only access half words
uint8_t values[2];
sFLASH_ReadBuffer(values, 0x80000 + address, 2);
return (values[0] << 8) | values[1];
}
int SparkFlash_write(int address, uint16_t value)
{
if (address & 1)
return -1; // error, can only access half words
uint8_t values[2] = {
(uint8_t)((value >> 8) & 0xff),
(uint8_t)(value & 0xff)
};
sFLASH_WriteBuffer(values, 0x80000 + address, 2);
return 2; // or anything else signifying it worked
}
so this currently creates the following output:
g
list
0,50,50,200,0,0
0,50,50,200,0,4
0,50,50,200,0,8
0,50,50,200,0,12
0,50,50,200,0,16
0,50,50,200,0,20
0,50,50,200,0,24
0,50,50,200,0,28
0,50,50,200,0,32
0,50,50,200,0,36
0,50,50,200,0,40
0,50,50,200,0,44
0,50,50,200,0,48
0,50,50,200,0,52
0,50,50,200,0,56
0,50,50,200,0,60
0,50,50,200,0,64
0,50,50,200,0,68
0,50,50,200,0,72
0,50,50,200,0,76
0,50,50,200,0,80
0,50,50,200,0,84
0,50,50,200,0,88
0,50,50,200,0,92
4096,50,50,200,0,96
0,50,50,200,0,100
0,50,50,200,0,104
0,50,50,200,0,108
0,50,50,200,0,112
0,50,50,200,0,116
0,50,50,200,0,120
18432,50,50,200,0,124
256,50,50,200,0,128
4,50,50,200,0,132
0,50,50,200,0,136
1,50,50,200,0,140
0,50,50,200,0,144
4096,50,50,200,0,148
65792,50,50,200,0,152
256,50,50,200,0,156
36,50,50,200,0,160
5140,50,50,200,0,164
1,50,50,200,0,168
0,50,50,200,0,172
10272,50,50,200,0,176
32,50,50,200,0,180
0,50,50,200,0,184
17,50,50,200,0,188
66560,50,50,200,0,192
1,50,50,200,0,196
any idea why most of the timestamps seem to be 0 or something small?(with an occasional power of 2 multiple??)
Thanks in advance
PS: feel free to reuse/improve on posted code above