Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #59 from bmedicke/mqtt-api
Browse files Browse the repository at this point in the history
create mqtt api
  • Loading branch information
nikobenedikt authored Jun 20, 2022
2 parents 28b82be + c6295a4 commit 4c0547a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@testing-library/user-event": "^13.5.0",
"better-react-mathjax": "^2.0.1",
"mathjs": "^10.6.1",
"precompiled-mqtt": "^4.3.13",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
Expand Down
29 changes: 29 additions & 0 deletions simulator/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Box from './Box.js'
import InfoOverlay from './InfoOverlay.js'
import JitterBox from './JitterBox.js'
import SweepBox from './SweepBox.js'
import mqtt from 'precompiled-mqtt'
import styles from './App.module.scss'
import {
CavityLength,
Expand Down Expand Up @@ -79,6 +80,34 @@ function App() {
const [isLengthJittering, setIsLengthJittering] = useState(false)
const [isLengthSweeping, setIsLengthSweeping] = useState(false)

// MQTT:
// mosquitto_sub -h test.mosquitto.org -t 'optical-cavity-simulator' | ts | tee app.log
// strict mode renders App twice (dev mode only):
// https://reactjs.org/docs/strict-mode.html
useEffect(() => {
const URL = 'mqtt://test.mosquitto.org:8080'
const client = mqtt.connect(URL)
const topic = 'optical-cavity-simulator'

client.on('connect', () => {
console.log(`connected to '${URL}', listening to '${topic}'`)
client.subscribe(topic, (err) => {
if (!err) {
client.publish(topic, `App() loaded`)
}
})
})

client.on('disconnect', () => {
console.log('disconnected')
})

client.on('message', (topic, message) => {
console.log(message.toString())
client.end()
})
}, [])

useEffect(() => {
setWavelengthColor(wavelength2rgb(wavelength))
changeFavicon(wavelength)
Expand Down

0 comments on commit 4c0547a

Please sign in to comment.