【发布时间】:2020-01-15 12:54:19
【问题描述】:
我写了一个简单的草图来发送一个连接和发布数据包到 cloudMQTT 服务器。我没有从 SIM900 返回任何错误,但 cloudMQTT 仪表板上没有任何显示。
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial( 9, 10 );
// These varaibles are passed to functionality that generates the connect and publish packets. This code was taken from a sketch written by Ravi Pujar
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;
const char MQTTHost[30] = "hairdresser.cloudmqtt.com";
const char MQTTPort[10] = "18958";
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 int MQTTKeepAlive = 60;
const char MQTTUsername[30] = "uroxeeil";
const char MQTTPassword[35] = "ifG3xIxaf5gT";
const char MQTTQOS = 0x00;
const char MQTTPacketID = 0x0001;
unsigned char buffer[ 64 ];
int counter = 0;
void setup()
{
// Set the babud rate of both the software serial and hardware serial
Serial.begin( 19200 );
SoftSerial.begin( 19200 );
delay(2000);
// This section of code sends AT commands to initialize the SIM900
//-------------- AT INITIALIZATION - This is working fine--------------//
SoftSerial.write( "\r\nAT\r\n" );
delay(3000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CIPSHUT\r\n" );
delay(3000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CIPMUX=0\r\n" );
delay(3000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CGATT=1\r\n" );
delay(3000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CSTT=\"myMTN\", \"\", \"\"\r\n" );
delay(3000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CIICR\r\n" );
delay(3000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CIFSR\r\n" );
delay(3000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CIPSTART=\"TCP\", \"hairdresser.cloudmqtt.com\", \"18958\"\r\n" );
delay(10000);
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
delay(2000);
//-------------- END OF AT INITIALIZATION --------------//
//This is where the issue lies
// Send the connect packet. This code was written by Ravi Pujar
SoftSerial.write( "\r\nAT+CIPSEND\r\n" );
delay(3000);
SoftSerial.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;
}
SoftSerial.write(encodedByte);
}
while (X > 0);
SoftSerial.write(MQTTProtocolNameLength >> 8);
SoftSerial.write(MQTTProtocolNameLength & 0xFF);
SoftSerial.print(MQTTProtocolName);
SoftSerial.write(MQTTLVL); // LVL
SoftSerial.write(MQTTFlags); // Flags
SoftSerial.write(MQTTKeepAlive >> 8);
SoftSerial.write(MQTTKeepAlive & 0xFF);
SoftSerial.write(MQTTClientIDLength >> 8);
SoftSerial.write(MQTTClientIDLength & 0xFF);
SoftSerial.print(MQTTClientID);
SoftSerial.write(MQTTUsernameLength >> 8);
SoftSerial.write(MQTTUsernameLength & 0xFF);
SoftSerial.print(MQTTUsername);
SoftSerial.write(MQTTPasswordLength >> 8);
SoftSerial.write(MQTTPasswordLength & 0xFF);
SoftSerial.print(MQTTPassword);
SoftSerial.write(0x1A);
delay( 2000 );
// Send the publish packet. This code was written by Ravi Pujar
SoftSerial.print("\r\nAT+CIPSEND\r\n");
delay(3000);
memset(str, 0, 250);
topiclength = sprintf((char * ) topic, MQTTTopic);
datalength = sprintf((char * ) str, "%s%u", topic, Counter);
delay(1000);
Serial.write(0x30);
X = datalength + 2;
do {
encodedByte = X % 128;
X = X / 128;
if (X > 0) {
encodedByte |= 128;
}
SoftSerial.write(encodedByte);
}
while (X > 0);
SoftSerial.write(topiclength >> 8);
SoftSerial.write(topiclength & 0xFF);
SoftSerial.print(str);
SoftSerial.write(0x1A);
}
void loop()
{
// Display any response that has been sent after the first CIPSEND
while ( SoftSerial.available() > 0 )
{
buffer[ counter++ ] = SoftSerial.read();
if( counter == 64 ) break;
}
Serial.write( buffer, counter );
for( int i = 0; i < counter; i++ )
{
buffer[ i ] = NULL;
}
counter = 0;
}
但是建立tcp连接后,除了我设备的IP之外,cloudMQTT服务器上什么都没有显示。
【问题讨论】:
标签: c++ tcp arduino serial-communication gprs