esp8266 send data to mesosfer not work!

Home - esp8266 send data to mesosfer not work!

Supri Yanto asked 2 years ago

i folow this tutorial https://mesosfer.com/community/how-to-send-push-notification-android-mobile-apps-from-mesosfer-backend-as-a-service.html.
and this code me.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = “apmeme”;
const char* password = “supriyanto”;
const char* mqtt_server = “api.mesosfer.com”;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
randomSeed(micros());
Serial.println(“”);
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
}
/// consumer
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(topic);
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
}
void reconnect() {
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
String clientId = “ESP8266Client-“;
clientId += String(random(0xffff), HEX);

// Attempt to connect broker
if (client.connect(clientId.c_str(), “xxx”, “zzzz”)) {
Serial.println(“connected”);
client.subscribe(“xxx”);
client.publish(“xxx”, “{\”appId\”:\”Q8lhfuQCoo\”,\”schemaId\”:\”3vaG7iJBfg\”,\”bucket\”:\”Log1\”,\”metadata\”:{\”sensor\”:\”foo\”}}”);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(” try again in 5 seconds”);
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
}

please help me , why my code not work.

Supri Yanto replied 2 years ago

there is something wrong this code // Attempt to connect broker
if (client.connect(clientId.c_str(), “espnotif”, “qDZnLdtAQotSbIBD”)) {
Serial.println(“connected”);
client.subscribe(“chanel1”);
client.publish(“chanel1”, “{\”appId\”:\”Q8lhfuQCoo\”,\”schemaId\”:\”3vaG7iJBfg\”,\”bucket\”:\”Log1\”,\”metadata\”:{\”sensor\”:\”foo\”}}”);

because data in bucket noting display

2 Answers
Badrullami Staff answered 2 years ago

Hi Supri,
Please change subscribe and publish value to this format –> (“device/your_username/your_channel”). Like example below :

  • client.subscribe(“device/xxx/yyy”);
  • client.publish(“device/xxx/yyy”,“{\”appId\”:\”Q8lhfuQCoo\”,\”schemaId\”:\”3vaG7iJBfg\”,\”bucket\”:\”Log1\”,\”metadata\”:{\”sensor\”:\”foo\”}}”);

 

Please try again.

Thanks.

Supri Yanto replied 2 years ago

this work thanks, 😀

Badrullami Staff replied 2 years ago

Please close and rate this post.. 🙂

Girly Saputri Staff answered 2 years ago

Hi Supri Yanto,
Are you satisfied with Badrullami\’s answer? If yes, please click the check button. If you don’t, let me know what can community do for you.

Your Answer