Creating a device that sends temperature and humidity data to the Raspberry Pi.
Parts are soldered to the universal board.
Connect SHT31 and ssd1306 in parallel with i2c. However, just connect the four lines in parallel. Easy easy.
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.
/*
* =========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.
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.
Next, I will write an article on the Raspberry Pi side.