LoginSignup
0
0

More than 5 years have passed since last update.

してもいい日の絶対サイン

Posted at

概要

例えれば、電子式YES/NO枕です。
夫婦間の微妙な問題をスパッと解決して、少子化防止にも貢献する、お助けツール。
webサービスから、貝殻ランプを遠隔操作する。
女性が、スマホ、PCから操作する。
arduino unoからesp8266でwifiにつなぎます。
mqttブローカーは、sangoを使ってます。
スイッチは、jsdo.itに置いてます。

写真

MVC-003S.JPG
iihi4.JPG

ムービー

回路図

iihi.JPG

サンプルコード

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
char buff[256];
long int ctime = millis();
String sendAT(String command, const int timeout)
{
    String response = "";
    Serial.print(command);
    long int time = millis();
    while ((time + timeout) > millis())
    {
        while (Serial.available())
        {
            char c = Serial.read();
            response += c;
        }
    }
    mySerial.print(response);
    return response;
}
String waitAT(const int timeout)
{
    String response = "";
    long int time = millis();
    while ((time + timeout) > millis())
    {
        while (Serial.available())
        {
            char c = Serial.read();
            response += c;
        }
    }
    mySerial.print(response);
    return response;
}
void setup()
{
    pinMode(13, OUTPUT);
    digitalWrite(13, LOW);
    mySerial.begin(115200);
    Serial.begin(115200);
    sendAT("AT+GMR\r\n", 2000);
    sendAT("AT+CWJAP=\"free-spot\",\"\"\r\n", 3000);
    sendAT("AT+CIPMUX=0\r\n", 1000);
    sendAT("AT+CIFSR\r\n", 1000);
    sendAT("AT+CIPSTART=\"TCP\",\"lite.mqtt.shiguredo.jp\",1883\r\n", 2000);
    sendAT("AT+CIPSEND=52\r\n", 3000);
    Serial.write(16);
    Serial.write(50);
    Serial.write(0);
    Serial.write(4);
    Serial.print("MQTT");
    Serial.write(4);
    Serial.write(194);
    Serial.write(0);
    Serial.write(60);
    Serial.write(0);
    Serial.write(4);
    Serial.print("papa");
    Serial.write(0);
    Serial.write(14);
    Serial.print("ohisama@github");
    Serial.write(0);
    Serial.write(16);
    sendAT("pass", 2000);
    sendAT("AT+CIPSEND=27\r\n", 3000);
    Serial.write(130);
    Serial.write(25);
    Serial.write(0);
    Serial.write(1);
    Serial.write(0);
    Serial.write(20);
    Serial.print("ohisama@github/test0");
    Serial.write(0);
    waitAT(2000);
    mySerial.print(" ok\r\n");
}
void loop()
{
    int ch_id, packet_len;
    char * pb;
    int i = 0;
    char c;
    if ((ctime + 29000) < millis())
    {
        String cipSend = "AT+CIPSEND=2\r\n";
        sendAT(cipSend, 3000);
        Serial.write(192);
        Serial.write(0);
        ctime = millis();
    }
    if (Serial.available())
    {
        while (Serial.available() > 0)
        {
            c = Serial.read();
            if (c > ' ' && c < 127)
            {
                buff[i++] = c;
            }
            if (i > 1 && buff[i - 2] == 'I' && buff[i - 1] == '+')
            {
                buff[i] = 0;
                i = 0;
                break;
            }
        }
        mySerial.print(buff);
        if (strncmp(buff, "+IPD,", 5) == 0)
        {
            mySerial.print(" ok1\r\n");
            sscanf(buff + 5, "%d", &packet_len);
            mySerial.print("@len: ");
            mySerial.println(packet_len);
            if (packet_len > 0)
            {
                if (packet_len == 27)
                {
                    digitalWrite(13, LOW);
                    mySerial.print("@on@");
                }
                else if (packet_len == 28)
                {
                    digitalWrite(13, HIGH);
                    mySerial.print("@off@");
                }
                else
                {
                    mySerial.print("@?@");
                }
            }
        }
        else
        {
            mySerial.print(" ng\r\n");
        }
    }
}
0
0
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
0
0