Advertisement
Guest User

Electron

a guest
Oct 17th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. // Include libraries
  2. #include "math.h"
  3.  
  4. // Settings
  5. unsigned long measurementInterval   = 60000;
  6. static bool DEBUG                   = true;
  7.  
  8. long previousMillis                 = 0;
  9. String disdroResponse;
  10. String base64Chars                  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-";
  11.  
  12. // Custom APN
  13. //STARTUP(cellular_credentials_set("internet", "", "", NULL));
  14.  
  15. // Set device mode to semi automatic
  16. SYSTEM_MODE(SEMI_AUTOMATIC);
  17.  
  18. void setup()
  19. {
  20.     if(DEBUG)
  21.     {
  22.         Serial.begin(9600);
  23.         Serial.println("Starting application");
  24.     }
  25.     Serial1.begin(9600);
  26. }
  27.  
  28.  
  29. void intToBase64(int number, String &currentBase64Value)
  30. {
  31.     for(int i = 3; i >= 1; i--)
  32.     {
  33.         int n   = (number % ((int) pow(64,i))) / ((int) pow(64,(i-1)));
  34.         if(n > 0 || i == 1 || currentBase64Value.length() > 0)
  35.         {
  36.             currentBase64Value      += base64Chars.charAt(n);
  37.         }
  38.     }
  39. }
  40.  
  41. void serialEvent1()
  42. {
  43.     while(Serial1.available())
  44.     {
  45.         char c              = Serial1.read();
  46.         disdroResponse      += c;
  47.     }
  48.    
  49.     // Calculate the amount of bins in the response by checking for the separator
  50.     int bins                = 0;
  51.     int lastSeparator       = 0;
  52.  
  53.     for(int i = 0; i < disdroResponse.length(); i++)
  54.     {
  55.         if(disdroResponse.charAt(i) == ',')
  56.         {
  57.             lastSeparator   = i;
  58.             bins++;
  59.         }
  60.     }
  61.    
  62.  
  63.     // Check if all 64 bins are and temperature are received (expects at least 5 chars for temperature)
  64.     if(bins >= 64 && disdroResponse.length() > (lastSeparator + 5))
  65.     {
  66.         String energyBins   = disdroResponse.substring(0, lastSeparator);
  67.         String temperature  = disdroResponse.substring(++lastSeparator);
  68.         disdroResponse      = "";
  69.         if(DEBUG)
  70.         {
  71.             Serial.print("Energy bins: ");
  72.             Serial.println(energyBins);
  73.             Serial.print("Temperature: ");
  74.             Serial.println(temperature.toFloat());
  75.         }
  76.        
  77.         int energyBinArray[65];
  78.         int currentBin      = 0;
  79.         int dropTotal       = 0;
  80.         String currentString;
  81.         String base64String;
  82.         String base64Bin;
  83.        
  84.         for(int i = 0; i < energyBins.length(); i++)
  85.         {
  86.             // End of bin
  87.             char currentChar                    = energyBins.charAt(i);
  88.             if(currentChar == ',')
  89.             {
  90.                 energyBinArray[currentBin]      = currentString.toInt();
  91.                 if(energyBinArray[currentBin] > 0 && currentBin > 0)
  92.                 {
  93.                     dropTotal                   += energyBinArray[currentBin];
  94.                 }
  95.                
  96.                 // Generate base 64 string
  97.                 base64Bin                       = "";
  98.                 intToBase64(energyBinArray[currentBin], base64Bin);
  99.                 base64Bin                       += ",";
  100.                 base64String.concat(base64Bin);
  101.                
  102.                 currentString                   = "";
  103.                 currentBin++;
  104.             }
  105.             else
  106.             {
  107.                 currentString                   += currentChar;
  108.             }
  109.         }
  110.        
  111.         if(DEBUG)
  112.         {
  113.             Serial.println(base64String);
  114.             Serial.print("Total drop amount: ");
  115.             Serial.println(dropTotal);
  116.         }
  117.        
  118.         if(Time.minute() == 0)
  119.         {
  120.             bool publishStatus;
  121.             if (!Particle.connected())
  122.             {
  123.                 if(DEBUG)
  124.                 {
  125.                     Serial.println("Connect to particle cloud");
  126.                 }
  127.                 Particle.connect();
  128.             }
  129.            
  130.             // Publish status after connection is established, or timeout after 20 seconds
  131.             if (waitFor(Particle.connected, 20000))
  132.             {
  133.                 FuelGauge fuel;
  134.                 CellularSignal sig      = Cellular.RSSI();
  135.                 String statusString     = "B=" + String(fuel.getSoC()).substring(0,5) + ",T=" + temperature + ",RSSI=" + sig.rssi;
  136.                 publishStatus           = Particle.publish("status", statusString, 60, PRIVATE);
  137.             }
  138.         }
  139.        
  140.         if(dropTotal >= 10)
  141.         {
  142.             // Publish energy bins to particle cloud
  143.             bool publish;
  144.            
  145.             // Connect to particle cloud if there's no connection yet
  146.             if (!Particle.connected())
  147.             {
  148.                 if(DEBUG)
  149.                 {
  150.                     Serial.println("Connect to particle cloud");
  151.                 }
  152.                 Particle.connect();
  153.             }
  154.            
  155.             // Publish message after connection is established, or timeout after 20 seconds
  156.             if (waitFor(Particle.connected, 20000))
  157.             {
  158.                 publish             = Particle.publish("measurement", base64String.substring(0, (base64String.length() - 2)), 60, PRIVATE);
  159.             }
  160.            
  161.             // Check status of message publish
  162.             if(!publish)
  163.             {
  164.                 if(DEBUG)
  165.                 {
  166.                     Serial.println("Publish failed");
  167.                 }
  168.                 // TODO: Save data to EEPROM
  169.             }
  170.             else
  171.             {
  172.                 if(DEBUG)
  173.                 {
  174.                     Serial.println("Publish success");
  175.                 }
  176.             }
  177.         }
  178.         else
  179.         {
  180.             if(DEBUG)
  181.             {
  182.                 Serial.println("Entering deep sleep mode");
  183.                 delay(2000);
  184.             }
  185.             System.sleep(SLEEP_MODE_DEEP, 55);
  186.         }
  187.        
  188.     }
  189.    
  190. }
  191.  
  192. void loop()
  193. {
  194.     unsigned long currentMillis   = millis();
  195.     if(((currentMillis - previousMillis) > measurementInterval) || (previousMillis == 0 && currentMillis > 5000) || (currentMillis < previousMillis))    // Important: second check is to account for millis overflow
  196.     {
  197.         previousMillis    = currentMillis;
  198.         disdroResponse    = "";
  199.        
  200.         if(DEBUG)
  201.         {
  202.             Serial.println("Requesting string");
  203.         }
  204.        
  205.         Serial1.write((byte) '?');
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement