Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device

Creating a device that sends temperature and humidity data to the Raspberry Pi.

Overall layout

Parts are soldered to the universal board. image.png

circuit

Connect SHT31 and ssd1306 in parallel with i2c. However, just connect the four lines in parallel. Easy easy.

image.png

Confirmation of i2c address

After deciding the wiring, check the address on the breadboard. The code referenced this page.

Run the code and make a note of the address that comes up. In this case, SHT31 was 0x45 and ssd1306 was 0x3C.

Write to ESP32

/*
 * =========WiFi Config==========
*/
#include "WiFi.h"
#include "AsyncUDP.h"
#include <stdio.h>

const char * ssid = "ssid";
const char * password = "ssid password";

//The IP address should be the same for all multiple ESP32.
AsyncUDP udp;
IPAddress ESP32_ip(192,168,x,x);
IPAddress server_ip(192,168,x,x);
IPAddress gateway(192,168, x, x);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(192, 168, x, x);

//Assign a unique number to the ESP32
int deviceNo = 1;

#define ssd1306_Address 0x3C  //i2c address of ssd1306
#define SHT31_Address  0x45  //I2C address of SHT31

WiFiServer server(80);

const int LEDPIN = 2;
const int PORTNO = 1234;
/*
 * =========SHT31 Config==========
*/
#include <SPI.h>
#include <Arduino.h>
#include <Wire.h>
#include "AE_SHT31.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example



//Set the address of SHT31
AE_SHT31 SHT31 = AE_SHT31(SHT31_Address);

float temp,humi;


/*
 * =========SeepSleep Config==========
*/

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  9     /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;

void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}






void setup() {
  //Set serial communication to 9600bps
  Serial.begin(9600);
  
  //Output characters serially
  //Soft reset SHT31
  SHT31.SoftReset();
  //Built-in heater 0:OFF 1:ON
  SHT31.Heater(0);

  if(!display.begin(SSD1306_SWITCHCAPVCC,ssd1306_Address )) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  display.clearDisplay();
  
  //Display settings for ssd1306
  display.drawPixel(10, 10, WHITE);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  
   temp = SHT31_TEMP();
   humi = SHT31_HUMI();
   
/*
 * =========Display display==========
*/
   
   display.clearDisplay();
   display.setCursor(0,0);
   display.print(" "); 
   display.print(temp); 
   display.println(" 'C");
   
   display.print(" "); 
   display.print(humi); 
   display.println(" %"); 
     
   display.display();
/*
 * =========WiFi Setup==========
*/

   pinMode(LEDPIN,OUTPUT);
    
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);

    WiFi.config(ESP32_ip, gateway, subnet, DNS); 
    
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.println("WiFi Failed");
        while(1) {
            delay(1000);
        }
    }
    
    if(udp.connect(server_ip, PORTNO)) {
        Serial.println("UDP connected");
        udp.onPacket([](AsyncUDPPacketpacket) {
            Serial.print("UDP Packet Type: ");
            Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
            Serial.print(", From: ");
            Serial.print(packet.remoteIP());
            Serial.print(":");
            Serial.print(packet.remotePort());
            Serial.print(", To: ");
            Serial.print(packet.localIP());
            Serial.print(":");
            Serial.print(packet.localPort());
            Serial.print(", Length: ");
            Serial.print(packet.length());
            Serial.print(", Data: ");
            Serial.write(packet.data(), packet.length());
            Serial.println();
            //reply to the client
            packet.printf("Got %u bytes of data", packet.length());
        });


    }

}



void loop()
{

   temp = SHT31_TEMP();
   humi = SHT31_HUMI();
   
/*
 * =========Display display==========
*/
   
   display.clearDisplay();
   display.setCursor(0,0);
   display.print(" "); 
   display.print(temp); 
   display.println(" 'C");
   
   display.print(" "); 
   display.print(humi); 
   display.println(" %"); 
     
   display.display();
   
/*
 * =========UDP transmission==========
*/
//dtostrf(Value to convert,Total number of characters after conversion,Number of digits after the decimal point,Variables to be stored after conversion);
    
    char udpStr1[6];
    char udpStr2[6];
    char buf[10];
    
    dtostrf(temp,5,2,udpStr1);
    dtostrf(humi,5,2,udpStr2);
    
    sprintf(buf,"%d,%s,%s",deviceNo,udpStr1,udpStr2);
    Serial.println(buf);
    
    udp.broadcastTo(buf, PORTNO);

/*
 * =========Post-processing==========
*/

   Ltika();
   ESP32_Sleep(10*60);
}

/*
 * =========Obtaining temperature and humidity of SHT31==========
*/

float SHT31_TEMP(){
    //Get temperature data from SHT31
  SHT31.GetTempHum();
  return SHT31.Temperature();
}

float SHT31_HUMI(){
    //Get humidity data from SHT31
  SHT31.GetTempHum();
  return SHT31.Humidity();
}

/*
 * =========ESP32 DeepSleep==========
*/

void ESP32_Sleep(int sleepime){
  
  delay(1000); //Take some time to open up the Serial Monitor
  ++bootCount;

  print_wakeup_reason();

  esp_sleep_enable_timer_wakeup(sleepime * uS_TO_S_FACTOR);
  esp_deep_sleep_start();

}

/*
 * =========L Chika==========
*/

void Ltika(){    
    for (int i=0;i<3;i++){
      digitalWrite(LEDPIN,HIGH);
      delay(100);
      digitalWrite(LEDPIN,LOW);
      delay(100);
    }
}

Line 204, sprintf (buf, "% d,% s,% s", deviceNo, udpStr1, udpStr2); The device number, temperature, and humidity data are sent together in comma-separated form. This is to distribute the data for each position after receiving UDP later.

Start-up

When you write to the microcomputer and start it, the temperature and humidity will be displayed on the display. If the communication is successful, the blue LED on the ESP32 board will blink three times. image.png

Next, I will write an article on the Raspberry Pi side.

Recommended Posts

Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device
Production of temperature control system with Raspberry Pi and ESP32 (1)
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
Get temperature and humidity with DHT11 and Raspberry Pi
Measure CPU temperature of Raspberry Pi with Python
Record temperature and humidity with systemd on Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Measure and compare temperature with Raspberry Pi and automatically generate graph
[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 1
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 2
Graph display of household power consumption with 3GPI and Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
CSV output of pulse data with Raspberry Pi (CSV output)
Get CPU information of Raspberry Pi with Python
Raspberry + am2302 Measure temperature and humidity with temperature and humidity sensor
Control music playback on a smartphone connected to Raspberry Pi 3 and bluetooth with AVRCP
Simple VPN construction of IPsec gateway with Ubuntu 20.04 and Raspberry Pi --2 StrongSwan VPN connection confirmation
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Easy IoT to start with Raspberry Pi and MESH
Detect mask wearing status with OpenCV and Raspberry Pi
Control brushless motors with GPIOs on Raspberry Pi Zero
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Control power on / off of USB port of Raspberry Pi
Installation of Docker on Raspberry Pi and L Chika
Install pyenv on Raspberry Pi and version control Python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Version control of Node, Ruby and Python with anyenv
Simulate temperature measurement with Raspberry Pi + Flask + SQLite + Ajax
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Raspberry Pi system monitoring
Create your own IoT platform using raspberry pi and ESP32 (Part 3) ~ ESP32 settings Analog temperature sensor
Control the display of RGB LED matirix electric bulletin board freely with Raspberry Pi 3B +
Easy introduction to home hack with Raspberry Pi and discord.py
Python beginner opens and closes interlocking camera with Raspberry Pi
I tried running Movidius NCS with python of Raspberry Pi3
Create an LCD (16x2) game with Raspberry Pi and Python
I tried using the DS18B20 temperature sensor with Raspberry Pi
Serial communication control with python and I2C communication (using USBGPIO8 device)
Creating a temperature / humidity monitor with Raspberry Pi (pigpio version)
Serial communication control with python and SPI communication (using USBGPIO8 device)
A program that receives the servo command of the radio control, interrupts the Raspberry Pi and logs it
Building a distributed environment with the Raspberry PI series (Part 2: PiServer analysis and alternative system design)
Image recognition of garbage with Edge (Raspberry Pi) from zero knowledge using AutoML Vsion and TPU
Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 to graph it Part 2
Mutter plants with Raspberry Pi
Control the motor with a motor driver using python on Raspberry Pi 3!
I tried to automate the watering of the planter with Raspberry Pi
Make a Kanji display compass with Raspberry Pi and Sense Hat
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Introduction of M5StickC (Temperature / Humidity measurement and MQTT transmission, UIFlow Python)
Periodically log the value of Omron environment sensor with Raspberry Pi
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC