Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //Include relevent libraries
- /////////////////////////////////////////////////////////////////////////////////////////////////
- #include <MQTT.h>
- #include <MQTTClient.h>
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //Set Variables used in computation
- /////////////////////////////////////////////////////////////////////////////////////////////////
- char serialData[80];
- int serialDataLength = 0;
- char temp;
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //Set constants used in computation
- /////////////////////////////////////////////////////////////////////////////////////////////////
- const char* ssid = "Ursalink_F01094"; //change this to your network
- const char* password = "AppliedEV2019"; //change this to your network password
- 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
- const int mqttPort = 1883; //This is the standard port
- const char* mqttUser = "TESTVEHICLE"; //self explan
- const char* mqttPassword = "AEVinternsaredabest1";
- const char* subtopicbackup = "AEV/LWT";
- const char* subtopic = "AEV/V1/listen";
- const char* posttopic = "AEV/CARS/TESTVEHICLE/GPSDATA";
- WiFiClient espClient;
- PubSubClient client(espClient);
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //Setup
- /////////////////////////////////////////////////////////////////////////////////////////////////
- void setup() {
- Serial.begin(9600);
- WiFi.begin(ssid, password); //WIFI CONNECT
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println("Connecting to WiFi..");
- }
- Serial.println("Connected to the WiFi network");
- client.setServer(mqttServer, mqttPort); //MQTT CONNECT
- client.setCallback(callback);
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP8266Client1", mqttUser, mqttPassword )) {
- Serial.println("Connected to MQTT messaging service");
- } else {
- Serial.print("failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- client.subscribe(subtopic);
- client.subscribe(subtopicbackup);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //On MQTT message recieve from subscription
- /////////////////////////////////////////////////////////////////////////////////////////////////
- void callback(char* topic, byte* payload, unsigned int length) { //CURRENTLY UNUSED
- Serial.print("Message arrived in topic: ");
- Serial.println(topic);
- Serial.print("Message: ");
- for (int i = 0; i < length; i++) {
- Serial.println((char)payload[i]);
- if ((char)(payload[i])==(char)('5')){
- client.publish(posttopic, "0");
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //Main loop
- /////////////////////////////////////////////////////////////////////////////////////////////////
- void loop() {
- client.loop(); //MUST BE CALLED TO MAINTAIN CONNECTION TO MQTT BROKER
- if (Serial.available() > 0) //READ SERIAL GPS DATA FROM URSALINK
- {
- serialDataLength = Serial.readBytes(serialData, 80);
- Serial.println();
- Serial.println("**********************");
- client.beginPublish(posttopic, serialDataLength, false); //BEGINS WRITTING AN MQTT MESSAGE
- for (int i = 0; i < serialDataLength; i++) {
- temp = serialData[i]; //CONVERT SERIAL BITS TO ACTUAL MESSAGE
- temp = ~temp;
- temp = temp >> 1;
- Serial.print(temp);
- client.write(temp); //WRITES MESSAGES
- }
- client.endPublish(); //SENDS MESSAGE
- Serial.println();
- Serial.println("**********************");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement