Shaukatali HusseinSahil Abdulalim
Published © GPL3+

Black Soldier Fly Farming - Creating protein content

Creating sustainable and accessible protein to complement animal feed

IntermediateWork in progress3 hours827

Things used in this project

Story

Read more

Schematics

Phase 1 Connections and Flow

Wire the sensors via the Wio Terminal Ports and behind as per the code

Phase 2 Connections and Flow

Similar to phase 1 but work in progress, will share the updated codes soon upon completion

Code

Wio Terminal + Sensors + Ubidots

Arduino
Complete and working code
#include <PubSubClient.h>
#include <rpcWiFi.h>
#include <TFT_eSPI.h>
#include <SensirionI2CSht4x.h>
#include <Wire.h>
#include"LIS3DHTR.h"

int sensor = A0;
int sensorvalue = 0;

LIS3DHTR<TwoWire> lis;

#define WIFISSID "*******" //your wifi name
#define PASSWORD "*******" // wifi password
#define TOKEN "**********************" // defined token from ubidots
#define VARIABLE_LABEL1 "temperature" 
#define VARIABLE_LABEL2 "humidity"
#define VARIABLE_LABEL3 "light"
#define VARIABLE_LABEL4 "soil moisture"
#define DEVICE_LABEL "wio-terminal" 
#define MQTT_CLIENT_NAME "*******" // MQTT name
 
const long interval = 100;
unsigned long previousMillis = 0;
 
char mqttBroker[] = "industrial.api.ubidots.com";
 
WiFiClient wifiClient;
PubSubClient client(wifiClient);
 
TFT_eSPI tft = TFT_eSPI();
SensirionI2CSht4x sht4x;
 
static float temp = 0;
static float humi = 0;
static int lightValue = 0;
static float moisturevalue= 0;


 
     // create spaces for storing sensor values
      char str_temp[6];
      char str_humi[6];
      static char str_light[6];
      char str_soilmoisture[6];
      char payload [700];
      char topic [150];
 
      void callback(char* topic, byte* payload, unsigned int length){
        Serial.begin(9600);
        Serial.print("Message arrived [");
        Serial.print(topic);
        Serial.print("] ");
        for (int i=0;i<length;i++) {
          Serial.print((char)payload[i]);
        }
      }
       
      void reconnect() {
        // loop until it is connected
        while (!client.connected()) {
          Serial.println("Attempting MQTT connection...");
       
        // Attempt to connect
        if (client.connect(MQTT_CLIENT_NAME, TOKEN,"")) {
          Serial.println("connected");
        }
        else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 2 seconds");
          delay(2000);
          }
        }
      }

      void read_soilmoisture()
      {
       moisturevalue = analogRead(sensor); 
       Serial.print("moisture is: ");
       Serial.println(moisturevalue);
       delay(200);
       
        
        }
 
            void read_sht40()
            {
              uint16_t error;
              char errorMessage[256];
              error = sht4x.measureHighPrecision(temp, humi);
              if (error) {
                  Serial.print("Error trying to execute measureHighPrecision(): ");
                  errorToString(error, errorMessage, 256);
                  Serial.println(errorMessage);
              } else {
                  Serial.print("Temperature:");
                  Serial.print(temp);
                  Serial.print("\t");
                  Serial.print("Humidity:");
                  Serial.println(humi);
              }
            }

        void read_builtin()
        {
          lightValue = analogRead(WIO_LIGHT);
          Serial.print("Light = ");
          Serial.println(lightValue);
        }
         
        void send_data()
        {
          dtostrf(temp, 4, 2, str_temp);
          dtostrf(humi, 4, 2, str_humi);
          dtostrf(lightValue, 4, 0, str_light);
          dtostrf(sensorvalue, 4 ,2, str_soilmoisture);
        
          if (!client.connected()) {
            reconnect();
          }
         
          // Builds the topic
          sprintf(topic, "%s", ""); // for cleaning the topic
          sprintf(topic, "%s%s", "/v2.0/devices/", DEVICE_LABEL);
         
          //Builds the payload
          sprintf(payload, "%s", ""); // for cleaning the payload
          sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); 
          sprintf(payload, "%s%s", payload, str_temp); // for adding the value 
          sprintf(payload, "%s}", payload); // for closing the dictionary brackets
          client.publish(topic, payload);
          delay(500);
         
          sprintf(payload, "%s", ""); 
          sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); 
          sprintf(payload, "%s%s", payload, str_humi); 
          sprintf(payload, "%s}", payload); 
          client.publish(topic, payload);
          delay(500);
        
          sprintf(payload, "%s", ""); 
          sprintf(payload, "{\"%s\":", VARIABLE_LABEL3); 
          sprintf(payload, "%s%s", payload, str_light); 
          sprintf(payload, "%s}", payload); 
          client.publish(topic, payload);
          delay(500);

          sprintf(payload, "%s", ""); 
          sprintf(payload, "{\"%s\":", VARIABLE_LABEL4); 
          sprintf(payload, "%s%s", payload, str_soilmoisture); 
          sprintf(payload, "%s}", payload); 
          client.publish(topic, payload);
          delay(500);
         
          
         
          client.loop();
        }
         
        void setup() {
          Serial.begin(115200);
          Wire.begin();
          lis.begin(Wire1);
          pinMode(WIO_LIGHT, INPUT);
          pinMode(sensor,INPUT);
          sht4x.begin(Wire);
         
          tft.begin();
          tft.setRotation(3);
          tft.setTextSize(2);
          tft.fillScreen(TFT_BLACK);
          
          lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); //Data output rate
          lis.setFullScaleRange(LIS3DHTR_RANGE_2G);
         
        //  while(!Serial);
         
          // Set WiFi to station mode and disconnect from an AP if it was previously connected
          WiFi.mode(WIFI_STA);
          WiFi.disconnect();
         
          tft.drawString("Connecting to WiFi...",20,120);
          WiFi.begin(WIFISSID, PASSWORD);
         
          while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            WiFi.begin(WIFISSID, PASSWORD);
          }
         
          tft.fillScreen(TFT_BLACK);
          tft.drawString("Connected to the WiFi",20,120);
         
          delay(1000);
          client.setServer(mqttBroker, 1883);
          client.setCallback(callback);
        }
         
        void loop() {
          read_sht40();    //Reading sht40 sensor values
          read_builtin();   //Reading buile-in sensor values
          read_soilmoisture();
          send_data();     //Sending data to Ubidots
          delay(5000);
        }

Credits

Shaukatali Hussein

Shaukatali Hussein

7 projects • 10 followers
Sahil Abdulalim

Sahil Abdulalim

5 projects • 8 followers
Tech enthusiast...not much to say😂

Comments