Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.51 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <GSM.h>
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5. #include <Adafruit_Sensor.h>
  6. #include <Adafruit_LSM303_U.h>
  7. #include <Adafruit_L3GD20_U.h>
  8. #include <Adafruit_9DOF.h>
  9. #include <Adafruit_BMP280.h>
  10. #include <SPI.h>
  11. #include <DAC_MCP49xx.h>
  12.  
  13.  
  14. // The Arduino pin used for the slave select / chip select
  15. #define SS_PIN 10
  16. #define BMP_SCK  (13)
  17. #define BMP_MISO (12)
  18. #define BMP_MOSI (11)
  19. #define BMP_CS   (10)
  20.  
  21. #define turbSensor A3
  22. #define phSensor A6
  23. #define uvSensor A7
  24. #define batVolt A2
  25. #define VBAT A8
  26. #define VSOL A9
  27. #define ISOL A10
  28. #define interupt1 19
  29. #define interupt2 20
  30. #define conv_en 18
  31. #define bat_en 17
  32. #define mppt 16
  33.  
  34. // Data wire is plugged into digital pin 4 on the Arduino
  35. #define ONE_WIRE_BUS 4
  36.  
  37. #define BOUYID 111
  38.  
  39. // PIN Number
  40. #define PINNUMBER "4908"
  41.  
  42. // APN data
  43. #define GPRS_APN       "web.htgpr" // replace your GPRS APN
  44. #define GPRS_LOGIN     "38591"    // replace with your GPRS login
  45. #define GPRS_PASSWORD  "38591" // replace with your GPRS password
  46.  
  47.  
  48. //Set up the DAC
  49. DAC_MCP49xx dac(DAC_MCP49xx::MCP4911, SS_PIN);
  50.  
  51. Adafruit_BMP280 bmp; // I2C
  52. /* Assign a unique ID to the sensors */
  53. Adafruit_9DOF                dof   = Adafruit_9DOF();
  54. Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
  55. Adafruit_LSM303_Mag_Unified   mag   = Adafruit_LSM303_Mag_Unified(30302);
  56.  
  57. /* Update this with the correct SLP for accurate altitude measurements */
  58. float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
  59.  
  60. sensors_event_t accel_event;
  61. sensors_event_t mag_event;
  62. sensors_vec_t   orientation;
  63.  
  64. // Setup a oneWire instance to communicate with any OneWire devices
  65. // (not just Maxim/Dallas temperature ICs)
  66. OneWire oneWire(ONE_WIRE_BUS);
  67.  
  68. // Pass our oneWire reference to Dallas Temperature.
  69. DallasTemperature sensors(&oneWire);
  70.  
  71. // initialize the library instance
  72. GSMClient client;
  73. GPRS gprs;
  74. GSM gsmAccess;
  75.  
  76. // URL, path & port (for example: arduino.cc)
  77. char server[] = "https://smart-bouy-timely-bonobo.cfapps.io";
  78. char path[] = "/bouyendpoint";
  79. int port = 443; // port 80 is the default for HTTP
  80.  
  81. int timercnt = 0;
  82.  
  83. bool send_measurements = false;
  84.  
  85. int varray[2];
  86. int iarray[2];
  87.  
  88.  
  89. float readAnalogSensorVoltage(short analogPin);
  90.  
  91. void initI2CSensors();
  92.  
  93. void imu_sensor();
  94.  
  95. void bmp280(short* temp, short* pressure);
  96.  
  97. float readTemperatureSensor();
  98.  
  99. void sendShort(short value);
  100.  
  101. void sendFloat(float value);
  102.  
  103. void sendMeasurements();
  104.  
  105. void runMPPT();
  106.  
  107.  
  108.  
  109. void setup() {
  110.   Serial.begin(9600);
  111.   while (!Serial) {
  112.     ;
  113.   }
  114.   boolean notConnected = true;
  115.  
  116.   while (notConnected) {
  117.     if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
  118.         (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
  119.       notConnected = false;
  120.     } else {
  121.       delay(1000);
  122.     }
  123.   }
  124.   if (client.connect(server, port)) {
  125.     // Make a HTTP request:
  126.     client.print("GET ");
  127.     client.print(path);
  128.     client.println(" HTTP/1.1");
  129.     client.print("Host: ");
  130.     client.println(server);
  131.     client.println("Connection: close");
  132.     client.println();
  133.   }
  134.  
  135.   initI2CSensors();
  136.  
  137.   sensors.begin();
  138.  
  139.   attachInterrupt(digitalPinToInterrupt(interupt1),interuptroutine,CHANGE);
  140.   attachInterrupt(digitalPinToInterrupt(interupt2),interuptroutine,CHANGE);
  141.  
  142.   pinMode(conv_en, OUTPUT);
  143.   pinMode(bat_en, OUTPUT);
  144.  
  145.   cli();         // disable global interrupts
  146.   TCCR1A = 0;    // set entire TCCR1A register to 0
  147.   TCCR1B = 0;    // set entire TCCR1A register to 0
  148.  
  149.   // enable Timer1 overflow interrupt:
  150.   bitSet(TIMSK1, TOIE1);
  151.   // preload timer 65536 - (16000000 / 1024 / 0.25)
  152.   TCNT1 = 65536 - (16000000/1024)/0.25;
  153.  
  154.   // set 1024 prescaler
  155.   bitSet(TCCR1B, CS12);
  156.   bitSet(TCCR1B, CS10);
  157.  
  158.   sei(); // enable all interrupts
  159.  
  160.   for (int i=0;i<2;i++){
  161.     varray[i] = readAnalogSensorVoltage(VSOL);
  162.     iarray[i] = readAnalogSensorVoltage(ISOL);
  163.   }
  164. }
  165.  
  166. ISR(Timer1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt
  167. {
  168.   timercnt++;
  169.   if (timercnt==900){
  170.     send_measurements = true;
  171.     timercnt = 0;
  172.   }
  173.   TCNT1 = 65536 - (16000000/1024)/0.25;
  174. }
  175.  
  176. void loop() {
  177.   if (send_measurements){
  178.     sendMeasurements();
  179.     send_measurements=false;
  180.   }
  181.   if (digitalRead(conv_en)==HIGH) {
  182.     runMPPT();
  183.   }
  184. }
  185.  
  186. void sendMeasurements(){
  187.   if (client.available()) {
  188.     short reading1, reading2;
  189.     client.print("{\"id\":");
  190.     client.print(BOUYID);
  191.     client.print(",\"d\":{[");
  192.     reading1 = readAnalogSensorVoltage(turbSensor);
  193.     sendShort(reading1);
  194.     client.print(",");
  195.     reading1 = readAnalogSensorVoltage(phSensor);
  196.     sendShort(reading1);
  197.     client.print(",");
  198.     reading1 = readAnalogSensorVoltage(uvSensor);
  199.     sendShort(reading1);
  200.     client.print(",");
  201.     reading1 = readAnalogSensorVoltage(batVolt);
  202.     sendShort(reading1);
  203.     client.print(",");
  204.  
  205.     imu_sensor();
  206.     sendFloat(orientation.roll);
  207.     client.print(",");
  208.     sendFloat(orientation.pitch);
  209.     client.print(",");
  210.     sendFloat(orientation.heading);
  211.     client.print(",");
  212.  
  213.     bmp280(&reading1,&reading2);
  214.     sendShort(reading1);
  215.     client.print(",");
  216.     sendShort(reading2);
  217.     client.print(",");
  218.  
  219.     float temp = readTemperatureSensor();
  220.     sendFloat(temp);
  221.  
  222.     client.print("]}}");
  223.   }
  224.   if (!client.available() && !client.connected()) {
  225.     client.stop();
  226.   }
  227. }
  228.  
  229. void runMPPT(){
  230.   int vsol = readAnalogSensorVoltage(VSOL);
  231.   int isol = readAnalogSensorVoltage(ISOL);
  232.  
  233.   int a = (varray[0]-varray[1])/(iarray[0]-iarray[1]);
  234.   a -= (varray[1]-vsol)/(iarray[1]-isol);
  235.   a /= iarray[0] + isol;
  236.  
  237.   int b = (varray[0]-varray[1])/(iarray[0]-iarray[1]) - a*(iarray[0]+iarray[1]);
  238.  
  239.   short imax = (short) -b/(2*a);
  240.   dac.output(imax);
  241.  
  242.   varray[0] = varray[1];
  243.   varray[1] = vsol;
  244.  
  245.   iarray[0] = iarray[1];
  246.   iarray[1] = isol;
  247. }
  248.  
  249. void sendShort(short value){
  250.   client.print("{\"v\":");
  251.   client.print(value);
  252.   client.print("}");
  253. }
  254.  
  255. void sendFloat(float value){
  256.   client.print("{\"v\":");
  257.   client.print(value);
  258.   client.print("}");
  259. }
  260.  
  261.  
  262. short readAnalogSensorVoltage(int analogPin){
  263.   return analogRead(analogPin);
  264. }
  265.  
  266. void initI2CSensors()
  267. {
  268.   if(!accel.begin())
  269.   {
  270.     while(1);
  271.   }
  272.   if(!mag.begin())
  273.   {
  274.     while(1);
  275.   }
  276.   if (!bmp.begin()) {
  277.     while (1);
  278.   }
  279.  
  280.   /* Default settings from datasheet. */
  281.   bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
  282.                   Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
  283.                   Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
  284.                   Adafruit_BMP280::FILTER_X16,      /* Filtering. */
  285.                   Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
  286. }
  287.  
  288. void imu_sensor(){
  289.   accel.getEvent(&accel_event);
  290.   dof.accelGetOrientation(&accel_event, &orientation);
  291.   mag.getEvent(&mag_event);
  292.   dof.magTiltCompensation(SENSOR_AXIS_Z, &mag_event, &accel_event);
  293.   dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation);
  294. }
  295.  
  296. void bmp280(short* temp, short* pressure){
  297.     *temp =  bmp.readTemperature();
  298.     *pressure = bmp.readPressure();
  299. }
  300.  
  301. float readTemperatureSensor(){
  302.   sensors.requestTemperatures();
  303.   return sensors.getTempCByIndex(0);
  304. }
  305.  
  306. void interuptroutine()          
  307. {                  
  308.   if (interupt1==HIGH){
  309.     digitalWrite(conv_en,HIGH);
  310.   } else {
  311.     digitalWrite(conv_en,LOW);
  312.   }
  313.   if (interupt1==HIGH && interupt2==HIGH){
  314.     digitalWrite(bat_en,LOW);
  315.   } else {
  316.     digitalWrite(bat_en,HIGH);
  317.   }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement