Initial version of the project to test the git server
This commit is contained in:
parent
cf4073f735
commit
cbf1d4e88e
|
|
@ -0,0 +1,133 @@
|
||||||
|
/* Read the temperature from a DS18b20 sensor
|
||||||
|
* from a ESP8266 NodeMCU
|
||||||
|
* 2021-06-06 Trs Creation
|
||||||
|
* 2023-07-10 Trs,Jfs Use deep sleep to reduce consumption, send value via UDP frame
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <DS18B20.h> // https://github.com/matmunk/DS18B20
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
//#include <WiFiClient.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <IPAddress.h>
|
||||||
|
|
||||||
|
// Configuration
|
||||||
|
static const uint8_t D2 = 4; // ESP8266 -- do not forget to pullup signal to 3V3 with ~1 kOhm
|
||||||
|
static const uint16_t DEEP_SLEEP_AFTER_S = 10;
|
||||||
|
static const uint16_t SLEEP_FOR_S = 30 * 60; // 30 minutes
|
||||||
|
static const uint16_t cSendMeasurementInterval = 2000;
|
||||||
|
static const char SSID[] = "<YOUR SSID>";
|
||||||
|
static const char PASS[] = "<YOUR PASSWORD>";
|
||||||
|
|
||||||
|
static const char cTargetIP[] = "<SERVER ADDRESS>";
|
||||||
|
static const uint16_t cTargetPort = 14000;
|
||||||
|
|
||||||
|
static const char cUDPPacketStart[] = "{\"sender\":\"ESPTempSender\",\"compile-date\":\"" __DATE__ "\",\"temperature\":\"";
|
||||||
|
static const char cUDPPacketEnd[] = "\"}";
|
||||||
|
|
||||||
|
// working variables
|
||||||
|
DS18B20 ds(D2);
|
||||||
|
uint8_t selected;
|
||||||
|
int32_t gSleepTime;
|
||||||
|
|
||||||
|
ESP8266WiFiSTAClass wifiMulti;
|
||||||
|
WiFiUDP UDP;
|
||||||
|
IPAddress gTarget;
|
||||||
|
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
// --- Preparation and Info
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.setTimeout(2000);
|
||||||
|
// Wait for serial to initialize.
|
||||||
|
while(!Serial) { }
|
||||||
|
|
||||||
|
Serial.print("\r\n" __DATE__);
|
||||||
|
Serial.println(" ESP Temperature sender. Sender on D2(4), Deep-sleep with reset from D0 to RST");
|
||||||
|
|
||||||
|
// --- Init temperature sensor
|
||||||
|
selected = ds.selectNext();
|
||||||
|
if (selected) {
|
||||||
|
Serial.println("Sensor found");
|
||||||
|
} else {
|
||||||
|
Serial.println("Sensor not found, no values will be reported!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Connect to WiFi network to send UDP frames to the server
|
||||||
|
WiFi.begin(SSID, PASS);
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.print(SSID);
|
||||||
|
int i = 0;
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(250);
|
||||||
|
Serial.print('.');
|
||||||
|
}
|
||||||
|
Serial.println('\n');
|
||||||
|
Serial.print("Connected to ");
|
||||||
|
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
|
||||||
|
Serial.print("IP address:\t");
|
||||||
|
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
|
||||||
|
|
||||||
|
gTarget.fromString(cTargetIP);
|
||||||
|
Serial.print("Sending to:\t");
|
||||||
|
Serial.print(gTarget);
|
||||||
|
Serial.print(" Port: ");
|
||||||
|
Serial.println(cTargetPort);
|
||||||
|
|
||||||
|
// ---
|
||||||
|
// System will go to deep sleep after this time
|
||||||
|
gSleepTime = millis() + DEEP_SLEEP_AFTER_S * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---
|
||||||
|
void UDPSendTemperature(float temperature)
|
||||||
|
{
|
||||||
|
UDP.beginPacket(gTarget, cTargetPort);
|
||||||
|
UDP.write(cUDPPacketStart, sizeof(cUDPPacketStart) - 1);
|
||||||
|
char tempBuff[10];
|
||||||
|
auto count = snprintf(tempBuff, 9, "%.1f", temperature);
|
||||||
|
UDP.write(tempBuff, count);
|
||||||
|
UDP.write(cUDPPacketEnd, sizeof(cUDPPacketEnd) - 1);
|
||||||
|
|
||||||
|
// close packet and send it
|
||||||
|
auto result = UDP.endPacket();
|
||||||
|
|
||||||
|
// For documentation purposes, also write the data to serial port
|
||||||
|
Serial.print("Sent with result ");
|
||||||
|
Serial.print(result);
|
||||||
|
Serial.print(": ");
|
||||||
|
Serial.print(cUDPPacketStart);
|
||||||
|
Serial.print(tempBuff);
|
||||||
|
Serial.println(cUDPPacketEnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
if (selected)
|
||||||
|
{
|
||||||
|
float fTemp = ds.getTempC();
|
||||||
|
Serial.print("Temperature is ");
|
||||||
|
Serial.print(fTemp);
|
||||||
|
Serial.println(" C");
|
||||||
|
UDPSendTemperature(fTemp);
|
||||||
|
} else {
|
||||||
|
Serial.println("Device not found!");
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t diff = gSleepTime - millis();
|
||||||
|
if (diff > 0)
|
||||||
|
{
|
||||||
|
Serial.print("Time/ms until sleep: ");
|
||||||
|
Serial.println(diff);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Serial.print("Going to deep sleep for ");
|
||||||
|
Serial.print(SLEEP_FOR_S);
|
||||||
|
Serial.println(" seconds ...");
|
||||||
|
ESP.deepSleep(SLEEP_FOR_S * 1e6); // in us
|
||||||
|
|
||||||
|
// Since a reset is executed, the program will start over with the setup() function
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(cSendMeasurementInterval);
|
||||||
|
}
|
||||||
24
README.md
24
README.md
|
|
@ -1,3 +1,25 @@
|
||||||
# WiFi-Temperature-Sender
|
# A rather simple WiFi temperature sender
|
||||||
|
|
||||||
Use a micro-controller with WiFi, e.g. a NodeMCU V2 or V3, to send temperature values read from a DS18B20 sensor to a server.
|
Use a micro-controller with WiFi, e.g. a NodeMCU V2 or V3, to send temperature values read from a DS18B20 sensor to a server.
|
||||||
|
|
||||||
|
## Concept
|
||||||
|
|
||||||
|
* The sender is a controller with WiFi that can read values from a temperature
|
||||||
|
sensor.
|
||||||
|
* Values are sent to a server with hard-coded IP address.
|
||||||
|
* The controller is configured to go to "deep sleep" after sending values.
|
||||||
|
* The wakeup feature of the controller shall be used to reactivate it after
|
||||||
|
about 30 min. Note that not all controller boards have such a feature,
|
||||||
|
e.g. the very simple ESP-01 boards to not make the wakeup pin accessible.
|
||||||
|
* Using deep sleep, it should be possible to run the sender for several days
|
||||||
|
or even weeks with a simple USB power bank.<br/>
|
||||||
|
Of course, a more sophisticated powering system would be nice.
|
||||||
|
* The messages are a simple UDP packet containing a JSON structure.
|
||||||
|
* The server listens at a pre-configured port and can process the
|
||||||
|
JSON structure.
|
||||||
|
* One server implementation is a Python script that collects the reported
|
||||||
|
values in a CSV file.
|
||||||
|
* The CSV file can be processed into a simple static web page for display in
|
||||||
|
the local network.
|
||||||
|
|
||||||
|
**WORK IN PROGRESS**
|
||||||
Loading…
Reference in New Issue