i am working on esp8266 connected to aws iot ,My project is to control lamp through esp8266 so after reading what should i do for getting the response from get/accepted shadow
i have to send empty parenthesis on get shadow then receive from get/accepted shadow
the problem here that i don't receive any responses from get/accepted shadow
and callback function didnt called untill now
also i tried to increase the size of mqtt packet but nothing happened
//MQTT config
const int maxMQTTpackageSize = 1048;
const int maxMQTTMessageHandlers = 1;
ESP8266WiFiMulti WiFiMulti;
AWSWebSocketClient awsWSclient(1000);
PubSubClient client(awsWSclient);
//# of connections
long connection = 0;
//generate random mqtt clientID
char generateClientID () {
char cID = new char[23]();
for (int i=0; i<22; i+=1)
cID[i]=(char)random(1, 256);
return cID;
}
//count messages arrived
int arrivedcount = 0;
//callback to handle mqtt messages
void callback(char topic, byte payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
//connects to websocket layer and mqtt layer
bool connect () {
if (client.connected()) {
client.disconnect ();
}
//delay is not necessary... it just help us to get a "trustful" heap space value
delay (1000);
Serial.print (millis ());
Serial.print (" - conn: ");
Serial.print (++connection);
Serial.print (" - (");
Serial.print (ESP.getFreeHeap ());
Serial.println (")");
//creating random client id
char* clientID = generateClientID ();
client.setServer(aws_endpoint, port);
if (client.connect(clientID)) {
Serial.println("connected");
return true;
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
return false;
}
}
//subscribe to a mqtt topic
void subscribe () {
client.setCallback(callback);
client.subscribe(aws_topic_sub);
//subscript to a topic
Serial.println("MQTT subscribed");
}
//send a message to a mqtt topic
void sendmessage () {
//send a message
char buf[100];
strcpy(buf, "{ }");
int rc = client.publish(aws_topic_pub, buf);
}
I also had this issue... I solved it by increasing the MQTT_MAX_PACKET_SIZE variable in the PubSubClient.h to 2048 (my shadow string size was too big).
i am working on esp8266 connected to aws iot ,My project is to control lamp through esp8266 so after reading what should i do for getting the response from get/accepted shadow i have to send empty parenthesis on get shadow then receive from get/accepted shadow
the problem here that i don't receive any responses from get/accepted shadow
and callback function didnt called untill now also i tried to increase the size of mqtt packet but nothing happened
Here what i should receive on get/accepted shadow
{ "state": { "desired": { "Switch": 1, "power": "0" }, "delta": { "Switch": 1, "power": "0" } }, "metadata": { "desired": { "Switch": { "timestamp": 1563210303 }, "power": { "timestamp": 1563210303 } } }, "version": 6, "timestamp": 1563322772 }
Here is the code
`#include
include
include
include
//AWS
include "sha256.h"
include "Utils.h"
//WEBSockets
include
include
//MQTT PUBSUBCLIENT LIB
include
//AWS MQTT Websocket
include "Client.h"
include "AWSWebSocketClient.h"
include "CircularByteBuffer.h"
extern "C" {
include "user_interface.h"
}
//AWS IOT config, change these:
char wifi_ssid[] = "Hopa"; char wifi_password[] = "HopaInternet00"; char aws_endpoint[] = "xxxxxxxxxxxxxxxxxxx"; char aws_key[] = "xxxxxxxxxxxxxxxxxxxxx"; char aws_secret[] = "xxxxxxxxxxxxxxx"; char aws_region[] = "xxxxxxxxxxxxxxxxx"; const char aws_topic_sub = "$aws/things/esp/shadow/get/accepted"; const char aws_topic_pub = "$aws/things/esp/shadow/get"; int port = 443;
//MQTT config const int maxMQTTpackageSize = 1048; const int maxMQTTMessageHandlers = 1;
ESP8266WiFiMulti WiFiMulti;
AWSWebSocketClient awsWSclient(1000);
PubSubClient client(awsWSclient);
//# of connections long connection = 0;
//generate random mqtt clientID char generateClientID () { char cID = new char[23](); for (int i=0; i<22; i+=1) cID[i]=(char)random(1, 256); return cID; }
//count messages arrived int arrivedcount = 0;
//callback to handle mqtt messages void callback(char topic, byte payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); }
//connects to websocket layer and mqtt layer bool connect () {
}
//subscribe to a mqtt topic void subscribe () { client.setCallback(callback); client.subscribe(aws_topic_sub); //subscript to a topic Serial.println("MQTT subscribed"); }
//send a message to a mqtt topic void sendmessage () { //send a message
char buf[100]; strcpy(buf, "{ }");
int rc = client.publish(aws_topic_pub, buf); }
void setup() { wifi_set_sleep_type(NONE_SLEEP_T); Serial.begin (115200); delay (2000); Serial.setDebugOutput(1);
}
void loop() {
//keep the mqtt up and running if (awsWSclient.connected ()) {
delay(100);
client.loop (); delay(100); subscribe (); sendmessage ();
} else { //handle reconnection if (connect ()){ subscribe ();
}
}`