NAzT
7/20/2017 - 7:47 AM

oled-mqtt.ino

oled-mqtt.ino

#include <Arduino.h>

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

ESPert espert;


#include "CMMC_Blink.hpp"
CMMC_Blink blinker;
#define relayPin 15

// Update these with values suitable for your network.
const char* ssid = "IoT-Workshop";
const char* password = "espresso";
const char* mqtt_server = "mqtt.espert.io";

#define inTopic "ESPert/nat/Command"
#define outTopic "ESPert/nat/Status"

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

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  pinMode(relayPin, OUTPUT);
  Serial.begin(115200);
  espert.init();
  espert.oled.init();


  espert.oled.clear();
  espert.oled.setTextSize(1);
  espert.oled.setTextColor(ESPERT_WHITE);
  espert.oled.setCursor(0, 0);

  blinker.init();
  blinker.blink(200, LED_BUILTIN);
  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);
  espert.oled.print("Connecting to ");
  espert.oled.println(ssid);

  WiFi.begin(ssid, password);

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

  blinker.blink(50, LED_BUILTIN);
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

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

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  payload[length] = '\0';
  String payloadString = String((char*)payload);

  espert.oled.clear();
  espert.oled.println(payloadString);
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  // Switch on the LED if an 1 was received as first character
  if (payloadString == "ON") {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    digitalWrite(relayPin, HIGH);
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else if (payloadString == "OFF") {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
    digitalWrite(relayPin, LOW);
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = String(ESP.getChipId());
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      blinker.detach(HIGH);
      // Once connected, publish an announcement...
      client.publish(outTopic, "hello world");
      // ... and resubscribe
      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()) {
    blinker.blink(50, LED_BUILTIN);
    reconnect();
  }
  client.loop();

  long now = millis();
  float temp = 25.00;
  float humid = 75.00;
  if (now - lastMsg > 2000) {
    lastMsg = now;
    // generate JSON=> {"temperature": 25.00, "humidity": 75.00}
    String text = "{";
    text += "\"temperature\": ";
    text += String(temp);
    text += ", \"humidity\": ";
    text += humid;
    text += "}";
    Serial.print("Publish message: ");
    Serial.println(text.c_str());
    client.publish(outTopic, text.c_str());
  }
}