Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include libraries
- #include "math.h"
- // Settings
- unsigned long measurementInterval = 60000;
- static bool DEBUG = true;
- long previousMillis = 0;
- String disdroResponse;
- String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-";
- // Custom APN
- //STARTUP(cellular_credentials_set("internet", "", "", NULL));
- // Set device mode to semi automatic
- SYSTEM_MODE(SEMI_AUTOMATIC);
- void setup()
- {
- if(DEBUG)
- {
- Serial.begin(9600);
- Serial.println("Starting application");
- }
- Serial1.begin(9600);
- }
- void intToBase64(int number, String ¤tBase64Value)
- {
- for(int i = 3; i >= 1; i--)
- {
- int n = (number % ((int) pow(64,i))) / ((int) pow(64,(i-1)));
- if(n > 0 || i == 1 || currentBase64Value.length() > 0)
- {
- currentBase64Value += base64Chars.charAt(n);
- }
- }
- }
- void serialEvent1()
- {
- while(Serial1.available())
- {
- char c = Serial1.read();
- disdroResponse += c;
- }
- // Calculate the amount of bins in the response by checking for the separator
- int bins = 0;
- int lastSeparator = 0;
- for(int i = 0; i < disdroResponse.length(); i++)
- {
- if(disdroResponse.charAt(i) == ',')
- {
- lastSeparator = i;
- bins++;
- }
- }
- // Check if all 64 bins are and temperature are received (expects at least 5 chars for temperature)
- if(bins >= 64 && disdroResponse.length() > (lastSeparator + 5))
- {
- String energyBins = disdroResponse.substring(0, lastSeparator);
- String temperature = disdroResponse.substring(++lastSeparator);
- disdroResponse = "";
- if(DEBUG)
- {
- Serial.print("Energy bins: ");
- Serial.println(energyBins);
- Serial.print("Temperature: ");
- Serial.println(temperature.toFloat());
- }
- int energyBinArray[65];
- int currentBin = 0;
- int dropTotal = 0;
- String currentString;
- String base64String;
- String base64Bin;
- for(int i = 0; i < energyBins.length(); i++)
- {
- // End of bin
- char currentChar = energyBins.charAt(i);
- if(currentChar == ',')
- {
- energyBinArray[currentBin] = currentString.toInt();
- if(energyBinArray[currentBin] > 0 && currentBin > 0)
- {
- dropTotal += energyBinArray[currentBin];
- }
- // Generate base 64 string
- base64Bin = "";
- intToBase64(energyBinArray[currentBin], base64Bin);
- base64Bin += ",";
- base64String.concat(base64Bin);
- currentString = "";
- currentBin++;
- }
- else
- {
- currentString += currentChar;
- }
- }
- if(DEBUG)
- {
- Serial.println(base64String);
- Serial.print("Total drop amount: ");
- Serial.println(dropTotal);
- }
- if(Time.minute() == 0)
- {
- bool publishStatus;
- if (!Particle.connected())
- {
- if(DEBUG)
- {
- Serial.println("Connect to particle cloud");
- }
- Particle.connect();
- }
- // Publish status after connection is established, or timeout after 20 seconds
- if (waitFor(Particle.connected, 20000))
- {
- FuelGauge fuel;
- CellularSignal sig = Cellular.RSSI();
- String statusString = "B=" + String(fuel.getSoC()).substring(0,5) + ",T=" + temperature + ",RSSI=" + sig.rssi;
- publishStatus = Particle.publish("status", statusString, 60, PRIVATE);
- }
- }
- if(dropTotal >= 10)
- {
- // Publish energy bins to particle cloud
- bool publish;
- // Connect to particle cloud if there's no connection yet
- if (!Particle.connected())
- {
- if(DEBUG)
- {
- Serial.println("Connect to particle cloud");
- }
- Particle.connect();
- }
- // Publish message after connection is established, or timeout after 20 seconds
- if (waitFor(Particle.connected, 20000))
- {
- publish = Particle.publish("measurement", base64String.substring(0, (base64String.length() - 2)), 60, PRIVATE);
- }
- // Check status of message publish
- if(!publish)
- {
- if(DEBUG)
- {
- Serial.println("Publish failed");
- }
- // TODO: Save data to EEPROM
- }
- else
- {
- if(DEBUG)
- {
- Serial.println("Publish success");
- }
- }
- }
- else
- {
- if(DEBUG)
- {
- Serial.println("Entering deep sleep mode");
- delay(2000);
- }
- System.sleep(SLEEP_MODE_DEEP, 55);
- }
- }
- }
- void loop()
- {
- unsigned long currentMillis = millis();
- if(((currentMillis - previousMillis) > measurementInterval) || (previousMillis == 0 && currentMillis > 5000) || (currentMillis < previousMillis)) // Important: second check is to account for millis overflow
- {
- previousMillis = currentMillis;
- disdroResponse = "";
- if(DEBUG)
- {
- Serial.println("Requesting string");
- }
- Serial1.write((byte) '?');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement