The best tools to make your project dreams come true

Login or Signup
USD


Smart Greenhouse with Cypress and Sparkfun

By Ben Roloff

Spring is in the air.  But unfortunately up here in the Northern Minnesota that will be short lived.  It is hard to have a garden in this climate all year round, unless you have a greenhouse.  A greenhouse allows you to make a little habitat with its own climate. With its own climate you will need to monitor the environment inside to make sure it is where it needs to be.  You would need to know things like temperature, humidity, and CO2 levels. Thankfully Sparkfun has a nice little board for environmental sensing. For the processing of the data and communication, we will look at Cypress’s PSoC 6 and PSoC 4 development boards.  We will be using the code and project by Sparkfun found here, and making a few modifications to their project.  Here is our video on the project with the details on how to make your own below.

 

 

BOM:

So before we get started let’s look at what we will all need.

Optional (but slightly important):

  • A greenhouse
  • Plants

Thingspeak:

So for this project we will need an IoT aggregator website.  There are a lot of options out there, but I am using the same website Sparkfun used in their project, called Thingspeak.  I tried some other ones, but Thingspeak worked the best with the Xbee because they give you the IP address for the API calls and there is a free version available.  The setup is easy, just make sure to select personal use. The commercial free version is only a trial. You will need to make a new channel.

New Channel

For this channel you will need 6 fields: temperature, humidity, and equivalent CO2, and because we are using two sensors we will need two of each field.  You will need the Write API Key later for the code.

Write API Key

Code:

Now that you have your parts, we need some code to make them work.  PSoC Creator has a nice widget for getting a program started and if you know C you can probably get some working code pretty quick, but any good programmer knows the most useful tool is copy & paste. Here is some great code to get started from Sparkfun’s Github.  The code for the PSoC 4 is ready to go and there are no changes needed.  You just need to plug in the PSoC 4. You may need to update the firmware which can be done in PSoC Programmer, which should have been downloaded with the PSoC Creator.  This process may also be necessary for the PSoC 6.

PSoC Programmer

The code was written for an older version of the PSoC 4 with BLE. So, when you try and program it, it will tell you it is the incorrect version.  You can easily fix this by right-clicking on the project and click Device Selector. The device being used is the CY8C4248LQI-BL583. You should be good to program the PSoC 4’s now.  

For the PSoC 6 there are a few modifications needed.  The first part is in wifi.c. You will need to put in your wifi details.  The SSID, the password, and security type, if different than WPA2, are all needed.  

 

Copy Code
 char buffer[40];       // Create a buffer for our commands
char ssid[] = "SSID"; // Wifi SSID
char ts_ip[] = "184.106.153.149"; // thingspeak.com IP address
char ssid_pw[] = "Password";
int dest_port = 80;
char encrypt_mode = WPA2;

 

The next part we will change will add a little more functionality.  The part of the code we will be changing will be in the host_main.c around line 936.  It is by this snippet of code.

Copy Code
sprintf(buffer, "GET /update?key=XXXXXXXXXXXXXXXXX&field1=%i&field2=%u&field3=%u&field4=%d&field5=%d 
HTTP/1.1\r\n", sensorData->temperature, sensorData->pressure, sensorData->humidity, sensorData->tvoc, sensorData->eco2);

All the code we change and add will be in the function that includes this.  This function handles the api call to Thingspeak and sends that sensor data.   The first thing you will need to do is to add in the Write API Key from Thingspeak.  It goes in the line of code above. Just replace all the X’s after key= with it. Now you are able to write to your own Thingspeak channel.  You will notice that the code above has the sensor data for one sensor and includes pressure and tVOC. We will need to make a few more changes to add a second sensor and make sure it is sending the correct data.

 

Copy Code
void DisplayData2(app_stc_sensor_data_t * sensorData)
{
app_stc_connection_info_t *appConnInfoPtr = GetAppConnInfoPtr();
static uint32_t tick = 0;
static double temp = 0.0;
static double temp2 = 0.0;
static double humid = 0.0;
static double humid2 = 0.0;
static int eco = 0;
static int eco2 = 0;
int device = 0;
static int device1 = 0;
static int device2 = 0;
if(appConnInfoPtr != NULL)
{
if(Cy_BLE_GetConnectionState(appConnInfoPtr->connHandle[sensorData->deviceNumber]) >= CY_BLE_CONN_STATE_CONNECTED)
{
/* Get BdAddress from bdHandle of the connected device */
cy_stc_ble_gap_peer_addr_info_t param = {.bdHandle = appConnInfoPtr->connHandle[sensorData->deviceNumber].bdHandle};
Cy_BLE_GAP_GetPeerBdAddr(&param);
DBG_PRINTF("\r\n");
PRINT_DEVICE_ADDR(param.bdAddr, appConnInfoPtr->connHandle[sensorData->deviceNumber] );
DBG_PRINTF(" -- Temp =%i\n\r", sensorData->temperature);
DBG_PRINTF(" -- Pres =%u\n\r", sensorData->pressure);
DBG_PRINTF(" -- Humid=%u\n\r", sensorData->humidity);
DBG_PRINTF(" -- TVOC =%d\n\r", sensorData->tvoc);
DBG_PRINTF(" -- ECO2 =%d\n\r", sensorData->eco2);
DBG_PRINTF(" -- Time =%d\n\r", systemClock);
device = sensorData->deviceNumber;
if ( device1 == 0 )
{
device1 = device;
}else if ( device2 == 0 && device != device1)
{
device2 = device;
}
if (device == device1)
{
temp = sensorData->temperature/100.0;
humid = sensorData->humidity/1024.0;
eco = sensorData->eco2;
}
if (device == device2)
{
temp2 = sensorData->temperature/100.0;
humid2 = sensorData->humidity/1024.0;
eco2 = sensorData->eco2;
}
if (systemClock - tick > 60)
{
tick = systemClock;
char buffer[100];
DBG_PRINTF("Posted!\n\r");
// In the following line, replace the Xs with your API key from ThingSpeak.
sprintf(buffer, "GET/update?key=XXXXXXXXXXXXXXXXX&field1=%4.2f&field2=%4.2f&field3=%d&field4=%4.2f&field5=%4.2f
&field6=%d HTTP/1.1\r\n",temp, humid, eco, temp2, humid2, eco2);
XBEE_UART_PutString(buffer);
XBEE_UART_PutString("Host: api.thingspeak.com\r\n\r\n");
}
}
}
}

 

You can see that quite a few changes have been made to the function.  I have added a few more variables for the sensor data and devices. I have made them static and a few of them doubles.  This is so that I don’t lose any sensor data depending on when the function is called. The doubles make sure I can send my data as floats.  We have variables for temperature, humidity, and equivalent CO2 for each sensor. I have set it up to recognize two different devices. The original code reads up to four different PSoC 4’s, but they don’t differentiate between them when sending the sensor data.  I have if statements checking if I have recognized two different devices. The first time I grab the data from each device, the device number is saved so that I can distinguish between each device. I can now store each devices data separately. We just needed to change the API call so that we send the new set of data we want.  Make sure you fill each field with the data that matches the fields you set up in Thingspeak. The fields for the humidity and temperature data should be floats. Your code should be good to go with these changes. You just need to program the PSoC 6. You can choose either core to program too, I used the CM0.

Assembly:

So now we have a bunch of parts and our boards programmed, we will need to put it all together.   The assembly is not too difficult. The hardest part will be getting the enclosures ready. The IoT shield is plugged into each PSoC board.  Ensure that the pins line up with the rails. The sensor cables plug into the IoT shields on the PSoC 4’s. When plugging into the sensors you can plug it into either side.   The Xbee plugs into the top of the shield on the PSoC 6. The silk screen matches the direction the Xbee plugs in. The enclosures take a little more work. The larger enclosures you will need to drill out one of the punch outs.  The grommet should fit nicely into it now. For the sensor enclosures you will need to dremel down the corners of the pcb boards. Just round them off. You can snap off the pegs on the inside of the enclosure so that it fits back into it.  They should fit nicely into the enclosures now. The PSoC 4’s should come with coin cell batteries that you can plug into them now before screwing the enclosure closed. You can now put the PSoC 4’s wherever you want in your greenhouse and they should be safe from water.  The PSoC 6 should sit outside the greenhouse, but close enough that it is within bluetooth range. Make sure to plug in both the PSoC 6 and the Pioneer IoT shield. This ensures that the Xbee wifi has enough power.

Conclusion:

Now it is time to get your gardening gloves, and get planting.  Your greenhouse awaits. Now you can easily monitor your greenhouse.  So you can have your tropical paradise even up here in Northern Minnesota.  If you are looking to build any other type of IoT system take a look at Cypress’s PSoC 6 and the Sparkfun Pioneer IoT shield for a nice easy start.

Garden Shot

Key Parts and Components

Add all Digi-Key Parts to Cart
  • 428-4103-ND
  • 1568-1813-ND
  • 1568-1706-ND
  • 428-3804-ND
  • 602-1377-ND