Created
May 7, 2018 20:07
-
-
Save truckershitch/42b5862cc5e31b0c343687b9235c9f33 to your computer and use it in GitHub Desktop.
ESP8266 NodeMCU Heartbeat File Monitor - note that LED pin is reversed here
This file contains 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
/* | |
* Heartbeat File Meter | |
* by truckershitch | |
* | |
* Added blink limit to prevent runaway blinking on boot | |
* Cleaned up loop and variables | |
* 11/22/17 | |
* Appended Wifi code for esp8266 module | |
* 04/19/18 | |
* | |
* based on | |
* Numerical Echo | |
* by Martijn | |
* 22 Nov 2008 | |
* | |
* and | |
* WiFi UDP Send and Receive String | |
* created 30 December 2012 | |
* by dlf (Metodo2 srl) | |
*/ | |
#include <SPI.h> | |
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
int status = WL_IDLE_STATUS; | |
char ssid[] = "<ssid>"; // your network SSID (name) | |
char pass[] = "<password>"; // your network password (use for WPA, or use as key for WEP) | |
//int keyIndex = 0; // your network key Index number (needed only for WEP) | |
unsigned int localPort = 2390; // local port to listen on | |
char packetBuffer[255]; //buffer to hold incoming packet | |
//char ReplyBuffer[] = "acknowledged"; // a string to send back | |
WiFiUDP Udp; | |
const int blinkOn = 250; // timeframe that the LED is switched ON | |
const int ASC_ZERO = 48; // ASCII for 0 (zero) | |
void setup() { | |
//Initialize LED | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(LED_BUILTIN, HIGH); // strangely this is off | |
//Initialize serial and wait for port to open: | |
Serial.begin(115200); | |
//while (!Serial) { | |
// ; // wait for serial port to connect. Needed for native USB port only | |
//} | |
// attempt to connect to Wifi network: | |
while (status != WL_CONNECTED) { | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(ssid); | |
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | |
status = WiFi.begin(ssid, pass); | |
// wait 10 seconds for connection: | |
delay(10000); | |
} | |
Serial.println("Connected to wifi"); | |
printWifiStatus(); | |
Serial.println("\nStarting connection to server..."); | |
// if you get a connection, report back via serial: | |
Udp.begin(localPort); | |
} | |
void blink_led(int value) { | |
int count = value - ASC_ZERO; // number of beats | |
if (count <= 15) { // validate input | |
for (int i = 1; i <= count; i++) { | |
digitalWrite(LED_BUILTIN, LOW); // LED on | |
delay(blinkOn); | |
digitalWrite(LED_BUILTIN, HIGH); // LED off | |
delay(blinkOn); | |
} | |
digitalWrite(LED_BUILTIN, HIGH); // LED off | |
delay(4 * blinkOn); // LED off - pausing here | |
} | |
} | |
void loop() { | |
// if there's data available, read a packet | |
int packetSize = Udp.parsePacket(); | |
if (packetSize) { | |
Serial.print("Received packet of size "); | |
Serial.println(packetSize); | |
Serial.print("From "); | |
IPAddress remoteIp = Udp.remoteIP(); | |
Serial.print(remoteIp); | |
Serial.print(", port "); | |
Serial.println(Udp.remotePort()); | |
// read the packet into packetBufffer | |
//int len = Udp.read(packetBuffer, 255); | |
int len = Udp.read(packetBuffer, 1); | |
if (len > 0) { | |
packetBuffer[len] = 0; | |
} | |
Serial.println("Contents:"); | |
Serial.println(packetBuffer); | |
int val = int(packetBuffer[0]); | |
Serial.print("Value of buffer: "); | |
Serial.println(val); | |
if (val > 0) { | |
blink_led(val); // blink away | |
} | |
// send a reply, to the IP address and port that sent us the packet we received | |
//Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); | |
//Udp.write(ReplyBuffer); | |
//Udp.endPacket(); | |
} | |
} | |
void printWifiStatus() { | |
// print the SSID of the network you're attached to: | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
// print your WiFi shield's IP address: | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP Address: "); | |
Serial.println(ip); | |
// print the received signal strength: | |
long rssi = WiFi.RSSI(); | |
Serial.print("signal strength (RSSI):"); | |
Serial.print(rssi); | |
Serial.println(" dBm"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment