Introduction: Iot Pan With ESP32 and Thingspeak

About: Student at Hogeschool van Amsterdam

The ESP32 a powerful IOT tool. In this Instructable i will teach you how to make a connected IOT prototype to measure moist such as oils in a steel pan. Also a servo will open the lid when the food gets too hot. The temperature will not be measured by a sensor. Instead it will be simulated by code.

The following was the setup enviroment for ESP32 with Arduino IDE.

- Windows 10
- Arduino IDE 1.8.2

To work with the ESP32 you need to download the ESP32 core files. A tutorial how to download and install this can be found here:

http://randomnerdtutorials.com/installing-the-esp3...

Installed? Great, lets start!

Step 1: Components

For this tutorial you need a few parts to work with:

- Esp32 devboard
- Soil Hygrometer
- Multiple Dupont Cables
- Solderless Breadboard
- Micro usb cable
- Red Led
- Green Led
- 2 1.0K ohm resistors

- Steel pan
- Matierial for a lid

You will also need the following:

- Adhesive tape

Step 2: Connect the Soil Hygrometer

Follow this wire-up diagram.

Pin 33 - A0
VCC - 5v
Gnd - Gnd

Also connect the + and - to the pins on the probe.

Step 3: Connect the Leds

Now it is time to connect the leds to the ESP32. Connect the positive ( longest ) pins to the esp32.

Red led - G25
Green led - G32

Step 4: Add Code to Test Hygrometer and Upload to Esp32

/* A part of this code is based on Michalis Vasilakis his tutorial - How to use a Soil Hygrometer Module
, this code is created for the smartpan prototype */

const int hygrometer = 33; //Connect the hygrometer to G33

int value;

const int ledGreen = 32; const int ledRed = 25;

void setup(){ Serial.begin(9600);

// attach leds pinMode(ledGreen, OUTPUT); digitalWrite(ledGreen, LOW); pinMode(ledRed, OUTPUT); digitalWrite(ledRed, LOW); }

void loop(){ value = analogRead(hygrometer); //Read analog value value = value; Serial.print("Soil humidity: "); Serial.print(value); Serial.println("%"); delay(2000); //Read every 2 sec.

// We will make the hygrometer respond to a amount of moist. We will take 2000 as the amount to change te led.

if (value < 2000){

digitalWrite(ledGreen, HIGH); digitalWrite(ledRed, LOW); }

if (value > 2000){

digitalWrite(ledGreen, LOW); digitalWrite(ledRed, HIGH); }

}

Step 5: Test If the Hygrometer Is Working

If you like, you can put the probe in some soil. If you open the serial monitor you should see values popping up. Try adding water to the soil. The Red led will fade and the Green led will light up!

Step 6: Adding the Servo

To make an automatic lid we attach a servo to the esp32. Follow the diagram as shown above.

Yellow - G26
Red - 3.3V
Black - Ground

We power the mini servo trough the 3.3v out of precaution so the servo does not affect the hygrometers readings.

The Esp32 does not allow analog output. This means we can't use the arduino's regular servo library. For the next step install the following servo library:

https://github.com/jkb-git/ESP32Servo

If you do not know how to install a library you can follow the steps written here.

Step 7: Update Your Code to Use the Servo

Annotations are made in this code to help you understand what is happening. Upload this code to your ESP32.We will simulate the temperature in the pan trough code. The pan will open the lid when it gets too hot.

#include <ESP32_Servo.h>

const int hygrometer = 33; int value;

Servo myservo; // atach the servo

int temp = 0; // setup a int to simulate a temp

const int ledGreen = 32; const int ledRed = 25;

void setup(){ Serial.begin(9600); pinMode(ledGreen, OUTPUT); digitalWrite(ledGreen, LOW); pinMode(ledRed, OUTPUT); digitalWrite(ledRed, LOW);

myservo.attach(26); myservo.write(0); }

void loop(){

value = analogRead(hygrometer); value = value; Serial.print("Soil humidity: "); Serial.print(value); Serial.println("%"); delay(2000);

if (value < 2000){

digitalWrite(ledGreen, HIGH); digitalWrite(ledRed, LOW);

}

if (value > 2000){

digitalWrite(ledGreen, LOW); digitalWrite(ledRed, HIGH);

}

temp = temp + 10; // Every two seconds the temperature rises by 10.

if (temp < 120){ myservo.write(180); // the lid is closed under the 120. }

if (temp > 120){ myservo.write(0); // the lid is open after the 120. }

if (temp > 150){ temp = 0; // the temperature drops after the lid is open for some seconds. }

// now we will also print the temperature in the serial monitor.

Serial.print("temperature: "); Serial.print(temp); Serial.println("'C");

}

Step 8: Attach the Servo and Hygrometer to the Pan

Using some adhesive tape attach the servo to the handle of the pan as shown in the picture. Make sure the servo is fully turned to the left using your hands.

Now attach the Hygrometer to the base of the pan.

Next is to cut out some foil or plastic in the same size as the top of your pan. Use some adhesive tape to attach it to your servo horn.

If you test you application you will see the hygrometer respond to moist in the pan. Also the lid will open when the simulated heat will reach hot. You can check the values in the serial monitor.

Step 9: Connect Your Pan to the Internet Trough Thingspeak

I you dot not have it already create a account at www.thingspeak.com. After that follow to following steps:

  • Create a new channel
  • Add the variables in the fields. This includes Moist and Temperature.
  • Save Channel

Now you can find your Api keys on the dashboard. Go to the tap on the homepage and find your 'Write Api Key'.

Step 10: Finish Your ESP32 Code to Connect to Thingspeak

The last step is to update the code to send the values to Thingspeak. Annotations are made in the code.

/* Code made by Ikbeneendaan for Instructables, parts based on code made by Hugo Gomes ( ESP32 weather station ) and Michalis Vasilakis ( Hygrometer ) */

#include ESP32_Servo.h

// Include library to connect the ESP32 to the internet #include <WiFi.h>

#include <WiFiMulti.h>

WiFiMulti WiFiMulti;

const char* ssid = "Yourssid"; // Your SSID (Name of your WiFi) const char* password = "Wifipass"; //Your Wifi password

const char* host = "api.thingspeak.com"; String api_key = "APIKEY"; // Your API Key provied by thingspeak

const int hygrometer = 33; int value;

Servo myservo; // atach the servo

int temp = 0; // setup a int to simulate a temp

const int ledGreen = 32; const int ledRed = 25;

void setup(){ Serial.begin(9600); pinMode(ledGreen, OUTPUT); digitalWrite(ledGreen, LOW); pinMode(ledRed, OUTPUT); digitalWrite(ledRed, LOW);

myservo.attach(26); myservo.write(0);

Connect_to_Wifi(); }

void loop(){

value = analogRead(hygrometer); value = value; Serial.print("Soil humidity: "); Serial.print(value); Serial.println("%"); delay(2000);

if (value < 2000){

digitalWrite(ledGreen, HIGH); digitalWrite(ledRed, LOW);

}

if (value > 2000){

digitalWrite(ledGreen, LOW); digitalWrite(ledRed, HIGH);

}

temp = temp + 10; // Every two seconds the temperature rises by 10.

if (temp < 120){ myservo.write(180); // the lid is closed under the 120. }

if (temp > 120){ myservo.write(0); // the lid is open after the 120. }

if (temp > 150){ temp = 0; // the temperature drops after the lid is open for some seconds. }

// now we will also print the temperature in the serial monitor.

Serial.print("temperature: "); Serial.print(temp); Serial.println("'C");

// call function to send data to Thingspeak Send_Data();

delay(5000);

}

void Connect_to_Wifi() {

// We start by connecting to a WiFi network WiFiMulti.addAP(ssid, password);

Serial.println(); Serial.println(); Serial.print("Wait for WiFi... ");

while (WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(500); }

Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP());

}

void Send_Data() {

// map the moist to 0 and 100% for a nice overview in thingspeak. value = constrain(value,0,5000); value = map(value,0,5000,100,0);

Serial.println("Prepare to send data");

// Use WiFiClient class to create TCP connections WiFiClient client;

const int httpPort = 80;

if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } else { String data_to_send = api_key; data_to_send += "&field1="; data_to_send += String(value); data_to_send += "&field2="; data_to_send += String(temp); // data_to_send += "&field3="; // data_to_send += String(pressure); data_to_send += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + api_key + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(data_to_send.length()); client.print("\n\n"); client.print(data_to_send);

delay(1000); }

client.stop();

}

Step 11: Done!

Your sensor values will start showing up at Thingspeak!