Valerie OdiohUbiMaker
Published

Greenhouse Monitoring/Alarm System with ESP8266 & Ubidots

A simple to build and easy-to-use web-based dashboard display with programmable intelligent alarm notifications based on your plant’s health

BeginnerFull instructions provided3,864
Greenhouse Monitoring/Alarm System with ESP8266 & Ubidots

Story

Read more

Code

Code snippet #7

Plain text
/****************************************
   Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"
#include <SHT1x.h>
/****************************************
   Define Constants
 ****************************************/
#define TOKEN "............................." // Your Ubidots TOKEN
#define WIFINAME "..................." //Your SSID
#define WIFIPASS "...................." // Your Wifi Pass
/*****Not to be changed****/
#define DEVICE_LABEL "plantms"
#define VARIABLE_LABEL_1 "light-intensity"
#define VARIABLE_LABEL_2 "temperature"
#define VARIABLE_LABEL_3 "soilmoisture"
#define VARIABLE_LABEL_4 "buzzer"
#define DATAPIN D7
#define CLCKPIN D5
#define LIGHT_SENSOR A0
#define BUZZER D3

Ubidots ubiclient(TOKEN);
WiFiClient client;
SHT1x sht1x(DATAPIN, CLCKPIN);
/****************************************
   Auxiliar Functions
 ****************************************/
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]);
  }
  if ((char)payload[0] == '1') {
    digitalWrite(BUZZER, HIGH);
  }
  else {
    digitalWrite(BUZZER, LOW);
  }
  Serial.println();
}
char* getUbidotsDevice(char* deviceLabel) {
  char* data = (char *) malloc(sizeof(char) * 700);
  char* response = (char *) malloc(sizeof(char) * 400);
  sprintf(data, "GET /api/v1.6/devices/%s/", deviceLabel);
  sprintf(data, "%s HTTP/1.1\r\n", data);
  sprintf(data, "%sHost: industrial.api.ubidots.com\r\nUser-Agent:esp8266/1.0\r\n", data);
  sprintf(data, "%sX-Auth-Token: %s\r\nConnection: close\r\n\r\n", data, TOKEN);
  if (client.connect("industrial.api.ubidots.com", 80)) {
    client.println(data);
  } else {
    return "e";
  }
  free(data);
  int timeout = 0;
  while (!client.available() && timeout < 5000) {
    timeout++;
    if (timeout >= 4999) {
      return "e";
    }
    delay(1);
  }
  int i = 0;
  while (client.available()) {
    response[i++] = (char)client.read();
    if (i >= 399) {
      break;
    }
  }
  char * pch;
  char * statusCode;
  int j = 0;
  pch = strtok (response, " ");
  while (pch != NULL) {
    if (j == 1 ) {
      statusCode = pch;
    }
    pch = strtok (NULL, " ");
    j++;
  }
  free(response);
  return statusCode;
}
/****************************************
   Main Functions
 ****************************************/
void setup() {
  // put your setup code here, to run once:
  pinMode(BUZZER, OUTPUT);
  ubiclient.ubidotsSetBroker("industrial.ubidots.com"); // Sets the broker properly for the business account
  ubiclient.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  ubiclient.wifiConnection(WIFINAME, WIFIPASS);
  ubiclient.begin(callback);
  if (!ubiclient.connected()) {
    ubiclient.reconnect();
  }
  // Getting info of device
  char* deviceStatus = getUbidotsDevice(DEVICE_LABEL);
  // verify if the device is created
  if (strcmp(deviceStatus, "404") == 0) {
    ubiclient.add(VARIABLE_LABEL_4, 0); //Insert your variable Labels and the value to be sen
    ubiclient.ubidotsPublish(DEVICE_LABEL);
    ubiclient.loop();
  }
  ubiclient.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL_4); //Insert the dataSource and Variable's Labels
}
void loop() {
  // put your main code here, to run repeatedly:
  if (!ubiclient.connected()) {
    ubiclient.reconnect();
    ubiclient.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL_4); //Insert the dataSource and Variable's Labels
  }
  // Taking sensor readings
  int light_intesity_reading = analogRead(LIGHT_SENSOR);
  float tempc = sht1x.readTemperatureC();
  float soilmoisture = sht1x.readHumidity();
  // Publishing values to assigned variables
  ubiclient.add(VARIABLE_LABEL_1, light_intesity_reading);
  ubiclient.add(VARIABLE_LABEL_2, tempc);
  ubiclient.ubidotsPublish(DEVICE_LABEL);
  ubiclient.loop();
  ubiclient.add(VARIABLE_LABEL_3, soilmoisture);
  ubiclient.ubidotsPublish(DEVICE_LABEL);
  ubiclient.loop();
  delay(1000);
}

Code snippet #8

Plain text
/****************************************
   Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"
#include <SHT1x.h>
/****************************************
   Define Constants
 ****************************************/
#define TOKEN "............................." // Your Ubidots TOKEN
#define WIFINAME "..................." //Your SSID
#define WIFIPASS "...................." // Your Wifi Pass
/*****Not to be changed****/
#define DEVICE_LABEL "plantms"
#define VARIABLE_LABEL_1 "light-intensity"
#define VARIABLE_LABEL_2 "temperature"
#define VARIABLE_LABEL_3 "soilmoisture"
#define VARIABLE_LABEL_4 "buzzer"
#define DATAPIN D7
#define CLCKPIN D5
#define LIGHT_SENSOR A0
#define BUZZER D3

Ubidots ubiclient(TOKEN);
WiFiClient client;
SHT1x sht1x(DATAPIN, CLCKPIN);
/****************************************
   Auxiliar Functions
 ****************************************/
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]);
  }
  if ((char)payload[0] == '1') {
    digitalWrite(BUZZER, HIGH);
  }
  else {
    digitalWrite(BUZZER, LOW);
  }
  Serial.println();
}
char* getUbidotsDevice(char* deviceLabel) {
  char* data = (char *) malloc(sizeof(char) * 700);
  char* response = (char *) malloc(sizeof(char) * 400);
  sprintf(data, "GET /api/v1.6/devices/%s/", deviceLabel);
  sprintf(data, "%s HTTP/1.1\r\n", data);
  sprintf(data, "%sHost: industrial.api.ubidots.com\r\nUser-Agent:esp8266/1.0\r\n", data);
  sprintf(data, "%sX-Auth-Token: %s\r\nConnection: close\r\n\r\n", data, TOKEN);
  if (client.connect("industrial.api.ubidots.com", 80)) {
    client.println(data);
  } else {
    return "e";
  }
  free(data);
  int timeout = 0;
  while (!client.available() && timeout < 5000) {
    timeout++;
    if (timeout >= 4999) {
      return "e";
    }
    delay(1);
  }
  int i = 0;
  while (client.available()) {
    response[i++] = (char)client.read();
    if (i >= 399) {
      break;
    }
  }
  char * pch;
  char * statusCode;
  int j = 0;
  pch = strtok (response, " ");
  while (pch != NULL) {
    if (j == 1 ) {
      statusCode = pch;
    }
    pch = strtok (NULL, " ");
    j++;
  }
  free(response);
  return statusCode;
}
/****************************************
   Main Functions
 ****************************************/
void setup() {
  // put your setup code here, to run once:
  pinMode(BUZZER, OUTPUT);
  ubiclient.ubidotsSetBroker("industrial.ubidots.com"); // Sets the broker properly for the business account
  ubiclient.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  ubiclient.wifiConnection(WIFINAME, WIFIPASS);
  ubiclient.begin(callback);
  if (!ubiclient.connected()) {
    ubiclient.reconnect();
  }
  // Getting info of device
  char* deviceStatus = getUbidotsDevice(DEVICE_LABEL);
  // verify if the device is created
  if (strcmp(deviceStatus, "404") == 0) {
    ubiclient.add(VARIABLE_LABEL_4, 0); //Insert your variable Labels and the value to be sen
    ubiclient.ubidotsPublish(DEVICE_LABEL);
    ubiclient.loop();
  }
  ubiclient.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL_4); //Insert the dataSource and Variable's Labels
}
void loop() {
  // put your main code here, to run repeatedly:
  if (!ubiclient.connected()) {
    ubiclient.reconnect();
    ubiclient.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL_4); //Insert the dataSource and Variable's Labels
  }
  // Taking sensor readings
  int light_intesity_reading = analogRead(LIGHT_SENSOR);
  float tempc = sht1x.readTemperatureC();
  float soilmoisture = sht1x.readHumidity();
  // Publishing values to assigned variables
  ubiclient.add(VARIABLE_LABEL_1, light_intesity_reading);
  ubiclient.add(VARIABLE_LABEL_2, tempc);
  ubiclient.ubidotsPublish(DEVICE_LABEL);
  ubiclient.loop();
  ubiclient.add(VARIABLE_LABEL_3, soilmoisture);
  ubiclient.ubidotsPublish(DEVICE_LABEL);
  ubiclient.loop();
  delay(1000);
}

Github file

https://github.com/nodemcu/nodemcu-devkit/wiki/Getting-Started-on-OSX

Github

https://github.com/ubidots/ubidots-mqtt-esp

Credits

Valerie Odioh

Valerie Odioh

1 project • 0 followers
As a Electrical and Electronics, I have been able to equip and develop impressive skills to excel in various projects.
UbiMaker

UbiMaker

53 projects • 230 followers
Maker @ ubidots.com

Comments