Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Attila Tőkés
Published © CC BY

Automatic Homeplant Irrigation

Save ALL the House Plants!

IntermediateFull instructions provided16 hours1,592

Things used in this project

Story

Read more

Schematics

topdesign_nwww7ZN8af.png

pins_otw29zfhtg_YsVvKRJTVl.jpg

Code

main.c

C/C++
The I2C sensor data logger app running on the Raspberry Pi.
/**
 * @file main.c
 *
 * @brief Main Entry point for the CY3240 i2c test
 *
 * Main Entry point for the CY3240 i2c test
 *
 * @ingroup CY3240
 *
 * @owner  Kevin Kirkup (kevin.kirkup@gmail.com)
 * @author Kevin Kirkup (kevin.kirkup@gmail.com)
 */
//////////////////////////////////////////////////////////////////////
/// @name Includes
//@{

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h> /* for getopt() */
#include <time.h>
#include <hidapi/hidapi.h>

#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "cy3240.h"
#include "cy3240_util.h"
#include "i2c_demo.h"

#define SLEEP_BETWEEN_CMD   (250000)

void print_buffer(const uint8_t* const buffer, uint16_t length) {
    printf("\nBuffer: (Length=%i)", length);

    for (int count = 0; count < length; count ++) {

        if(count % 16 == 0) {
            printf("\n0%d: ", (int)(count / 16));
        }

        if(count % 4 == 0) {
            printf(" %02x",buffer[count]);

        } else {
            printf("%02x",buffer[count]);
        }
    }

    printf("\n");
}


//@} End of Private Methods

typedef enum __attribute__((packed)) { SENSE, PUMP, WAIT} IrrigateState;

typedef struct __attribute__((packed)) {
	uint32_t __place__holder;
    uint32_t irrigateThreshold;
    uint32_t pumpDuration;
    uint32_t waitDuration;

    IrrigateState state;

    uint16_t soilResistance;
    int16_t temperature;
    uint16_t humidity;
    uint16_t illuminance;        
} sensor_data;


int main(int argc, char *argv[]) {
    int iface_num = 0;
    int retry;

    void *handle = 0;

    uint16_t length = 0;

    sensor_data sensorData;

    // Initialize the device
    cy3240_factory(
            &handle,
            iface_num,
            50,
            CY3240_POWER_3_3V,
            CY3240_BUS_I2C,
            CY3240_CLOCK__100kHz
            );

    // Open the device
    cy3240_open(handle);

	usleep(SLEEP_BETWEEN_CMD);

    // Configure the bridge controller using the default settings
    cy3240_reconfigure(
            handle,
            CY3240_POWER_3_3V,
            CY3240_BUS_I2C,
            CY3240_CLOCK__100kHz);

	usleep(SLEEP_BETWEEN_CMD);

    length = sizeof(sensorData);
	uint16_t wLength = 3 * sizeof(uint32_t);

	long lastMod = -1;
	
	while (true) {
		cy3240_read(handle, 0x08, (uint8_t*) &sensorData, &length);

		// print_buffer(data, length);


			fprintf(stdout, "data: %d %.2f %.2f %d %d %d %d %d\n", 
				sensorData.soilResistance,
				sensorData.temperature / 100.0,
				sensorData.humidity / 10.0,
				sensorData.illuminance,
                                sensorData.state,
				sensorData.irrigateThreshold,
				sensorData.pumpDuration,
				sensorData.waitDuration);

		//}
		usleep(1000000);

		struct stat attr;
	    stat("/tmp/irrigator-params.txt", &attr);
		long newLastMod = attr.st_mtime;
	    printf("Last modified time: %s %d\n", ctime(&attr.st_mtime), newLastMod);
		if (newLastMod > lastMod) {
			lastMod = newLastMod;
			
			FILE *file = fopen("/tmp/irrigator-params.txt", "r");
			fscanf(file, "%d %d %d", &sensorData.irrigateThreshold, &sensorData.pumpDuration, &sensorData.waitDuration);
			fprintf(stdout, "new parans:%d %d %d\n",
				sensorData.irrigateThreshold,
				sensorData.pumpDuration,
				sensorData.waitDuration);

			fclose(file);

			wLength = 4 * sizeof(uint32_t);
			fprintf(stdout, "wLength: %d\n", wLength);
			
			cy3240_write(handle,
				    0x08, //0x08,
				    (uint8_t*) &sensorData,
				    &wLength);

			fprintf(stdout, "wrote: %d\n", wLength);
								
		}

	}

    // Configure the bridge controller using the default settings
    cy3240_reconfigure(
            handle,
            CY3240_POWER_5V,
            CY3240_BUS_I2C,
            CY3240_CLOCK__100kHz);

    usleep(SLEEP_BETWEEN_CMD);

    // Close the device
    cy3240_close(handle);

    return 0;
}

Automatic Houseplant Irrigator

C/C++
The PSoc Creator project
No preview (download only).

I2C-USB bridge

The library used to communicate with the I2C-USB bridge

Credits

Attila Tőkés

Attila Tőkés

37 projects • 221 followers
Software Engineer experimenting with hardware projects involving IoT, Computer Vision, ML & AI, FPGA, Crypto and other related technologies.

Comments