Tag Archives: SHT15

Logging temperature data with Python / MySQL / Arduino over serial

While back, I did create an arduino setup, and has since then been meaning to do something more useful with it.

Well, it took a while. I fiddled around a bit, and now finally managed to combine it with looking into python.

I’ve pasted the pythoncode in below, you will need python-mysql and pyserial (pip install).


import io
import serial
import MySQLdb

ser = serial.Serial('COM3', 9600) 
db = MySQLdb.connect(host="IP", user="USER", passwd="MYSECRET",db="DB")
cursor = db.cursor()
while True:
    out = ser.readline()
    # example output ' Temperature: 21.94 -  Humidity: 21.58\n
    print (out)
    tempdata = out[14:19]
    huedata = out[33:38]
    #trim the output
    tempdata.strip()
    huedata.strip()
    query = ("INSERT INTO mytemperaturetable (`temp`,`hue`) VALUES('%s','%s')") %(tempdata,huedata)
    cursor.execute(query)
    db.commit()
db.close()
ser.close()

You can find my original post here.

For the Arduino you need to hook up it to something that gets the mesurements; I did it and needed to fetch SHT1x.h.
The unclean arduino code looks like this (it still beeps when the obvious heat comes on):


#include 

#define dataPin 8
#define clockPin 12

  int del=100; 
  int delo = 10000;
  int val = 0;
  const int speakerPin = 6;
  const char notes[] = "ccggaagffeeddc ";
  const int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
  const int length = sizeof (beats) / sizeof (beats [0]);
  const int tempo = 50;
  
void playTone(int tone, int duration) {
 for (long i = 0; i < duration * 1000L; i += tone * 2) {
   digitalWrite(speakerPin, HIGH);
   delayMicroseconds(tone);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone);
 }
}
void playNote(char note, int duration) {
  const char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  const int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
	playTone(tones[i], duration);
    }
   }
}
void playTune (const char* notes, const int* beats, int length, int tempo)
{
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
     delay(beats[i] * tempo); // rest
  } else {
     playNote(notes[i], beats[i] * tempo);
  }
  }
  delay(tempo / 2);
}

// The setup() method runs once, when the sketch starts
void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);  
}

void loop()
{
  SHT1x sht1x(dataPin, clockPin);
  float tempC = sht1x.readTemperatureC();
  float tempF = sht1x.readTemperatureF();
  float humidity = sht1x.readHumidity(); 
  float maxtemp = 30.10; 
  
 
    if (tempC > maxtemp){
      delo = 1000;
      Serial.print("WARNING - HIGH TEMP");
      digitalWrite(2, HIGH);   // turn on LED on pin 2
      delay(del);              // wait (length determined by value of 'del')
      digitalWrite(2, LOW);    // turn it off

      digitalWrite(3, HIGH);   // turn on LED on pin 3
      delay(del);              // wait
      digitalWrite(3, LOW);    // turn it off
      
      digitalWrite(4, HIGH);   // turn on LED on pin 4
      delay(del);              // wait
      digitalWrite(4, LOW);    // turn it off
      
      digitalWrite(5, HIGH);   // turn on LED on pin 5
      delay(del);              // wait
      digitalWrite(5, LOW);    // turn it off
      
      digitalWrite(4, HIGH);   // turn on LED on pin 4
      delay(del);              // wait
      digitalWrite(4, LOW);    // turn it off

      digitalWrite(3, HIGH);   // turn on LED on pin 3
      delay(del);              // wait
      digitalWrite(3, LOW);    // turn it off

      playTune (notes, beats, length, tempo);
      
    }
    else
    {
     delo = 10000;
     digitalWrite(2, HIGH);   // turn on LED on pin 2
     delay(del);              // wait (length determined by value of 'del')
     digitalWrite(2, LOW);    // turn it off  
    }
 
  Serial.print(" delo = ");
  Serial.print(delo);
  Serial.print(" ");
  Serial.print(" Temp C - F: ");
  Serial.print(tempC);
  Serial.print(" - ");
  Serial.print(tempF);
  Serial.print(" Humidity :");
  Serial.print(humidity);
  Serial.print("\n");
  delay(delo); 
}

Arduino Uno with SHT15

So yeah. I am “done” with the thing. Need to fix it into a permanent state… But here is a demo of it running and getting into “to hot state” in a youtube clip, and another.

You’ll need the SHT1x library if you don’t wanna create it yourself.

Here’s the code I am running:

#include <"SHT1x.h"> 

#define dataPin 8
#define clockPin 12

  int del=100; 
  int delo = 10000;
  int val = 0;
  const int speakerPin = 6;
  const char notes[] = "ccggaagffeeddc ";
  const int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
  const int length = sizeof (beats) / sizeof (beats [0]);
  const int tempo = 50;
  
void playTone(int tone, int duration) {
 for (long i = 0; i < duration * 1000L; i += tone * 2) {
   digitalWrite(speakerPin, HIGH);
   delayMicroseconds(tone);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone);
 }
}
void playNote(char note, int duration) {
  const char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  const int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
	playTone(tones[i], duration);
    }
   }
}
void playTune (const char* notes, const int* beats, int length, int tempo)
{
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
     delay(beats[i] * tempo); // rest
  } else {
     playNote(notes[i], beats[i] * tempo);
  }
  }
  delay(tempo / 2);
}

// The setup() method runs once, when the sketch starts
void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);  
}

void loop()
{
  SHT1x sht1x(dataPin, clockPin);
  float tempC = sht1x.readTemperatureC();
  float tempF = sht1x.readTemperatureF();
  float humidity = sht1x.readHumidity(); 
  float maxtemp = 25.60; 
  
 
    if (tempC > maxtemp){
      delo = 1000;
      Serial.print("WARNING - HIGH TEMP");
      digitalWrite(2, HIGH);   // turn on LED on pin 2
      delay(del);              // wait (length determined by value of 'del')
      digitalWrite(2, LOW);    // turn it off

      digitalWrite(3, HIGH);   // turn on LED on pin 3
      delay(del);              // wait
      digitalWrite(3, LOW);    // turn it off
      
      digitalWrite(4, HIGH);   // turn on LED on pin 4
      delay(del);              // wait
      digitalWrite(4, LOW);    // turn it off
      
      digitalWrite(5, HIGH);   // turn on LED on pin 5
      delay(del);              // wait
      digitalWrite(5, LOW);    // turn it off
      
      digitalWrite(4, HIGH);   // turn on LED on pin 4
      delay(del);              // wait
      digitalWrite(4, LOW);    // turn it off

      digitalWrite(3, HIGH);   // turn on LED on pin 3
      delay(del);              // wait
      digitalWrite(3, LOW);    // turn it off

      playTune (notes, beats, length, tempo);
      
    }
    else
    {
     delo = 10000;
     digitalWrite(2, HIGH);   // turn on LED on pin 2
     delay(del);              // wait (length determined by value of 'del')
     digitalWrite(2, LOW);    // turn it off  
    }
 
  Serial.print(" delo = ");
  Serial.print(delo);
  Serial.print(" ");
  Serial.print(" Temp C - F: ");
  Serial.print(tempC);
  Serial.print(" - ");
  Serial.print(tempF);
  Serial.print(" Humidity :");
  Serial.print(humidity);
  Serial.print("\n");
  delay(delo); 
}

The schematics is to come. but, you’ll prolly figure it out – hint would be to look at the code.

And to top it of, here’s some stats that apply to Adruino Uno R3 and the Nano;

Speed test
----------
F_CPU = 16000000 Hz
1/F_CPU = 0.0625 us
The next tests are runtime compensated for overhead
  nop                       : 0.063 us
  avr gcc I/O               : 0.125 us
  Arduino digitalRead       : 3.650 us
  Arduino digitalWrite      : 3.903 us
  pinMode                   : 4.623 us
  multiply volatile byte    : 0.632 us
  divide volatile byte      : 12.893 us
  multiply volatile integer : 1.449 us
  divide volatile integer   : 15.218 us
  multiply volatile long    : 4.530 us
  multiply single float     : 7.110 us
  multiply double float     : 7.110 us
  divide double float       : 79.978 us
  itoa()                    : 16.488 us
  ltoa()                    : 239.578 us
  dtostrf()                 : 79.278 us
  random()                  : 94.253 us
  y |= (1<