Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <GSM.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_LSM303_U.h>
- #include <Adafruit_L3GD20_U.h>
- #include <Adafruit_9DOF.h>
- #include <Adafruit_BMP280.h>
- #include <SPI.h>
- #include <DAC_MCP49xx.h>
- // The Arduino pin used for the slave select / chip select
- #define SS_PIN 10
- #define BMP_SCK (13)
- #define BMP_MISO (12)
- #define BMP_MOSI (11)
- #define BMP_CS (10)
- #define turbSensor A3
- #define phSensor A6
- #define uvSensor A7
- #define batVolt A2
- #define VBAT A8
- #define VSOL A9
- #define ISOL A10
- #define interupt1 19
- #define interupt2 20
- #define conv_en 18
- #define bat_en 17
- #define mppt 16
- // Data wire is plugged into digital pin 4 on the Arduino
- #define ONE_WIRE_BUS 4
- #define BOUYID 111
- // PIN Number
- #define PINNUMBER "4908"
- // APN data
- #define GPRS_APN "web.htgpr" // replace your GPRS APN
- #define GPRS_LOGIN "38591" // replace with your GPRS login
- #define GPRS_PASSWORD "38591" // replace with your GPRS password
- //Set up the DAC
- DAC_MCP49xx dac(DAC_MCP49xx::MCP4911, SS_PIN);
- Adafruit_BMP280 bmp; // I2C
- /* Assign a unique ID to the sensors */
- Adafruit_9DOF dof = Adafruit_9DOF();
- Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
- Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
- /* Update this with the correct SLP for accurate altitude measurements */
- float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
- sensors_event_t accel_event;
- sensors_event_t mag_event;
- sensors_vec_t orientation;
- // Setup a oneWire instance to communicate with any OneWire devices
- // (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- // initialize the library instance
- GSMClient client;
- GPRS gprs;
- GSM gsmAccess;
- // URL, path & port (for example: arduino.cc)
- char server[] = "https://smart-bouy-timely-bonobo.cfapps.io";
- char path[] = "/bouyendpoint";
- int port = 443; // port 80 is the default for HTTP
- int timercnt = 0;
- bool send_measurements = false;
- int varray[2];
- int iarray[2];
- float readAnalogSensorVoltage(short analogPin);
- void initI2CSensors();
- void imu_sensor();
- void bmp280(short* temp, short* pressure);
- float readTemperatureSensor();
- void sendShort(short value);
- void sendFloat(float value);
- void sendMeasurements();
- void runMPPT();
- void setup() {
- Serial.begin(9600);
- while (!Serial) {
- ;
- }
- boolean notConnected = true;
- while (notConnected) {
- if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
- (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
- notConnected = false;
- } else {
- delay(1000);
- }
- }
- if (client.connect(server, port)) {
- // Make a HTTP request:
- client.print("GET ");
- client.print(path);
- client.println(" HTTP/1.1");
- client.print("Host: ");
- client.println(server);
- client.println("Connection: close");
- client.println();
- }
- initI2CSensors();
- sensors.begin();
- attachInterrupt(digitalPinToInterrupt(interupt1),interuptroutine,CHANGE);
- attachInterrupt(digitalPinToInterrupt(interupt2),interuptroutine,CHANGE);
- pinMode(conv_en, OUTPUT);
- pinMode(bat_en, OUTPUT);
- cli(); // disable global interrupts
- TCCR1A = 0; // set entire TCCR1A register to 0
- TCCR1B = 0; // set entire TCCR1A register to 0
- // enable Timer1 overflow interrupt:
- bitSet(TIMSK1, TOIE1);
- // preload timer 65536 - (16000000 / 1024 / 0.25)
- TCNT1 = 65536 - (16000000/1024)/0.25;
- // set 1024 prescaler
- bitSet(TCCR1B, CS12);
- bitSet(TCCR1B, CS10);
- sei(); // enable all interrupts
- for (int i=0;i<2;i++){
- varray[i] = readAnalogSensorVoltage(VSOL);
- iarray[i] = readAnalogSensorVoltage(ISOL);
- }
- }
- ISR(Timer1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
- {
- timercnt++;
- if (timercnt==900){
- send_measurements = true;
- timercnt = 0;
- }
- TCNT1 = 65536 - (16000000/1024)/0.25;
- }
- void loop() {
- if (send_measurements){
- sendMeasurements();
- send_measurements=false;
- }
- if (digitalRead(conv_en)==HIGH) {
- runMPPT();
- }
- }
- void sendMeasurements(){
- if (client.available()) {
- short reading1, reading2;
- client.print("{\"id\":");
- client.print(BOUYID);
- client.print(",\"d\":{[");
- reading1 = readAnalogSensorVoltage(turbSensor);
- sendShort(reading1);
- client.print(",");
- reading1 = readAnalogSensorVoltage(phSensor);
- sendShort(reading1);
- client.print(",");
- reading1 = readAnalogSensorVoltage(uvSensor);
- sendShort(reading1);
- client.print(",");
- reading1 = readAnalogSensorVoltage(batVolt);
- sendShort(reading1);
- client.print(",");
- imu_sensor();
- sendFloat(orientation.roll);
- client.print(",");
- sendFloat(orientation.pitch);
- client.print(",");
- sendFloat(orientation.heading);
- client.print(",");
- bmp280(&reading1,&reading2);
- sendShort(reading1);
- client.print(",");
- sendShort(reading2);
- client.print(",");
- float temp = readTemperatureSensor();
- sendFloat(temp);
- client.print("]}}");
- }
- if (!client.available() && !client.connected()) {
- client.stop();
- }
- }
- void runMPPT(){
- int vsol = readAnalogSensorVoltage(VSOL);
- int isol = readAnalogSensorVoltage(ISOL);
- int a = (varray[0]-varray[1])/(iarray[0]-iarray[1]);
- a -= (varray[1]-vsol)/(iarray[1]-isol);
- a /= iarray[0] + isol;
- int b = (varray[0]-varray[1])/(iarray[0]-iarray[1]) - a*(iarray[0]+iarray[1]);
- short imax = (short) -b/(2*a);
- dac.output(imax);
- varray[0] = varray[1];
- varray[1] = vsol;
- iarray[0] = iarray[1];
- iarray[1] = isol;
- }
- void sendShort(short value){
- client.print("{\"v\":");
- client.print(value);
- client.print("}");
- }
- void sendFloat(float value){
- client.print("{\"v\":");
- client.print(value);
- client.print("}");
- }
- short readAnalogSensorVoltage(int analogPin){
- return analogRead(analogPin);
- }
- void initI2CSensors()
- {
- if(!accel.begin())
- {
- while(1);
- }
- if(!mag.begin())
- {
- while(1);
- }
- if (!bmp.begin()) {
- while (1);
- }
- /* Default settings from datasheet. */
- bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
- Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
- Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
- Adafruit_BMP280::FILTER_X16, /* Filtering. */
- Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
- }
- void imu_sensor(){
- accel.getEvent(&accel_event);
- dof.accelGetOrientation(&accel_event, &orientation);
- mag.getEvent(&mag_event);
- dof.magTiltCompensation(SENSOR_AXIS_Z, &mag_event, &accel_event);
- dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation);
- }
- void bmp280(short* temp, short* pressure){
- *temp = bmp.readTemperature();
- *pressure = bmp.readPressure();
- }
- float readTemperatureSensor(){
- sensors.requestTemperatures();
- return sensors.getTempCByIndex(0);
- }
- void interuptroutine()
- {
- if (interupt1==HIGH){
- digitalWrite(conv_en,HIGH);
- } else {
- digitalWrite(conv_en,LOW);
- }
- if (interupt1==HIGH && interupt2==HIGH){
- digitalWrite(bat_en,LOW);
- } else {
- digitalWrite(bat_en,HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement