Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include "ESP8266WebServer.h"
- #include <WebSocketsServer.h>
- /*Datetime*/
- /*Datetime*/
- /*Json library*/
- #include <ArduinoJson.h>
- /*Json library*/
- /*Scale library*/
- #include "HX711.h"
- /*Scale library*/
- /*Nfc library*/
- #include <SPI.h>
- #include <PN532_SPI.h>
- #include <PN532.h>
- #include <NfcAdapter.h>
- /*Nfc library*/
- /*Scale*/
- HX711 scale(D1, D2);
- /*Websocket server*/
- WebSocketsServer webSocket = WebSocketsServer(81);
- /*Nfc*/
- PN532_SPI pn532spi(SPI, D8);
- NfcAdapter nfc = NfcAdapter(pn532spi);
- const char* ssid = "HUAWEI-B525-5D26";
- const char* password = "2R5FDHGYNAE";
- long blinkDelay = 500;
- ESP8266WebServer server(80);
- uint8_t con = 0;
- void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
- switch (type) {
- case WStype_DISCONNECTED:
- Serial.printf("[%u] Disconnected!\n", num);
- break;
- case WStype_CONNECTED:
- {
- IPAddress ip = webSocket.remoteIP(num);
- Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
- con = num;
- // send message to client
- webSocket.sendTXT(num, "Connected");
- }
- break;
- case WStype_TEXT:
- Serial.printf("[%u] get Text: %s\n", num, payload);
- // send message to client
- // webSocket.sendTXT(num, "message here");
- // send data to all connected clients
- // webSocket.broadcastTXT("message here");
- break;
- case WStype_BIN:
- Serial.printf("[%u] get binary length: %u\n", num, length);
- hexdump(payload, length);
- // send message to client
- // webSocket.sendBIN(num, payload, length);
- break;
- }
- }
- void handleRootPath() {
- if (server.hasArg("delay")) {
- blinkDelay = server.arg("delay").toInt();
- }
- //HTML, CSS and JavaScript being send to the webserver
- 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>");
- }
- void handleJsRootPath() {
- server.send(200,"text/html", "");
- }
- void handleCssRootPath() {
- server.send(200, "text / html", "");
- }
- void setup() {
- pinMode(D3, OUTPUT);
- Serial.begin(9600);
- /*74880*/
- delay(800);
- Serial.println();
- Serial.print("Trying to connect to AP with SSID: ");
- Serial.print(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.print("Connected to ip: ");
- Serial.println(WiFi.localIP());
- Serial.print("Port: ");
- Serial.println("80");
- /*Signal strength to AP*/
- long rssi = WiFi.RSSI();
- Serial.print("Strength of signal for AP : ");
- Serial.print(rssi);
- Serial.println(" dBm");
- server.on("/", handleRootPath);
- server.on("/js", handleJsRootPath);
- server.on("/css", handleCssRootPath);
- server.begin();
- webSocket.begin();
- /*Nfc begin*/
- nfc.begin();
- webSocket.onEvent(webSocketEvent);
- }
- void loop() {
- int data[2];
- char* consumabledata[1];
- DynamicJsonBuffer jsonBuffer;
- JsonObject& root = jsonBuffer.createObject();
- if (nfc.tagPresent()) {
- NfcTag tag = nfc.read();
- if (tag.hasNdefMessage()) {
- NdefRecord record = tag.getNdefMessage().getRecord(0);
- int payloadLength = record.getPayloadLength();
- char payload[payloadLength + 1];
- record.getPayload((byte*)payload);
- payload[payloadLength] = '\0';
- /*Serial.println(payload);*/
- data[0] = 1;/*insert time and date here to return in Json.*/
- data[1] = scale.get_units();
- consumabledata[1] = payload;
- root["Id"] = data[0];
- root["Weight"] = data[1];
- root["Consumable"] = consumabledata[1];
- String jsonStr;
- root.printTo(jsonStr);
- Serial.println(jsonStr);
- webSocket.sendTXT(con, jsonStr);
- server.handleClient();
- webSocket.loop();
- delay(5000);
- }
- } else {
- data[0] = 1;/*insert time and date here to return in Json.*/
- data[1] = scale.get_units();
- consumabledata[0] = "Nothing on weight";
- /*Json*/
- root["Id"] = data[0];
- root["Weight"] = data[1];
- root["Consumable"] = consumabledata[0];
- String jsonStr;
- root.printTo(jsonStr);
- Serial.println(jsonStr);
- webSocket.sendTXT(con, jsonStr);
- server.handleClient();
- webSocket.loop();
- delay(5000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement