Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////////////////////
  2. //Include relevent libraries
  3. /////////////////////////////////////////////////////////////////////////////////////////////////
  4. #include <MQTT.h>
  5. #include <MQTTClient.h>
  6. #include <ESP8266WiFi.h>
  7. #include <PubSubClient.h>
  8.  
  9. /////////////////////////////////////////////////////////////////////////////////////////////////
  10. //Set Variables used in computation
  11. /////////////////////////////////////////////////////////////////////////////////////////////////
  12. char serialData[80];
  13. int serialDataLength = 0;
  14. char temp;
  15.  
  16.  
  17. /////////////////////////////////////////////////////////////////////////////////////////////////
  18. //Set constants used in computation
  19. /////////////////////////////////////////////////////////////////////////////////////////////////
  20. const char* ssid = "Ursalink_F01094"; //change this to your network
  21. const char* password = "AppliedEV2019"; //change this to your network password
  22. const char* mqttServer = "203.45.199.194"; //change this to the IP of the machine on the local network that is hosting the MQTT server
  23. const int mqttPort = 1883; //This is the standard port
  24. const char* mqttUser = "TESTVEHICLE"; //self explan
  25. const char* mqttPassword = "AEVinternsaredabest1";
  26. const char* subtopicbackup = "AEV/LWT";
  27. const char* subtopic = "AEV/V1/listen";
  28. const char* posttopic = "AEV/CARS/TESTVEHICLE/GPSDATA";
  29.  
  30. WiFiClient espClient;
  31. PubSubClient client(espClient);
  32.  
  33.  
  34. /////////////////////////////////////////////////////////////////////////////////////////////////
  35. //Setup
  36. /////////////////////////////////////////////////////////////////////////////////////////////////
  37. void setup() {
  38. Serial.begin(9600);
  39.  
  40.  
  41. WiFi.begin(ssid, password); //WIFI CONNECT
  42. while (WiFi.status() != WL_CONNECTED) {
  43. delay(500);
  44. Serial.println("Connecting to WiFi..");
  45. }
  46. Serial.println("Connected to the WiFi network");
  47.  
  48.  
  49.  
  50. client.setServer(mqttServer, mqttPort); //MQTT CONNECT
  51. client.setCallback(callback);
  52. while (!client.connected()) {
  53. Serial.println("Connecting to MQTT...");
  54. if (client.connect("ESP8266Client1", mqttUser, mqttPassword )) {
  55. Serial.println("Connected to MQTT messaging service");
  56. } else {
  57. Serial.print("failed with state ");
  58. Serial.print(client.state());
  59. delay(2000);
  60. }
  61. }
  62.  
  63. client.subscribe(subtopic);
  64. client.subscribe(subtopicbackup);
  65. }
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////////////////////////////////////////
  69. //On MQTT message recieve from subscription
  70. /////////////////////////////////////////////////////////////////////////////////////////////////
  71. void callback(char* topic, byte* payload, unsigned int length) { //CURRENTLY UNUSED
  72. Serial.print("Message arrived in topic: ");
  73. Serial.println(topic);
  74. Serial.print("Message: ");
  75. for (int i = 0; i < length; i++) {
  76. Serial.println((char)payload[i]);
  77. if ((char)(payload[i])==(char)('5')){
  78. client.publish(posttopic, "0");
  79. }
  80. }
  81. }
  82.  
  83. /////////////////////////////////////////////////////////////////////////////////////////////////
  84. //Main loop
  85. /////////////////////////////////////////////////////////////////////////////////////////////////
  86. void loop() {
  87. client.loop(); //MUST BE CALLED TO MAINTAIN CONNECTION TO MQTT BROKER
  88. if (Serial.available() > 0) //READ SERIAL GPS DATA FROM URSALINK
  89. {
  90. serialDataLength = Serial.readBytes(serialData, 80);
  91. Serial.println();
  92. Serial.println("**********************");
  93. client.beginPublish(posttopic, serialDataLength, false); //BEGINS WRITTING AN MQTT MESSAGE
  94. for (int i = 0; i < serialDataLength; i++) {
  95.  
  96. temp = serialData[i]; //CONVERT SERIAL BITS TO ACTUAL MESSAGE
  97. temp = ~temp;
  98. temp = temp >> 1;
  99.  
  100. Serial.print(temp);
  101. client.write(temp); //WRITES MESSAGES
  102. }
  103. client.endPublish(); //SENDS MESSAGE
  104. Serial.println();
  105. Serial.println("**********************");
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement