Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This #include statement was automatically added by the Particle IDE.
- #include <SparkFunOpenlog.h>
- // This #include statement was automatically added by the Particle IDE.
- #include <google-maps-device-locator.h>
- /*
- * Project: ATS_LOC_LTE_SW_005
- * Modified from GVR_LOC_003_SWITCH
- * Description: Program to read data from Serial port and transmit it via Cellular to a remote server.
- * Author: Todd Adams / Tony Lassak
- * Date: 5/01/2019
- Changes:
- Changed data rate back to 5 sec for testing with COSTCO
- Changed variables to more concise variables to keep data down
- 2019-05-16 - Added function to accept command and pass back to PLC.
- 2019-06-07 - Added functions for automatic time reboot on loss of signal.
- */
- //#include "Serial2/Serial2.h"
- //#include "Serial4/Serial4.h"
- #define TOKEN "rug6GdH07cAhm72eKu4G" //Token goes with MQTT access to Thingboard
- #define DELAY_BEFORE_REBOOT (180 * 1000) // set a reboot delay of 15 seconds
- //OpenLog myLog;
- STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
- //MQTT client("demo.thingsboard.io", 1883, callback);
- //MQTT client("64.132.48.46", 1883, callback);
- //retained char deviceName[32];
- String deviceName;
- String dataLine;
- String deviceIP;
- String strGmid;
- String strFile;
- String strDate;
- String strTime;
- String strPress;
- String strCycles;
- String strFaultCode;
- String strFaults;
- String strMode;
- String strLong = "0";
- String strLat = "0";
- String strAcc = "0";
- String locationInfo;
- int led1 = D2;
- int timing = 5000; // Set timing for 15 seconds
- // REBOOT VARIABLES
- unsigned int rebootDelay = DELAY_BEFORE_REBOOT;
- unsigned long rebootStart;
- bool rebootFlag;
- GoogleMapsDeviceLocator locator;
- String readLineFromSerial() {
- // Read Serial data
- String line = "";
- line.concat(deviceName);
- line.concat(",");
- //String line = "data: ";
- char ch;
- if (Serial1.available() > 0) {
- // Read data from PLC as long as there is data in the buffer
- while (Serial1.available() > 0) {
- ch = Serial1.read();
- if ((ch != '~') && (ch != '#')) {
- if (ch == '@') break;
- line.concat(ch);
- } //End if
- } //End While
- }
- return line;
- }
- void handler(const char *topic, const char *data) {
- deviceIP = String(data);
- }
- // recieve message
- void callback(char* topic, byte* payload, unsigned int length) {
- char p[length + 1];
- memcpy(p, payload, length);
- p[length] = NULL;
- if (!strcmp(p, "RED"))
- RGB.color(255, 0, 0);
- else if (!strcmp(p, "GREEN"))
- RGB.color(0, 255, 0);
- else if (!strcmp(p, "BLUE"))
- RGB.color(0, 0, 255);
- else
- RGB.color(255, 255, 255);
- delay(1000);
- }
- void saveName(const char *topic, const char *data) {
- deviceName = String(data);
- }
- void locationCallback(float lat, float lng, float accuracy) {
- strLat = String(lat);
- strLong = String(lng);
- strAcc = String(accuracy);
- locationInfo = String(lat)+","+String(lng)+","+String(accuracy);
- waitUntil( Particle.connected );
- Particle.publish( "Location", locationInfo, PRIVATE );
- }
- void myLocationHandler(const char *event, const char *data) {
- }
- int issueCommand(String command) {
- String c;
- c.concat("(");
- c.concat(command);
- c.concat(")");
- Serial1.println(c);
- }
- void setup() {
- // Establish proper Time Stamp
- Time.setFormat(TIME_FORMAT_ISO8601_FULL);
- Serial1.begin(9600);
- //Serial4.begin(9600);
- pinMode(led1, OUTPUT);
- //Wire.begin(); //Initialize I2C
- //myLog.begin(); //Open connection to OpenLog (no pun intended)
- //int led1 = D2;
- // New Function to send a command back to PLC.
- Particle.function("Command",issueCommand);
- Particle.variable("locationInfo", locationInfo);
- Particle.subscribe("particle/device/ip", handler);
- Particle.publish("particle/device/ip");
- // grab Location information
- //Particle.subscribe(System.deviceID() + "/hook-response/deviceLocator", myLocationHandler,MY_DEVICES);
- //System.deviceID() +
- ///3c0024001247373333353132/0
- //if (deviceName == "") {
- Particle.subscribe("particle/device/name", saveName);
- //delay(1000);
- Particle.publish("particle/device/name");
- //delay(5000);
- // }
- Particle.subscribe("PLC_data", handler);
- //client.connect(deviceName,TOKEN, NULL);
- locator.withSubscribe(locationCallback).withLocateOnce();
- //locator.withSubscribe(locationCallback).withLocatePeriodic( 60 );
- //locator.withLocatePeriodic(30).withEventName("deviceLocator").withSubscribe(locationCallback);
- //locator.withLocateOnce();
- //locator.withLocatePeriodic(30);
- // Just added.
- Particle.connect();
- delay(1000);
- }
- void loop() {
- locator.loop();
- // Added functions to facilitate timed reboot
- if (!Particle.connected() && !rebootFlag) {
- rebootStart = millis();
- rebootFlag == true;
- }
- if (!Cellular.ready() && !rebootFlag) {
- rebootStart = millis();
- rebootFlag == true;
- }
- if ((rebootFlag) && (millis() - rebootStart >= rebootDelay)) {
- System.reset();
- }
- // Get network time stamp
- Time.setFormat(TIME_FORMAT_ISO8601_FULL);
- uint32_t ts = Time.now();
- String datetime = Time.format(ts);
- // line is the data going to the SD CArd
- // newline is the data going to the WEB
- String line = "";
- String newLine = "";
- line = readLineFromSerial();
- if (line != "") {
- String line2 = line;
- char *r = const_cast<char*>(line2.c_str());
- strGmid = strtok(r,",");
- strFile = strtok(NULL,",");
- strDate = strtok(NULL,",");
- strTime = strtok(NULL,",");
- strPress = strtok(NULL,",");
- strCycles = strtok(NULL,",");
- strFaultCode = strtok(NULL,",");
- strFaults = strtok(NULL,",");
- strMode = strtok(NULL,",");
- // Print to SD Card
- //Serial4.print(line);
- //myLog.print(line2);
- newLine.concat("{\"ID\":\""); // change from GMid
- newLine.concat(strGmid);
- //newLine.concat("\",\"DateTime\":\""); // Removed Datatime from stream
- // newLine.concat(datetime);
- //newLine.concat("\",\"Time\":\"");
- //newLine.concat(strTime);
- newLine.concat("\",\"P\":"); // Changed from "Pressure"
- newLine.concat(strPress);
- newLine.concat(",\"C\":"); // Changed from "Cycles"
- newLine.concat(strCycles);
- newLine.concat(",\"F\":"); // Changed from "Fault Code"
- newLine.concat(strFaultCode);
- newLine.concat(",\"E\":\""); // changed from "Faults"
- newLine.concat(strFaults);
- newLine.concat("\",\"M\":"); // Changed from "Mode"
- newLine.concat(strMode);
- newLine.concat(",\"La\":"); //Changed from "Latitude"
- newLine.concat(strLat);
- newLine.concat(",\"Lo\":"); // Changed from "Longitude"
- newLine.concat(strLong);
- newLine.concat(",\"A\":"); // Changed from "Accuracy"
- newLine.concat(strAcc);
- newLine.concat("}");
- // Prinst to SD Card (Serial4 is Pin C2/C3)
- // Serial4.print(line);
- // line = "Testing 1-2-3";
- // Particle.publish("PLC_data",newLine);
- if(strMode!="") {
- Particle.publish("PLC_data", newLine, PRIVATE);
- // Print to SD Card
- //Serial4.print(line);
- }
- //digitalWrite(led1, HIGH);
- // publish/subscribe
- //if (client.connect(deviceName,TOKEN, NULL)) {
- //client.publish("v1/devices/me/telemetry",newLine);
- Particle.variable("DataLine", &newLine, STRING);
- //}
- }
- //delay(100);
- //digitalWrite(led1,LOW);
- delay(timing);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement