Advertisement
Tempist

BLEtoUDP [Flashed into NodeMCU]

Jul 5th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Contact Me
  2. //Discord: wizard#7815
  3. //Twitter: @itsWizardly
  4. //Instagram: @imWizardly
  5.  
  6. //SENDER
  7. #include <ESP8266WiFi.h>
  8. #include <WiFiUdp.h>
  9.  
  10. //UDP handle
  11. WiFiUDP Udp;
  12. unsigned int UDP_Port = 5000;
  13. char* UDP_IP = "192.168.4.1"; //IP address of the RECEIVER
  14. char PacketBuffer[1024]; //Buffer used for UDP data
  15.  
  16. //Wifi handle
  17. const char* ssid = "YOUR_SSID";
  18. const char* password = "YOUR_PASSWORD";
  19. IPAddress ip(192, 168, 4, 2); //set a static IP for this device
  20. IPAddress gateway(192, 168, 4, 1);
  21. IPAddress subnet(255, 255, 255, 0);
  22.  
  23. bool flag = false; //to handle complete read of BLE data
  24. int count = 0; //counter for checking correct length of received buffer from BLE, starts from 0 to 19
  25.  
  26. void setup() {
  27.     memset(&PacketBuffer, (char)0, 1024); //set all buffer to 0
  28.     pinMode(LED_BUILTIN, OUTPUT); //LOW = WiFi connected; HIGH = WiFi not connected
  29.     digitalWrite(LED_BUILTIN, HIGH);
  30.     Serial.begin(115200); //BLE serial
  31.     WiFi.config(ip, gateway, subnet);
  32.     WiFi.mode(WIFI_STA); //station mode
  33.     WiFi.begin(ssid, password);
  34.     Serial.println();
  35.     Serial.print("Wait for WiFi");
  36.     //wait for wireless connection
  37.     while (WiFi.status() != WL_CONNECTED) {
  38.         delay(500);
  39.         Serial.print(".");
  40.     }
  41.     digitalWrite(LED_BUILTIN, LOW);
  42.     Serial.println("");
  43.     Serial.println("WiFi connected");
  44.     Serial.println("IP address: " + WiFi.localIP().toString());
  45.     Serial.println();
  46.     Udp.begin(UDP_Port); //initialize UDP
  47. }
  48.  
  49. void loop() {
  50.     //if wireless connection drops, try to reconnect to it
  51.     while (WiFi.status() != WL_CONNECTED) {
  52.         digitalWrite(LED_BUILTIN, HIGH);
  53.         delay(500);
  54.         Serial.print(".");
  55.     }
  56.  
  57.     digitalWrite(LED_BUILTIN, LOW);
  58.     if (Serial.available()) {
  59.         //read one char at the time and store it to che progressive 'count' position in the buffer array
  60.         Serial.read(&PacketBuffer[count], 1);
  61.         //checking for carriage return and line feed chars
  62.         //replace carriage return (if for any reasons is present) with whitespace to avoid complications in the buffer processing by the receiver
  63.         if (PacketBuffer[count] == '\r') PacketBuffer[count] = ' ';
  64.         else if (PacketBuffer[count] == '\n') {
  65.             //at this point the one buffer from BLE serial is completely processed
  66.             PacketBuffer[count] = (char)0;
  67.             count = 0; //reset counter
  68.             flag = true; //complete data
  69.         }
  70.         else {
  71.             //increment counter for next char read
  72.             count++;
  73.         }
  74.     }
  75.  
  76.     if (flag) {
  77.         //start send data from [1] and not [0] due to how data is sent by the T-skin.
  78.         //the data in [0] is treated by a serial read as a terminator char (char)0.
  79.         //if this data ends up in the buffer that we send the calid data after that char will be ignored
  80.         //sending data from 2nd element is less time consuming that shifting all buffer
  81.         //Serial.println(&PacketBuffer[1]); //for debug
  82.         //here we send via UDP the data from BLE
  83.         Udp.beginPacket(UDP_IP, UDP_Port);
  84.         Udp.write(&PacketBuffer[1]);
  85.         Udp.endPacket();
  86.         flag = false; //reset flag for next buffer
  87.         memset(PacketBuffer, (char)0, 1024); //set all buffer to 0
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement