GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
Revision 0:fcab3b154e49, committed 2014-11-11
- Comitter:
- Trumple
- Date:
- Tue Nov 11 17:33:41 2014 +0000
- Child:
- 1:27b35752c5d0
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue Nov 11 17:33:41 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/mbed/code/SDFileSystem/#7b35d1709458
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Nov 11 17:33:41 2014 +0000
@@ -0,0 +1,116 @@
+#include "mbed.h"
+#include "snail.h"
+#include "sensorinterface.h"
+#include "sdcard.h"
+
+#define DEBUG
+
+time_t lastPollTime = 0;
+time_t pollInterval = 30;
+
+bool isBasenode = false;
+
+Serial pc(USBTX, USBRX);
+
+char* getBasenodeAddress(snail* xbee)
+{
+ snail::baseaddressrequest request;
+ xbee->send(request);
+
+ //TODO: only continue if type is base address reply
+ while(!xbee->isMessageWaiting())
+ {}
+
+ //snail::message reply = xbee->readMessage();
+
+ return "";
+}
+
+int main()
+{
+ #ifdef DEBUG
+ pc.printf("[MAIN] Starting up...\r\n");
+ printf(" . .\r\n");
+ printf(" / `. .' \\\r\n");
+ printf(" .---. < > < > .---.\r\n");
+ printf(" | \\ \\ - ~ ~ - / / |\r\n");
+ printf(" ~-..-~ ~-..-~\r\n");
+ printf(" \\~~~\\.' `./~~~/\r\n");
+ printf(" .-~~^-. \\__/ \\__/\r\n");
+ printf(" .' O \\ / / \\ \\\r\n");
+ printf("(_____, `._.' | } \\/~~~/\r\n");
+ printf(" `----. / } | / \\__/\r\n");
+ printf(" `-. | / | / `. ,~~|\r\n");
+ printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n");
+ printf(" | / | / ~-. `-. _||_||_\r\n");
+ printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n");
+ printf("\r\n");
+ printf("\r\n");
+ printf(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
+ printf("\r\n");
+ printf("\r\n");
+ #endif
+
+ sensorinterface sensors = sensorinterface();
+ sdcard sd = sdcard();
+ snail xbee = snail();
+
+ //TODO: read basenode pin
+ #ifdef DEBUG
+ pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode);
+ #endif
+
+ time_t timestamp = 0;
+ //if (isBasenode)
+ //TODO: get timestamp from API
+ //TODO: be ready to broadcast as the basenode
+ //else
+ //TODO: request timestamp from base node
+ //TODO: find out who basenode is
+
+ set_time(timestamp);
+
+ //TODO: load configuration from SD
+
+ if (isBasenode)
+ while(1)
+ {
+ #ifdef DEBUG
+ pc.printf("[MAIN] Basenode is idle\r\n");
+ #endif
+
+ wait(5);
+ }
+ else
+ while(1)
+ {
+ //if xbee interrupt has woken us up
+ //transmit 10 latest readings
+
+ //check if it's time to poll TODO: add check to see if sensorinterface is ready
+ if (time(NULL) - lastPollTime > 10)
+ {
+ #ifdef DEBUG
+ pc.printf("[MAIN] Requesting data...\r\n");
+ #endif
+ sensors.requestData();
+ lastPollTime = time(NULL);
+ }
+
+ //if there is data waiting for us...
+ if (sensors.isDataWaiting())
+ {
+ #ifdef DEBUG
+ pc.printf("[MAIN] Data waiting, reading data...\r\n");
+ #endif
+ d_reply data = sensors.readData();
+ #ifdef DEBUG
+ for (int i = 0; i < data.readings.size(); i++)
+ pc.printf("0x%.4X|", data.readings[i]);
+ #endif
+ //log
+ sd.write(static_cast<long int>(time(NULL)), data);
+ }
+
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Nov 11 17:33:41 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sdcard.cpp Tue Nov 11 17:33:41 2014 +0000
@@ -0,0 +1,47 @@
+#include <string>
+#include "sdcard.h"
+#include <map>
+
+sdcard::sdcard(): sd(p5, p6, p7, p8, "sd")
+{
+ typeLookup[0x00] = "soil_con";
+ typeLookup[0x01] = "soil_temp";
+ typeLookup[0x02] = "amb_temp";
+
+ //first write sometimes fails, let's ensure the first write isn't real data
+ FILE *fp = fopen("/sd/boot", "w");
+ fprintf(fp, "boot");
+ fclose(fp);
+}
+
+int sdcard::write(long int time, d_reply data)
+{
+
+ string sd_name = "/sd/";
+ string sd_location ="";
+ char time_s[64];
+
+ string l_type = typeLookup[data.type];
+
+ sd_location = sd_name + l_type;
+ mkdir(sd_location.c_str(), 0777);
+
+ sprintf(time_s, "%d", time);
+ sd_location += "/" + string(time_s) + ".txt";
+
+ FILE *fp = fopen(sd_location.c_str(), "w");
+
+ if (fp == NULL)
+ {
+ printf("File pointer null, failed to open file\r\n");
+ }
+
+ string serializedData;
+
+ for (int i = 0; i < data.readings.size(); i++)
+ fprintf(fp, "%i", data.readings[i]);
+
+ fclose(fp);
+
+ return 1;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sdcard.h Tue Nov 11 17:33:41 2014 +0000
@@ -0,0 +1,12 @@
+#include "SDFileSystem.h"
+#include "sensorinterface.h"
+
+class sdcard
+{
+ public:
+ sdcard(); // MOSI, MISO, CKL, CS -> PINS 2, 7, 5, 1, ON SD CARD WITH 4=VCC, 6=GND, 8,9=NC
+ int write(long int time_holder, d_reply data);
+ private:
+ map<char,string> typeLookup;
+ SDFileSystem sd;
+};
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sensorinterface.cpp Tue Nov 11 17:33:41 2014 +0000
@@ -0,0 +1,200 @@
+#include "sensorinterface.h"
+
+sensorinterface::sensorinterface() : i2c(p28,p27), readyLine(p30)
+{
+ readyLine.fall(this, &sensorinterface::readyTrigger);
+
+ this->ready = false;
+
+ char buffer[255];
+
+ for (char i=1; i<=254; i=i+2)
+ {
+ //last bit determines read/write not actual address
+ if (this->i2c.read(i, &buffer[0], 1) ==0)
+ {
+ //returns 0 if ack i.e. a device is found
+ sensor newSensor;
+ newSensor.type = findType(i);
+ this->sensors[i] = newSensor;
+ #ifdef DEBUG
+ printf("[SIF] Device found at 0x%.2X, device count: %i\r\n", i, this->sensors.size());
+ #endif
+ }
+ }
+}
+
+bool sensorinterface::isDataWaiting()
+{
+ return this->ready;
+}
+
+void sensorinterface::requestData()
+{
+ currentSensor = sensors.begin();
+
+ #ifdef DEBUG
+ printf("[SIF] Beginning device query chain from 0x%.2X\r\n", currentSensor->first);
+ #endif
+
+ sendRequest(currentSensor->first);
+}
+
+void sensorinterface::readyTrigger()
+{
+ this->ready = true;
+}
+
+#ifdef DEBUG
+void sensorinterface::error(int e_num)
+{
+ printf("[SIF] Error %i\r",e_num);
+}
+#endif
+
+int sensorinterface::update()
+{
+ char buffer[255];
+
+ for (char i=1; i<=254; i=i+2)
+ {
+ //last bit determines read/write not actual address
+
+ bool connected = (this->i2c.read(i, &buffer[0], 1) == 0);
+ bool registered = (this->sensors.count(i) > 0);
+
+ if (connected)
+ {
+ if (!registered)
+ {
+ //device added
+ #ifdef DEBUG
+ printf("[SIF] Device added at %i\r\n", i);
+ #endif
+ sensor newSensor;
+ newSensor.type = findType(i);
+ this->sensors[i] = newSensor;
+ }
+ }
+ else if (registered)
+ {
+ if (!connected)
+ {
+ //device removed
+ #ifdef DEBUG
+ printf("[SIF] Device removed at %i\r\n", i);
+ #endif
+ this->sensors.erase(i);
+ }
+ }
+ }
+
+ return 1;
+}
+
+
+char sensorinterface::findType(int address)
+{
+ char temp[3];
+ char cmd[1] = {0x54};
+
+ this->i2c.write(address, cmd, 1);
+ int type_timeout = 10;
+ i2c.stop();
+ i2c.start();
+ while( (this->i2c.read(address, temp, 3)) && (type_timeout) )
+ {
+ --type_timeout;
+ if (type_timeout == 2)
+ {
+ #ifdef DEBUG
+ error(3);
+ #endif
+ return 1;
+ }
+ }
+
+ if (temp[0] != 'T')
+ {
+ #ifdef DEBUG
+ error(1);
+ #endif
+ }
+ else
+ {
+ //seperate the incoming bits
+ if ((temp[1] != 0x00) && (temp[1] != 0x01) && (temp[1] != 0x02))
+ {
+ //if its not a recognised type
+ #ifdef DEBUG
+ error(2);
+ #endif
+ }
+ }
+ return temp[1];
+}
+
+int sensorinterface::sendRequest(char address)
+{
+ char cmd[1] = {0x52};
+
+ this->i2c.write(address, cmd, 1);
+
+ return 1;
+}
+
+d_reply sensorinterface::readData()
+{
+ #ifdef DEBUG
+ printf("[SIF] Reading data from current device (0x%.2X)\r\n", currentSensor->first);
+ #endif
+ char address = currentSensor->first;
+ char cmd[1] = {0x44};
+ char buffer [18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ this->i2c.write(address, cmd, 1);
+ wait(0.01);
+ this->i2c.read(address, buffer, 18);
+
+ d_reply reply;
+
+ reply.type = currentSensor->second.type;
+
+ for (int i = 2; i <= 17; i += 2)
+ {
+ uint16_t reading = buffer[i];
+ reading = reading << 8;
+ reading |= buffer[i+1];
+ reply.readings.push_back(reading);
+ }
+
+ if (buffer[0] != 'D')
+ {
+ //if it doesn't send the correct header bit, something has gone wrong
+ #ifdef DEBUG
+ error(0);
+ #endif
+ }
+ else
+ {
+
+ }
+
+ this->ready = false;
+
+ if (currentSensor != sensors.end() && sensors.size() > 1 )
+ {
+ #ifdef DEBUG
+ printf("[SIF] Continuing chain of devices, just read from device: 0x%.2X\r\n", currentSensor->first);
+ #endif
+ currentSensor++;
+ sendRequest(currentSensor->first);
+ }
+ #ifdef DEBUG
+ else
+ {
+ printf("[SIF] All devices have been read from, end of chain\r\n");
+ }
+ #endif
+
+ return reply;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sensorinterface.h Tue Nov 11 17:33:41 2014 +0000
@@ -0,0 +1,45 @@
+#ifndef SENSORINTERFACE_H
+
+#define SENSORINTERFACE_H
+
+#include "mbed.h"
+#include <map>
+#include <vector>
+
+#define DEBUG
+
+struct sensor
+{
+ char type;
+ int interval;
+};
+
+
+struct d_reply
+{
+ char type;
+ vector<uint16_t> readings;
+};
+
+
+class sensorinterface
+{
+ public:
+ sensorinterface();
+ void requestData();
+ bool isDataWaiting();
+ d_reply readData();
+ private:
+ map<char, sensor> sensors;
+ map<char, sensor>::iterator currentSensor;
+ int update();
+ void error(int);
+ char findType(int address);
+ int sendRequest(char address);
+ void readyTrigger();
+ bool ready;
+
+ I2C i2c;
+ InterruptIn readyLine;
+};
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snail.lib Tue Nov 11 17:33:41 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/Trumple/code/snail/#0b79538c5e5a
