Commit 1673159f authored by Patrick's avatar Patrick

working JSON sending with temperature sensor

parent 19446e14
......@@ -7,6 +7,8 @@ GPRS Gprs;
GSM GsmAccess;
GSMClient Client;
#define DEBUG_MODE 1
//teal_mallet_gsm::teal_mallet_gsm(Client& aClient) :Client(aClient)
//{
//}
......@@ -97,94 +99,6 @@ uint8_t teal_mallet_gsm::endConnection()
}
}
/*
int16_t teal_mallet_gsm::sendOneReading(uint16_t _y, uint8_t _m, uint8_t _d, uint8_t _hh, uint8_t _mm, uint8_t _ss, double _float_reading, t_SensorType _sensor_type, bool send_to_serial)
{
if (_client_connection == true) {
// create the body dynamically and send after length header
Print* s;
#if DEBUG_MODE > 0
for (int i=0; i<2; i++) {
if (i == 0) {
// first time client
s = &Client;
} else {
s = &Serial;
}
#else
for (int i=0; i<1; i++) {
s = &Client;
#endif
// then send the actual headers
s->print(F("POST "));
s->print(F("/api/"));
s->print(F("a3b40af1-16b5-4727-bcdc-60b0816a1e7b"));
s->print(F("/station/"));
s->print(F("a6c33855-49a0-5858-8ffe-3da7c2b0412d"));
s->print(F("/measurements"));
s->println(F(" HTTP/1.1"));
s->print(F("Host: "));
s->println(server);
s->println(F("Connection: close"));
s->println(F("Content-Type: application/json"));
s->print(F("Content-length: ")); // need to calculate the content length before sending, so:
// start the jsonPrint with null output (discard the actual output characters)
json.beginNull();
// (optional) print the JSON nicely with tabs and spaces (comment out for fewer characters)
json.prettyPrint();
// build the json array (outputting to nowhere)
buildAMON();
// then send how many bytes that took
s->println(json.length());
// finish the headers with a blank line
s->println();
// then restart jsonPrint with a new target (sending to client)
json.begin(*s);
// build the json
buildJSON();
// finish the last line of the message (jsonPrint won't press return)
s->println();
}
if (send_to_serial == true) {
Serial.println(body);
}
// wait for any incoming data
unsigned long timeoutStart = millis();
uint16_t char_counter = 0;
uint16_t status_code = 0;
while ( (Client.connected() || Client.available()) && ( (millis() - timeoutStart) < 30000UL) ) {
if (Client.available()) {
char c = Client.read();
char_counter++;
// Print out this character
if (send_to_serial == true) {
Serial.print(c);
}
if (char_counter > 9 && char_counter < 13) {
// TODO: we should probably check that we've got numbers too!
//chars 10, 11 & 12 form the status_code
status_code = (status_code * 10) + (c - '0');
}
// We read something, reset the timeout counter
timeoutStart = millis();
} else {
// We haven't got any data, so let's pause to allow some to arrive
delay(1000);
}
}
return status_code;
} else {
// if you didn't get a connection to the server:
return -1;
}
}
*/
char* teal_mallet_gsm::ftoa(char *a, double f, int precision)
{
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
......@@ -199,7 +113,7 @@ char* teal_mallet_gsm::ftoa(char *a, double f, int precision)
return ret;
}
int16_t teal_mallet_gsm::sendReadings(PGM_P param, PGM_P quali, PGM_P dtype, PGM_P units, PGM_P perid, t_measurement* the_measurements, uint8_t read_head, uint8_t read_tail)
int16_t teal_mallet_gsm::sendReadings(PGM_P *param, PGM_P *quali, PGM_P *dtype, PGM_P *units, PGM_P *perid, t_measurement* the_measurements, uint8_t num_mmnt, uint8_t read_head, uint8_t read_tail)
{
if (_client_connection == true) {
// create the body dynamically and send after length header
......@@ -208,9 +122,9 @@ int16_t teal_mallet_gsm::sendReadings(PGM_P param, PGM_P quali, PGM_P dtype, PGM
for (int i=0; i<2; i++) {
if (i == 0) {
// first time client
s = &Client;
} else {
s = &Serial;
} else {
s = &Client;
}
#else
for (int i=0; i<1; i++) {
......@@ -232,10 +146,12 @@ int16_t teal_mallet_gsm::sendReadings(PGM_P param, PGM_P quali, PGM_P dtype, PGM
s->print(F("Content-length: ")); // need to calculate the content length before sending, so:
// start the jsonPrint with null output (discard the actual output characters)
json.beginNull();
// (optional) print the JSON nicely with tabs and spaces (comment out for fewer characters)
#if DEBUG_MODE > 0
// (optional) print the JSON nicely with tabs and spaces
json.prettyPrint();
#endif
// build the json array (outputting to nowhere)
buildAMON(param[0], quali[0], dtype[0], units[0], perid[0], the_measurements, read_head, read_tail);
buildAMON(&param[0], &quali[0], &dtype[0], &units[0], &perid[0], the_measurements, num_mmnt, read_head, read_tail);
// then send how many bytes that took
s->println(json.length());
// finish the headers with a blank line
......@@ -243,7 +159,7 @@ int16_t teal_mallet_gsm::sendReadings(PGM_P param, PGM_P quali, PGM_P dtype, PGM
// then restart jsonPrint with a new target (sending to client)
json.begin(*s);
// build the json
buildAMON(param[0], quali[0], dtype[0], units[0], perid[0], the_measurements, read_head, read_tail);
buildAMON(&param[0], &quali[0], &dtype[0], &units[0], &perid[0], the_measurements, num_mmnt, read_head, read_tail);
// finish the last line of the message (jsonPrint won't press return)
s->println();
}
......@@ -279,14 +195,17 @@ int16_t teal_mallet_gsm::sendReadings(PGM_P param, PGM_P quali, PGM_P dtype, PGM
}
}
int8_t teal_mallet_gsm::buildAMON(PGM_P param, PGM_P quali, PGM_P dtype, PGM_P units, PGM_P perid, t_measurement* the_measurements, uint8_t read_head, uint8_t read_tail)
int8_t teal_mallet_gsm::buildAMON(PGM_P *param, PGM_P *quali, PGM_P *dtype, PGM_P *units, PGM_P *perid, t_measurement* the_measurements, uint8_t num_mmnt, uint8_t read_head, uint8_t read_tail)
{
int8_t measurements_built = 0;
// calculate the number of readings
uint8_t numReadings = sizeof(param)/sizeof(*param);
// calculate the number of measurements
uint8_t max_mmnts = sizeof(the_measurements);
uint8_t num_mmnts = ((read_head + max_mmnts) - read_tail) % max_mmnts;
#if DEBUG_MODE > 1
Serial.print(F("num_mmnt: "));
Serial.println(num_mmnt);
delay(100);
#endif
json.openObject();
json.writeKey(F("devices"));
......@@ -327,7 +246,14 @@ int8_t teal_mallet_gsm::buildAMON(PGM_P param, PGM_P quali, PGM_P dtype, PGM_P u
//send each measurement in the array
while (read_tail != read_head) {
// increment the read_tail
read_tail = (read_tail + 1) % max_mmnts;
read_tail = (read_tail + 1) % num_mmnt;
#if DEBUG_MODE > 1
Serial.print(F("HEAD: "));
Serial.println(read_head);
Serial.print(F("TAIL: "));
Serial.println(read_tail);
delay(20);
#endif
// then build each measurements json
json.openObject();
json.writeKey(F("type"));
......@@ -359,7 +285,7 @@ int8_t teal_mallet_gsm::buildAMON(PGM_P param, PGM_P quali, PGM_P dtype, PGM_P u
sprintf(buff, "%02d", the_measurements[read_tail].mm);
json.appendValue(buff);
json.appendValue(':');
sprintf(buff, "%02d", the_measurements[read_tail].ss);
sprintf(buff, "%02d", 0); // time should be on the ordinal so round down
json.appendValue(buff);
json.appendValue('Z');
json.closeValue();
......
......@@ -66,7 +66,7 @@ private:
// character string of set precision from float/double input
char* ftoa(char *a, double f, int precision);
int8_t buildAMON(PGM_P param, PGM_P quali, PGM_P dtype, PGM_P units, PGM_P perid, t_measurement* the_measurements, uint8_t read_head, uint8_t read_tail);
int8_t buildAMON(PGM_P *param, PGM_P *quali, PGM_P *dtype, PGM_P *units, PGM_P *perid, t_measurement* the_measurements, uint8_t num_mmnt, uint8_t read_head, uint8_t read_tail);
public:
//teal_mallet_gsm(Client& aClient);
......@@ -82,7 +82,7 @@ public:
// send the hash and size headers, followed by a single reading
// returns the http status code
//int16_t sendOneReading(uint16_t _y, uint8_t _m, uint8_t _d, uint8_t _hh, uint8_t _mm, uint8_t _ss, double _float_reading, t_SensorType _sensor_type, bool send_to_serial = true);
int16_t sendReadings(PGM_P param, PGM_P quali, PGM_P dtype, PGM_P units, PGM_P perid, t_measurement* the_measurements, uint8_t read_head, uint8_t read_tail);
int16_t sendReadings(PGM_P *param, PGM_P *quali, PGM_P *dtype, PGM_P *units, PGM_P *perid, t_measurement* the_measurements, uint8_t num_mmnt, uint8_t read_head, uint8_t read_tail);
};
#endif
......@@ -35,8 +35,9 @@ state_machine_e g_current_state = SLEEP_IDLE;
t_SensorData last_reading_time;
#define ORDINAL_INTERVAL 1
#define NUM_MMNT 10 // measurement buffer length
#define NUM_MMNT 5 // measurement buffer length
// then create a pre-sized array of these structs to hold a set of timestamped measurements
uint8_t num_mmnt = NUM_MMNT;
t_measurement measurements[NUM_MMNT] = {};
uint8_t readings_head = 0;
......@@ -45,7 +46,7 @@ uint8_t readings_tail = 0;
// sends happen after a fixed number of ordinals
t_SensorData last_sending_time;
// how many readings should be collected for sending (<NUM_MMNT)
#define NUM_TO_SEND 1
#define NUM_TO_SEND 3
#if NUM_TO_SEND >= NUM_MMNT
#error "Can't hold all the readings, reduce NUM_TO_SEND below NUM_MMNT"
......@@ -181,15 +182,14 @@ t_SensorData get_reading(sensor_e target) {
}
void send_reading() {
#if DEBUG_MODE > 0
Serial.print(F("send_reading() at: "));
delay(100);
#endif
t_SensorData send_time = get_reading(TMP);
#if DEBUG_MODE > 0
Serial.print(F("sending "));
Serial.print((readings_head - readings_tail) % NUM_MMNT);
Serial.println(F(" readings"));
Serial.print(((readings_head + NUM_MMNT) - readings_tail) % NUM_MMNT);
Serial.print(F(" readings ("));
Serial.print(readings_tail);
Serial.write('–');
Serial.print(readings_head);
Serial.println(F(")"));
delay(100);
#endif
#if DEBUG_MODE > 0
......@@ -203,7 +203,7 @@ void send_reading() {
#if DEBUG_MODE > 0
Serial.println(F("sendReadings..."));
#endif
TM.sendReadings(param[0], quali[0], dtype[0], units[0], perid[0], measurements, readings_head, readings_tail);
TM.sendReadings(&param[0], &quali[0], &dtype[0], &units[0], &perid[0], measurements, num_mmnt, readings_head, readings_tail);
#if DEBUG_MODE > 0
Serial.println(F("endConnection..."));
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment