LoginSignup
3
4

More than 5 years have passed since last update.

arduinoで温度コントロール

Last updated at Posted at 2016-02-17

概要

arduino megaでPID使って温度コントロールしてみた。
センサーはサーミスタ、熱源はドライヤー、対象はスロットルボディ。
c#でsangoにmqtt pubしてjsdoでwebsocketでsubしてグラフ化しました。

写真

MVC-018S.JPG

グラフ

paho6.JPG

回路図

ondo.JPG

サンプルコード

#include <PID_v1.h>

unsigned long time;
int sampleTime = 1000;
double Setpoint = 0;
double Input = 0;
double Output = 0;
PID body(&Input, &Output, &Setpoint, 20.0, 5.0, 3.0, DIRECT);
float r1 = 15000.0;
int i = 0;

float temp1(float rr0)
{
    float t;
    t = log(rr0);
    t = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * t * t)) * t);
    return t - 273.15;
}
void setup()
{
    Serial.begin(115200);
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);
    Setpoint = 25.0;
    body.SetOutputLimits(-255.0, 255.0);
    body.SetSampleTime(sampleTime);
    body.SetMode(AUTOMATIC);
}
void loop()
{
    int d;
    float r;
    if (millis() - time >= sampleTime)
    {
        time = millis();
        d += analogRead(A0);
        r = r1 * d / (1024.0 - d);
        Input = temp1(r);
        i++;
        if (i > 19)
        {
            i = 0;
            Serial.println(Input);
        }
        if (body.Compute())
        {
            //Serial.println(Output);
        }
        else
        {
            Serial.println("ng!!");
        }
        if (Output > 0)
        {
            digitalWrite(10, LOW);
        }
        else
        {
            digitalWrite(10, HIGH);
        }
    }
}

サンプルコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using uPLibrary.Networking.M2Mqtt;

namespace ConsoleApplication6
{
    class Program
    {
        private static MqttClient client;
        static void Main(string[] args)
        {            
            client = new MqttClient("lite.mqtt.shiguredo.jp", 1883, false, null);
            var ret = client.Connect("paho", "ohisama@github", "");
            Console.WriteLine("Connected with result code {0}", ret);
            SerialPort port = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            try
            {
                port.Open();
                port.DtrEnable = true;
                port.RtsEnable = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }
            Console.WriteLine("port open>");
            string comand = null;
            comand = Console.ReadLine();
        }
        private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort port = (SerialPort)sender;
            string data = port.ReadExisting();
            Console.Write(data);
            pub(data);
        }
        private static void pub(string value)
        {
            Console.WriteLine("pub");
            //Console.WriteLine(value);
            Byte[] dat = System.Text.Encoding.GetEncoding("SHIFT-JIS").GetBytes(value);
            client.Publish("ohisama@github/test0", dat, 0, false);
        }
    }
}

サンプルコード

var myChart;
window.onload = function() {
    var ctx = document.getElementById("chart").getContext("2d");
    var data = {
        labels : ["", "", "", "", "", "", "", "", "", "", ""],
        datasets : [{
            label : "temp",
            fillColor : "rgba(151, 187, 205, 0.2)",
            strokeColor : "rgba(151, 187, 205, 1)",
            pointColor : "rgba(151, 187, 205, 1)",
            pointStrokeColor : "#fff",
            pointHighlightFill : "#fff",
            pointHighlightStroke : "rgba(151, 187, 205, 1)",
            data : [20, 20, 20, 20, 20, 20, 20, 20, 20]
        }]
    };
    myChart = new Chart(ctx).Line(data, {
        scaleOverride : true,
        scaleSteps : 10,
        scaleStepWidth : 3.0,
        scaleStartValue : 10,
        scaleShowGridLines : false
    });
}
var client;
function setup() {
    var userName = 'ohisama@github';
    var password = '';
    var websocketUrl = 'ws://lite.mqtt.shiguredo.jp:8080/mqtt';
    client = new Paho.MQTT.Client(websocketUrl, 'papo');
    client.connect({
         userName : userName,
         password : password,
         onSuccess : onConnect,
         onFailure : failConnect
    });
    client.onConnectionLost = onConnectionLost;
    client.onMessageArrived = onMessageArrived;
}
function failConnect(e) {
    alert('failed!');
}
function onConnect() {
    alert("connect");
    client.subscribe('ohisama@github/test0');
}
function onMessageArrived(message) {
    var val = message.payloadString;
    var s = parseFloat(val);
    myChart.addData([s], "");
    myChart.removeData();
}
function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0) 
    {
        alert("onConnectionLost:" + responseObject.errorMessage);
    }
}
setup();
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4