Skip to content

Instantly share code, notes, and snippets.

@akiraak
Created November 20, 2021 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiraak/e90e2b52d5af271cc1e0be53c3e0f7ed to your computer and use it in GitHub Desktop.
Save akiraak/e90e2b52d5af271cc1e0be53c3e0f7ed to your computer and use it in GitHub Desktop.
コーヒー豆焙煎機のESP32コード
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include "Ambient.h"
#include "max6675.h"
#define SECOND (1000)
#define MINUTE (SECOND * 60)
#define HOUR (MINUTE * 60)
#define DAY (HOUR * 24)
struct DATETIME {
int day;
int hour;
int minute;
int sec;
};
#define WIFI_SSID "******"
#define WIFI_PASSWORD "******"
#define AMB_CHANNEL_ID *****
#define AMB_WRITE_KEY "****************"
#define AMB_INTERVAL (1000 * 10)
#define AMB_FIELD_SPEED 1
#define AMB_FIELD_TEMPERATURE 2
WiFiClient wifiClient;
Ambient ambient;
#define ENABLE_PIN 17
#define FORWORD_PIN 12
#define BACK_PIN 14
#define FORWORD_CH 0
#define BACK_CH 1
#define MOTOR_VOL_PIN 33
#define FAN_VOL_PIN 34
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int thermoDO = 19;
int thermoCS = 23;
int thermoCLK = 5;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
void setup() {
Serial.begin(115200);
Serial.println();
setupDisplay();
setupWifi();
setupAmb();
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW);
ledcSetup(FORWORD_CH, 1000, 8);
ledcAttachPin(FORWORD_PIN, FORWORD_CH);
ledcSetup(BACK_CH, 1000, 8);
ledcAttachPin(BACK_PIN, BACK_CH);
Serial.println("Ready...");
}
void setupDisplay() {
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
Serial.println("Initialized Display");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.printf("Hello world!");
display.display();
}
void setupWifi() {
Serial.print("Wifi Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Wifi Connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void setupAmb() {
ambient.begin(AMB_CHANNEL_ID, AMB_WRITE_KEY, &wifiClient);
}
void showDisplay(float motorSpeed, float temp) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.printf("Speed:%d%%", (int)(motorSpeed * 100));
display.setCursor(0, 16);
display.printf("Temp:%dC", (int)temp);
display.display();
}
void motorSpeed(float speed) {
#define MIN 64
#define MAX 255
if(speed > 0.0) {
digitalWrite(ENABLE_PIN, HIGH);
int sp = MIN + ((MAX- MIN) * speed);
ledcWrite(FORWORD_CH, sp);
ledcWrite(BACK_CH, 0);
Serial.printf("forword speed:%d\n", sp);
}else if(speed < 0.0){
digitalWrite(ENABLE_PIN, HIGH);
int sp = MIN + (MAX - MIN) * (-speed);
ledcWrite(FORWORD_CH, sp);
ledcWrite(BACK_CH, 0);
Serial.printf("back speed:-%d\n", sp);
}else{
digitalWrite(ENABLE_PIN, LOW);
Serial.println("speed:0");
}
}
void ambPost(float motorSpeed, float temperature) {
float motorSpeedPercent = motorSpeed * 100;
ambient.set(AMB_FIELD_SPEED, motorSpeedPercent);
ambient.set(AMB_FIELD_TEMPERATURE, temperature);
ambient.send();
Serial.printf("Ambient Posted. SPEED:%f TEMP:%f\n", motorSpeedPercent, temperature);
}
int liveCounter = 0;
unsigned long prevPostTime = 0;
unsigned long nextPostTime = AMB_INTERVAL;
float motorSpeedVal = 0;
float temperatureVal = 0;
void loop() {
unsigned long liveTime = millis();
DATETIME dateTime = dateTimeFromMsec(liveTime);
// 一定の間隔でデータをAmbientにアップする
if(liveCounter > 0 && (liveTime >= nextPostTime || liveTime < prevPostTime)) {
prevPostTime = liveTime;
nextPostTime = liveTime + AMB_INTERVAL;
ambPost(motorSpeedVal, temperatureVal);
Serial.printf("Posted %d:%ddays %02d:%02d:%02d\n", liveCounter, dateTime.day, dateTime.hour, dateTime.minute, dateTime.sec);
}
// モーターの速度を制御
int motorVol = analogRead(MOTOR_VOL_PIN);
int fanVol = analogRead(FAN_VOL_PIN);
Serial.printf("MOTOR:%d: FAN:%d\n", motorVol, fanVol);
motorSpeedVal = (float)motorVol / 4095.0;
motorSpeed(motorSpeedVal);
// モーターの速度と温度をディスプレイに表示
temperatureVal = thermocouple.readCelsius();
showDisplay(motorSpeedVal, temperatureVal);
liveCounter++;
delay(500);
}
DATETIME dateTimeFromMsec(unsigned long timeMsec) {
DATETIME dateTime;
dateTime.day = timeMsec / DAY;
dateTime.hour = timeMsec % DAY / HOUR;
dateTime.minute = timeMsec % HOUR / MINUTE;
dateTime.sec = timeMsec % MINUTE / SECOND;
return dateTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment