Juan Carlos
Published © MIT

Automatic control of wine store

The wine store should be a place that is constantly monitored and regularized. This automatic control of wine store solution can do that.

AdvancedWork in progress3 hours786
Automatic control of wine store

Things used in this project

Hardware components

SparkFun ESP8266 Thing - Dev Board
SparkFun ESP8266 Thing - Dev Board
Wifi controller
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Grove - Light Sensor
Seeed Studio Grove - Light Sensor
×1

Software apps and online services

Cayenne
myDevices Cayenne
cronniot

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

Circuit map

Code

ESP8266 firmware

C/C++
Comunicate the ESP8266 device with Cronniot IOT Service
/**
    Cronniot_ESP8266
    Purpose: Comunicate the ESP8266 device with Cronniot IOT Service

    @author Juan Carlos Gras
    @version 1.0 07/29/17
    @licence MIT
*/

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

#define MQTT_SERVER "cronniot.com"
#define MQTT_PORT 8883

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

typedef struct { 
  String deviceId;
  uint8_t pin;
} pinDevice;

//######## Fill data ################
const char* ssid = "#############";       //wifi name
const char* password = "##############";          //wifi password
const char* controllerId = "#############";     //ESP8266 identifier
const char* secretKey = "#############";    //user secret code
const pinDevice digitPins[] {               //definition of pins to use: device Id / pin number
  {"w3mm6ajzu", 10},
  {"deviceId", 10}
};
//###################################

WiFiClientSecure wifiClient;
PubSubClient client(MQTT_SERVER, MQTT_PORT, callback, wifiClient);

void setup() {
  Serial.begin(115200);
  for(uint8_t i = 0; i < sizeof(digitPins)/sizeof(pinDevice); ++i) {
      pinMode(digitPins[i].pin, OUTPUT);
      digitalWrite(digitPins[i].pin, LOW);
  }
  delay(100);
  WiFi.begin(ssid, password); //start wifi subsystem
  reconnect();
  delay(2000);
}

void loop() {
  if (!client.connected() && WiFi.status() == 3) { reconnect(); }   //reconnect if connection is lost
  client.loop();
  delay(10);
}

void callback(char* topic, byte* payload, unsigned int length) {  
  String msDeviceId;
  String msSecretKey;
  bool readDevice = true;

  for(int i = 2; i < length; i++) {
      if(payload[i] == '_') { 
        readDevice = false;
        continue;
      }
      if(readDevice) { msDeviceId += (char)payload[i]; }
      else { msSecretKey += (char)payload[i]; }
  }

  char* outputQueue = new char[length + 3];
  strcpy(outputQueue, msDeviceId.c_str());
  strcat(outputQueue, "_");
  strcat(outputQueue, msSecretKey.c_str());
  strcat(outputQueue, "_out");
  
  if(msSecretKey != secretKey) {
    client.publish(outputQueue, "-1");                               //publish to the MQTT server an error message
    return;
  }
  
  uint8_t pin = 0;
  uint8_t a = 0;
  const uint8_t arrLength = sizeof(digitPins)/sizeof(pinDevice);

  while(pin == 0 && a < arrLength) {
    if(digitPins[a].deviceId == msDeviceId) { pin = digitPins[a].pin; }
    else { a++; }      
  }

  if(pin == 0) {
    client.publish(outputQueue, "-1");                              //publish to the MQTT server an error message
    return;
  }
  if(payload[0] == '1'){
    digitalWrite(pin, HIGH);                                        //turn the device pin 'on'
    client.publish(outputQueue, "1");                               //publish to the MQTT server a confirmation message
  }
  else if (payload[0] == '0'){
    digitalWrite(pin, LOW);                                         //turn the device pin 'off'
    client.publish(outputQueue, "0");                               //publish to the MQTT server a confirmation message
  }
  else if (payload[0] == 'S'){
    client.publish(outputQueue, String(digitalRead(pin)).c_str());  //publish to the MQTT server an status confirmation message
  }
}

/**
    Connect to the WIFI network and to the MQTT server
*/
void reconnect() {
  if(WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.println(ssid);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");  
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }

  if(WiFi.status() == WL_CONNECTED){
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");      
      uint8_t mac[6];
      WiFi.macAddress(mac);
      if (client.connect((char*) macToStr(mac).c_str(), "#############", "#############")) {
        Serial.print("\tMTQQ Connected");
        client.subscribe(controllerId);
      }
      else { Serial.println("\tFailed."); abort(); }
    }
  }
}

/**
    Generate unique name from MAC address
    
    @param numeric array of mac address
    @return a String mac address
*/
String macToStr(const uint8_t* mac) {
  String result;
  for (uint8_t i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5) { result += ':'; }
  }
  return "esp8266-" + result;
}

Credits

Juan Carlos

Juan Carlos

0 projects • 2 followers

Comments