Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #define MODULE_ID 20
  2.  
  3. #define DATAGRAM_PREFIX_SIZE 1
  4.  
  5. // Serial debug enabled if DEBUG is defined (comment for production)
  6. //#define DEBUG
  7.  
  8. #define REMOTE_UPDATE
  9.  
  10. #define UDP_MAX_PACKET_SIZE 512
  11.  
  12. #include <ESP8266WiFi.h>
  13. #include <WiFiUDP.h>
  14. IPAddress remote = IPAddress(0, 0, 0, 0);
  15.  
  16. #if defined(REMOTE_UPDATE)
  17. #include <ESP8266WebServer.h>
  18. #include <ESP8266mDNS.h>
  19. #include <ESP8266HTTPUpdateServer.h>
  20. #include <DNSServer.h>
  21. #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
  22. #endif
  23.  
  24. #if defined(REMOTE_UPDATE)
  25. String _hostbase = "module-" + String(MODULE_ID);
  26. const char* host = _hostbase.c_str();
  27. const char* update_path = "/firmware";
  28. const char* update_username = "admin";
  29. const char* update_password = "admin";
  30. ESP8266WebServer httpServer(80);
  31. ESP8266HTTPUpdateServer httpUpdater;
  32. #endif
  33.  
  34. int wifiConnected = false;
  35. unsigned int localPort = 8888;
  36. WiFiUDP UDP;
  37. boolean udpConnected = false;
  38. char packetBuffer[UDP_MAX_PACKET_SIZE]; //buffer to hold incoming packet,
  39. char ReplyBuffer[5] = "ACKI"; // a string to send back
  40.  
  41. #include "wifi_credentials.h"
  42. #include <NeoPixelBus.h>
  43. #define NUMLEDS 150
  44.  
  45. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(NUMLEDS);
  46.  
  47. void setup() {
  48. // put your setup code here, to run once:
  49. pinMode(D0, OUTPUT);
  50. pinMode(D3, INPUT_PULLUP);
  51. digitalWrite(D0, HIGH);
  52.  
  53. #if defined(DEBUG)
  54. Serial.begin(115200);
  55. Serial.println("Initializing...");
  56. #endif
  57.  
  58. strip.Begin();
  59. for (int i = 0; i < NUMLEDS; i++)
  60. strip.SetPixelColor(i, RgbColor(0, 0, 0));
  61. strip.Show();
  62.  
  63. wifiConnected = connectWifi();
  64. if (wifiConnected)
  65. {
  66. if (UDP.begin(localPort) == 1)
  67. {
  68. for (int i = wifiConnected; i < NUMLEDS; i++)
  69. {
  70. strip.SetPixelColor(i, RgbColor(0, 64, 0));
  71. strip.Show();
  72. delay(10);
  73. }
  74. }
  75. else
  76. {
  77. while (1)
  78. {
  79. for (int i = 0; i < NUMLEDS; i++)
  80. strip.SetPixelColor(i, RgbColor(64, 0, 0));
  81. strip.Show();
  82. delay(500);
  83. for (int i = 0; i < NUMLEDS; i++)
  84. strip.SetPixelColor(i, RgbColor(0, 0, 0));
  85. strip.Show();
  86. delay(500);
  87. }
  88. }
  89. }
  90. else
  91. {
  92. while (1)
  93. {
  94. for (int i = 0; i < NUMLEDS; i++)
  95. strip.SetPixelColor(i, RgbColor(64, 0, 0));
  96. strip.Show();
  97. delay(1000);
  98. for (int i = 0; i < NUMLEDS; i++)
  99. strip.SetPixelColor(i, RgbColor(0, 0, 0));
  100. strip.Show();
  101. delay(1000);
  102. }
  103. }
  104.  
  105. #if defined(REMOTE_UPDATE)
  106. MDNS.begin(host);
  107. httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  108. httpServer.begin();
  109. MDNS.addService("http", "tcp", 80);
  110. #endif
  111.  
  112. #if defined(DEBUG)
  113. Serial.println("Succesfully initialized");
  114. #endif
  115.  
  116. }
  117.  
  118.  
  119. uint32_t ledBlinkTimer = 0;
  120. bool ledState = 0;
  121. float fps = 0.0;
  122. bool serverIsActive = 0;
  123. uint32_t serverActivityTimer = 0;
  124.  
  125. void loop() {
  126. // put your main code here, to run repeatedly:
  127. #if defined(REMOTE_UPDATE)
  128. httpServer.handleClient();
  129. #endif
  130.  
  131. if (digitalRead(D3) == LOW)
  132. {
  133.  
  134. }
  135.  
  136. if (millis() - ledBlinkTimer > 1000)
  137. {
  138. ledBlinkTimer = millis();
  139. digitalWrite(D0, ledState);
  140. ledState = 1 - ledState;
  141.  
  142. if (millis() - serverActivityTimer < 500)
  143. {
  144. UDP.beginPacket(UDP.remoteIP(), 7777);
  145. UDP.write(0xF);
  146. UDP.write((uint8_t)(fps));
  147. UDP.write(MODULE_ID);
  148. UDP.endPacket();
  149. }
  150. }
  151.  
  152. if (millis() - serverActivityTimer > 250)
  153. {
  154. fps = 0;
  155. }
  156.  
  157. int packetSize = UDP.parsePacket();
  158. if (packetSize)
  159. {
  160. //get remote IP
  161. remote = UDP.remoteIP();
  162. //read the packet into packetBufffer
  163. UDP.read(packetBuffer, UDP_MAX_PACKET_SIZE);
  164.  
  165. serverActivityTimer = millis();
  166.  
  167. //Send back IP adress and my ID to main server
  168. if (packetBuffer[0] == 0xDE) //DEtect
  169. {
  170. serverIsActive = true;
  171. UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
  172. UDP.write(0xDE);
  173. UDP.write(WiFi.localIP()[0]);
  174. UDP.write(WiFi.localIP()[1]);
  175. UDP.write(WiFi.localIP()[2]);
  176. UDP.write(WiFi.localIP()[3]);
  177. UDP.write(MODULE_ID);
  178. UDP.endPacket();
  179. }
  180. else if (packetBuffer[0] == 0xDA) //DAta
  181. {
  182. for (int i = 1; i < NUMLEDS + 1; i++)
  183. {
  184. strip.SetPixelColor(i - 1, RgbColor(packetBuffer[3 * i + DATAGRAM_PREFIX_SIZE],
  185. packetBuffer[3 * i + 1 + DATAGRAM_PREFIX_SIZE],
  186. packetBuffer[3 * i + 2 + DATAGRAM_PREFIX_SIZE]));
  187. }
  188. strip.Show();
  189. ledState = 1 - ledState;
  190. digitalWrite(D0, ledState);
  191.  
  192. processfps();
  193. }
  194. }
  195. }
  196.  
  197.  
  198. int connectWifi() {
  199. boolean state = true;
  200. int i = 0;
  201. WiFi.mode(WIFI_STA);
  202. WiFi.begin(ssid, password);
  203.  
  204. // Wait for connection
  205. while (WiFi.status() != WL_CONNECTED) {
  206. delay(500);
  207. strip.SetPixelColor(i++, RgbColor(64, 0, 0));
  208. strip.Show();
  209. if (i == NUMLEDS) {
  210. #if defined(DEBUG)
  211. Serial.println("Failed to connect to wifi...");
  212. #endif
  213. return 0;
  214. }
  215. #if defined(DEBUG)
  216. Serial.print(".");
  217. #endif
  218. }
  219. #if defined(DEBUG)
  220. Serial.println("Wifi OK");
  221. Serial.println(WiFi.localIP());
  222. #endif
  223. return i;
  224. }
  225.  
  226. uint32_t fpstimer = 0;
  227. uint32_t frameCount = 0;
  228. void processfps()
  229. {
  230. frameCount++;
  231. if (frameCount % 10 == 0)
  232. {
  233. frameCount = 0;
  234. //Serial.print("FPS : ");
  235. fps = float(10000.0 / (millis() - fpstimer));
  236. //Serial.println(fps);
  237. fpstimer = millis();
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement