-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32.ino
112 lines (101 loc) · 3.61 KB
/
ESP32.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//////////// edit these values for each sensor
const char* location = "locationNameHere"; // sensor location name, avoid spaces
const char* ssid = "SSIDnameHere"; // your network SSID (name of wifi network)
const char* password = "passwordHere"; // your network password
const char* udpAddress = "192.168.1.100"; // your syslog server destination IP
const int udpPort = 3333; // syslog server destination port. no need to change this unless you know what you are doing
/////////// no edits below this line unless you know what you are doing
#include <esp_task_wdt.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFiClientSecure.h>
#define tempPin0 14
#define WDT_TIMEOUT 30
OneWire oneWire0(tempPin0);
DallasTemperature sensor0(&oneWire0);
int i=0;int last = millis();
boolean connected = false;
WiFiUDP udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
delay(100);
sensor0.begin();
Serial.print("Attempting to connect to SSID: "); Serial.println(ssid);
connectToWiFi(ssid, password);
// attempt to connect to Wifi network:
while (WiFi.status() != 3) {
Serial.print("wifi status: "); Serial.print(WiFi.status()); Serial.println("");
Serial.print("Attempting to connect to SSID: "); Serial.println(ssid);
connectToWiFi(ssid, password);delay(2000);
}
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("My IP is: ");
Serial.println(WiFi.localIP());
Serial.println("Configuring WDT...");
esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
esp_task_wdt_add(NULL); //add current thread to WDT watch
}
void(* resetFunc) (void) = 0; //declare reset function at address 0 - MUST BE ABOVE LOOP
void loop() {
Serial.print("millis() = ");Serial.print(String( millis() ) );
Serial.print(", status: ");Serial.println(WiFi.status());
if(millis() > 3600000 )resetFunc();
while (WiFi.status() != 3) {
Serial.print("wifi status: "); Serial.print(WiFi.status());
Serial.println(""); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
connectToWiFi(ssid, password);delay(2000);
}
sensor0.requestTemperatures(); // send command
float temp0C = sensor0.getTempCByIndex(0);
float temp0F = (temp0C*9/5) + 32;
Serial.print("Temperature #0 is: ");
Serial.print(temp0C);
Serial.print(" C / ");
Serial.print(temp0F);
Serial.println(" F");
String mydata = String(temp0F) + " " + location;
Serial.print("sending packet: '");
Serial.print(mydata);
Serial.println("'");
// send to listening server
if(connected){
//Send a packet
udp.beginPacket(udpAddress,udpPort);
udp.print(mydata);
udp.endPacket();
}
else{
resetFunc(); // RESET IF DISCONNECTED
}
delay(5000);
esp_task_wdt_reset();
}
void connectToWiFi(const char * ssid, const char * pwd){
Serial.println("Connecting to WiFi network: " + String(ssid));
WiFi.disconnect(true);
WiFi.onEvent(WiFiEvent);
WiFi.setHostname(location);
WiFi.begin(ssid, password);
Serial.println("Waiting for WIFI connection...");
}
void WiFiEvent(WiFiEvent_t event){
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
//When connected set
Serial.print("WiFi connected! IP address: ");
Serial.println(WiFi.localIP());
//initializes the UDP state
//This initializes the transfer buffer
udp.begin(WiFi.localIP(),udpPort);
connected = true;
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
connected = false;
break;
default: break;
}
}