Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.40 KB | None | 0 0
  1. // This #include statement was automatically added by the Particle IDE.
  2. #include <SparkFunOpenlog.h>
  3.  
  4. // This #include statement was automatically added by the Particle IDE.
  5. #include <google-maps-device-locator.h>
  6.  
  7.  
  8.  
  9.  
  10. /*
  11.  
  12.  * Project:         ATS_LOC_LTE_SW_005
  13.  
  14.  *                  Modified from GVR_LOC_003_SWITCH
  15.  
  16.  * Description:     Program to read data from Serial port and transmit it via Cellular to a remote server.
  17.  * Author:          Todd Adams / Tony Lassak
  18.  * Date:            5/01/2019
  19.  
  20.  Changes:
  21.  
  22.  Changed data rate  back to 5 sec for testing with COSTCO
  23.  
  24.  Changed variables to more concise variables to keep data down
  25.  
  26.  2019-05-16 - Added function to accept command and pass back to PLC.
  27.  
  28.  2019-06-07 - Added functions for automatic time reboot on loss of signal.
  29.  
  30.  */
  31.  
  32.  
  33. //#include "Serial2/Serial2.h"
  34. //#include "Serial4/Serial4.h"
  35.  
  36. #define TOKEN "rug6GdH07cAhm72eKu4G" //Token goes with MQTT access to Thingboard
  37.  
  38. #define DELAY_BEFORE_REBOOT (180 * 1000) // set a reboot delay of 15 seconds
  39.  
  40. //OpenLog myLog;
  41.  
  42. STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
  43.  
  44.  
  45. //MQTT client("demo.thingsboard.io", 1883, callback);
  46. //MQTT client("64.132.48.46", 1883, callback);
  47.  
  48.  
  49. //retained char deviceName[32];
  50.  
  51. String deviceName;
  52. String dataLine;
  53. String deviceIP;
  54.  
  55. String strGmid;
  56. String strFile;
  57. String strDate;
  58. String strTime;
  59. String strPress;
  60. String strCycles;
  61. String strFaultCode;
  62. String strFaults;
  63. String strMode;
  64. String strLong = "0";
  65. String strLat = "0";
  66. String strAcc = "0";
  67.  
  68. String locationInfo;
  69.  
  70. int led1 = D2;
  71.  
  72. int timing = 5000; // Set timing for 15 seconds
  73.  
  74. // REBOOT VARIABLES
  75.  
  76. unsigned int rebootDelay = DELAY_BEFORE_REBOOT;
  77. unsigned long rebootStart;
  78. bool rebootFlag;
  79.  
  80. GoogleMapsDeviceLocator locator;
  81.  
  82.  
  83. String readLineFromSerial() {
  84.  
  85.    // Read Serial data
  86.  
  87.    
  88.    String line = "";
  89.    
  90.    line.concat(deviceName);
  91.    line.concat(",");
  92.    
  93.    //String  line = "data: ";
  94.    char ch;
  95.  
  96.   if (Serial1.available() > 0) {
  97.  
  98.      // Read data from PLC as long as there is data in the buffer
  99.  
  100.      while (Serial1.available() > 0) {
  101.  
  102.        ch = Serial1.read();
  103.  
  104.        if ((ch != '~') && (ch != '#')) {
  105.  
  106.          if (ch == '@') break;
  107.  
  108.          line.concat(ch);
  109.  
  110.        }  //End if
  111.  
  112.      } //End While
  113.  
  114.    }
  115.  
  116.    return line;
  117.  
  118.  }
  119.  
  120.  void handler(const char *topic, const char *data) {
  121.      
  122.      deviceIP = String(data);
  123.  
  124.  }
  125.  
  126.  
  127. // recieve message
  128. void callback(char* topic, byte* payload, unsigned int length) {
  129.     char p[length + 1];
  130.     memcpy(p, payload, length);
  131.     p[length] = NULL;
  132.  
  133.     if (!strcmp(p, "RED"))
  134.         RGB.color(255, 0, 0);
  135.     else if (!strcmp(p, "GREEN"))
  136.         RGB.color(0, 255, 0);
  137.     else if (!strcmp(p, "BLUE"))
  138.         RGB.color(0, 0, 255);
  139.     else
  140.         RGB.color(255, 255, 255);
  141.     delay(1000);
  142. }
  143.  
  144. void saveName(const char *topic, const char *data) {
  145.    
  146.     deviceName = String(data);
  147.    
  148. }
  149.  
  150. void locationCallback(float lat, float lng, float accuracy) {
  151.    
  152.    
  153.     strLat = String(lat);
  154.     strLong = String(lng);
  155.     strAcc = String(accuracy);
  156.    
  157.     locationInfo = String(lat)+","+String(lng)+","+String(accuracy);
  158.  
  159.    
  160.     waitUntil( Particle.connected );
  161.     Particle.publish( "Location", locationInfo, PRIVATE );
  162.    
  163.  
  164.  
  165.    
  166. }
  167.  
  168.  
  169. void myLocationHandler(const char *event, const char *data) {
  170.  
  171. }
  172.  
  173.  
  174. int issueCommand(String command) {
  175.    
  176.     String c;
  177.    
  178.     c.concat("(");
  179.     c.concat(command);
  180.     c.concat(")");
  181.    
  182.     Serial1.println(c);
  183.    
  184. }
  185.  
  186.  
  187. void setup() {
  188.    
  189.     // Establish proper Time Stamp
  190.     Time.setFormat(TIME_FORMAT_ISO8601_FULL);
  191.    
  192.     Serial1.begin(9600);
  193.     //Serial4.begin(9600);
  194.  
  195.     pinMode(led1, OUTPUT);
  196.    
  197.     //Wire.begin(); //Initialize I2C
  198.     //myLog.begin(); //Open connection to OpenLog (no pun intended)
  199.  
  200.     //int led1 = D2;
  201.    
  202.     // New Function to send a command back to PLC.
  203.     Particle.function("Command",issueCommand);
  204.  
  205.     Particle.variable("locationInfo", locationInfo);
  206.  
  207.     Particle.subscribe("particle/device/ip", handler);
  208.     Particle.publish("particle/device/ip");
  209.    
  210.     // grab Location information
  211.    
  212.     //Particle.subscribe(System.deviceID() + "/hook-response/deviceLocator", myLocationHandler,MY_DEVICES);
  213.  
  214.  
  215.     //System.deviceID() +
  216.     ///3c0024001247373333353132/0
  217.    
  218.     //if (deviceName == "") {
  219.     Particle.subscribe("particle/device/name", saveName);
  220.         //delay(1000);
  221.     Particle.publish("particle/device/name");
  222.         //delay(5000);
  223.    // }
  224.    
  225.    
  226.     Particle.subscribe("PLC_data", handler);
  227.  
  228.     //client.connect(deviceName,TOKEN, NULL);
  229.     locator.withSubscribe(locationCallback).withLocateOnce();
  230.     //locator.withSubscribe(locationCallback).withLocatePeriodic( 60 );
  231.     //locator.withLocatePeriodic(30).withEventName("deviceLocator").withSubscribe(locationCallback);
  232.     //locator.withLocateOnce();
  233.  
  234.     //locator.withLocatePeriodic(30);
  235.    
  236.     // Just added.
  237.     Particle.connect();
  238.  
  239.     delay(1000);
  240. }
  241.  
  242.  
  243.  
  244. void loop() {
  245.    
  246.     locator.loop();
  247.    
  248.    
  249.     // Added functions to facilitate timed reboot
  250.    
  251.    
  252.     if (!Particle.connected() && !rebootFlag) {
  253.        
  254.         rebootStart = millis();
  255.         rebootFlag == true;
  256.        
  257.    }
  258.    
  259.     if (!Cellular.ready() && !rebootFlag) {
  260.        
  261.         rebootStart = millis();
  262.         rebootFlag == true;
  263.     }
  264.    
  265.    if ((rebootFlag) && (millis() - rebootStart  >= rebootDelay)) {
  266.        System.reset();
  267.    }
  268.    
  269.        
  270.  
  271.    
  272.     // Get network time stamp
  273.    
  274.     Time.setFormat(TIME_FORMAT_ISO8601_FULL);
  275.    
  276.     uint32_t ts = Time.now();
  277.      
  278.     String datetime = Time.format(ts);
  279.      
  280.    
  281.    // line is the data going to the SD CArd
  282.    // newline is the data going to the WEB
  283.  
  284.  
  285.  
  286.     String line = "";
  287.     String newLine = "";
  288.    
  289.     line = readLineFromSerial();
  290.  
  291.      if (line != "") {
  292.  
  293.  
  294.         String line2 = line;
  295.        
  296.         char *r = const_cast<char*>(line2.c_str());
  297.        
  298.         strGmid = strtok(r,",");
  299.         strFile = strtok(NULL,",");
  300.         strDate = strtok(NULL,",");
  301.         strTime = strtok(NULL,",");
  302.         strPress = strtok(NULL,",");
  303.         strCycles = strtok(NULL,",");
  304.         strFaultCode = strtok(NULL,",");
  305.         strFaults = strtok(NULL,",");
  306.         strMode = strtok(NULL,",");
  307.        
  308.        
  309.         // Print to SD Card
  310.         //Serial4.print(line);
  311.        
  312.         //myLog.print(line2);
  313.        
  314.  
  315.         newLine.concat("{\"ID\":\"");  // change from GMid
  316.         newLine.concat(strGmid);
  317.         //newLine.concat("\",\"DateTime\":\"");  // Removed Datatime from stream
  318.        // newLine.concat(datetime);
  319.         //newLine.concat("\",\"Time\":\"");
  320.         //newLine.concat(strTime);
  321.         newLine.concat("\",\"P\":");  // Changed from "Pressure"
  322.         newLine.concat(strPress);
  323.         newLine.concat(",\"C\":");  // Changed from "Cycles"
  324.         newLine.concat(strCycles);
  325.         newLine.concat(",\"F\":");  //  Changed from "Fault Code"
  326.         newLine.concat(strFaultCode);
  327.         newLine.concat(",\"E\":\"");  // changed from "Faults"
  328.         newLine.concat(strFaults);
  329.         newLine.concat("\",\"M\":");  // Changed from "Mode"
  330.         newLine.concat(strMode);
  331.        
  332.         newLine.concat(",\"La\":");  //Changed from "Latitude"
  333.         newLine.concat(strLat);
  334.         newLine.concat(",\"Lo\":"); // Changed from "Longitude"
  335.         newLine.concat(strLong);
  336.         newLine.concat(",\"A\":");  // Changed from "Accuracy"
  337.         newLine.concat(strAcc);
  338.        
  339.         newLine.concat("}");
  340.        
  341.         // Prinst to SD Card (Serial4 is Pin C2/C3)
  342.         // Serial4.print(line);
  343.         // line = "Testing 1-2-3";
  344.      
  345.        
  346.      
  347.         // Particle.publish("PLC_data",newLine);
  348.        
  349.         if(strMode!="") {
  350.        
  351.             Particle.publish("PLC_data", newLine, PRIVATE);
  352.            
  353.             // Print to SD Card
  354.             //Serial4.print(line);
  355.         }
  356.      
  357.         //digitalWrite(led1, HIGH);
  358.        
  359.    
  360.         // publish/subscribe
  361.        
  362.         //if (client.connect(deviceName,TOKEN, NULL)) {
  363.            
  364.             //client.publish("v1/devices/me/telemetry",newLine);
  365.            
  366.         Particle.variable("DataLine", &newLine, STRING);
  367.         //}
  368.            
  369.  
  370.     }
  371.  
  372.   //delay(100);
  373.   //digitalWrite(led1,LOW);
  374.   delay(timing);
  375.  
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement