v0.0.1
This commit is contained in:
commit
e52b2750dd
1 changed files with 82 additions and 0 deletions
82
NodeMCUWiFiModule.ino
Normal file
82
NodeMCUWiFiModule.ino
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <spi_flash.h>
|
||||||
|
|
||||||
|
#define WIFI_SSID ""
|
||||||
|
#define WIFI_PASS ""
|
||||||
|
|
||||||
|
#define IP_BROADCAST "192.168.1.255"
|
||||||
|
#define UDP_PORT 8176
|
||||||
|
|
||||||
|
String status = "";
|
||||||
|
float distance = -1;
|
||||||
|
float duration;
|
||||||
|
WiFiUDP client;
|
||||||
|
|
||||||
|
const int triggerPin = D6;
|
||||||
|
const int echoPin = D5;
|
||||||
|
|
||||||
|
SPI_DEVICE_TXBIT_LSBFIRST;
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
delay(5000);
|
||||||
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
|
pinMode(echoPin, INPUT);
|
||||||
|
pinMode(triggerPin, OUTPUT);
|
||||||
|
|
||||||
|
status = "Connecting";
|
||||||
|
while(WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
status = WiFi.localIP().toString();
|
||||||
|
Serial.begin(9600);
|
||||||
|
Serial.println("Got IP: " + status);
|
||||||
|
int err = client.begin(81);
|
||||||
|
if(err == 0) {
|
||||||
|
Serial.println("Failed to create UDP Socket");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Created UDP Socket at port 81");
|
||||||
|
}
|
||||||
|
Serial.println(String("IP BROADCAST ID SET TO: ") + String(IP_BROADCAST));
|
||||||
|
Serial.println(String("UDP PORT SET TO: ") + String(UDP_PORT));
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
digitalWrite(triggerPin, LOW);
|
||||||
|
delayMicroseconds(2);
|
||||||
|
digitalWrite(triggerPin, HIGH);
|
||||||
|
delayMicroseconds(10);
|
||||||
|
digitalWrite(triggerPin, LOW);
|
||||||
|
duration = pulseIn(echoPin, HIGH);
|
||||||
|
distance = (duration*.0343)/2;
|
||||||
|
|
||||||
|
if(WiFi.status() != WL_CONNECTED) {
|
||||||
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
|
while(WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String d = String(distance);
|
||||||
|
String d2 = String(duration);
|
||||||
|
Serial.println("Distance is: " + d);
|
||||||
|
Serial.println("Duration is: " + d2);
|
||||||
|
byte *buff = new byte[4];
|
||||||
|
byte *array = (byte *) &distance;
|
||||||
|
for(int i=0;i<4;i++) {
|
||||||
|
buff[i] = array[3-i];
|
||||||
|
}
|
||||||
|
Serial.print("Data flipped is: ");
|
||||||
|
for(int i=0;i<4;i++) {
|
||||||
|
Serial.print(buff[i], HEX);
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
|
||||||
|
client.beginPacket(IP_BROADCAST, UDP_PORT);
|
||||||
|
client.write(buff, sizeof(float));
|
||||||
|
client.endPacket();
|
||||||
|
delay(3000);
|
||||||
|
delete[] buff;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue