Advertisement
ceterumcenseo

ESP send raw frames

Nov 30th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. // ESP send raw frames
  2. // src https://stackoverflow.com/questions/46224416/esp8266-send-beacon-frame-with-deep-sleep-mode-does-not-work-properly
  3.  
  4. #include <ESP8266WiFi.h>
  5. extern "C" {
  6. #include "user_interface.h"
  7. }
  8.  
  9. void setup() {
  10. delay(500);
  11. // sendBeacon("ESP8266");
  12. // ESP.deepSleep(10e5);
  13. }
  14.  
  15. void loop() {
  16. sendBeacon("ESP8266");
  17. }
  18.  
  19. void sendBeacon(char* ssid) {
  20. // Randomize channel //
  21. byte channel = 1;
  22. wifi_set_channel(channel);
  23.  
  24. uint8_t packet[128] = { 0x80, 0x00, //Frame Control
  25. 0x00, 0x00, //Duration
  26. /*4*/ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, //Destination address
  27. /*10*/ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, //Source address - overwritten later
  28. /*16*/ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, //BSSID - overwritten to the same as the source address
  29. /*22*/ 0xc0, 0x6c, //Seq-ctl
  30. //Frame body starts here
  31. /*24*/ 0x83, 0x51, 0xf7, 0x8f, 0x0f, 0x00, 0x00, 0x00, //timestamp - the number of microseconds the AP has been active
  32. /*32*/ 0xFF, 0x00, //Beacon interval
  33. /*34*/ 0x01, 0x04, //Capability info
  34. /* SSID */
  35. /*36*/ 0x00
  36. };
  37.  
  38. int ssidLen = strlen(ssid);
  39. packet[37] = ssidLen;
  40.  
  41. for(int i = 0; i < ssidLen; i++) {
  42. packet[38+i] = ssid[i];
  43. }
  44.  
  45. uint8_t postSSID[13] = {0x01, 0x08, 0x82, 0x84, 0x8b, 0x96, 0x24, 0x30, 0x48, 0x6c, //supported rate
  46. 0x03, 0x01, 0x04 /*DSSS (Current Channel)*/ };
  47.  
  48. for(int i = 0; i < 12; i++) {
  49. packet[38 + ssidLen + i] = postSSID[i];
  50. }
  51.  
  52. packet[50 + ssidLen] = channel;
  53.  
  54. // get SRC MAC
  55. unsigned char mac[6];
  56. WiFi.macAddress(mac);
  57. packet[10] = packet[16] = mac[0];
  58. packet[11] = packet[17] = mac[1];
  59. packet[12] = packet[18] = mac[2];
  60. packet[13] = packet[19] = mac[3];
  61. packet[14] = packet[20] = mac[4];
  62. packet[15] = packet[21] = mac[5];
  63.  
  64. int packetSize = 51 + ssidLen;
  65.  
  66. wifi_send_pkt_freedom(packet, packetSize, 0);
  67. delay(1);
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement