// 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 "time.h"
#include <OneWire.h>
#include <DallasTemperature.h>
// #include <WiFi.h>
#include <Wire.h>
//int ledPin = 25; // Пин для Диода
//boolean pOut = false;
// 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);
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 30 /* Time ESP32 will go to sleep (in seconds) */
#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 setup() {
//pinMode(ledPin, OUTPUT); // объявите светодиод в качестве выходного сигнала
Serial.begin(115200);
delay(100);
Serial.println(" \n\n=======================================================\n");
Serial.println(__FILE__);
Serial.println("Compiled: " + String(__DATE__) + " " + String(__TIME__));
//Print the wakeup reason for ESP32
print_wakeup_reason();
Serial.println("Connecting to WiFi ");
int tail = 20;
WiFi.begin("Wokwi-GUEST", "", 6);
while ((WiFi.status() != WL_CONNECTED) && tail--) {
delay(250);
Serial.print(".");
}
if (tail <= 1)
{
Serial.println("\n\n\nESP.restart() !!!");
ESP.restart();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.print(WiFi.localIP());
Serial.println(" Online");
//Serial.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
//digitalWrite(ledPin, HIGH); // включаем светодиод
tm_Get();
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
Serial.printf("%s Temperature=%s ºC\n", sTm.c_str(), String(temperatureC, 1));
/*
Next we decide what all peripherals to shut down/keep on
By default, ESP32 will automatically power down the peripherals
not needed by the wakeup source, but if you want to be a poweruser
this is for you. Read in detail at the API docs
http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
Left the line commented as an example of how to configure peripherals.
The line below turns off all RTC peripherals in deep sleep.
*/
//esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
//Serial.println("Configured all RTC Peripherals to be powered down in sleep");
// First we configure the wake up source. We set our ESP32 to wake up every 5 seconds
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
/*
Now that we have setup a wake cause and if needed setup the
peripherals state in deep sleep, we can now start going to
deep sleep.
In the case that no wake up sources were provided but deep
sleep was started, it will sleep forever unless hardware
reset occurs.
*/
Serial.println("Going to sleep now");
delay(100);
Serial.flush();
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
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;
}
}
//int cnt = 0;
void tm_Get()
{
char buffer[25]; // large enough for any DateTime, including invalid ones
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
sTm = "Time Err";
return;
}
sprintf(buffer, "%04d%02d%02d%02d%02d%02d",
(timeinfo.tm_year + 1970),
timeinfo.tm_mon,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec
);
sTm = String(buffer);
}
void loop() {
//This is not going to be called
}
/*
void loop() {
// put your main code here, to run repeatedly:
int val = digitalRead(ledPin); // считывание входного значения светодиод
if (val != HIGH) { // проверьте, является ли входной сигнал HIGH
digitalWrite(ledPin, HIGH); // Если да то включаем светодиод
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
}
delay(1000); // TODO: Build something amazing!
if (cnt++ > 5)
{
tm_Get();
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
Serial.printf("%s Temperature=%s ºC\n", sTm.c_str(), String(temperatureC, 1));
cnt = 0;
}
}
*/
Loading
ds18b20
ds18b20