Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include "ESP8266WebServer.h"
  3. #include <WebSocketsServer.h>
  4. /*Datetime*/
  5. /*Datetime*/
  6. /*Json library*/
  7. #include <ArduinoJson.h>
  8. /*Json library*/
  9. /*Scale library*/
  10. #include "HX711.h"
  11. /*Scale library*/
  12.  
  13. /*Nfc library*/
  14. #include <SPI.h>
  15. #include <PN532_SPI.h>
  16. #include <PN532.h>
  17. #include <NfcAdapter.h>
  18. /*Nfc library*/
  19.  
  20. /*Scale*/
  21. HX711 scale(D1, D2);
  22.  
  23. /*Websocket server*/
  24. WebSocketsServer webSocket = WebSocketsServer(81);
  25.  
  26. /*Nfc*/
  27. PN532_SPI pn532spi(SPI, D8);
  28. NfcAdapter nfc = NfcAdapter(pn532spi);
  29.  
  30. const char* ssid = "HUAWEI-B525-5D26";
  31. const char* password = "2R5FDHGYNAE";
  32. long blinkDelay = 500;
  33. ESP8266WebServer server(80);
  34. uint8_t con = 0;
  35.  
  36. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  37.  
  38. switch (type) {
  39. case WStype_DISCONNECTED:
  40. Serial.printf("[%u] Disconnected!\n", num);
  41. break;
  42. case WStype_CONNECTED:
  43. {
  44. IPAddress ip = webSocket.remoteIP(num);
  45. Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  46. con = num;
  47. // send message to client
  48. webSocket.sendTXT(num, "Connected");
  49. }
  50. break;
  51. case WStype_TEXT:
  52. Serial.printf("[%u] get Text: %s\n", num, payload);
  53.  
  54. // send message to client
  55. // webSocket.sendTXT(num, "message here");
  56.  
  57. // send data to all connected clients
  58. // webSocket.broadcastTXT("message here");
  59. break;
  60. case WStype_BIN:
  61. Serial.printf("[%u] get binary length: %u\n", num, length);
  62. hexdump(payload, length);
  63.  
  64. // send message to client
  65. // webSocket.sendBIN(num, payload, length);
  66. break;
  67. }
  68.  
  69. }
  70.  
  71. void handleRootPath() {
  72.  
  73. if (server.hasArg("delay")) {
  74.  
  75. blinkDelay = server.arg("delay").toInt();
  76.  
  77. }
  78. //HTML, CSS and JavaScript being send to the webserver
  79. server.send(200, "text/html", "<!doctype html><html><head><meta name=\"mobile-web-app-capable\"content=\"yes\"><meta name=\"apple-mobile-web-app-capable\"content=\"yes\"><script src=\"https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js\"></script><script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.js\"></script><title>Weight</title></head><body onload=\"init();\" style=\"background: linear-gradient(to right, #555588, #333366 25%, #000033);\"><h1 style=\"color: rgb(255, 99, 132);\">Weight</h1><h3 style=\"color: rgb(255, 99, 132);\">Consumable: </h3><input type='text' id='text2'/><h3 style=\"color: rgb(255, 99, 132);\">Current weight: </h3><input type='text' id='text1'/><script>var chart = null; var chartData = []; var ws = new WebSocket('ws://192.168.8.110:81'); var scatterChart = null; function init(){ var ctx = document.getElementById('lineChart').getContext('2d'); scatterChart = new Chart(ctx, {type: 'scatter', data: {datasets: [{label: 'Statistics', backgroundColor: 'rgb(255,99,132)', borderColor: 'rgb(255,99,132)', showLine: true, fill: false, data: [] }]},options: { scales: { xAxes: [{type: 'linear', position: 'bottom'}]}}}); } ws.onmessage = function(dataReceived){var j = JSON.parse(dataReceived.data); if (j == null) return; if (chartData[j.Id] == null) { chartData[j.Id] = []; } if (localStorage[\"chartData\"] !== null && localStorage[\"chartData\"] !== undefined) { json = JSON.parse(localStorage[\"chartData\"]); chartData[j.Id] = json; } chartData[j.Id].push({x : Date.now(), y : j.Weight}); localStorage[\"chartData\"] = JSON.stringify(chartData[j.Id]); console.log(chartData[j.Id]); document.getElementById('text1').value = j.Weight; document.getElementById('text2').value = j.Consumable; scatterChart.data.datasets[0].data = chartData[j.Id]; scatterChart.update(); }</script><div style=\"width=400px;\"><canvas id=\"lineChart\"height=\"300\"width=\"800\"></canvas></div></body></html>");
  80.  
  81.  
  82. }
  83.  
  84. void handleJsRootPath() {
  85.  
  86. server.send(200,"text/html", "");
  87.  
  88. }
  89.  
  90. void handleCssRootPath() {
  91.  
  92. server.send(200, "text / html", "");
  93.  
  94. }
  95.  
  96. void setup() {
  97. pinMode(D3, OUTPUT);
  98. Serial.begin(9600);
  99. /*74880*/
  100. delay(800);
  101. Serial.println();
  102. Serial.print("Trying to connect to AP with SSID: ");
  103. Serial.print(ssid);
  104.  
  105. WiFi.begin(ssid, password);
  106.  
  107. while (WiFi.status() != WL_CONNECTED) {
  108. delay(500);
  109. Serial.print(".");
  110. }
  111. Serial.println();
  112. Serial.print("Connected to ip: ");
  113. Serial.println(WiFi.localIP());
  114. Serial.print("Port: ");
  115. Serial.println("80");
  116. /*Signal strength to AP*/
  117. long rssi = WiFi.RSSI();
  118. Serial.print("Strength of signal for AP : ");
  119. Serial.print(rssi);
  120. Serial.println(" dBm");
  121.  
  122. server.on("/", handleRootPath);
  123. server.on("/js", handleJsRootPath);
  124. server.on("/css", handleCssRootPath);
  125. server.begin();
  126.  
  127. webSocket.begin();
  128. /*Nfc begin*/
  129. nfc.begin();
  130. webSocket.onEvent(webSocketEvent);
  131.  
  132. }
  133. void loop() {
  134.  
  135. int data[2];
  136. char* consumabledata[1];
  137. DynamicJsonBuffer jsonBuffer;
  138.  
  139. JsonObject& root = jsonBuffer.createObject();
  140.  
  141. if (nfc.tagPresent()) {
  142. NfcTag tag = nfc.read();
  143. if (tag.hasNdefMessage()) {
  144. NdefRecord record = tag.getNdefMessage().getRecord(0);
  145. int payloadLength = record.getPayloadLength();
  146. char payload[payloadLength + 1];
  147. record.getPayload((byte*)payload);
  148. payload[payloadLength] = '\0';
  149.  
  150. /*Serial.println(payload);*/
  151. data[0] = 1;/*insert time and date here to return in Json.*/
  152. data[1] = scale.get_units();
  153. consumabledata[1] = payload;
  154.  
  155. root["Id"] = data[0];
  156. root["Weight"] = data[1];
  157. root["Consumable"] = consumabledata[1];
  158.  
  159. String jsonStr;
  160. root.printTo(jsonStr);
  161. Serial.println(jsonStr);
  162. webSocket.sendTXT(con, jsonStr);
  163. server.handleClient();
  164. webSocket.loop();
  165. delay(5000);
  166.  
  167. }
  168.  
  169. } else {
  170.  
  171. data[0] = 1;/*insert time and date here to return in Json.*/
  172. data[1] = scale.get_units();
  173. consumabledata[0] = "Nothing on weight";
  174. /*Json*/
  175.  
  176. root["Id"] = data[0];
  177. root["Weight"] = data[1];
  178. root["Consumable"] = consumabledata[0];
  179.  
  180. String jsonStr;
  181. root.printTo(jsonStr);
  182. Serial.println(jsonStr);
  183. webSocket.sendTXT(con, jsonStr);
  184. server.handleClient();
  185. webSocket.loop();
  186. delay(5000);
  187.  
  188. }
  189.  
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement