Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PrawiraGenestonlia committed Nov 1, 2018
1 parent 27718c4 commit e4b1836
Show file tree
Hide file tree
Showing 6 changed files with 689 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Archives/CNDH/CNDH.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define DEBUG 1
//#define LOCAL_STORAGE 1
#define LOCAL_STORAGE 1

#include <Wire.h>
#include "AS726X.h"
Expand Down
91 changes: 59 additions & 32 deletions CNDH_DEMO_CSV/CNDH_DEMO_CSV.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
#include "SparkFun_I2C_GPS_Arduino_Library.h"
#include <TinyGPS++.h>
#endif
#include <QwiicMux0x49.h>
#include <QwiicMux.h>
#include <QwiicMux0x10.h>
#include <SdFat.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <CSVFile.h>
//#include <CSVFile.h>

#define PIN_SPI_MOSI 11
#define PIN_SPI_MISO 12
#define PIN_SPI_CLK 13
#define PIN_SD_CS 4
#define PIN_OTHER_DEVICE_CS 8
#define SD_CARD_SPEED SPI_FULL_SPEED
#define FILENAME "DEMO.csv"
//#define FILENAME "data.csv"

AS726X SpectralSensor;
SdFat SD;
File myFile;
CSVFile csv;
//CSVFile csv;
RF24 radio(7, 8); // CE, CSN
#ifdef GPS 1
TinyGPSPlus gps; //Declare gps object
Expand All @@ -44,7 +44,7 @@ void setup() {
//Disable SPI devices
pinMode(PIN_SD_CS, OUTPUT);
digitalWrite(PIN_SD_CS, HIGH);

#if PIN_OTHER_DEVICE_CS > 0
pinMode(PIN_OTHER_DEVICE_CS, OUTPUT);
digitalWrite(PIN_OTHER_DEVICE_CS, HIGH);
Expand All @@ -53,9 +53,12 @@ void setup() {
Serial.begin(115200);
pinMode(A1, OUTPUT);
Wire.begin();
enableMuxPort4(9);
enableMuxPort(1);
SpectralSensor.begin();
disableMuxPort(1);
enableMuxPort(2);
SpectralSensor.begin();
disableMuxPort4(9);
disableMuxPort(2);
#ifdef GPS 1
enableMuxPort1(0);
if (myI2CGPS.begin() == false) {//Checks for succesful initialization of GPS
Expand All @@ -69,17 +72,18 @@ void setup() {
Serial.println("SD card begin error");
return;
}
csv.open(FILENAME, O_RDWR | O_CREAT);
csv.close();
// csv.open(FILENAME, O_RDWR | O_CREAT);
// csv.close();
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
sd_write("this program is initialized \n");
Serial.println("The program is successfully initialized");
}

void loop() {
initSdFile();
// initSdFile();
update_output();
// Serial.print(output_string);

Expand All @@ -88,24 +92,29 @@ void loop() {
output_string.toCharArray(buff, 32);
radio.write(&buff, sizeof(buff));
Serial.print(buff);
csv.gotoBeginOfLine();
csv.addField(buff);
csv.addLine();
csv.close();

// csv.gotoBeginOfLine();
// csv.addField(buff);
// csv.addLine();
// csv.close();
delay(200);
}

void initSdFile() {
if (SD.exists(FILENAME) && !SD.remove(FILENAME))
{
Serial.println("Failed init remove file");
return;
}
if (!csv.open(FILENAME, O_RDWR | O_CREAT)) {
Serial.println("Failed open file");
}
sd_write(buff);
transmit_to_SAMD(buff);

}

//void initSdFile() {
// if (SD.exists(FILENAME) && !SD.remove(FILENAME))
// {
// Serial.println("Failed init remove file");
// return;
// }
// if (!csv.open(FILENAME, O_RDWR | O_CREAT)) {
// Serial.println("Failed open file");
// }
//}

void update_output() {
output_string = "$;";
#ifdef GPS 1
Expand All @@ -126,23 +135,29 @@ void update_output() {
delay(50);
output_string += ";";
#endif
enableMuxPort4(9);
enableMuxPort(1);
SpectralSensor.takeMeasurements();
delay(10);
output_string += SpectralSensor.getTemperature();
output_string += ";";
output_string += SpectralSensor.getCalibratedR();
disableMuxPort4(9);
disableMuxPort(1);
enableMuxPort(2);
SpectralSensor.takeMeasurements();
delay(10);
output_string += ";";
output_string += SpectralSensor.getCalibratedR();
disableMuxPort(2);
output_string += ";#";
output_string += "\n";
}

void sd_write(String info) {
myFile = SD.open("24102018.txt", FILE_WRITE);
myFile = SD.open("data.txt", FILE_WRITE);
if (myFile) {
digitalWrite(A1, HIGH);
Serial.print("Writing to 24102018.txt...");
myFile.println(info);
Serial.print("Writing to data.txt...");
myFile.print(info);
// close the file:
myFile.close();
Serial.println("done.");
Expand All @@ -154,9 +169,9 @@ void sd_write(String info) {
}

void sd_read() {
myFile = SD.open("24102018.txt");
myFile = SD.open("data.txt");
if (myFile) {
Serial.println("24102018.txt:");
Serial.println("data.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Expand All @@ -171,7 +186,7 @@ void sd_read() {
}

void sd_read_lastline() {
myFile = SD.open("24102018.txt");
myFile = SD.open("data.txt");
Serial.print("This is the last line: ");
while (true) {
cr = myFile.read();
Expand All @@ -184,3 +199,15 @@ void sd_read_lastline() {
myFile.close();
}

void transmit_to_SAMD(String info){
enableMuxPort(7);
Wire.beginTransmission(7); // transmit to device #9

for (int i=0;i<32;i++){
Wire.write(char(info[i]));
}
// Wire.write("HELLO"); // sends x
Wire.endTransmission(); // stop transmitting
disableMuxPort(7);
}

154 changes: 154 additions & 0 deletions CNDH_SAMB_BOARD_CLIENT/CNDH_SAMB_BOARD_CLIENT.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Both the TX and RX ProRF boards will need a wire antenna. We recommend a 3" piece of wire.
This example is a modified version of the example provided by the Radio Head
Library which can be found here:
www.github.com/PaulStoffregen/RadioHeadd
*/
#define GPS 1
#include <SPI.h>
#include <Wire.h>
//Radio Head Library:
#include <RH_RF95.h>
#ifdef GPS 1
#include <QwiicMux0x10.h>
#include "SparkFun_I2C_GPS_Arduino_Library-modified.h"
#include <TinyGPS++.h>
#endif

#ifdef GPS 1
TinyGPSPlus gps; //Declare gps object
I2CGPS myI2CGPS; //Hook object to the library
#endif

// We need to provide the RFM95 module's chip select and interrupt pins to the
// rf95 instance below.On the SparkFun ProRF those pins are 12 and 6 respectively.
RH_RF95 rf95(12, 6);

int LED = 13; //Status LED is on pin 13
String str ="";
char strchar[32]={};
int packetCounter = 0; //Counts the number of packets sent
long timeSinceLastPacket = 0; //Tracks the time stamp of last packet received

// The broadcast frequency is set to 921.2, but the SADM21 ProRf operates
// anywhere in the range of 902-928MHz in the Americas.
// Europe operates in the frequencies 863-870, center frequency at 868MHz.
// This works but it is unknown how well the radio configures to this frequency:
//float frequency = 864.1;
float frequency = 921.2; //Broadcast frequency

String output_string = "";
const byte address[6] = "00001";
char buff[32];
char cr;
int incomingByte = 0; // for incoming serial data

void setup()
{
pinMode(LED, OUTPUT);
Wire.begin(7);
Wire.onReceive(receiveEvent);
SerialUSB.begin(115200);
// It may be difficult to read serial messages on startup. The following line
// will wait for serial to be ready before continuing. Comment out if not needed.
while (!SerialUSB);
SerialUSB.println("RFM Client!");

#ifdef GPS 1
enableMuxPort1(0);
if (myI2CGPS.begin() == false) {//Checks for succesful initialization of GPS
SerialUSB.println("Module failed to respond. Please check wiring.");
while (1); //Freeze!
}
disableMuxPort1(0);
#endif
update_output();
SerialUSB.print(output_string);
//Initialize the Radio.
if (rf95.init() == false) {
SerialUSB.println("Radio Init Failed - Freezing");
while (1);
}
else {
//An LED inidicator to let us know radio initialization has completed.
SerialUSB.println("Transmitter up!");
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
}

// Set frequency
rf95.setFrequency(frequency);

// Transmitter power can range from 14-20dbm.
rf95.setTxPower(14, true);
}


void loop()
{
SerialUSB.println("Sending message");

//Send a message to the other radio
uint8_t toSend[] = "Hi there!";
//sprintf(toSend, "Hi, my counter is: %d", packetCounter++);
rf95.send(toSend, sizeof(toSend));
rf95.waitPacketSent();

// Now wait for a reply
byte buf[RH_RF95_MAX_MESSAGE_LEN];
byte len = sizeof(buf);

if (rf95.waitAvailableTimeout(2000)) {
// Should be a reply message for us now
if (rf95.recv(buf, &len)) {
SerialUSB.print("Got reply: ");
SerialUSB.println((char*)buf);
//SerialUSB.print(" RSSI: ");
//SerialUSB.print(rf95.lastRssi(), DEC);
}
else {
SerialUSB.println("Receive failed");
}
}
else {
SerialUSB.println("No reply, is the receiver running?");
}
delay(500);
}

void update_output() {
output_string = "$;";
#ifdef GPS 1
enableMuxPort1(0);
while (myI2CGPS.available()) { //available() returns the number of new bytes available from the GPS module
gps.encode(myI2CGPS.read()); //Feed the GPS parser
}
output_string += gps.date.day();
output_string += "/";
output_string += gps.date.month();
output_string += "/";
output_string += gps.date.year();
output_string += ";";
output_string += gps.location.lat(); //GPS latitude
output_string += ";";
output_string += gps.location.lng(); //GPS longitude
disableMuxPort1(0);
#endif
delay(50);

output_string += ";#";
output_string += "\n";
}


void receiveEvent(int howMany)
{
while(Wire.available()){
str=Wire.read();
SerialUSB.print(str);
}
SerialUSB.println();

}
Loading

0 comments on commit e4b1836

Please sign in to comment.