pasquale123456
Published © CC BY-NC-SA

Lover's Helper

A simple way to send message to your loves, plus another awesome feature!

IntermediateShowcase (no instructions)344
Lover's Helper

Things used in this project

Hardware components

Arduino Oplà IoT Kit
Arduino Oplà IoT Kit
×1

Software apps and online services

Arduino IoT Cloud
Arduino IoT Cloud

Story

Read more

Code

lover.ino

Arduino
It is the sketch that has to be uploaded on the Arduino .
#include <Arduino_MKRIoTCarrier.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <WiFiNINA.h>
#include <Arduino_JSON.h>
#include <math.h>
MKRIoTCarrier carrier;

//global variable declaration
int displayScreen = 0; //change it from 0 to 5 to have different screen for message

// strings that contain message
String display_message_1;
String display_message_2;
String display_message_3;
String display_message_4;
String display_message_5;

uint32_t colorGreen = carrier.leds.Color(255, 0, 0);
uint32_t colorBlue = carrier.leds.Color( 0, 0, 255);
uint32_t noColor = carrier.leds.Color( 0, 0, 0);

//wifi setting you should use the secret tab in the cloud to use it or cancel and replace with a string
char ssid[] = SECRET_SSID; //  your network SSID (name)
char pass[] = SECRET_PASS; //  your network PASSWORD 


void onMessageChange();

String carrier_message;
String message;
float humidity;
float pressure;
float temperature;

String bodyName;
String planet;
String explorerName;
String explorerDate;
double gravity;
double density;

char *planets[] = {"jupiter", "io", "europe",
                   "callisto", "mars", "mercury",
                   "venus", "terre", "uranus", "neptune",
                   "saturne", "phoebe", "ganymede", "titan",
                   "pluton", "triton", "titania", "charon", "ariel",
                   "tethys", "protee"
                  };

int status = WL_IDLE_STATUS;
char server[] = "api.le-systeme-solaire.net";

WiFiConnectionHandler ArduinoIoTPreferredConnection(ssid, pass);

WiFiClient client;


/////////////setup function start////////////////
void setup() {

  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  //Get Cloud Info/errors , 0 (only errors) up to 4
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();

  //Wait to get cloud connection to init the carrier
  while (ArduinoCloud.connected() != 1) {
    ArduinoCloud.update();
    delay(500);
  }


  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    // wait 1 seconds for connection:
    delay(1000);
  }

  Serial.println("Connected to wifi");
  delay(500);
  CARRIER_CASE = true; // false if the carrier is not in the plastic case
  carrier.begin();
  carrier.display.setRotation(0);
  carrier.display.fillScreen(ST77XX_MAGENTA);
  carrier.display.setCursor(30, 60);
  carrier.display.setTextColor(ST77XX_WHITE);
  carrier.display.setTextSize(2);
  carrier.display.print("Searching");
  delay(500);
  carrier.display.print(".");
  delay(500);
  carrier.display.print(".");
  delay(500);
  carrier.display.print(".");
  delay(500);
  carrier.display.print(".");

  planetUpdate(); // the function does not stop until it found that json file

  carrier.display.fillScreen(ST77XX_MAGENTA);
  carrier.display.setCursor(30, 60);
  carrier.display.setTextColor(ST77XX_WHITE);
  carrier.display.setTextSize(2);
  carrier.display.print("Update complete");
  carrier.display.setCursor(20, 90);
  carrier.display.print("Data from: ");
  carrier.display.setCursor(20, 130);
  carrier.display.print(bodyName);
}
/////////////setup function end////////////////



/////////////loop function start////////////////
void loop() {

  ArduinoCloud.update();
  carrier.Buttons.update();

  delay(100);
  carrier.Buttons.update();
  temperature = carrier.Env.readTemperature();
  humidity = carrier.Env.readHumidity();
  pressure = carrier.Pressure.readPressure();


  if (carrier.Buttons.onTouchDown(TOUCH0)) {
    displayScreen = truncf(temperature / 10);
    carrier_message = "Readed my love";
    currentScreen();
    notification();

  }



  if (carrier.Buttons.onTouchDown(TOUCH1)) {
    carrier.display.fillScreen(ST77XX_RED);
    carrier.display.setCursor(20, 60);
    carrier.display.setTextColor(ST77XX_WHITE);
    carrier.display.setTextSize(2);
    carrier.display.print(bodyName);
    carrier.display.setCursor(20, 90);
    carrier.display.print("Is a moon of: ");
    carrier.display.setCursor(20, 110);
    carrier.display.println(planet);
    delay(500);
  }

  if (carrier.Buttons.onTouchDown(TOUCH2)) {
    carrier.display.fillScreen(ST77XX_GREEN);
    carrier.display.setCursor(20, 60);
    carrier.display.setTextColor(ST77XX_WHITE);
    carrier.display.setTextSize(2);
    carrier.display.print(bodyName);
    carrier.display.setCursor(20, 90);
    carrier.display.print("Discovered by: ");
    carrier.display.setCursor(20, 110);
    carrier.display.println(explorerName);
    carrier.display.setCursor(20, 130);
    carrier.display.print("Date: ");
    carrier.display.print(explorerDate);
    delay(500);
  }

  if (carrier.Buttons.onTouchDown(TOUCH3)) {
    carrier.display.fillScreen(ST77XX_BLUE);
    carrier.display.setCursor(20, 60);
    carrier.display.setTextColor(ST77XX_WHITE);
    carrier.display.setTextSize(2);
    carrier.display.print(bodyName);
    carrier.display.setCursor(20, 90);
    carrier.display.print("Gravity is: ");
    carrier.display.setCursor(20, 130);
    carrier.display.print(gravity);
    carrier.display.print(" m/s2");
    delay(500);
  }

  if (carrier.Buttons.onTouchDown(TOUCH4)) {
    carrier.display.fillScreen(ST77XX_BLACK);
    carrier.display.setCursor(20, 60);
    carrier.display.setTextColor(ST77XX_WHITE);
    carrier.display.setTextSize(2);
    carrier.display.print(bodyName);
    carrier.display.setCursor(20, 110);
    carrier.display.print("Density is: ");
    carrier.display.print(density);
    carrier.display.print(" g/cm3");
  }

}//end loop

/////////////loop function end////////////////


////other function that the main code use/////
void onMessageChange() {
  notification();
  message_queue();
  currentScreen();
}

//switch the message in a fifo logic on 5 position to have it saved (anyway you can't read it from the carrier, Ii left for future upgrade)
void message_queue() {
  display_message_5 = display_message_4;
  display_message_4 = display_message_3;
  display_message_3 = display_message_2;
  display_message_2 = display_message_1;
  display_message_1 = message;
}

// notification function, it makes some funny thing to make sure my girlfriend read the message
void notification() {
  for (int i = 0; i <= 2; i++) {
    carrier.leds.fill(colorBlue, 0, 5);
    carrier.leds.show();
    carrier.Buzzer.sound(200);
    delay(500);

    carrier.leds.fill(colorGreen, 0, 5);
    carrier.leds.show();
    carrier.Buzzer.noSound();
    delay(500);
  }

  carrier.leds.fill(noColor, 0, 5);
  carrier.leds.show();
}

// it change the sreen color of the screen, now the color is in function of the temperature 
//but could be implemented changing the value of the displayScreen variable (for example from the cloud)
void currentScreen() {
  if (displayScreen == 0) {
    carrier.display.fillScreen(ST77XX_BLUE); //red background
    carrier.display.setTextColor(ST77XX_WHITE); //white text
    carrier.display.setTextSize(3); //medium sized text
    carrier.display.setCursor(100, 30);
    carrier.display.print(1);
    carrier.display.setTextSize(2); //medium sized text
    carrier.display.setCursor(10, 90);
    carrier.display.print(display_message_1);
  }

  else if (displayScreen == 1) {
    carrier.display.fillScreen(ST77XX_RED); //red background
    carrier.display.setTextColor(ST77XX_WHITE); //white text
    carrier.display.setTextSize(3); //medium sized text
    carrier.display.setCursor(100, 30);
    carrier.display.print(2);
    carrier.display.setTextSize(2); //medium sized text
    carrier.display.setCursor(10, 90);
    carrier.display.print(display_message_2);
  }

  else if (displayScreen == 2) {
    carrier.display.fillScreen(ST77XX_BLACK); //red background
    carrier.display.setTextColor(ST77XX_WHITE); //white text
    carrier.display.setTextSize(3); //medium sized text
    carrier.display.setCursor(100, 30);
    carrier.display.print(3);
    carrier.display.setTextSize(2); //medium sized text
    carrier.display.setCursor(10, 90);
    carrier.display.print(display_message_3);
  }

  else if (displayScreen == 3) {
    carrier.display.fillScreen(ST77XX_GREEN); //red background
    carrier.display.setTextColor(ST77XX_WHITE); //white text
    carrier.display.setTextSize(3); //medium sized text
    carrier.display.setCursor(100, 30);
    carrier.display.print(4);
    carrier.display.setTextSize(2); //medium sized text
    carrier.display.setCursor(10, 90);
    carrier.display.print(display_message_4);
  }

  else if (displayScreen == 4) {
    carrier.display.fillScreen(ST77XX_ORANGE); //red background
    carrier.display.setTextColor(ST77XX_WHITE); //white text
    carrier.display.setTextSize(3); //medium sized text
    carrier.display.setCursor(100, 30);
    carrier.display.print(5);
    carrier.display.setTextSize(2); //medium sized text
    carrier.display.setCursor(10, 90);
    carrier.display.print(display_message_5);
  }

  carrier.display.setTextSize(4);
  carrier.display.setCursor(60, 30); //sets new position for printing (x and y)
  carrier.display.print("<");
  carrier.display.setCursor(140, 30); //sets new position for printing (x and y)
  carrier.display.print(">");
}


// it update planet information, it use an external website to update
void planetUpdate() {

  int randomPlanet = random(0, 20);

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.print("GET ");
    client.print("/rest/bodies/");
    client.print(planets[randomPlanet]);
    client.println(" HTTP/1.0");

    client.println("Host: t");
    client.println("Connection: close");
    client.println();
  } else {
    Serial.println("unable to connect");
  }
  delay(1000);

  String line = "";
  while (client.connected()) {
    line = client.readStringUntil('\n');
    Serial.println(line);
    JSONVar myObject = JSON.parse(line);

    bodyName = JSON.stringify(myObject["englishName"]);
    planet = JSON.stringify(myObject["aroundPlanet"]["planet"]);
    explorerName = JSON.stringify(myObject["discoveredBy"]);
    explorerDate = JSON.stringify(myObject["discoveryDate"]);
    gravity = myObject["gravity"];
    density = myObject["density"];

    delay(100);

    if (line.startsWith("{")) {
      break;
    }
  }
}

//function that is automatic generated from the cloud
void initProperties() {
  ArduinoCloud.addProperty(carrier_message, READWRITE, ON_CHANGE);
  ArduinoCloud.addProperty(message, READWRITE, ON_CHANGE, onMessageChange);
  ArduinoCloud.addProperty(humidity, READ, 1 * SECONDS, NULL);
  ArduinoCloud.addProperty(pressure, READ, 1 * SECONDS, NULL);
  ArduinoCloud.addProperty(temperature, READ, 1 * SECONDS, NULL);
}

Credits

pasquale123456
0 projects • 1 follower

Comments