// Long Distance Hug by Ana Jay, code from Parihug IoT Hug project by Xyla Foxlin, adapted 2023 & 2024 by Becky Stern
// Watch the video: https://youtu.be/u6gj8VzrHBo
// Tutorial: https://beckystern.com/2023/11/27/hug-sensing-iot-parihug-toy-w-xyla-foxlin/
// This code is in the Public Domain
#define SECRET_SSID "Internet of Things Class"
#define SECRET_OPTIONAL_PASS "iheartarduino"
#define SECRET_DEVICE_KEY "gg0N8MV!wlYjIqlA8m#Db@yW@"
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
// Use dedicated hardware SPI pins
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
const char DEVICE_LOGIN_NAME[] = "15f3eccb-0c6f-46a8-8ef1-183a610a3a3c";
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = SECRET_DEVICE_KEY; // Secret device password
void onHugChange();
bool hug;
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(hug, READWRITE, ON_CHANGE, onHugChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
#include <Wire.h>
#include "Adafruit_MPRLS.h"
// You dont *need* a reset and EOC pin for most uses, so we set to -1 and don't connect
#define RESET_PIN -1 // set to any GPIO pin # to hard-reset on begin()
#define EOC_PIN -1 // set to any GPIO pin to read end-of-conversion by pin
Adafruit_MPRLS mpr = Adafruit_MPRLS(RESET_PIN, EOC_PIN);
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
//pinMode(12, OUTPUT);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
Serial.println("Testing MPRLS sensor...");
if (! mpr.begin()) {
Serial.println("Failed to communicate with MPRLS sensor, check wiring?");
while (1) {
delay(10);
}
}
Serial.println("Found MPRLS sensor");
// turn on backlite
pinMode(TFT_BACKLITE, OUTPUT);
digitalWrite(TFT_BACKLITE, HIGH);
// turn on the TFT / I2C power supply
pinMode(TFT_I2C_POWER, OUTPUT);
digitalWrite(TFT_I2C_POWER, HIGH);
delay(10);
// initialize TFT
tft.init(135, 240); // Init ST7789 240x135
tft.setRotation(3);
tft.fillScreen(ST77XX_BLACK);
// large block of text
tft.fillScreen(ST77XX_BLACK);
testdrawtext(
"Booting hugs... ",
ST77XX_WHITE);
delay(1000);
tft.fillScreen(ST77XX_BLACK);
Serial.println(F("Initialized"));
}
void loop() {
ArduinoCloud.update();
float pressure_hPa = mpr.readPressure();
Serial.print("Pressure (hPa): "); Serial.println(pressure_hPa);
Serial.print("Pressure (PSI): "); Serial.println(pressure_hPa / 68.947572932);
if (pressure_hPa > 1100){
hug=true;
Serial.println("Hug = true");
// Serial.println("buzz buzz loop");
// digitalWrite(12, HIGH);
// delay(1000);
// digitalWrite(12, LOW);
// large block of text
tft.fillScreen(ST77XX_BLACK);
testdrawtext(
"Hugs! I love you. ",
ST77XX_WHITE);
delay(5000);
tft.fillScreen(ST77XX_BLACK);
}else{
hug=false;
Serial.println("Hug = false");
}
delay(1000);
}
/*
Since Hug is READ_WRITE variable, onHugChange() is
executed every time a new value is received from IoT Cloud.
*/
void onHugChange() {
if(hug==true){
Serial.println("onHugChange has been called");
// digitalWrite(12, HIGH);
// delay(1000);
// digitalWrite(12, LOW);
tft.fillScreen(ST77XX_BLACK);
testdrawtext(
"Hugs! I love you. ",
ST77XX_WHITE);
delay(5000);
tft.fillScreen(ST77XX_BLACK);
}
}
void testdrawtext(char *text, uint16_t color) {
tft.setCursor(0, 0);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(text);
}