Project tutorial
Seat Monitor

Seat Monitor © GPL3+

Using ARTIK cloud to monitor cabin seat state.

  • 3,057 views
  • 3 comments
  • 7 respects

Components and supplies

Apps and online services

About this project

Here another project with ARTIK Cloud.

Seat monitor that can be used to monitor if there have person on the vehicles seat (train, bus, boat but not plane).

  • Advantages : Can direct update old vehicles make people more easy to find seat.
  • Disadvantages: Can't use on plane because RF.

Hardware

I have

  • MKR1000 as vehicle controller and on board led to show if there have empty seat.
  • A Piezo sensor to monitor seat state use Ameba as Wifi buffer send data to cloud.

Device and Rules

Video

I am lazy to add comment to video so I write them below.

At first seat is empty so the controller show green light.

If someone or something push the sensor it will send data to cloud. When seat is full controller LED will auto off. Here I just use one component to show this project.

I use ARTIK cloud to judge seat is empty or not by piezo sensor value.

Code

controllerArduino
controller MKR1000
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <WiFi101.h>

const char* _SSID     = "_SSID"; //Wi-Fi SSID 
const char* _PASSWORD = "_PASSWORD"; // Wi-Fi Password 

// MQTT - Artik Cloud Server params
char Artik_Cloud_Server[]     = "api.artik.cloud"; // Server 
int  Artik_Cloud_Port         = 8883; // MQTT Port
char Client_Name[] = "train"; // Any Name 
char Device_ID[]   = "Device_ID"; // DEVICE ID
char Device_TOKEN[]   = "Device_TOKEN"; //  DEVICE TOKEN
char MQTT_Publish[]      = "/v1.1/messages/Device_ID"; // (/v1.1/messages/"DEVICE ID")
char MQTT_Subscription[] = "/v1.1/actions/Device_ID"; // (/v1.1/actions/"DEVICE ID")

char buf[200]; // Json Data to Artik Cloud 
char receivebuf[200]; // Json Data receive from Artik Cloud 
WiFiSSLClient client; //must use SSL client when MQTT protocol
MQTTClient MQTT_Artik_Client; // MQTT Protocol

//led SET
const int BROADLEDPin = 6; //use on broad led to show seat state, LED on have seat/ off seat is full

//data ram
int haveseat; //1:have seat / 0: no seat

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(topic);
  Serial.print(" - ");
  Serial.print(payload);
  Serial.println();
  String t = topic;
  if(t == MQTT_Subscription) // check DEVICE ID
  {
    parseBuffer(payload);
  }
}

void setup() {
  Serial.begin(57600);  
  // Wifi Setting
  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  MQTT_Artik_Client.begin(Artik_Cloud_Server, Artik_Cloud_Port, client); // Connect to Artik Server
  while (!MQTT_Artik_Client.connect(Client_Name, Device_ID, Device_TOKEN)) { // Connect to Artik IOT Device 
    Serial.print("*");
    delay(1000);
  }
  MQTT_Artik_Client.subscribe(MQTT_Subscription);//set subscribe
  //power on setting
  pinMode(BROADLEDPin, OUTPUT);
  digitalWrite(BROADLEDPin, HIGH);   
  haveseat=1; //default seat is empty
  loadBuffer();
  MQTT_Artik_Client.publish(MQTT_Publish, buf);// Publishing data to the Artik Cloud 
  Serial.println("Publishing..");
}

void loadBuffer(void) {//load function, make data to json form
  StaticJsonBuffer<200> jsonBuffer;
  /*
   * smaple field
   * {"have_seat":1}
   */
   //creat field data make it short or ARTIK cloud not work
  JsonObject& dataPair = jsonBuffer.createObject();
  dataPair["have_seat"]=haveseat;
  dataPair.printTo(buf, sizeof(buf));
  Serial.println(buf);
  
}
void parseBuffer(String _payload) { //parse function
  StaticJsonBuffer<200> jsonBuffer;
  String json = _payload;
  
  json.toCharArray(receivebuf,200); //chang string to char or json can't work

  //*****************************
  JsonObject& root = jsonBuffer.parseObject(receivebuf);
  if (!root.success()) {
    Serial.println("parseObject() failed");
  }
  //****************************
  /*
   *{"actions":[{"name":"seaton","parameters":{"s1":1}}]}
   */
  int seatstate = root["actions"][0]["parameters"]["s1"]; //parse data from receive buffer, as sample show above seatstate will be 1 or 0 after string {"s1": 
  if(seatstate == 1) //seat full, LED off
  {
    digitalWrite(BROADLEDPin, LOW); //LED off
    haveseat=0;//clear haveseat flag and load it to buffer then publishing data to the Artik Cloud
    loadBuffer();
    MQTT_Artik_Client.publish(MQTT_Publish, buf);
    Serial.println("Publishing..");
  }
  if(seatstate ==0 ) // have seat, LED on
  {
     digitalWrite(BROADLEDPin, HIGH);   //LED on
    haveseat=1; //set haveseat flag and load it to buffer then publishing data to the Artik Cloud 
    loadBuffer();
    MQTT_Artik_Client.publish(MQTT_Publish, buf);
    Serial.println("Publishing..");
  }

}
void loop() {
  for (int i =0; i<10;i++){ // delay 1 Mnts
    MQTT_Artik_Client.loop(); //client loop
    Serial.println(buf); //print buffer to check , can be remove to add performance
    delay(6000);
  }  
}
seat sensorArduino
Ameba RTL8195 Board
/*
 Thanks Ameba Basic MQTT example
 I mark some point I stuck
*/

#include <WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.

char ssid[] = "ssid";     // your network SSID (name)
char pass[] = "pass";  // your network password
int status  = WL_IDLE_STATUS;    // the Wifi radio's status
//******************************************************************************
//must double check when you copy the code
//******************************************************************************
// MQTT - Artik Cloud Server params
char mqttServer[]     = "api.artik.cloud";
int  mqttPort         = 8883;
char clientId[]       = "seat";
char Device_ID[]   = "Device_ID"; // DEVICE ID
char Device_TOKEN[]   = "Device_TOKEN"; //  DEVICE TOKEN
char publishTopic[]   = "/v1.1/messages/Device_ID";
char subscribeTopic[] = "/v1.1/actions/Device_ID";
char publishPayload[128];

int sensorValue;
int sensorValuetemp;
//******************************************************************************
//must use SSL when MQTT protocol
//******************************************************************************
WiFiSSLClient wifiClient;
PubSubClient client(mqttServer, mqttPort,wifiClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(clientId, Device_ID, Device_TOKEN)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(publishTopic, publishPayload);
      // ... and resubscribe
      client.subscribe(subscribeTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void publish(void) //publish seat sensor value to cloud
{
  /*
   * smaple field
   * {"seatvalue":1}
   */
   //creat field data make it short or ARTIK cloud not work
  memset(publishPayload, 0, sizeof(publishPayload));
  sprintf(publishPayload, "{\"seatvalue\":%d}",sensorValue);
  printf("\t%s\r\n", publishPayload);
  client.publish(publishTopic, publishPayload);
}
void setup()
{
  Serial.begin(38400);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 5 seconds for connection:
    delay(5000);
  }
  reconnect();
  Serial.println("initial sensorValue");
  Serial.println(sensorValue);
  // Allow the hardware to sort itself out
  delay(1500);
}

void loop()
{
  
  if (!client.connected()) { //check connect state and reconnect
    reconnect();
  }
  for (int i =0; i<10;i++)
  { // delay 1 Mnts
   client.loop();
   sensorValue = analogRead(A0); //read sensor value
   Serial.println(sensorValue);
   if(sensorValue!=sensorValuetemp) //publish when sensorValue change
   {
      publish();
      sensorValuetemp = sensorValue; //save current sensor value
   }
    Serial.println(publishPayload);
    delay(6000);
  }  
}

Schematics

seat monitor
seat monitor
Sch

Comments

Similar projects you might like

Monitor Fire and Temperature Using ARTIK Cloud

Project tutorial by Samsung IoT

  • 2,585 views
  • 0 comments
  • 21 respects

Monitor and control your irrigation system with a mobile app

Project tutorial by Officine Innesto

  • 3,765 views
  • 0 comments
  • 9 respects

Event Monitor for Independent Senior Citizens

Project tutorial by Team Rethink IoT

  • 3,655 views
  • 4 comments
  • 26 respects

Portable Environment Monitor

Project tutorial by Chathuranga Liyanage

  • 2,350 views
  • 3 comments
  • 6 respects

IoT System To Monitor Soil Moisture With Arduino

Project tutorial by Francesco Azzola

  • 2,905 views
  • 0 comments
  • 6 respects

Monitor fridge with Arduino MKR1000 and thethings.iO

Project tutorial by thethings.iO

  • 2,121 views
  • 1 comment
  • 5 respects
Add projectSign up / Login