Created
January 27, 2022 02:32
-
-
Save mattncsu/637384eb5c22cf7916f489167e4f0582 to your computer and use it in GitHub Desktop.
Posted as an exmaple of receiving UDP broadcasts from Weatherflow Tempest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> //D8:A0:1D:63:45:5C | |
#include <ESPAsyncWebServer.h> | |
#include <DS18B20.h> | |
#include "AsyncUDP.h" | |
#include <ArduinoJson.h> | |
#include "FastLED-timers.h" | |
#include <AsyncElegantOTA.h> | |
#include <AsyncTCP.h> | |
#include <ArduinoJson.h> | |
AsyncWebServer server(80); | |
AsyncUDP udp; | |
const int port = 50222; | |
#define LED 2 | |
#define VFD 32 //pin for reading VFD 0-10V signal | |
//DS18B20 Temperature Sensors | |
DS18B20 ds(33); | |
uint8_t address1[] = {40, 80, 34, 117, 208, 1, 60, 162}; | |
uint8_t address2[] = {40, 67, 55, 117, 208, 1, 60, 14}; | |
uint8_t selected; | |
const char* ssid = ""; //WIFI NAME | |
const char* password = ""; //WIFI PASSWORD | |
const char* apikey = ""; | |
// Structure example to send data | |
// Must match the receiver structure | |
typedef struct struct_message { | |
float waterTemp; | |
float vaultTemp; | |
float rpm; | |
float w_s; | |
double airTemp; | |
double rhVal; | |
int lux; | |
} struct_message; | |
// Create a struct_message called myData | |
struct_message myData; | |
StaticJsonDocument<100> doc; | |
char json_string[100]; | |
size_t len; | |
void connectUDPReceiver() | |
{ | |
if (udp.listen(port)) | |
{ | |
Serial.println("UDP connected"); | |
udp.onPacket([](AsyncUDPPacket packet) { | |
StaticJsonDocument<384> doc; | |
DeserializationError error = deserializeJson(doc, packet.data(), packet.length()); | |
if (error) | |
{ | |
Serial.print(F("deserializeJson() failed: ")); | |
// Serial.println(error.f_str()); | |
return; | |
} | |
const char *type = doc["type"]; | |
if (strcmp(type, "obs_st") == 0) | |
{ | |
JsonArray obs = doc["obs"][0]; | |
// sscanf(obs[7], "%f",&myData.airTemp); | |
// myData.airTemp = myData.airTemp *22.0/7.0+9.0; | |
// sscanf(obs[8], "%f", &myData.rhVal); | |
// sscanf(obs[9], "%i", &myData.lux); | |
myData.airTemp = (obs[7]); | |
myData.airTemp = myData.airTemp*9.0/5.0+32.0; | |
myData.rhVal = (obs[8]); | |
myData.lux = (obs[9]); | |
Serial.print("Temperature: "); | |
Serial.println(myData.airTemp); | |
Serial.print("Humidity: "); | |
Serial.println(myData.rhVal); | |
Serial.print("Lux: "); | |
Serial.println(myData.lux); | |
} | |
if (strcmp(type, "rapid_wind") == 0) | |
{ | |
// JsonArray ob = doc["ob"]; | |
// myData.w_s = atof(ob[1]); | |
// if (myData.w_s > 10) | |
// { | |
// Serial.print("Wind: "); | |
// Serial.println(myData.w_s); | |
// } | |
} | |
if (strcmp(type, "evt_strike") == 0) | |
{ | |
Serial.println("Strike Detected!!!"); | |
} | |
if (strcmp(type, "evt_precip") == 0) | |
{ | |
Serial.println("Raining"); | |
} | |
}); | |
} | |
} | |
void TempRead() { | |
selected = ds.select(address1); | |
if (selected) { | |
myData.waterTemp=ds.getTempF(); | |
} else { | |
Serial.println("Device 1 not found!"); | |
} | |
selected = ds.select(address2); | |
if (selected) { | |
myData.vaultTemp=ds.getTempF(); | |
} else { | |
Serial.println("Device 2 not found!"); | |
} | |
} | |
void setup() | |
{ | |
pinMode(LED, OUTPUT); | |
Serial.begin(115200); | |
WiFi.mode(WIFI_MODE_STA); | |
Serial.print("Connecting to "); Serial.println(ssid); | |
digitalWrite(LED, HIGH); | |
WiFi.begin(ssid, password); | |
if (WiFi.waitForConnectResult() != WL_CONNECTED) { | |
Serial.println("WiFi Failed"); | |
while(1) { | |
delay(1000); | |
} | |
} | |
Serial.println(WiFi.localIP()); | |
connectUDPReceiver(); | |
digitalWrite(LED, LOW); | |
// Allocate the JSON document | |
server.on("/rpm", HTTP_GET, [](AsyncWebServerRequest *request){ | |
Serial.println("Request received"); | |
request->send(200, "text/plain", (String)myData.rpm); | |
}); | |
server.on("/vault", HTTP_GET, [](AsyncWebServerRequest *request){ | |
Serial.println("Request received"); | |
request->send(200, "text/plain", (String)myData.vaultTemp); | |
}); | |
server.on("/air", HTTP_GET, [](AsyncWebServerRequest *request){ | |
Serial.println("Request received"); | |
request->send(200, "text/plain", (String)myData.airTemp); | |
}); | |
server.on("/rh", HTTP_GET, [](AsyncWebServerRequest *request){ | |
Serial.println("Request received"); | |
request->send(200, "text/plain", (String)myData.rhVal); | |
}); | |
server.on("/water", HTTP_GET, [](AsyncWebServerRequest *request){ | |
Serial.println("Request received"); | |
request->send(200, "text/plain", (String)myData.waterTemp); | |
}); | |
server.on("/json", HTTP_GET, [](AsyncWebServerRequest *request){ | |
Serial.println("JSON Request received"); | |
request->send(200, "text/plain", (String)json_string); | |
}); | |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ | |
Serial.println("Home Request received"); | |
String html = "<HTML><BODY>RPM:"+String(myData.rpm)+"<BR>Water:"+String(myData.waterTemp)+"<BR>Air:"+String(myData.airTemp)+"F @ "+String(myData.rhVal)+"<BR>Vault:"+String(myData.vaultTemp); | |
request->send(200, "text/html", html); | |
}); | |
AsyncElegantOTA.begin(&server); // Start ElegantOTA | |
server.begin(); | |
TempRead(); //get initial temperature reading | |
} | |
void calcRPM(){ | |
int samples=150; | |
unsigned int hz = 0; | |
for (uint8_t i = 1; i <=samples; i++){ | |
hz += analogRead(VFD); | |
// Serial.print(hz/i); Serial.print(","); | |
// delay(1); | |
} | |
// Serial.println(); | |
myData.rpm=hz / samples; | |
} | |
void SerialOutput(){ | |
Serial.print("Water: "); | |
Serial.print(myData.waterTemp); | |
//Serial.print(rev); | |
Serial.print(" Vault: "); | |
Serial.print(myData.vaultTemp); | |
Serial.print(" RPM: "); | |
Serial.println(myData.rpm); | |
} | |
void createJson(){ | |
doc["w"] = (int)(myData.waterTemp); | |
doc["v"] = (int)(myData.vaultTemp); | |
doc["a"] = (int)(myData.airTemp); | |
doc["h"] = (int)(myData.rhVal); | |
doc["l"] = myData.lux; | |
doc["r"] = (int)myData.rpm; | |
serializeJson(doc, json_string); | |
} | |
void loop() | |
{ | |
EVERY_N_SECONDS(10) {TempRead();} | |
EVERY_N_MILLISECONDS(1000) { | |
calcRPM(); | |
createJson(); | |
} | |
EVERY_N_SECONDS(2) {SerialOutput();} | |
if (myData.rpm > 0){ | |
EVERY_N_MILLISECONDS(1000) { | |
// | |
} | |
} else { | |
EVERY_N_SECONDS(5) { | |
// | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment