Skip to content

Commit

Permalink
Sync with library changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyhuangyc committed Mar 5, 2018
1 parent e545be7 commit 6a3f0ab
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 129 deletions.
1 change: 0 additions & 1 deletion firmware_v4/telelogger_esp8266/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#define WIFI_SSID "FREEMATICS"
#define WIFI_PASSWORD "PASSWORD"
#define XBEE_BAUDRATE 115200 /* 9600 for ESP8266 with older firmware */
#define MAX_RECV_LEN 128

/**************************************
* Server settings
Expand Down
19 changes: 9 additions & 10 deletions firmware_v4/telelogger_esp8266/telelogger_esp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ public:
}
bool netInit()
{
// set xBee module serial baudrate
bool gotIP = false;
// test the module by issuing ATE0 command and confirming response of "OK"
if (!netSendCommand("ATE0\r\n")) return false;
if (rxLen) {
if (strstr_P(rxBuf, "WIFI GOT IP")) {
if (strstr_P(rxBuf, PSTR("WIFI GOT IP"))) {
// WIFI automatically connected
gotIP = true;
}
Expand Down Expand Up @@ -147,13 +146,13 @@ public:
char *p = strstr_P(buffer, PSTR("+IPD,"));
if (p) {
p += 5;
if (rxBuf) free(rxBuf);
rxLen = atoi(p);
if (rxLen > MAX_RECV_LEN) rxLen = MAX_RECV_LEN;
rxBuf = (char*)malloc(rxLen + 1);
rxBuf[0] = 0;
char *q = strchr(p, ':');
if (q++) {
int maxLen = buffer + sizeof(buffer) - q;
if (rxLen > maxLen) rxLen = maxLen;
if (rxBuf) free(rxBuf);
rxBuf = (char*)malloc(rxLen + 1);
memcpy(rxBuf, q, rxLen);
rxBuf[rxLen] = 0;
} else {
Expand Down Expand Up @@ -311,7 +310,7 @@ public:
#if USE_MEMS
if (!checkState(STATE_MEMS_READY)) {
Serial.print("#MEMS...");
if (mems.memsInit()) {
if (mems.begin()) {
setState(STATE_MEMS_READY);
Serial.println("OK");
} else {
Expand Down Expand Up @@ -461,7 +460,7 @@ public:
float motion = 0;
for (byte n = 0; n < 10; n++) {
float acc[3] = {0};
mems.memsRead(acc);
mems.read(acc);
for (byte i = 0; i < 3; i++) {
float m = (acc[i] - accBias[i]);
motion += m * m;
Expand Down Expand Up @@ -527,7 +526,7 @@ private:
// load accelerometer and temperature data
float acc[3] = {0};
int16_t temp; // device temperature (in 0.1 celcius degree)
mems.memsRead(acc, 0, 0, &temp);
mems.read(acc, 0, 0, &temp);
logData(PID_ACC, (int)(acc[0] * 100), (int)(acc[1] * 100), (int)(acc[2] * 100));
if ((txCount % 100) == 1) {
logData(PID_DEVICE_TEMP, temp / 10);
Expand Down Expand Up @@ -571,7 +570,7 @@ private:
int n;
for (n = 0; n < 100; n++) {
float acc[3] = {0};
mems.memsRead(acc);
mems.read(acc);
accBias[0] += acc[0];
accBias[1] += acc[1];
accBias[2] += acc[2];
Expand Down
2 changes: 0 additions & 2 deletions firmware_v4/telelogger_sim800_udp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@

#define GPS_SERIAL_BAUDRATE 115200L

#define RAM_CACHE_SIZE 80 /* bytes */

#endif // CONFIG_H_INCLUDED
119 changes: 3 additions & 116 deletions firmware_v4/telelogger_sim800_udp/telelogger_sim800_udp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -269,106 +269,6 @@ bool CTeleClientSIM800::getLocation(NET_LOCATION* loc)
return false;
}

class CStorageRAM {
public:
bool init(unsigned int cacheSize)
{
if (m_cacheSize != cacheSize) {
uninit();
m_cache = new char[m_cacheSize = cacheSize];
}
return true;
}
void uninit()
{
if (m_cache) {
delete m_cache;
m_cache = 0;
m_cacheSize = 0;
}
}
void purge() { m_cacheBytes = 0; m_samples = 0; }
unsigned int length() { return m_cacheBytes; }
char* buffer() { return m_cache; }
void dispatch(const char* buf, byte len)
{
// reserve some space for checksum
int remain = m_cacheSize - m_cacheBytes - len - 3;
if (remain < 0) {
// m_cache full
return;
}
if (m_dataTime) {
// log timestamp
uint32_t ts = m_dataTime;
m_dataTime = 0;
log(0, ts);
}
// store data in m_cache
memcpy(m_cache + m_cacheBytes, buf, len);
m_cacheBytes += len;
m_cache[m_cacheBytes++] = ',';
m_samples++;
}

void header(uint16_t feedid)
{
m_cacheBytes = sprintf(m_cache, "%X#", (unsigned int)feedid);
}
void tailer()
{
if (m_cache[m_cacheBytes - 1] == ',') m_cacheBytes--;
m_cacheBytes += sprintf(m_cache + m_cacheBytes, "*%X", (unsigned int)checksum(m_cache, m_cacheBytes));
}
virtual void log(uint16_t pid, int16_t value)
{
char buf[16];
byte len = sprintf_P(buf, PSTR("%X=%d"), pid, value);
dispatch(buf, len);
}
virtual void log(uint16_t pid, int32_t value)
{
char buf[20];
byte len = sprintf_P(buf, PSTR("%X=%ld"), pid, value);
dispatch(buf, len);
}
virtual void log(uint16_t pid, uint32_t value)
{
char buf[20];
byte len = sprintf_P(buf, PSTR("%X=%lu"), pid, value);
dispatch(buf, len);
}
virtual void log(uint16_t pid, int value1, int value2, int value3)
{
char buf[24];
byte len = sprintf_P(buf, PSTR("%X=%d;%d;%d"), pid, value1, value2, value3);
dispatch(buf, len);
}
virtual void logCoordinate(uint16_t pid, int32_t value)
{
char buf[24];
byte len = sprintf_P(buf, PSTR("%X=%d.%06lu"), pid, (int)(value / 1000000), abs(value) % 1000000);
dispatch(buf, len);
}
virtual void timestamp(uint32_t ts)
{
m_dataTime = ts;
}
virtual uint16_t samples() { return m_samples; }
protected:
byte checksum(const char* data, int len)
{
byte sum = 0;
for (int i = 0; i < len; i++) sum += data[i];
return sum;
}
uint32_t m_dataTime = 0;
uint16_t m_samples = 0;
unsigned int m_cacheSize = 0;
unsigned int m_cacheBytes = 0;
char* m_cache = 0;
};

class CTeleLogger : public CTeleClientSIM800
{
public:
Expand Down Expand Up @@ -401,7 +301,7 @@ public:

if (!checkState(STATE_MEMS_READY)) {
Serial.print("MEMS...");
if (mems.memsInit()) {
if (mems.begin()) {
setState(STATE_MEMS_READY);
Serial.println("OK");
} else {
Expand Down Expand Up @@ -439,11 +339,6 @@ public:

txCount = 0;

if (!checkState(STATE_STORAGE_READY)) {
cache.init(RAM_CACHE_SIZE);
setState(STATE_STORAGE_READY);
}

if (!login()) {
return false;
}
Expand All @@ -453,12 +348,7 @@ public:
}
void loop()
{
#if RAM_CACHE_SIZE <= 128
cache.purge();
#endif
if (cache.length() == 0) {
cache.header(feedid);
}
cache.header(feedid);
cache.timestamp(millis());

// process GPS data if connected
Expand Down Expand Up @@ -518,7 +408,6 @@ public:

if ((txCount % SERVER_SYNC_INTERVAL) == (SERVER_SYNC_INTERVAL - 1)) {
// sync
bool success = false;
for (byte n = 0; n < MAX_CONN_ERRORS; n++) {
Serial.print("SYNC...");
if (!notifyServer(EVENT_SYNC)) {
Expand All @@ -528,7 +417,6 @@ public:
connErrors = 0;
txCount++;
Serial.println("OK");
success = true;
break;
}
}
Expand Down Expand Up @@ -628,7 +516,6 @@ public:
shutDownNet();
}
cache.uninit();
clearState(STATE_STORAGE_READY);
if (errors > MAX_OBD_ERRORS) {
// inaccessible OBD treated as end of trip
feedid = 0;
Expand Down Expand Up @@ -712,7 +599,7 @@ private:
{
float acc[3];
int16_t temp;
mems.memsRead(acc, 0, 0, &temp);
mems.read(acc, 0, 0, &temp);
cache.log(PID_ACC, (int16_t)(acc[0] * 100), (int16_t)(acc[1] * 100), (int16_t)(acc[2] * 100));
cache.log(PID_DEVICE_TEMP, temp / 10);
}
Expand Down

0 comments on commit 6a3f0ab

Please sign in to comment.