top of page

WORK4.IOT

TIME:2020/11/20

BACKGROUND.

智者大师想要盗取快乐星球的秘宝,于是他必须要进入快乐星球的宝库。

Master wise, who appeared last week, is the spiritual leader of happy planet, and the people of the world admire him very much.

但是宝库门上安有一个门锁,必须通过快乐星球管理人贝贝的同意才可以将门打开。到底,贝贝有没有识破智者大师的身份呢?

However, Mater wise seems to have his own secret ……

 

 

 

THOUGHTS.

智者大师按下门上的按钮,系统会自动向管理员的手机发送请求开门的信息。

Master Sophie seems to be actually a villain, because he can't open the box of wishes, so he wants to transform it so that everyone can't open the box of wishes.

管理员确认开门后,门锁将会自动打开。

He transformed the box and added a password lock to the box. If you input the wrong password three times, the box will explode in 30 seconds!

implementation technology.

ARDUINO.
 

图片3.png
图片2.png
图片1.png

ARDUINO CODE test.

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

#include <ArduinoJson.h>

 

#include <aliyun_mqtt.h>

 

 

#define SENSOR_PIN 13    //pin define

#define LED D4

#define LED2 D5

#define PRODUCT_KEY     "*****"//exchange  PRODUCT_KEY

#define DEVICE_NAME     "*****"//exchange  DEVICE_NAME

#define DEVICE_SECRET   "e1dae1bb344c49a704387dc461ecf505"//exchange DEVICE_SECRET

 

#define DEV_VERSION      "S-TH-WIFI-v1.0-20190220"

 

#define WIFI_SSID       "*****"//exchange WIFI

#define WIFI_PASSWD     "******"//exchange WIFI password

 

 

 

#define ALINK_BODY_FORMAT         "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"%s\",\"params\":%s}"

#define ALINK_TOPIC_PROP_POST     "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"

#define ALINK_TOPIC_PROP_POSTRSP  "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post_reply"

#define ALINK_TOPIC_PROP_SET      "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/service/property/set"

#define ALINK_METHOD_PROP_POST    "thing.event.property.post"

#define ALINK_TOPIC_DEV_INFO      "/ota/device/inform/" PRODUCT_KEY "/" DEVICE_NAME ""    

#define ALINK_VERSION_FROMA      "{\"id\": 123,\"params\": {\"version\": \"%s\"}}"

unsigned long lastMs = 0;

int val=0;

 

WiFiClient   espClient;

PubSubClient mqttClient(espClient);

 

void init_wifi(const char *ssid, const char *password)

{

    WiFi.mode(WIFI_STA);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED)

    {

        Serial.println("WiFi does not connect, try again ...");

        delay(500);

    }

 

    Serial.println("Wifi is connected.");

    Serial.println("IP address: ");

    Serial.println(WiFi.localIP());

}

 

void mqtt_callback(char *topic, byte *payload, unsigned int length)

{

    Serial.print("Message arrived [");

    Serial.print(topic);

    Serial.print("] ");

    payload[length] = '\0';

    Serial.println((char *)payload);

  

    Serial.println("");

    Serial.println((char *)payload);

    Serial.println("");

  

    if (strstr(topic, ALINK_TOPIC_PROP_SET))

    {

        StaticJsonBuffer<100> jsonBuffer;

        JsonObject &root = jsonBuffer.parseObject(payload);

        int params_LightSwitch = root["params"]["LightSwitch"];

        if(params_LightSwitch==1)

        { Serial.println("Led on");

          digitalWrite(LED,HIGH);}

        else

         { Serial.println("Led off");

          digitalWrite(LED,LOW);}

        

 

        

        if (!root.success())

        {

            Serial.println("parseObject() failed");

            return;

        }

    }

}

void mqtt_version_post()

{

    char param[512];

    char jsonBuf[1024];

 

    //sprintf(param, "{\"MotionAlarmState\":%d}", digitalRead(13));

    sprintf(param, "{\"id\": 123,\"params\": {\"version\": \"%s\"}}", DEV_VERSION);

   // sprintf(jsonBuf, ALINK_BODY_FORMAT, ALINK_METHOD_PROP_POST, param);

    Serial.println(param);

    mqttClient.publish(ALINK_TOPIC_DEV_INFO, param);

}

void mqtt_check_connect()

{

    while (!mqttClient.connected())//mqttδ����

    {

        while (connect_aliyun_mqtt(mqttClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET))

        {

            Serial.println("MQTT connect succeed!");

            //client.subscribe(ALINK_TOPIC_PROP_POSTRSP);

            mqttClient.subscribe(ALINK_TOPIC_PROP_SET);

            

            Serial.println("subscribe done");

            mqtt_version_post();

        }

    }

    

}

 

void mqtt_interval_post()

{

    char param[512];

    char jsonBuf[1024];

 

    //sprintf(param, "{\"MotionAlarmState\":%d}", digitalRead(13));

    sprintf(param, "{\"LightSwitch\":%d,\"range\":%d}",!digitalRead(LED),val);

    sprintf(jsonBuf, ALINK_BODY_FORMAT, ALINK_METHOD_PROP_POST, param);

    Serial.println(jsonBuf);

    mqttClient.publish(ALINK_TOPIC_PROP_POST, jsonBuf);

}

 

 

void setup()

{

 

    pinMode(SENSOR_PIN, INPUT);

    pinMode(LED, OUTPUT);

    pinMode(LED2, OUTPUT);

    /* initialize serial for debugging */

    Serial.begin(115200);

 

    Serial.println("Demo Start");

 

    init_wifi(WIFI_SSID, WIFI_PASSWD);

 

    mqttClient.setCallback(mqtt_callback);

}

 

// the loop function runs over and over again forever

void loop()

{

    val= analogRead(A0);

    if (val>500)

    {digitalWrite(LED2,HIGH);}

    else

    {digitalWrite(LED2,LOW);}

  

    if (millis() - lastMs >= 5000)

    {

        lastMs = millis();

        mqtt_check_connect();

        /* Post */        

        mqtt_interval_post();

    }

 

    mqttClient.loop();

 

    unsigned int WAIT_MS = 2000;

    if (digitalRead(SENSOR_PIN) == HIGH)

    {

        Serial.println("Motion detected!");

    }

    else

    {

        Serial.println("Motion absent!");

    }

    delay(WAIT_MS); // ms

    Serial.println(millis() / WAIT_MS);

}

arduino.servo

c1d64c2acf69f8976c66921ac9638d6.jpg

ARDUINO CODE final.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

#include <Servo.h>

#include <aliyun_mqtt.h>
 

#define SENSOR_PIN 13    //pin define
#define LED D4
#define LED2 D5
#define PIN_SERVO D1
Servo myservo;

#define PRODUCT_KEY     "a1Ui7Ds4Gv2"//exchange  PRODUCT_KEY
#define DEVICE_NAME     "happyplanet-003"//exchange  DEVICE_NAME
#define DEVICE_SECRET   "aRdJvyte8ak4aRnX"//exchange DEVICE_SECRET

#define DEV_VERSION      "S-TH-WIFI-v1.0-20190220"

#define WIFI_SSID       "Mate3000"//exchange WIFI
#define WIFI_PASSWD     "bene1234"//exchange WIFI password

 
#define ALINK_BODY_FORMAT         "{\"id\":\"123\",\"version\":\"1.0\",\"method\":\"%s\",\"params\":%s}"
#define ALINK_TOPIC_PROP_POST     "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
#define ALINK_TOPIC_PROP_POSTRSP  "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post_reply"
#define ALINK_TOPIC_PROP_SET      "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/service/property/set"
#define ALINK_METHOD_PROP_POST    "thing.event.property.post"
#define ALINK_TOPIC_DEV_INFO      "/ota/device/inform/" PRODUCT_KEY "/" DEVICE_NAME ""    
#define ALINK_VERSION_FROMA      "{\"id\": 123,\"params\": {\"version\": \"%s\"}}"
unsigned long lastMs = 0;
int val=0;

WiFiClient   espClient;
PubSubClient mqttClient(espClient);
 
void init_wifi(const char *ssid, const char *password)
{
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.println("WiFi does not connect, try again ...");
        delay(500);
    }
 
    Serial.println("Wifi is connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}
 
void mqtt_callback(char *topic, byte *payload, unsigned int length)
{
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    payload[length] = '\0';
    Serial.println((char *)payload);
  
    Serial.println("");
    Serial.println((char *)payload);
    Serial.println("");
  
    if (strstr(topic, ALINK_TOPIC_PROP_SET))
    {
        StaticJsonBuffer<100> jsonBuffer;
        JsonObject &root = jsonBuffer.parseObject(payload);
        int params_LightSwitch = root["params"]["LightSwitch"];
        int params_Servo = root["params"]["Servo"];
        /*if(params_LightSwitch==1)
        { Serial.println("Led on");
          digitalWrite(LED,HIGH);}
        else
         { Serial.println("Led off");
          digitalWrite(LED,LOW);}
        */
        if(params_Servo==90)
        {myservo.write(90);}
        else
        {myservo.write(0);}

        
        if (!root.success())
        {
            Serial.println("parseObject() failed");
            return;
        }
    }
}
void mqtt_version_post()
{
    char param[512];
    char jsonBuf[1024];
 
    //sprintf(param, "{\"MotionAlarmState\":%d}", digitalRead(13));
    sprintf(param, "{\"id\": 123,\"params\": {\"version\": \"%s\"}}", DEV_VERSION);
   // sprintf(jsonBuf, ALINK_BODY_FORMAT, ALINK_METHOD_PROP_POST, param);
    Serial.println(param);
    mqttClient.publish(ALINK_TOPIC_DEV_INFO, param);
}
void mqtt_check_connect()
{
    while (!mqttClient.connected())//mqttδ����
    {
        while (connect_aliyun_mqtt(mqttClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET))
        {
            Serial.println("MQTT connect succeed!");
            //client.subscribe(ALINK_TOPIC_PROP_POSTRSP);
            mqttClient.subscribe(ALINK_TOPIC_PROP_SET);
            
            Serial.println("subscribe done");
            mqtt_version_post();
        }
    }
    
}
 
void mqtt_interval_post()
{
    char param[512];
    char jsonBuf[1024];
 
    //sprintf(param, "{\"MotionAlarmState\":%d}", digitalRead(13));
    sprintf(param, "{\"LightSwitch\":%d,\"range\":%d}",!digitalRead(LED),val);
    sprintf(jsonBuf, ALINK_BODY_FORMAT, ALINK_METHOD_PROP_POST, param);
    Serial.println(jsonBuf);
    mqttClient.publish(ALINK_TOPIC_PROP_POST, jsonBuf);
}
 
 
void setup()
{

    myservo.attach(PIN_SERVO);
    myservo.write(0);
    /*pinMode(SENSOR_PIN, INPUT);
    pinMode(LED, OUTPUT);
    pinMode(LED2, OUTPUT);
     initialize serial for debugging */
    Serial.begin(115200);
 
    Serial.println("Demo Start");
 
    init_wifi(WIFI_SSID, WIFI_PASSWD);
 
    mqttClient.setCallback(mqtt_callback);
}
 
// the loop function runs over and over again forever
void loop()
{
    val= analogRead(A0);
    if (val>500)
    {digitalWrite(LED2,HIGH);}
    else
    {digitalWrite(LED2,LOW);}
  
    if (millis() - lastMs >= 5000)
    {
        lastMs = millis();
        mqtt_check_connect();
        /* Post */        
        mqtt_interval_post();
    }
 
    mqttClient.loop();
 
    unsigned int WAIT_MS = 2000;
    if (digitalRead(SENSOR_PIN) == HIGH)
    {
        Serial.println("Motion detected!");
    }
    else
    {
        Serial.println("Motion absent!");
    }
    delay(WAIT_MS); // ms
    Serial.println(millis() / WAIT_MS);
}

bottom of page