How Hot Does it Get Inside a Car During a Heat Wave?
Tags: programming, go, iot, showdev
Published: 2021-08-05 15:42:21

---
title: How Hot Does it Get Inside a Car During a Heat Wave?
published: true
description: "Using a Raspberry Pi to measure the temperature inside a Ford Focus in the sun during a record breaking heatwave. Full how to and source code."
tags: programming, golang, iot, showdev
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td3r1e41hvr3u3f94v0l.jpg
canonical_url: https://www.jeremymorgan.com/tutorials/go/how-to-iot-data-go-sqlite/
---

An [unprecedented heat wave](https://wpde.com/news/nation-world/sunday-will-be-hottest-day-ever-for-portland-other-pacific-nw-cities) rocked the Pacific Northwest in June. Oregon turned into an oven. This gave me yet another reason to break out a Raspberry Pi and collect some data. I sent it to [Adafruit](https://io.adafruit.com/) because it remains one of the greatest places to send and look at IoT data.


![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqiw9p4n49zibz1esu92.png)

During the first day I placed a Raspberry Pi under my deck to collect and send temperature and humidity data.

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2htbt0afh84al6yl4ias.jpg)

I wanted to get ambient air temperature as accurately as possible. I placed the Raspberry Pi (Zero) under the deck so it wouldn't be in direct sunlight. It's in shade 24/7. The breeze is blocked by joists.

Then my son had an incredible idea:

```
Let's put a sensor in a car to see how hot it gets inside the cabin.
```

Great! So I set up a 2nd Raspberry Pi for that purpose. I put it in the center console of our Ford Focus. I didn't want that one in direct sunlight either. The car itself was in direct sunlight the entire time. I parked it in an area where it didn't receive shade.

I cracked open each window about 2 inches:

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzaeiisp349ka5jm5cni.jpg)

Then measured the results over a few days.

In this article, I'll show you how we built this setup, and the data I found.

{% twitch ObedientGloriousMuleImGlitch-JIfQ9mveWhA-vWOn %}

## Step 1: Setting up the Raspberry Pis

To gather and send this data, I used two Raspberry Pis set up identically.

#### Under the Deck (Outside Temperature)
- [Raspberry Pi Zero W](https://amzn.to/3AQSOg9)
- [AM2302 Temperature and Humidity Sensor](https://amzn.to/39zKrdV)
- Raspberry Pi OS
- Python Script using [Adafruit DHT Circuit Library](https://learn.adafruit.com/dht/dht-circuitpython-code)

#### Inside the car

- [Raspberry Pi 3](https://amzn.to/3keFfRM)
- [AM2302 Temperature and Humidity Sensor](https://amzn.to/39zKrdV)
- Raspberry Pi OS
- Python Script using [Adafruit DHT Circuit Library](https://learn.adafruit.com/dht/dht-circuitpython-code)

I wired up the sensors like so:

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqx2kknb7ar9tagk6h8i.jpg)

They were both set up [with these instructions](/tutorials/raspberry-pi/how-to-iot-adafruit-raspberrypi/) if you'd like to try it yourself.

I even [did a live stream](https://www.twitch.tv/videos/1069561843) when I set up the pi in the car if you want to see it in action.

## Step 2: Gathering the Data

I sent the data to [Adafruit](https://io.adafruit.com/) and let it collect data for a few days. Adafruit gave me some nice realtime graphs to look at:

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k4b9nzzrrejaj35igwdy.jpg)

This was great for realtime data. Setting this is up is very easy, again I cover [how to send data to Adafruit](/tutorials/raspberry-pi/how-to-iot-adafruit-raspberrypi/) and set up a dashboard.

While the dashboards are neat, I wanted to take the entire data set and look at it. The dashboards only show a 24 hour window, but Adafruit collects the data and you can export it to JSON or CSV.

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qit41oji9o7ak4dh3dw3.jpg)

So I exported the data for the few days into CSV files. I could browse through them and spot check things but I wanted to merge all the data. I decided to drop them into a Sqlite database to do that.

## Step 3: Processing the Data

I had a couple of issues right off the bat when dropping these into a database.

- I have four separate feeds of information:
  - Outside temperature
  - Outside humidity
  - Car temperature
  - Car Humidity

I had to consolidate them into a single database. Which created another issue:

- The samples were all taken at slightly different times.

The sensors took samples every thirty seconds. The two Raspberry Pis were not synchronized, and there was some clock drift.

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sz1ybwxefu2e6erexl2y.jpg)
![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5hlr0arxrzzo6rkc71o.jpg)

Since the timestamps showed that sensors never took temperatures at the exact same time, I would have to sacrifice some precision.

I tackled this problem [live on my Twitch stream](https://www.twitch.tv/videos/1082027932). I decided to round off the seconds and use that timestamp as an index for the values.

```go
timestamp := records[i][3][:len(records[i][3])-4]
seconds, err := strconv.Atoi(string(timestamp[len(timestamp)-2:]))

if err != nil {
  log.Printf("Error: %v", err)
}

if seconds >= 30 {
  timestamp = timestamp[:len(timestamp)-2] + "30"
} else {
  timestamp = timestamp[:len(timestamp)-2] + "00"
}

ourValue, err := strconv.ParseFloat(records[i][1], 64)

```

I wrote a Go program live on stream that did the following:

- Read in CSV files (one per data stream)
- Stored them in Sqlite

First, I took [the CSV with the fewest records](https://github.com/JeremyMorgan/HotCar/blob/main/csvs/CarTemperature.csv) in it. That way I knew we would have the most overlapping values. Some data streams were longer, but they wouldn't have values in the smaller streams. So this was the starting point.

I read in that file, and created a table with the timestamp and the value included. The timestamp would be our index.

```go
func FirstInsert(db *sql.DB, timestamp string, value float64) (bool, error) {

	insertReadingSql := `INSERT INTO Reading (TimeStamp, CarTemperature) VALUES (?,?)`

	statement, err := db.Prepare(insertReadingSql)

	if err != nil {
		log.Println("Failed to prepare SQL Statement")
		return false, err
	}

	_, err = statement.Exec(timestamp, value)

	if err != nil {
		log.Println("Failed to insert data")
		log.Printf("Timestamp: %v \n Value: %v \n", timestamp, value)
		return false, err
	}

	return true, nil
}
```

Then for the subsequent feeds, I read in each of those .CSVs and inserted values to the corresponding timestamps:

```go
func InsertData(db *sql.DB, columnname string, timestamp string, value float64) (bool, error) {

	updateReadingSql := `UPDATE Reading SET ` + columnname + ` =? WHERE TimeStamp = ?`

	statement, err := db.Prepare(updateReadingSql)

	if err != nil {
		log.Println("Failed to prepare SQL Statement")
		return false, err
	}
	_, err = statement.Exec(value, timestamp)

	if err != nil {
		log.Println("Failed to insert data")
		return false, err
	}

	return true, nil
}
```

And in the end I had a nice Sqlite database with the values I needed.

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eolsajr966mb1asepw32.jpg)

You can download [all this data and the application to process it here](https://github.com/JeremyMorgan/HotCar).

## So How Hot Did it Get??

Now I can dive into the data and try to get a good handle on how hot it got outside and how hot it was in the car.

### Maximum temperature outside:

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/loy3su2recprwymwo2r4.jpg)

### Maximum temperature in the car:

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/slwsjs41tp1e7oxixluo.jpg)

Wow. So that was a bit unexpected. And of course, I'd like to match things up a little to see the inverse of each.

The temperature reached its hottest point at _2021-06-29 00:34:30_ so, June 29th at 5:34PM.

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st07yvadya3lu0qtrk82.jpg)

The outside temperature peaked at **119.84** and the temperature inside the car was **159.08**. Incredible.

So what about the peak car temperature?

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4p4pkp8gqi1yru1m61ya.jpg)

Unfortunately, the outside temperature sensor failed to read during this time but it was about an hour later, and reached **170.78** degrees. This is with windows cracked open. While this was during 100+ temperatures this is something to consider when you see a child or pet in a car on a hot day.

There are some other things we can look at with this data. Let's see the relationship between the two temperatures.

Here's a nice graph of the car temperature vs outside temperature:

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwa58q0fvrt9nlkspmgw.jpg)

It's interesting how late at night when things "cooled down" to around 80 degrees, the two temperatures almost equalize. Yet if we zoom in deeper we can visually see how the heat builds in the car:

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oi4d2sphi1s4f1r8j008.jpg)

At a certain point, it even appears the car was _cooler_ than the outside temperature.

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75mfd41dxoroday13tdv.jpg)

At midnight the car was a mere **two degrees hotter** than the outside temperature.

At 5am it was **four degrees cooler** in the car.

A couple of hours later it was **eight degrees hotter** than outside.

Near the peak of the day it was **50 degrees hotter** in the car. Incredible.

You can see the peaks and valleys in the graphs.

I'll be doing even more with this data in the future. Very curious about how it ramped up and I want to look into it more.


## Conclusion

So I've always been against leaving children and pets in your car on a hot day. Most of us are. But it's interesting to see exactly how hot it gets inside a car. This is absolute justification for calling the police when you see it. _end rant_.

This data is unique in that we were measuring during 110+ degree days, but you can still see the effects of the car. The only time the car _wasn't_ hotter than the outside is when the sun wasn't on it. So we can deduce the sun shining through windows (they are not tinted on this car) affects the inside temperature. If it were an 80 degree sunny day I imagine the inside still capable of being 120 degrees or more.

![Processing Iot Data Sqlite and Golang](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6u1j98l1sgueuagkpu79.jpg)

> Lesson: We all know cars sitting in the sun get hotter, now we can see exactly how much hotter.

I wanted to share this data, how I put it together and what the results were. I hope you've enjoyed it.

You can download [this data](https://github.com/JeremyMorgan/HotCar) and process it yourself. The timestamps are UTC and I am in the PST region so keep that in mind. I'm curious to see what others might do with this data.

And be sure to [check out some of the live streams](http://bit.ly/JeremyMorganTwitch) I've done working on this data.

Questions? Comments? [Yell at me!](http://bit.ly/JeremyCMorgan)