Created
April 23, 2022 01:31
-
-
Save LintangWisesa/56751b8766fdd71175bc41d0ba803229 to your computer and use it in GitHub Desktop.
Wio Terminal x Qubitro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define WIFI_SSID "YOUR_WIFI_SSID" | |
| #define WIFI_PASS "YOUR_WIFI_PASSWORD" | |
| #define MQTT_BROKER "broker.qubitro.com" | |
| #define MQTT_USER "YOUR_QUBITRO_DEVICE_ID" | |
| #define MQTT_PASS "YOUR_QUBITRO_DEVICE_TOKEN" | |
| #define MQTT_CLID "YOUR_QUBITRO_DEVICE_ID" | |
| #define MQTT_PORT 1883 | |
| #define MQTT_TOPIC "YOUR_QUBITRO_DEVICE_ID" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // import libraries | |
| #include "credential.h" | |
| #include "TFT_eSPI.h" | |
| #include "LIS3DHTR.h" | |
| #include "rpcWiFi.h" | |
| #include <ArduinoMqttClient.h> | |
| // objects | |
| TFT_eSPI tft; | |
| LIS3DHTR<TwoWire> lis; | |
| int button = 0; | |
| WiFiClient wifiClient; | |
| MqttClient mqttClient(wifiClient); | |
| const char* ssid = WIFI_SSID; | |
| const char* password = WIFI_PASS; | |
| void setup() { | |
| // TFT LCD setup | |
| tft.begin(); | |
| tft.setRotation(3); | |
| tft.fillScreen(TFT_BLACK); | |
| // sensors setup | |
| lis.begin(Wire1); | |
| lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); | |
| lis.setFullScaleRange(LIS3DHTR_RANGE_2G); | |
| pinMode(WIO_LIGHT, INPUT); | |
| pinMode(WIO_MIC, INPUT); | |
| pinMode(WIO_5S_PRESS, INPUT_PULLUP); | |
| // connect WiFi | |
| WiFi.mode(WIFI_STA); | |
| WiFi.disconnect(); | |
| tft.setTextSize(2); | |
| tft.drawString("Processing...", 20, 20); | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| WiFi.begin(ssid, password); | |
| } | |
| tft.setTextSize(2); | |
| tft.drawString("WiFi connected!", 20, 50); | |
| // connect to Qubitro mqtt broker | |
| mqttClient.setId(MQTT_CLID); | |
| mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS); | |
| if (!mqttClient.connect(MQTT_BROKER, MQTT_PORT)){ | |
| tft.setTextSize(2); | |
| tft.drawString("Failed connect to Qubitro", 20, 80); | |
| } | |
| tft.setTextSize(2); | |
| tft.drawString("Qubitro connected!", 20, 80); | |
| delay(3000); | |
| tft.fillScreen(TFT_BLACK); | |
| } | |
| void loop() { | |
| int light = analogRead(WIO_LIGHT); | |
| int mic = analogRead(WIO_MIC); | |
| float x, y, z; | |
| x = lis.getAccelerationX(); | |
| y = lis.getAccelerationY(); | |
| z = lis.getAccelerationZ(); | |
| // event: if button pressed, publish data to Qubitro | |
| if (digitalRead(WIO_5S_PRESS) == LOW) { | |
| button += 1; | |
| // publish data | |
| mqttClient.poll(); | |
| mqttClient.beginMessage(MQTT_TOPIC); | |
| // send JSON string | |
| mqttClient.print("{"); | |
| mqttClient.print("\"accx\":"); | |
| mqttClient.print(x); | |
| mqttClient.print(","); | |
| mqttClient.print("\"accy\":"); | |
| mqttClient.print(y); | |
| mqttClient.print(","); | |
| mqttClient.print("\"accz\":"); | |
| mqttClient.print(z); | |
| mqttClient.print(","); | |
| mqttClient.print("\"light\":"); | |
| mqttClient.print(light); | |
| mqttClient.print(","); | |
| mqttClient.print("\"mic\":"); | |
| mqttClient.print(mic); | |
| mqttClient.print(","); | |
| mqttClient.print("\"button\":"); | |
| mqttClient.print(button); | |
| mqttClient.print("}"); | |
| mqttClient.endMessage(); | |
| } | |
| // show sensor values on TFT LCD | |
| tft.setTextSize(2); | |
| tft.drawString("Acc X:", 20, 20); | |
| tft.drawString("Acc Y:", 20, 95); | |
| tft.drawString("Acc Z:", 20, 165); | |
| tft.drawString("Light:", 180, 20); | |
| tft.drawString("Microphone:", 180, 95); | |
| tft.drawString("Button:", 180, 165); | |
| tft.setTextSize(4); | |
| tft.drawString(String(x), 20, 45); | |
| tft.drawString(String(y), 20, 120); | |
| tft.drawString(String(z), 20, 190); | |
| tft.drawString(String(light), 180, 45); | |
| tft.drawString(String(mic), 180, 120); | |
| tft.drawString(String(button), 180, 190); | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment