Skip to content

Instantly share code, notes, and snippets.

@jaythomas
Created April 15, 2021 01:24
Show Gist options
  • Save jaythomas/0f9becea61da928d38879eb3563897fa to your computer and use it in GitHub Desktop.
Save jaythomas/0f9becea61da928d38879eb3563897fa to your computer and use it in GitHub Desktop.
Wemos ESP8266 D1 Mini wi-fi voltage and server example
//****** Battery Voltage Monitoring ********************
// WiFi documentation: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/station-class.html
#include <ESP8266WiFi.h>
// WifiUdp documentation: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/udp-examples.html
#include <WiFiUdp.h>
// Create udp class instance
WiFiUDP udp;
// Pinout configuration
// https://arduino-projekte.info/wp-content/uploads/2017/03/wemos_d1_mini_pro_pinout.png
byte PIN_D0 = 16;
byte PIN_D1 = 5;
byte PIN_D2 = 4;
byte PIN_D3 = 0;
byte PIN_D4 = 2;
byte PIN_D5 = 14;
byte PIN_D6 = 12;
byte PIN_D7 = 13;
byte PIN_D8 = 15;
// WiFi credentials
char hostname[]= "JayStation"; // Name for identifying this device on the network
char ssid[] = "WifiSSID"; // WiFi ssid
char pass[] = "WifiPassword"; // WiFi password (set to "" for open networks)
// Server
const char* server = "192.168.1.11"; // Your computer's IP address
const uint16_t port = 3381;
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.hostname(hostname);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" WiFi connected.");
Serial.print("SSID:\t");
Serial.println(WiFi.SSID());
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
Serial.print("Gateway:\t");
Serial.println(WiFi.gatewayIP());
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void loop() {
// Voltage divider
// R1 = 220k + 100k + 220k (540k)
// R2 = 100k
float calib_factor = 5.28; // change this value to calibrate the battery voltage
unsigned long raw_power = analogRead(A0);
String message = String(raw_power * calib_factor/1024);
char const *buffer = message.c_str();
udp.begin(port);
udp.beginPacket(server, port);
udp.write(buffer, message.length());
udp.endPacket();
delay(8000);
}
// UDP Server listens for UDP datagrams and logs them to the console
const dgram = require('dgram')
const server = dgram.createSocket('udp4')
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`)
server.close()
})
server.on('message', (msg, rinfo) => {
console.log(`[${makeTimestamp()}] ${rinfo.address}:${rinfo.port} - ${msg}`)
})
server.on('listening', () => {
const address = server.address()
console.log(`server listening ${address.address}:${address.port}`)
})
server.bind(3381)
// Prepend zero to single-digit value
function zeroPad(input) {
return ('0' + input).slice(-2)
}
// Return string like YYYY-MM-DD HH:MM:SS
function makeTimestamp() {
const dt = new Date()
return `${dt.getFullYear()}-${zeroPad(dt.getMonth() + 1)}-${zeroPad(dt.getDate())} ` +
`${zeroPad(dt.getHours())}:${zeroPad(dt.getMinutes())}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment