Today we are going to MAKE IoT Plant, a plant connected to the internet that notifies you on your cell phone when it needs to be watered ! For that you will need a soil moisture sensor to measure the plant's soil moisture and an ESP8266 NodeMCU wifi module to send the data to the internet, as simple as that 🙂
This project Sponsor by NextPCB
Thank You NextPCB:
This project is successfully completed because of the help and support from NextPCB.
NextPCB is one of the most experienced PCB manufacturers in Global, has specialized in the PCB and assembly industry for over 15 years. Not only could NextPCB provide the most innovative printed circuit boards and assembly technologies in the highest quality standards, the fastest delivery turnaround as fast as 24 hours.
Guys if you have a PCB project, please visit their website and get exciting discounts and coupons
Only 0$ for 5–10pcs PCB Prototypes :- Click Here
Register and get $100 from NextPCB :- Click Here
See more info about PCB Assembly Capabilities: Click Here
SuppliesESP8266 NodeMCU
soil moisture sensor
jumper cable
Step 1: ThingSpeakIn this first part, we will link the ESP8266 NodeMCU to ThingSpeak, showing how to send plant moisture data to the cloud. In the next posts you will learn how to trigger the information to your cell phone.
The NodeMCU Wifi module is one of the most popular cards / platforms for prototyping IoT solutions. As already shown in this blog, NodeMCU works perfectly integrated with MQTT, enabling remote control and monitoring. In this post, you will be shown how to integrate NodeMCU with another great IoT platform: ThingSpeak.
Step 2: How Does ThingSpeak Work?The ThingSpeak Is a platform that enables IoT, at no cost, upload numerical data. Such data is stored in the cloud, where it is then allowed to be visualized in real-time graphics. That is, if you want to monitor any numerical quantity (humidity, temperature, pressure, etc.) remotely via the Internet, ThingSpeak is a good choice. It is worth mentioning that ThingSpeak is constantly evolving, so new features can be added at any time.
ThingSpeak has only one limitation: the time between data uploads must be at least 15 seconds. If this is disobeyed, data sent outside this time frame will be ignored or not logged. In the design of this post, uploads will be made at 30-second intervals.
Step 3: Uploading Data to ThingSpeakTo send data to ThingSpeak, an HTTP request is made to the ThingSpeak server. An HTTP request is nothing more than a string (containing the HTTP request information) sent via socket TCP client (who wants to make the request) to a socket TCP server (server that will receive the request, in this case the ThingSpeak server) through from port 80.
Another important information is that, in the HTTP request, there will be a writing key for the ThingSpeak channel. It is a string (unique for each channel), necessary for ThingSpeak to “know” which channel to direct the data sent to. Note: the established TCP socket does not have a long lifetime. The connection to the server ends as soon as it sends its response to the request. In other words, an HTTP request was made to be “short-lived”, therefore it is intended only for brief data traffic between socket TCP client and socket TCP server.
Step 4: IoT Plant With ESP8266 NodeMCUThe Plant IoT project with ESP8266 NodeMCU consists of making the NodeMCU send, every thirty seconds, the percentage humidity measured to a ThingSpeak channel. For this example, I created a public channel ( link ), whose writing key is KGFP6X4JQTJZFIAP. However, I strongly recommend you to create your account at ThingSpeak and create your own there, so you can have fun at will!
Step 5: Electrical Scheme of the IoT PlantIn terms of hardware, you will need a NodeMCU, a hygrometer sensor, 1 100 ohm resistor and 1 200 ohm resistor for the voltage divider, jumpers and breadboard.
The power is provided by the USB cable (the same cable used in programming the NodeMCU). The hygrometer sensor is read through the NodeMCU's single ADC pin (A0), which allows the reading of the percentage humidity (0 - 100%).
Note the schematic circuit, which will be used in the IoT Plant project:
Step 6: Programming the ESP8266 NodeMCU ModuleCode
//NextPCB
#include <ESP8266WiFi.h>
//defines
#define SSID_REDE " "
#define SENHA_REDE " "
#define INTERVALO_ENVIO_THINGSPEAK 30000
//constantes e variáveis globais
char EnderecoAPIThingSpeak[] = "api.thingspeak.com";
String ChaveEscritaThingSpeak = "KGFP6X4JQTJZFIAP";
long lastConnectionTime;
WiFiClient client;
//prototypes
void EnviaInformacoesThingspeak(String StringDados);
void FazConexaoWiFi(void);
float FazLeituraUmidade(void);
/*
* Implementações
*/
//Função: envia informações ao ThingSpeak
//Parâmetros: String com a informação a ser enviada
//Retorno: nenhum
void EnviaInformacoesThingspeak(String StringDados)
{
if (client.connect(EnderecoAPIThingSpeak, 80))
{
//faz a requisição HTTP ao ThingSpeak
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+ChaveEscritaThingSpeak+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(StringDados.length());
client.print("\n\n");
client.print(StringDados);
lastConnectionTime = millis();
Serial.println("- Informações enviadas ao ThingSpeak!");
}
}
//Função: faz a conexão WiFI
//Parâmetros: nenhum
//Retorno: nenhum
void FazConexaoWiFi(void)
{
client.stop();
Serial.println("Conectando-se à rede WiFi...");
Serial.println();
delay(1000);
WiFi.begin(SSID_REDE, SENHA_REDE);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connectado com sucesso!");
Serial.println("IP obtido: ");
Serial.println(WiFi.localIP());
delay(1000);
}
float FazLeituraUmidade(void)
{
int ValorADC;
float UmidadePercentual;
ValorADC = analogRead(0); //978 -> 3,3V
Serial.print("[Leitura ADC] ");
Serial.println(ValorADC);
UmidadePercentual = 100 * ((978-(float)ValorADC) / 978);
Serial.print("[Umidade Percentual] ");
Serial.print(UmidadePercentual);
Serial.println("%");
return UmidadePercentual;
}
void setup()
{
Serial.begin(9600);
lastConnectionTime = 0;
FazConexaoWiFi();
Serial.println("Planta IoT com ESP8266 NodeMCU");
}
//loop principal
void loop()
{
float UmidadePercentualLida;
int UmidadePercentualTruncada;
char FieldUmidade[11];
if (client.connected())
{
client.stop();
Serial.println("- Desconectado do ThingSpeak");
Serial.println();
}
UmidadePercentualLida = FazLeituraUmidade();
UmidadePercentualTruncada = (int)UmidadePercentualLida;
if(!client.connected() &&
(millis() - lastConnectionTime > INTERVALO_ENVIO_THINGSPEAK))
{
sprintf(FieldUmidade,"field1=%d",UmidadePercentualTruncada);
EnviaInformacoesThingspeak(FieldUmidade);
}
delay(1000);
}
Step 7: Result of the IoT Plant on ThingSpeakAfter uploading the source code to NodeMCU, open the Serial Monitor ( with baud rate at 9600 baud ) to follow what NodeMCU is doing.
The resulting graph will look something like the figure abov
Step 8: IoT Plant With ESP8266 NodeMCU - Part 2in this post the project will be improved. Now, your plant will tweet when it needs to be watered! That is, now your plant will use Twitter !
IoT plant design requirements with ESP8266
In terms of hardware, the design is identical to that of the post Planta IoT with ESP8266 NodeMCU
Part 1, using the ESP8266 NodeMCU module and the soil moisture sensor.
So, no additional hardware is needed for your plant to be able to send you Tweets.
The source code is also identical, so from the point of view of the NodeMCU software nothing is changed in relation to the design of the previous post.
Step 9: How Does Twitter Integration Work?n this project, the Tweet is generated by a ThingSpeak Application / resource called React.
This feature allows you to schedule an action to be taken upon meeting a certain condition (based on your channel data on ThingSpeak).
In the case of this project, a React will be created and configured to send a Tweet automatically if the measured soil moisture is less than 50%. In summary, the one who actually takes care of sending the Tweet is ThingSpeak, which is yet another very interesting feature of the platform
Step 10: Walkthrough: Tweeting With ThingSpeakTo make ThingSpeak send Tweets using the React feature, follow the steps:
Create a Twitter account dedicated to your project. You can also use your own account for this if you want too (here the choice is free). As with the part 1 post, you will need to create a channel for your ThingSpeak project.
Here, use the same channel used in the post in part 1.
Once logged in to the Twitter account you want to link to ThingSpeak, log in to ThingSpeak in another browser tab (keeping the Twitter tab open).
On ThingSpeak's main page, click on Applications, as shown in the figure above:
Step 11: Stetup ThingspeakOn the screen that appears, click on the “New React” button Now, React must be created as shown in the following figure.Observations:
a) If you do not have a Twitter account linked to ThingSpeak, on this same page there will be an option to link.
b) Select in If channel your ThingSpeak channel that should run React) In then tweet, inform the text that should be tweeted, remembering that this text is fixed.
Click Save React and you're done! Now it is enough for ThingSpeak to receive less than 50% soil moisture data for a Tweet (with the text configured in the previous step) to be generated!
Comments