Skip to content

Instantly share code, notes, and snippets.

@BrianAdams
Last active August 29, 2015 14:06
Show Gist options
  • Save BrianAdams/d0ab0b660d7d4447c444 to your computer and use it in GitHub Desktop.
Save BrianAdams/d0ab0b660d7d4447c444 to your computer and use it in GitHub Desktop.
#include <AltSoftSerial.h> //Include the software serial library
#include <Wire.h>
const byte i2c_address=0x77;
AltSoftSerial altSerial; //Name the software serial library altSerial (this cannot be omitted)
int s0 = 7; //Arduino pin 7 to control pin S0
int s1 = 6; //Arduino pin 6 to control pin S1
char sensordata[30]; //A 30 byte character array to hold incoming data from the sensors
byte sensor_bytes_received=0; //We need to know how many characters bytes have been received
struct data
{
struct {
float ec;
float tds;
float sal;
float sg;
}
conductivity;
float pH;
float DO;
float ORP;
float temperature;
};
const int data_size=sizeof(data);
const int waiting_time = 1000;
data data_from_sensors = {
{
0, 0, 0, 0 }
,
0,
0,
0,
0
};
void setup() {
pinMode(s1, OUTPUT); //Set the digital pin as output.
pinMode(s0, OUTPUT); //Set the digital pin as output.
pinMode(2, OUTPUT); //Set pin 2 as an output
Serial.begin(38400); //Set the hardware serial port to 38400
altSerial.begin(38400); //Set the soft serial port to 38400
Wire.begin(i2c_address);
Wire.onRequest(requestEvent);
}
void loop(){
data_from_sensors.temperature = read_temp();
open_channel(0);
Serial.print("Conductivity==>");
altSerial.print("R\r");
leeBuffer();
const char *delimitador=",";
data_from_sensors.conductivity.ec=atof(strtok(sensordata,delimitador));
data_from_sensors.conductivity.tds=atof(strtok(NULL,delimitador));
data_from_sensors.conductivity.sal=atof(strtok(NULL,delimitador));
data_from_sensors.conductivity.sg=atof(strtok(NULL,delimitador));
open_channel(1);
Serial.print("pH==>");
altSerial.print("R\r");
leeBuffer();
data_from_sensors.pH=atof(sensordata);
open_channel(2);
Serial.print("DO==>");
altSerial.print("R\r");
leeBuffer();
data_from_sensors.DO=atof(sensordata);
open_channel(3);
Serial.print("ORP==>");
altSerial.print("R\r");
leeBuffer();
data_from_sensors.ORP=atof(sensordata);
Serial.println();
Serial.println(" DATA IN STRUCTURE ");
Serial.println("-------------------");
Serial.print("Conductividad==> EC: ");
Serial.print(data_from_sensors.conductivity.ec);
Serial.print(", TDS: ");
Serial.print(data_from_sensors.conductivity.tds);
Serial.print(", SAL: ");
Serial.print(data_from_sensors.conductivity.sal);
Serial.print(", SG: ");
Serial.println(data_from_sensors.conductivity.sg);
Serial.print("pH==> ");
Serial.println(data_from_sensors.pH);
Serial.print("DO==> ");
Serial.println(data_from_sensors.DO);
Serial.print("ORP==> ");
Serial.println(data_from_sensors.ORP);
// resto de datos de la estructura
Serial.print("Temperatura==> ");
Serial.println(data_from_sensors.temperature);
Serial.println("-------------------");
Serial.println(" DATA IN STRUCTURE END ");
Serial.println();
delay(5000);
}
void leeBuffer(){
delay(waiting_time);
if(altSerial.available() > 0){ //If data has been transmitted from an Atlas Scientific device
sensor_bytes_received=altSerial.readBytesUntil(13,sensordata,30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received
sensordata[sensor_bytes_received]=0; //we add a 0 to the spot in the array just after the last character we received. This will stop us from transmitting incorrect data that may have been left in the buffer
Serial.println(sensordata);
}
}
void open_channel(int channel){ //This function controls what UART port is opened.
if (channel & 1){
digitalWrite(s0, HIGH);
}
else {
digitalWrite(s0,LOW);
}
if (channel & 2){
digitalWrite(s1, HIGH);
}
else {
digitalWrite(s1,LOW);
}
delay(100);
int bytesResiduo=altSerial.available();
if(bytesResiduo > 0){
sensor_bytes_received=altSerial.readBytes(sensordata, bytesResiduo);
sensordata[bytesResiduo]=0;
Serial.print("ATENCION: DATOS NO LEIDOS: ");
Serial.println(sensordata);
}
}
float read_temp(void){ //the read temperature function
float v_out; //voltage output from temp sensor
float temp; //the final temperature is stored here
digitalWrite(A0, LOW); //set pull-up on analog pin
digitalWrite(2, HIGH); //set pin 2 high, this will turn on temp sensor
delay(2); //wait 2 ms for temp to stabilize
v_out = analogRead(0); //read the input pin
digitalWrite(2, LOW); //set pin 2 low, this will turn off temp sensor
v_out*=.0048; //convert ADC points to volts (we are using .0048 because this device is running at 5 volts)
v_out*=1000; //convert volts to millivolts
temp= 0.0512 * v_out -20.5128; //the equation from millivolts to temperature
return temp; //send back the temp
}
void requestEvent(){
byte *pDatos= (byte *) &data_from_sensors;
Wire.write(pDatos, data_size);
}
Arduslave.h
#ifndef __ArduSlave_H_
#define __ArduSlave_H_
#include <Arduino.h>
#include "Device.h"
struct data
{
struct {
float ec;
float tds;
float sal;
float sg;
}
conductivity;
float pH;
float DO;
float ORP;
float temperature;
};
const int data_size=sizeof(data);
const unsigned arduslave_timeout=100;
class ArduSlave : public Device {
public:
ArduSlave():Device(){};
void device_setup();
void device_loop(Command cmd);
};
#endif
arduslave.cpp:
#include "AConfig.h"
#if(HAS_ARDUSLAVE)
#include "ArduSlave.h"
// #include "Settings.h"
//#include "Timer.h"
#include <Wire.h>
/*
Sketch to read a MS5803-14BA pressure sensor, written from scratch.
Will output data to the serial console.
Written by Walt Holm
Initial revision 10 Oct 2013
Rev 1 12 Oct 2013 -- Implements 2nd order temperature compensation
*/
const int DevAddress = ARDUSLAVE_I2CADDRESS; // 7-bit I2C address of the MS5803
void ArduSlave::device_setup(){
//Settings::capability_bitarray |= (1 DEAPTH_CAPABLE);
Serial.println("ArduSlave setup.");
Wire.begin();
delay(10);
}
void ArduSlave::device_loop(Command command){
Wire.requestFrom(DevAddress, 32);
data data_from_arduslave;
unsigned int millis_start = millis();
byte *pBdata = (byte *) &data_from_arduslave;
for (int i=0; i<data_size; i++){
if (Wire.available()){
pBdata[i]=Wire.read();
}
else {
if (((unsigned int)millis() - millis_start) > arduslave_timeout) {
Serial.println("log:Failed to read ArduSlave from I2C");
return;
}
}
}
Serial.println();
Serial.println(" DATA IN ARDUSLAVE ");
Serial.println();
Serial.print("Conductividad==> EC: ");
Serial.print(data_from_arduslave.conductivity.ec);
Serial.print(", TDS: ");
Serial.print(data_from_arduslave.conductivity.tds);
Serial.print(", SAL: ");
Serial.print(data_from_arduslave.conductivity.sal);
Serial.print(", SG: ");
Serial.println(data_from_arduslave.conductivity.sg);
Serial.print("pH==> ");
Serial.println(data_from_arduslave.pH);
Serial.print("DO==> ");
Serial.println(data_from_arduslave.DO);
Serial.print("ORP==> ");
Serial.println(data_from_arduslave.ORP);
// resto de datos de la estructura
Serial.print("Temperatura==> ");
Serial.println(data_from_arduslave.temperature);
Serial.println();
Serial.println(" DATA IN ARDUSLAVE END ");
Serial.println();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment