Zoran Roncevic
Published © GPL3+

AllThingsTalk Maker Platform & NodeMCU

Getting started project for "AllThingsTalk MAKER". Client app works on NodeMCU.

IntermediateProtip1 hour789
AllThingsTalk Maker Platform & NodeMCU

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

AllThingsTalk Maker
AllThingsTalk Maker

Story

Read more

Code

att_mqtt.ino

C/C++
/*
  Basic ESP8266 MQTT example
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

// Update these with values suitable for your network.

const char* ssid = "WiFi SSID .....";
const char* password = "WiFi password .....";
const char* mqtt_server = "api.allthingstalk.io";

char deviceId[] = "YourDeviceId...";
char token[] = "YourDeviceToken...";

char outTopicCounter[] = "device/[YourDeviceId]/asset/[AssetName]/state";
char inTopic[] = "device/[YourDeviceId]/asset/+/command";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];

long int counter = 0;

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {

  StaticJsonBuffer<500> jsonBuffer;
  char json[500];

  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    json[i] = (char)payload[i];
  }
  json[length] = '\0';
  
  JsonObject& root = jsonBuffer.parseObject(json);

  if (root.success()) {
    const char* vrednost = root["value"];
    Serial.println(vrednost);
    if (strcmp(vrednost,"1") == 0) {
      digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    } else {
      digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
    }
  } else {
    Serial.println("Parsing JSON failed");
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client", token, "")) {
      Serial.println("connected");
      client.subscribe(inTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 1000) {
    lastMsg = now;
    ++counter;
    String value;
    value = String(counter);
    snprintf (msg, 75, "0|%s", value.c_str());
    client.publish(outTopicCounter, msg);
  }
}

Credits

Zoran Roncevic

Zoran Roncevic

19 projects • 117 followers
Hackster Live ambassador in Serbia. Organizer of Maker NS community.

Comments