C++ Can';t使用Arduino和SIM900连接或发布到MQTT代理
我编写了一个简单的草图,将连接和发布数据包发送到cloudMQTT服务器。我没有从SIM900返回任何错误,但cloudMQTT仪表板上没有显示任何内容C++ Can';t使用Arduino和SIM900连接或发布到MQTT代理,c++,tcp,arduino,serial-communication,gprs,C++,Tcp,Arduino,Serial Communication,Gprs,我编写了一个简单的草图,将连接和发布数据包发送到cloudMQTT服务器。我没有从SIM900返回任何错误,但cloudMQTT仪表板上没有显示任何内容 #include <SoftwareSerial.h> SoftwareSerial SoftSerial( 9, 10 ); // These varaibles are passed to functionality that generates the connect and publish packets. This co
#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;
}
#包括
软件系列软件系列(9,10);
//这些变量被传递给生成连接和发布数据包的功能。此代码取自Ravi Pujar编写的草图
无符号整数计数器=0;
无符号长数据长度、校验和、RLength;
无符号短topiclength;
未签名字符主题[30];
char-str[250];
无符号字符编码字节;
int X;
无符号短MQTTProtocolNameLength;
无符号短MQTTCLIENTDLENGHT;
无符号短MQTTUsernameLength;
无符号短MQTTPasswordLength;
const char MQTTHost[30]=“hairdresser.cloudmqtt.com”;
常量字符MQTTPort[10]=“18958”;
常量字符MQTTClientID[20]=“ABCDEF”;
常量字符MQTTTopic[30]=“valetron”;
常量字符MQTTProtocolName[10]=“MQIsdp”;
常量字符MQTTLVL=0x03;
常量字符MQTTFlags=0xC2;
常量无符号整数MQTTKeepAlive=60;
常量字符MQTTUsername[30]=“uroxeeil”;
常量字符MQTTPassword[35]=“ifG3xIxaf5gT”;
常量字符MQTTQOS=0x00;
常量字符MQTTPacketID=0x0001;
无符号字符缓冲区[64];
int计数器=0;
无效设置()
{
//设置软件串行和硬件串行的babud速率
连载《开始》(19200);
SoftSerial.begin(19200年);
延迟(2000年);
//这段代码发送AT命令来初始化SIM900
//--------------在初始化时-这工作正常--------------//
SoftSerial.write(“\r\nAT\r\n”);
延迟(3000);
while(SoftSerial.available()>0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0)
{
缓冲区[counter++]=SoftSerial.read();
如果(计数器==64)中断;
}
串行写入(缓冲器、计数器);
对于(int i=0;i0){
编码字节|=128;
}
软串行写入(编码字节);
}
而(X>0);
SoftSerial.write(MQTTProtocolNameLength>>8);
SoftSerial.write(MQTTProtocolNameLength&0xFF);
SoftSerial.print(MQTTProtocolName);
SoftSerial.write(MQTTLVL);//LVL
SoftSerial.write(MQTTFlags);//标志
SoftSerial.write(MQTTKeepAlive>>8);
SoftSerial.write(MQTTKeepAlive&0xFF);
SoftSerial.write(mqttclientdleength>>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(MQTTPass
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial( 9, 10 );
// Go Look up the structure of MQTT packets and from there you will learn how to compile these
byte connectPacket[ 19 ] =
{
0x10, 0x11, 0x00, 0x04, 0x4D, 0x51, 0x54, 0x54, 0x04, 0x02, 0x00, 0x3C, 0x00, 0x05, 0x54, 0x32, 0x54, 0x49, 0x44
};
byte publishPacket[ 26 ] =
{
0x30, 0x18, 0x00, 0x0E, 0x54, 0x32, 0x54, 0x5F, 0x54, 0x6F, 0x70, 0x69, 0x63, 0x5F, 0x44, 0x65, 0x6D, 0x6F, 0x54, 0x65, 0x6D, 0x70, 0x3A, 0x20, 0x33, 0x30
};
unsigned char buffer[ 64 ];
int counter = 0;
// Simple function to send an AT command and see its response
void sendAtCommandWithResponse( char message[] )
{
SoftSerial.write( message );
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;
}
void setup()
{
Serial.begin( 19200 );
SoftSerial.begin( 19200 );
delay(10000);
// -- Configure the SIM900 and establish the TCP connection to the server
sendAtCommandWithResponse( "\r\nAT\r\n" );
delay(2000);
//-----
sendAtCommandWithResponse( "\r\nAT+CIPSHUT\r\n" );
delay(2000);
//-----
sendAtCommandWithResponse( "\r\nAT+CIPMUX=0\r\n" );
delay(2000);
//-----
sendAtCommandWithResponse( "\r\nAT+CGATT=1\r\n" );
delay(2000);
//-----
sendAtCommandWithResponse( "\r\nAT+CSTT=\"myMTN\", \"\", \"\"\r\n" );
delay(2000);
//-----
sendAtCommandWithResponse( "\r\nAT+CIICR\r\n" );
delay(2000);
//-----
sendAtCommandWithResponse( "\r\nAT+CIFSR\r\n" );
delay(2000);
//-----
SoftSerial.write( "\r\nAT+CIPSTART=\"TCP\", \"test.mosquitto.org\", \"1883\"\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);
//-----
// -- Send the connect packet
sendAtCommandWithResponse( "\r\nAT+CIPSEND\r\n" );
delay(3000);
SoftSerial.write( connectPacket, sizeof( connectPacket ) );
// This tells the SIM900 to send the packet
SoftSerial.write(0x1A);
delay(3000);
// -- Send the publish packet
sendAtCommandWithResponse( "\r\nAT+CIPSEND\r\n" );
delay(3000);
SoftSerial.write( publishPacket, sizeof( publishPacket ) );
// This tells the SIM900 to send the packet
SoftSerial.write(0x1A);
}
void loop()
{
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;
}