├── sistem.png ├── backend ├── requirements.txt ├── dockerfile └── main.py ├── frontend ├── src │ ├── index.js │ ├── three.js │ ├── App.js │ └── model.js ├── public │ └── index.html ├── nginx.conf ├── dockerfile ├── package.json └── README.md ├── .gitignore ├── README.md ├── iot └── test.ino └── docker-compose.yml /sistem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BilalAlpaslan/iot-websocket-react/HEAD/sistem.png -------------------------------------------------------------------------------- /backend/requirements.txt: -------------------------------------------------------------------------------- 1 | Fastapi==0.71.0 2 | uvicorn[standart]==0.12.3 3 | websockets 4 | gunicorn==20.0.4 -------------------------------------------------------------------------------- /frontend/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import 'bootstrap/dist/css/bootstrap.min.css'; 5 | 6 | ReactDOM.render(,document.getElementById('root')); 7 | 8 | -------------------------------------------------------------------------------- /backend/dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.9.0-alpine 2 | 3 | ADD requirements.txt /requirements.txt 4 | 5 | RUN pip install -r requirements.txt 6 | 7 | ADD main.py /main.py 8 | 9 | EXPOSE 80 10 | CMD uvicorn main:app --host 0.0.0.0 --port 80 --ws websockets -------------------------------------------------------------------------------- /frontend/src/three.js: -------------------------------------------------------------------------------- 1 | import * as THREE from 'three'; 2 | 3 | 4 | window.THREE = THREE; // THREE.OrbitControls expects THREE to be a global object 5 | require('three/examples/js/controls/OrbitControls'); 6 | 7 | 8 | export default {...THREE, OrbitControls: window.THREE.OrbitControls}; -------------------------------------------------------------------------------- /frontend/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | React App 6 | 7 | 8 | 9 |
10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # dependencies 2 | */node_modules 3 | /.pnp 4 | .pnp.js 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | *.pyc 23 | __pycache__/ 24 | -------------------------------------------------------------------------------- /frontend/nginx.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | 4 | location / { 5 | root /usr/share/nginx/html; 6 | index index.html index.htm; 7 | try_files $uri /index.html; 8 | } 9 | 10 | error_page 500 502 503 504 /50x.html; 11 | location = /50x.html { 12 | root /usr/share/nginx/html; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![resim](https://github.com/BilalAlpaslan/iot-websocket-react/blob/master/sistem.png) 2 | 3 | backend: Python/FastAPI 4 | 5 | frontend: Javascript/React 6 | 7 | 8 | 9 | -- 10 | Setup Steps 11 | -- 12 | ``` 13 | git clone https://github.com/BilalAlpaslan/iot-websocket-react.git 14 | cd iot-websocket-react 15 | 16 | docker-compose build 17 | docker-compose up -d 18 | ``` 19 | 20 | -- 21 | And You can Look: 22 | -- 23 | http://localhost:8008/ 24 | -------------------------------------------------------------------------------- /iot/test.ino: -------------------------------------------------------------------------------- 1 | int a=0; 2 | int led =13; 3 | char x="b"; 4 | 5 | void setup() { 6 | Serial.begin(9600); 7 | pinMode(led,OUTPUT); 8 | } 9 | 10 | void loop() { 11 | Serial.println(a); 12 | if (Serial.available() > 0) { 13 | x = Serial.read(); 14 | Serial.println(x); 15 | if(x==97){ 16 | digitalWrite(led,HIGH); 17 | } 18 | else if(x==98){ 19 | digitalWrite(led,LOW); 20 | } 21 | } 22 | 23 | a++; 24 | delay(1000); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.4" 2 | 3 | services: 4 | backend: 5 | build: 6 | context: ./backend/ 7 | dockerfile: ./Dockerfile 8 | container_name: backend 9 | restart: always 10 | ports: 11 | - 8000:80 12 | 13 | frontend: 14 | build: 15 | context: ./frontend/ 16 | dockerfile: ./Dockerfile 17 | container_name: frontend 18 | restart: always 19 | ports: 20 | - 8008:80 -------------------------------------------------------------------------------- /frontend/dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:16.9.0 as build 2 | 3 | RUN mkdir -p /usr/src/app 4 | WORKDIR /usr/src/app 5 | 6 | ADD ./package.json /usr/src/app/package.json 7 | 8 | RUN npm install react-scripts@4.0.3 -g 9 | RUN npm install 10 | 11 | ADD . /usr/src/app 12 | 13 | RUN npm run build 14 | 15 | ##################################### 16 | 17 | FROM nginx:stable-alpine 18 | 19 | RUN rm /etc/nginx/conf.d/default.conf 20 | COPY nginx.conf /etc/nginx/conf.d 21 | 22 | COPY --from=build /usr/src/app/build /usr/share/nginx/html 23 | EXPOSE 80 24 | CMD ["nginx", "-g", "daemon off;"] 25 | -------------------------------------------------------------------------------- /frontend/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "frontend", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.9", 7 | "@testing-library/react": "^11.2.5", 8 | "@testing-library/user-event": "^12.8.3", 9 | "bootstrap": "^4.6.0", 10 | "react": "^17.0.1", 11 | "react-bootstrap": "^1.5.2", 12 | "react-dom": "^17.0.1", 13 | "react-scripts": "4.0.3", 14 | "reactstrap": "^8.9.0", 15 | "three": "^0.126.1", 16 | "web-vitals": "^1.1.0", 17 | "websocket": "^1.0.33" 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "eslintConfig": { 26 | "extends": [ 27 | "react-app", 28 | "react-app/jest" 29 | ] 30 | }, 31 | "browserslist": { 32 | "production": [ 33 | ">0.2%", 34 | "not dead", 35 | "not op_mini all" 36 | ], 37 | "development": [ 38 | "last 1 chrome version", 39 | "last 1 firefox version", 40 | "last 1 safari version" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /backend/main.py: -------------------------------------------------------------------------------- 1 | import serial 2 | from serial.tools import list_ports 3 | from fastapi import FastAPI, WebSocket, WebSocketDisconnect 4 | 5 | 6 | app = FastAPI() 7 | 8 | 9 | def get_port() -> serial.Serial: 10 | ports = [p.device for p in list_ports.comports() if 'USB' in p.description] 11 | if not ports: 12 | raise IOError("Seri Baglantili cihaz yok!") 13 | return serial.Serial(ports[0], 9600) 14 | 15 | 16 | ser = get_port() 17 | 18 | 19 | @app.websocket("/") 20 | async def websocket_endpoint(websocket: WebSocket): 21 | websocket.accept() 22 | 23 | try: 24 | while True: 25 | data = await websocket.receive_text() 26 | 27 | if data == "97": 28 | ser.write("a".encode()) 29 | print("a") 30 | elif data == "98": 31 | ser.write("b".encode()) 32 | print("b") 33 | else: 34 | print("[another commend]", data) 35 | 36 | except WebSocketDisconnect as e: 37 | print("disconnect", e) 38 | 39 | if __name__ == '__main__': 40 | import uvicorn 41 | uvicorn.run( 42 | "main:app", 43 | host="192.168.1.104", 44 | port=8001, 45 | ) 46 | -------------------------------------------------------------------------------- /frontend/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { w3cwebsocket as W3CWebSocket } from "websocket"; 3 | import Model from "./model"; 4 | 5 | var client = new W3CWebSocket("ws://localhost:8008"); 6 | 7 | class App extends Component { 8 | state = { 9 | data: [], 10 | }; 11 | 12 | buttonClick = (data) => client.send(data) 13 | 14 | componentDidMount() { 15 | client.onopen = () => console.log("WebSocket Connected"); 16 | 17 | client.onmessage = (message) => { 18 | if (Number.isInteger(parseInt(message.data))) { 19 | this.setState({ data: this.state.data.concat([message.data]) }); 20 | 21 | if (this.state.data.length > 9) 22 | this.setState({ data: this.state.data.slice(1, this.state.data.length) }); 23 | 24 | } 25 | }; 26 | } 27 | 28 | render() { 29 | return ( 30 |
31 | 32 | 33 |
34 |
35 | {this.state.data.map(data => 36 |
{data}
37 | )} 38 |
39 |
40 |
41 | {/* */} 42 |
43 |
44 |
45 |
46 | ); 47 | } 48 | } 49 | 50 | export default App; 51 | -------------------------------------------------------------------------------- /frontend/src/model.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | 3 | import THREE from "./three"; 4 | 5 | 6 | class Model extends Component { 7 | componentDidMount() { 8 | 9 | var scene = new THREE.Scene(); 10 | 11 | var WIDTH = window.innerWidth/2-150; 12 | var HEIGHT = window.innerHeight-150; 13 | 14 | var camera = new THREE.PerspectiveCamera(70,WIDTH / HEIGHT,0.1,10000); 15 | // camera.position.z = 0; 16 | 17 | var renderer = new THREE.WebGLRenderer(); 18 | renderer.setSize(WIDTH, HEIGHT); 19 | 20 | this.mount.appendChild(renderer.domElement); 21 | 22 | window.onload=function(){ 23 | this.controls = new THREE.OrbitControls(camera); 24 | } 25 | 26 | 27 | 28 | // ADD OBJECT 29 | // var geometry = new THREE.BoxGeometry(2, 2, 2); 30 | // var material = new THREE.MeshPhongMaterial( { 31 | // color: 0x156289, 32 | // emissive: 0x072534, 33 | // side: THREE.DoubleSide, 34 | // flatShading: true 35 | // } ); 36 | var sphereMaterial = new THREE.MeshLambertMaterial( { 37 | color: 0xCC0000, 38 | wireframe: false 39 | }); 40 | 41 | var RADIUS = 30; 42 | var SEGMENTS = 30; 43 | var RINGS = 120; 44 | 45 | // var cube = new THREE.Mesh(geometry, material); 46 | var sphere = new THREE.Mesh(new THREE.CylinderGeometry(RADIUS,SEGMENTS,RINGS),sphereMaterial); 47 | // scene.add(cube); 48 | 49 | sphere.position.x = 15; 50 | sphere.position.y = -20; 51 | sphere.position.z = -300; 52 | 53 | scene.add(sphere); 54 | 55 | // ADD LIGHTS 56 | var lights = []; 57 | lights[ 0 ] = new THREE.PointLight( 0xffffff, 1, 0 ); 58 | lights[ 1 ] = new THREE.PointLight( 0xffffff, 1, 0 ); 59 | lights[ 2 ] = new THREE.PointLight( 0xffffff, 1, 0 ); 60 | 61 | lights[ 0 ].position.set( 0, 200, 0 ); 62 | lights[ 1 ].position.set( 100, 200, 100 ); 63 | lights[ 2 ].position.set( - 100, - 200, - 100 ); 64 | 65 | scene.add( lights[ 0 ] ); 66 | scene.add( lights[ 1 ] ); 67 | scene.add( lights[ 2 ] ); 68 | 69 | 70 | 71 | // SCALE ON RESIZE 72 | var tanFOV = Math.tan( ( ( Math.PI / 180 ) * camera.fov / 2 ) ); 73 | var windowHeight = window.innerHeight; 74 | window.addEventListener( 'resize', onWindowResize, false ); 75 | function onWindowResize( event ) { 76 | camera.aspect = window.innerWidth / window.innerHeight; 77 | camera.fov = ( 360 / Math.PI ) * Math.atan( tanFOV * ( window.innerHeight / windowHeight ) ); 78 | 79 | camera.updateProjectionMatrix(); 80 | camera.lookAt( scene.position ); 81 | 82 | renderer.setSize( window.innerWidth, window.innerHeight ); 83 | renderer.render( scene, camera ); 84 | } 85 | 86 | 87 | // ANIMATE THE SCENE 88 | var animate = function() { 89 | requestAnimationFrame(animate); 90 | sphere.rotation.x = THREE.Math.degToRad(80); 91 | sphere.rotation.y = THREE.Math.degToRad(110); 92 | sphere.rotation.z = THREE.Math.degToRad(90); 93 | 94 | renderer.render(scene, camera); 95 | }; 96 | 97 | animate(); 98 | } 99 | 100 | render() {return
(this.mount = ref)} />;} 101 | } 102 | export default Model 103 | -------------------------------------------------------------------------------- /frontend/README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 39 | 40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | --------------------------------------------------------------------------------