Advertisement
babyyoda_

GSM_MQTT

Jun 5th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int led = 13;
  2. unsigned int Counter = 0;
  3. unsigned long datalength, CheckSum, RLength;
  4. unsigned short topiclength;
  5. unsigned char topic[30];
  6. char str[250];
  7. unsigned char encodedByte;
  8. int X;
  9. unsigned short MQTTProtocolNameLength;
  10. unsigned short MQTTClientIDLength;
  11. unsigned short MQTTUsernameLength;
  12. unsigned short MQTTPasswordLength;
  13. const char MQTTHost[30] = "broker.mqttdashboard.com";
  14. const char MQTTPort[10] = "8000";
  15. const char MQTTClientID[20] = "clientId8032";
  16. const char MQTTTopic[30] = "SampleTopic";
  17. const char MQTTProtocolName[10] = "";
  18. const char MQTTLVL = 0x03;
  19. const char MQTTFlags = 0xC2;
  20. const unsigned int MQTTKeepAlive = 60;
  21. const char MQTTUsername[30] = "";
  22. const char MQTTPassword[35] = "";
  23. const char MQTTQOS = 0x00;
  24. const char MQTTPacketID = 0x0001;
  25. void setup() {
  26.   pinMode(led, OUTPUT);
  27.   Serial.begin(9600);
  28.   Serial.println("Arduino MQTT Tutorial, Valetron Systems @www.raviyp.com ");
  29.   delay(3000);
  30. }
  31. void SendConnectPacket(void) {
  32.   Serial.print("\r\nAT+CIPSEND\r\n");
  33.   delay(3000);
  34.   Serial.write(0x10);
  35.   MQTTProtocolNameLength = strlen(MQTTProtocolName);
  36.   MQTTClientIDLength = strlen(MQTTClientID);
  37.   MQTTUsernameLength = strlen(MQTTUsername);
  38.   MQTTPasswordLength = strlen(MQTTPassword);
  39.   datalength = MQTTProtocolNameLength + 2 + 4 + MQTTClientIDLength + 2 + MQTTUsernameLength + 2 + MQTTPasswordLength + 2;
  40.   X = datalength;
  41.   do {
  42.     encodedByte = X % 128;
  43.     X = X / 128;
  44.     if (X > 0) {
  45.       encodedByte |= 128;
  46.     }
  47.     Serial.write(encodedByte);
  48.   }
  49.   while (X > 0);
  50.   Serial.write(MQTTProtocolNameLength >> 8);
  51.   Serial.write(MQTTProtocolNameLength & 0xFF);
  52.   Serial.print(MQTTProtocolName);
  53.   Serial.write(MQTTLVL); // LVL
  54.   Serial.write(MQTTFlags); // Flags
  55.   Serial.write(MQTTKeepAlive >> 8);
  56.   Serial.write(MQTTKeepAlive & 0xFF);
  57.   Serial.write(MQTTClientIDLength >> 8);
  58.   Serial.write(MQTTClientIDLength & 0xFF);
  59.   Serial.print(MQTTClientID);
  60.   Serial.write(MQTTUsernameLength >> 8);
  61.   Serial.write(MQTTUsernameLength & 0xFF);
  62.   Serial.print(MQTTUsername);
  63.   Serial.write(MQTTPasswordLength >> 8);
  64.   Serial.write(MQTTPasswordLength & 0xFF);
  65.   Serial.print(MQTTPassword);
  66.   Serial.write(0x1A);
  67. }
  68. void SendPublishPacket(void) {
  69.   Serial.print("\r\nAT+CIPSEND\r\n");
  70.   delay(3000);
  71.   memset(str, 0, 250);
  72.   topiclength = sprintf((char * ) topic, MQTTTopic);
  73.   datalength = sprintf((char * ) str, "%s%u", topic, Counter);
  74.   delay(1000);
  75.   Serial.write(0x30);
  76.   X = datalength + 2;
  77.   do {
  78.     encodedByte = X % 128;
  79.     X = X / 128;
  80.     if (X > 0) {
  81.       encodedByte |= 128;
  82.     }
  83.     Serial.write(encodedByte);
  84.   }
  85.   while (X > 0);
  86.   Serial.write(topiclength >> 8);
  87.   Serial.write(topiclength & 0xFF);
  88.   Serial.print(str);
  89.   Serial.write(0x1A);
  90. }
  91. void SendSubscribePacket(void) {
  92.   Serial.print("\r\nAT+CIPSEND\r\n");
  93.   delay(3000);
  94.   memset(str, 0, 250);
  95.   topiclength = strlen(MQTTTopic);
  96.   datalength = 2 + 2 + topiclength + 1;
  97.   delay(1000);
  98.   Serial.write(0x82);
  99.   X = datalength;
  100.   do {
  101.     encodedByte = X % 128;
  102.     X = X / 128;
  103.     if (X > 0) {
  104.       encodedByte |= 128;
  105.     }
  106.     Serial.write(encodedByte);
  107.   }
  108.   while (X > 0);
  109.   Serial.write(MQTTPacketID >> 8);
  110.   Serial.write(MQTTPacketID & 0xFF);
  111.   Serial.write(topiclength >> 8);
  112.   Serial.write(topiclength & 0xFF);
  113.   Serial.print(MQTTTopic);
  114.   Serial.write(MQTTQOS);
  115.   Serial.write(0x1A);
  116. }
  117. void loop() {
  118.   Serial.print("AT+CSTT=\"www\",\"\",\"\"\r\n");
  119.   delay(1000);
  120.   Serial.print("AT+CIPMODE=0\r\n");
  121.   delay(1000);
  122.   Serial.print("AT+CIICR\r\n");
  123.   delay(5000);
  124.   Serial.print("AT+CIPSTART=\"TCP\",\"broker.mqttdashboard.com\",\"8000\"\r\n");
  125.   delay(4000);
  126.   SendConnectPacket();
  127.   delay(5000);
  128.   SendSubscribePacket();
  129.   while (1) {
  130.     if (Serial.available() > 0) {
  131.       str[0] = Serial.read();
  132.       Serial.write(str[0]);
  133.       if (str[0] == '1')
  134.         digitalWrite(led, HIGH);
  135.       if (str[0] == '0')
  136.         digitalWrite(led, LOW);
  137.     }
  138.   }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement