CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Translate code MQTT Arduino

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

Translate code MQTT Arduino
PostPosted: Mon Apr 20, 2020 4:07 pm     Reply with quote

Hi, I try to translate this code from Arduino, but i don't know how use the printf.

The idea is make a ConnectPacket first and after send the PublishPacket of course once again make the connection TCP, but no print fine the buffer, maybe because the printf is bad write.
Code:

int led = 13;
unsigned int Counter = 0;
unsigned long datalength, CheckSum, RLength;
unsigned short topiclength;
unsigned char topic[30];
char str[250];
unsigned char encodedByte;
int X;
unsigned short MQTTProtocolNameLength;
unsigned short MQTTClientIDLength;           
unsigned short MQTTUsernameLength;
unsigned short MQTTPasswordLength;           
char MQTTHost[30] = "io.adafruit.com";
char MQTTPort[10] = "1883";
char MQTTClientID[20] = "ABCDEF";
char MQTTTopic[30] = "cvargcal/feeds/testv";
char MQTTProtocolName[10] = "MQTT";                       
char MQTTLVL = 0x03;
char MQTTFlags = 0xC2;
unsigned int MQTTKeepAlive = 60;
char MQTTUsername[30] = "cvargcal";                                   
char MQTTPassword[35] = "aio_xJeb83eGnlYAEDVgxlfyqsuKMttk";
char MQTTQOS = 0x00;
char MQTTPacketID = 0x0001;                                   
   
                                         
//!             
//!                                                   
//!void setup() {
//!  pinMode(led, OUTPUT);
//!  Serial.begin(9600);       
//!  Serial.println("Arduino MQTT Tutorial, Valetron Systems @www.raviyp.com ");
//!  delay(3000);
//!}
void SendConnectPacket() {                                     
  fprintf(uart2,0x10);    // Serial.write(0x10); 
  MQTTProtocolNameLength = strlen(MQTTProtocolName);
  MQTTClientIDLength = strlen(MQTTClientID);
  MQTTUsernameLength = strlen(MQTTUsername);
  MQTTPasswordLength = strlen(MQTTPassword);
  datalength = MQTTProtocolNameLength + 2 + 4 + MQTTClientIDLength + 2 + MQTTUsernameLength + 2 + MQTTPasswordLength + 2;
  X = datalength;         
 
                                             
  do {
    encodedByte = X % 128;
    X = X / 128;                 
    if (X > 0) {                   
      encodedByte |= 128;                       
    }                                                                                 
    fprintf(uart2,encodedByte);              //Serial.write(encodedByte);       
  }   
  while (X > 0);                                   
                                                                 
                                               
fprintf(uart2,(MQTTProtocolNameLength >> 8));       //!  Serial.write(MQTTProtocolNameLength >> 8);
fprintf(uart2,(MQTTProtocolNameLength & 0xFF));     //!  Serial.write(MQTTProtocolNameLength & 0xFF);   
fprintf(uart2,(MQTTProtocolName));                  //!  Serial.print(MQTTProtocolName);     
fprintf(uart2,(MQTTLVL));                           //!  Serial.write(MQTTLVL); // LVL
fprintf(uart2,(MQTTFlags));                         //!  Serial.write(MQTTFlags); // Flags         
fprintf(uart2,(MQTTKeepAlive >> 8));                //!  Serial.write(MQTTKeepAlive >> 8);
fprintf(uart2,(MQTTKeepAlive & 0xFF));              //!  Serial.write(MQTTKeepAlive & 0xFF);
fprintf(uart2,(MQTTClientIDLength >> 8));           //!  Serial.write(MQTTClientIDLength >> 8);
fprintf(uart2,(MQTTClientIDLength & 0xFF));         //!  Serial.write(MQTTClientIDLength & 0xFF);                         
fprintf(uart2,(MQTTClientID));                      //!  Serial.print(MQTTClientID);
fprintf(uart2,(MQTTUsernameLength >> 8));           //!  Serial.write(MQTTUsernameLength >> 8);       
fprintf(uart2,(MQTTUsernameLength & 0xFF));         //!  Serial.write(MQTTUsernameLength & 0xFF);
fprintf(uart2,(MQTTUsername));                      //!  Serial.print(MQTTUsername);
fprintf(uart2,(MQTTPasswordLength >> 8));           //!  Serial.write(MQTTPasswordLength >> 8);
fprintf(uart2,(MQTTPasswordLength & 0xFF));         //!  Serial.write(MQTTPasswordLength & 0xFF);
fprintf(uart2,(MQTTPassword));                      //!  Serial.print(MQTTPassword);                                                   
}                                               
                                         
void SendPublishPacket() {                         

  memset(str, 0, 250);
  str="57";
  topiclength = sprintf((char * ) topic, MQTTTopic);
  datalength = sprintf((char * ) str, "%s%u", topic, Counter);
  delay_ms(1000); 
  fprintf(uart2,0x30);                                //Serial.write(0x30);
  X = datalength + 2;                                 
  do {
    encodedByte = X % 128;                       
    X = X / 128;                       
    if (X > 0) {
      encodedByte |= 128;
    }                                                                         
    fprintf(uart2,encodedByte);              //Serial.write(encodedByte);   
  }                                       
  while (X > 0);                               
   fprintf(uart2,(topiclength >> 8));         //!  Serial.write(topiclength >> 8);           
   fprintf(uart2,(topiclength & 0xFF));       //!  Serial.write(topiclength & 0xFF);
   fprintf(uart2,(str));                      //!  Serial.print(str);                 
                                         
}                                         

void SendSubscribePacket() {
                               
  memset(str, 0, 250);
  topiclength = strlen(MQTTTopic);
  datalength = 2 + 2 + topiclength + 1;
  delay_ms(1000);
  fprintf(uart2,0x82);      //Serial.write(0x82);
  X = datalength;
  do {
    encodedByte = X % 128;                                               
    X = X / 128;                                       
    if (X > 0) {                   
      encodedByte |= 128;                       
    }
    fprintf(uart2,encodedByte);              //Serial.write(encodedByte);   
  }
  while (X > 0);     
   fprintf(uart2,(MQTTPacketID >> 8));     //!  Serial.write(MQTTPacketID >> 8);       
   fprintf(uart2,(MQTTPacketID & 0xFF));   //!  Serial.write(MQTTPacketID & 0xFF);
   fprintf(uart2,(topiclength >> 8));      //!  Serial.write(topiclength >> 8);
   fprintf(uart2,(topiclength & 0xFF));    //!  Serial.write(topiclength & 0xFF);                 
   fprintf(uart2,(MQTTTopic));             //!  Serial.print(MQTTTopic); 
   fprintf(uart2,(MQTTQOS));               //!  Serial.write(MQTTQOS); 
                           
}                   
     


Here is the original:
http://www.raviyp.com/arduino-mqtt-library-with-publish-and-subscribe-example/
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 20, 2020 4:44 pm     Reply with quote

Here is my opinion of how the SendConnectPacket() routine should be
translated to CCS, with test program:
Code:

#include <18F46K22.h>
#device PASS_STRINGS=IN_RAM
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#include <string.h>

#define LED PIN_B0

unsigned int16 Counter = 0;
unsigned int32 datalength, CheckSum, RLength;
unsigned int16 topiclength;
unsigned char topic[30];
signed char str[250];
unsigned char encodedByte;
signed int16 X;

unsigned int16 MQTTProtocolNameLength;
unsigned int16 MQTTClientIDLength;
unsigned int16 MQTTUsernameLength;
unsigned int16 MQTTPasswordLength;
const char MQTTHost[30] = "m10.cloudmqtt.com";
const char MQTTPort[10] = "17434";
const char MQTTClientID[20] = "ABCDEF";
const char MQTTTopic[30] = "valetron";
const char MQTTProtocolName[10] = "MQIsdp";
const char MQTTLVL = 0x03;
const char MQTTFlags = 0xC2;
const unsigned int16 MQTTKeepAlive = 60;
const char MQTTUsername[30] = "dxxkgkpp";
const char MQTTPassword[35] = "qAUZBdaSIULx";
const char MQTTQOS = 0x00;
const char MQTTPacketID = 0x0001;


void SendConnectPacket(void)
{
 printf("\r\nAT+CIPSEND\r\n");
 delay_ms(3000);
 putc(0x10);

 MQTTProtocolNameLength = strlen(MQTTProtocolName);
 MQTTClientIDLength = strlen(MQTTClientID);
 MQTTUsernameLength = strlen(MQTTUsername);
 MQTTPasswordLength = strlen(MQTTPassword);
 datalength = MQTTProtocolNameLength + 2 + 4 + MQTTClientIDLength + 2 + MQTTUsernameLength + 2 + MQTTPasswordLength + 2;
 X = datalength;
 do{
    encodedByte = X % 128;
    X = X / 128;
    if(X > 0) {
       encodedByte |= 128;
      }
   putc(encodedByte);
   }while (X > 0);


 putc(MQTTProtocolNameLength >> 8);
 putc(MQTTProtocolNameLength & 0xFF);
 
 printf(MQTTProtocolName);
 
 putc(MQTTLVL); // LVL
 putc(MQTTFlags); // Flags
 putc(MQTTKeepAlive >> 8);
 putc(MQTTKeepAlive & 0xFF);
 putc(MQTTClientIDLength >> 8);
 putc(MQTTClientIDLength & 0xFF);
 
 printf(MQTTClientID);

 putc(MQTTUsernameLength >> 8);
 putc(MQTTUsernameLength & 0xFF);

 printf(MQTTUsername);

 putc(MQTTPasswordLength >> 8);
 putc(MQTTPasswordLength & 0xFF);

 printf(MQTTPassword);
 
 putc(0x1A);
}

//======================================
void main()
{         

SendConnectPacket();

while(TRUE);
}
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

PostPosted: Mon Apr 20, 2020 5:24 pm     Reply with quote

PCM programmer wrote:
Here is my opinion of how the SendConnectPacket() routine should be
translated to CCS, with test program:
Code:

.....
while(TRUE);
}


Ohh now the data look so good..... I did the connection and can send data across mqtt:

10 2C 00 06 4D 51 49 73 64 70 03 C2 00 3C 00 06
41 42 43 44 45 46 00 08 64 78 78 6B 67 6B 70 70

Thank you
microjack64



Joined: 10 Apr 2020
Posts: 7

View user's profile Send private message

Arduino printf or putc in pic ccs
PostPosted: Mon Apr 20, 2020 11:32 pm     Reply with quote

I was doing the next Arduino Nano but it has many issues. Now i am using PIC, Very Happy
but i have same issues. Your code really helped me. Please more code or examples. Add for mqtt and how to send data using esp.
_________________
mini engineer at https://projectiot123.com
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group