Alessandro Felicetti
Published © MIT

Smart Cable Car

Provide cableways a reliable feedback. Patent pending.

IntermediateFull instructions provided2 hours480

Things used in this project

Hardware components

Spresense boards (main & extension)
Sony Spresense boards (main & extension)
×1
Generic ESP32 IC
Espressif Generic ESP32 IC
×1
DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
MQ135 Air quality sensor
×1

Software apps and online services

ThingSpeak API
ThingSpeak API
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics

Code

Spresense Code

Arduino
#include <GNSS.h>          // GNSS library
#include <dht11.h>         // DHT11 temperature sensor library
#include <EasyTransfer.h>  // Library for serial transfer of multiple datas


#define DHT11PIN 2 // Pin for DHT11 sensor
#define AIRPIN A3
#define MOVPIN A5 // Pin for IR sensor. Using analog read because pull-up breaks compatibility
dht11 DHT11; // Dht object
static SpGnss Gnss; // SpGnss object
EasyTransfer ETout;


// Serial output values
struct SEND_DATA_STRUCTURE{
  int16_t temp;
  int16_t movement;
  float longit;
  float latit;
  double altit;
  int16_t air;
};
SEND_DATA_STRUCTURE mydataout;
struct RECEIVE_DATA_STRUCTURE{
  int audio_track;
};
RECEIVE_DATA_STRUCTURE mydatain;


void setupGNSS(){ // Function to setup GNSS module
  int result;
  result = Gnss.begin(); // Function to setup GNSS module
  if (result == 0){
    result = Gnss.start(COLD_START);
    if (result == 0) ledOff(PIN_LED0);
  }
}


void setup() {  // Setup routine. Called when Spresense is powered up
  delay(10000); // Leave some time to the ESP board
  Serial2.begin(57600);  // Setup serial communication with ESP
  ETout.begin(details(mydataout), &Serial2);
  Serial.begin(115200); // Setup serial communication for debug
  ledOn(PIN_LED0);
  setupGNSS();
  Serial.println("Setup completed");
}


void loop() {  // Loop routine
  ledOn(PIN_LED3); // Debug loop status
  DHT11.read(DHT11PIN);  // Read temperature
  mydataout.temp = DHT11.temperature;
  mydataout.air = analogRead(AIRPIN); // Read air quality
  // Check movements
  if(analogRead(MOVPIN) > 100){
    mydataout.movement = 1;
  }else{
    mydataout.movement = 0;
  }
  // Read GPS data
  if(Gnss.waitUpdate(-1)){
    SpNavData datagps;
    Gnss.getNavData(&datagps);
      mydataout.longit = datagps.longitude;
      mydataout.latit = datagps.latitude;
      mydataout.altit = datagps.altitude;
  }
  // Debug GPS presence
  if(mydataout.altit>1){ 
    ledOn(PIN_LED2);
  }else{
    ledOff(PIN_LED2);
  }
  // Send sensors data to ESP
  ETout.sendData();
  Serial.println("Data sent");
  ledOff(PIN_LED3);
  delay(10000);
}

ESP32 Code

Arduino
#include <WiFi.h>          // ESP WiFi libraries
#include <WiFiMulti.h> 
#include <EasyTransfer.h>  // Library for serial transfer of multiple datas
#define RXD2 16
#define TXD2 17


const char* ssid      = "network"; // Your SSID (Name of your WiFi)
const char* password  = "*******"; //Your Wifi password
const char* host      = "api.thingspeak.com";
String      api_key   = "O5**********5XS"; // Your API Key provied by thingspeak


EasyTransfer ETin;
// Serial input values
struct RECEIVE_DATA_STRUCTURE{
  int16_t temp;
  int16_t movement;
  float longit;
  float latit;
  double altit;
  int16_t air;
};
RECEIVE_DATA_STRUCTURE mydatain;


void setup() {  // Setup routine. Called when ESP is powered up
  Serial2.begin(57600, SERIAL_8N1, RXD2, TXD2);  // Setup serial communication with Spresense
  ETin.begin(details(mydatain), &Serial2);
  WiFi.mode(WIFI_STA);    
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}


void loop() {  // Loop routine
  //check and see if a data packet has come in  
  if(ETin.receiveData()){
    // call function to send data to Thingspeak
    SendTS();
  }
  delay(500);
}


void SendTS(){
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    return;
  }
  else
  {
    String data_to_send = api_key;
    data_to_send += "&field1=";
    data_to_send += String(mydatain.temp);
    data_to_send += "&field2=";
    data_to_send += String(mydatain.movement);
    data_to_send += "&field3=";
    data_to_send += String(mydatain.latit);
    data_to_send += "&field4=";
    data_to_send += String(mydatain.longit);
    data_to_send += "&field5=";
    data_to_send += String(mydatain.altit);
    data_to_send += "&field6=";
    data_to_send += String(mydatain.air);
    data_to_send += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + api_key + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(data_to_send.length());
    client.print("\n\n");
    client.print(data_to_send);
  }
  delay(2000);
  client.stop();
}

Credits

Alessandro Felicetti

Alessandro Felicetti

3 projects • 4 followers
I am an electrical systems designer. I started reading about electronics about 10 years ago, now my room seems a poor man's power plant.

Comments