// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
/*
Эмуляция ESP8266
1 ---------------------|-----------|\
| | \
| | \
2 ------x--------------| | |
| | | |
| | 4.7k | | |
| | | | /
| | | /
3 ------x--------------|-----------|/
1 - Gnd
2 - DPIO5
3 - Vcc
*/
#include <WiFi.h>
#include <ESPping.h>
#include <IRremote.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// #include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int PIN_RECEIVER = 16; // Вход для ИК приемника
IRrecv receiver(PIN_RECEIVER); // приемник иК
int ledPin = 25; // Пин для Диода
boolean pOut = false;
int inputPin = 27; // Пин для датчика движения
int pirState = LOW; // мы начинаем, предполагая, что движение не обнаружено
int val = 0; // переменная для считывания статуса pin-кода
int buzPin = 33;
// GPIO where the DS18B20 is connected to
const int oneWireBus = 12; // GPIO5 (D1)
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 3 * 3600
#define UTC_OFFSET_DST 0
String sTm = "000000000000";
float temperatureC = -1.0;
//String IP[] = {"122.154.139.137", "122.154.139.137"};
//String servers[] = {"www.edoc.ksu.ac.th", "www.ktm.ksu.ac.th"};
//String IP[] = {"195.34.224.1", "192.168.224.11"};
//String servers[] = {"rbc.ru", "zzzrbc1.ru"};
//String responseTime[5];
//String responseText, statusText;
const char* remote_host = "rbc.ru";
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
char buffer[25]; // large enough for any DateTime, including invalid ones
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
LCD.setCursor(8, 0);
//LCD.println(&timeinfo, "%H:%M:%S");
LCD.print("t="); LCD.println(String(temperatureC, 1));
LCD.setCursor(0, 1);
//LCD.println(&timeinfo, "%d/%m/%Y %Z");
LCD.println(&timeinfo, "%Y%m%d%H%M%S");
// breakTime1(getLocalTime(&timeinfo));
// sprintf(buffer, "%04d%02d%02d%02d%02d%02d",
// (tm.Year + 1970), tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second);
// sTm = String(buffer);
}
void setup() {
pinMode(ledPin, OUTPUT); // объявите светодиод в качестве выходного сигнала
pinMode(inputPin, INPUT); // объявите датчик в качестве входного сигнала
pinMode(buzPin, OUTPUT);
Serial.begin(115200);
delay(1000);
Serial.println(" \n\n=======================================================\n");
Serial.println(__FILE__);
Serial.println("Compiled: " + String(__DATE__) + " " + String(__TIME__));
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
LCD.begin(16, 2);
LCD.print("<press a button>");
receiver.enableIRIn(); // Запуск ИК приемника
digitalWrite(ledPin, HIGH); // включаем светодиод
}
//Функция вывода нового значения на индикатор
void lcdPrint(char* text)
{
//LCD.clear();
LCD.setCursor(0, 0);
//LCD.print("button pressed:");
//LCD.setCursor(0, 1);
//LCD.print(text);
//LCD.print(" code: ");
//LCD.print(receiver.decodedIRData.command);
LCD.print("c: "); LCD.print(receiver.decodedIRData.command);
}
//Функция преобразования кода в название
void translateIR()
{
LCD.clear();
switch (receiver.decodedIRData.command) {
case 162:
lcdPrint("POWER");
break;
case 226:
lcdPrint("MENU");
break;
case 34:
lcdPrint("TEST");
Prnt_Clock();
break;
case 2:
lcdPrint("PLUS");
break;
case 194:
lcdPrint("BACK");
break;
case 224:
lcdPrint("PREV.");
break;
case 168:
lcdPrint("PLAY");
test_Ping();
break;
case 144:
lcdPrint("NEXT");
break;
case 104:
lcdPrint("num: 0");
break;
case 152:
lcdPrint("MINUS");
break;
case 176:
LCD.clear();
//lcdPrint("key: C");
break;
case 48:
lcdPrint("num: 1");
break;
case 24:
lcdPrint("num: 2");
break;
case 122:
lcdPrint("num: 3");
break;
case 16:
lcdPrint("num: 4");
break;
case 56:
lcdPrint("num: 5");
break;
case 90:
lcdPrint("num: 6");
break;
case 66:
lcdPrint("num: 7");
break;
case 74:
lcdPrint("num: 8");
break;
case 82:
lcdPrint("num: 9");
break;
default:
LCD.clear();
LCD.print(receiver.decodedIRData.command);
LCD.print(" other button");
}
}
void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(inputPin); // считывание входного значения (датчик движения)
if (val == HIGH) { // проверьте, является ли входной сигнал HIGH
digitalWrite(ledPin, HIGH); // Если да то включаем светодиод
digitalWrite(buzPin, LOW);
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (!pOut) { // проверьте, является ли входной сигнал HIGH
digitalWrite(ledPin, HIGH); // Если да то включаем светодиод
//pOut = false;
pOut = true;
delay(100);
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
pOut = false;
//pOut = true;
delay(1000);
}
digitalWrite(buzPin, HIGH);
}
delay(100); // TODO: Build something amazing!
if (receiver.decode()) //проверка новых данных
{
translateIR();
receiver.resume(); //запуск приема новой команды
}
// sensors.requestTemperatures();
// temperatureC = sensors.getTempCByIndex(0);
// // Serial.printf("%s Temperature=%f ºC\n", String(temperatureC,1), temperatureC);
// printLocalTime();
// delay(1000);
//delay(50);
}
void Prnt_Clock()
{
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
// Serial.printf("%s Temperature=%f ºC\n", String(temperatureC,1), temperatureC);
printLocalTime();
//delay(1000);
delay(10);
}
void test_Ping()
{
LCD.setCursor(0, 1);
LCD.println("Ping ...");
// Ping_test2();
delay(10);
//}
//void Ping_test2() {
Serial.println("Ping test start...");
//const char* remote_host = "rbc.ru";
String text = "";
text += "server: " + String(remote_host) + " - ";
if (Ping.ping(remote_host)) {
text += "Online " + String(Ping.averageTime());
Serial.println(text);
text = String(remote_host) + " - " + String(Ping.averageTime());
LCD.setCursor(0, 1);
LCD.println(text);
}
else
{
text += "Offline --";
Serial.println(text);
}
}
/*
PIR sensor tester
https://docs.wokwi.com/parts/wokwi-pir-motion-sensor
*/
/*
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
Pin names
Name Description
GND Ground
OUT Output (digital)
VCC Supply voltage
Attributes
Name Description Default value
delayTime The number of seconds OUT pin will stay high "5"
inhibitTime The number of seconds the sensor will ignore motion when OUT returns to low "1.2"
retrigger Set to "0" to disable retriggering ""
Using the sensor
To trigger the PIR motion sensor:
Select the sensor by clicking on it (while the simulation is running).
A small popup window will open. Click on "Simulate Motion".
Triggering the sensor will drive the OUT pin high for 5 seconds (delay time), and then
go low again. The sensor will ignore any further input for the next 1.2 seconds (inhibit time),
and then start sensing for motion again.
You can adjust the high duration of the OUT pin by setting the delayTime attribute (on a physical
sensor you use a potentiometer to set the delay).
The default setting of the sensor is to retrigger: the sensor keeps checking for motion
while the OUT pin is high. It will extend the delay time every time another motion event
is detected. You can disable this behavior by setting the "retrigger" attribute to "0".
*/