Publish data to adafruit using ESP8266 wifi module and arduino


#1

i am trying to publish data to [io.adafruit] using ESP8266 wifi module and arduino. can you help me? i am using your code for sending mqtt packet but its not working…
You said that check protocol name and AIO key. I checked it but it is still same.
Can you suggest me any document libraries or anything that will help me


#2

Code i used is bellow:

#include “WiFiEsp.h”

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include “SoftwareSerial.h”
SoftwareSerial mySerial(6, 7); // RX, TX
#endif

char ssid[] = “Cohive L-5 C”; // your network SSID (name)
char pass[] = “P!!c@123”; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio’s status
String pc;
String wifi;
unsigned int counter=10;
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] = “io.adafruit.com”;
const char MQTTPort[10] = “1883”;
const char MQTTClientID[20] = “ABCDEF”;
const char MQTTTopic[50] = “mayuresh4007/feeds/abc”;
const char MQTTProtocolName[10]=“MQTT”;
const char MQTTLVL = 0x03;
const char MQTTFlags = 0xC2;
unsigned int MQTTKeepAlive=60;
const char MQTTUsername[30] =“mayuresh4007”;
const char MQTTPassword[35] =“AIO key”;
const char MQTTQOS =0X00;
const char MQTTPacketID =0x0001;
void setup()
{
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
mySerial.begin(115200);
// initialize ESP module
/*WiFi.init(&mySerial);

// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
*/
mySerial.println(“AT”);
mySerial.println(“ATE1”);
//mySerial.write(Serial.read());
// }

delay(200);
if (mySerial.available()) {
wifi=mySerial.readString();
Serial.println(wifi);
}

  mySerial.println("AT+CWJAP=\"Cohive L-5 C\",\"P!!c@123\"");
  delay(5000);
  do{

pc=mySerial.readString();
Serial.println(pc);
}while(mySerial.available());
// Serial.println(“You’re connected to the network”);
mySerial.println(“AT+CWMODE=3”);
delay(500);
if (mySerial.available()) {
wifi=mySerial.readString();
Serial.println(wifi);
}
mySerial.println(“AT+CIPMUX=0”);
delay(500);
if (mySerial.available()) {
wifi=mySerial.readString();
Serial.println(wifi);
}
mySerial.println(“AT+CIPMODE=0”);
delay(500);
if (mySerial.available()) {
wifi=mySerial.readString();
Serial.println(wifi);
}

}

void loop()
{

mySerial.println(“AT+CIPSTART=“TCP”,“io.adafruit.com”,1883”);
delay(1000);
do{

pc=mySerial.readString();
Serial.println(pc);
}while(mySerial.available());
//delay(3000);
SendConnectPacket();
do{

pc=mySerial.readString();
// delay(500);
Serial.println(pc);
}while(mySerial.available());
//delay(3000);
SendPublishPacket();
delay(500);
do{

pc=mySerial.readString();
Serial.println(pc);
}while(mySerial.available());
delay(3000);

}

void SendConnectPacket(void)
{

MQTTProtocolNameLength=strlen(MQTTProtocolName);
MQTTClientIDLength=strlen(MQTTClientID);
MQTTUsernameLength=strlen(MQTTUsername);
MQTTPasswordLength=strlen(MQTTPassword);
datalength=MQTTProtocolNameLength+2+4+MQTTClientIDLength+2+MQTTUsernameLength+2+MQTTPasswordLength;
X=datalength;
mySerial.println("\r\nAT+CIPSEND=50\r\n");
delay(3000);
mySerial.write(0x10);

do
{
encodedByte=X%128;
X=X/128;
if(X>0){encodedByte |= 128;}
mySerial.write(encodedByte);
// delay(50);
}

while(X>0);

mySerial.write(MQTTProtocolNameLength >> 8);
mySerial.write(MQTTProtocolNameLength & 0xFF);
mySerial.write(MQTTProtocolName);
//delay(50);
mySerial.write(MQTTLVL);
mySerial.write(MQTTFlags);
mySerial.write(MQTTKeepAlive >> 8);
mySerial.write(MQTTKeepAlive & 0xFF);
//delay(50);
mySerial.write(MQTTClientIDLength >> 8);
mySerial.write(MQTTClientIDLength & 0xFF);
mySerial.write(MQTTClientID);
//delay(50);
mySerial.write(MQTTUsernameLength >> 8);
mySerial.write(MQTTUsernameLength & 0xFF);
mySerial.write(MQTTUsername);
//delay(50);
mySerial.write(MQTTPasswordLength >> 8);
mySerial.write(MQTTPasswordLength & 0xFF);
mySerial.write(MQTTPassword);
//delay(50);
mySerial.write(0x1A);
//delay(50);
}

void SendPublishPacket(void)
{
mySerial.write("\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);
mySerial.write(0x30);
X=datalength+2;
do
{

encodedByte = X%128;
X=X/128;
if(X>0){encodedByte |= 128;}
mySerial.write(encodedByte);
//delay(50);
}

while(X > 0);

mySerial.write(topiclength >> 8);
mySerial.write(topiclength & 0xFF);
mySerial.write(str);
//delay(50);
mySerial.write(0x1A);
}


#3

Hi,
Did you try the library that Adafruit provides for connecting to their server ?