現在手機原來越強,但也吃電非常兇。每個人幾乎至少一個
隨身充 又稱 充電寶,隨時奉上電源。
本篇主角是3年前小米超薄充電寶,多次充放電後裡面電池終於掛了。
收工!!!!
IoT萬物聯網是個很大的發展空間,尤其是在家庭應用是最接近我們生活,提供智能互動提升生活水平。IoT Internet of Things is a big scope for development, especially in home applications providing intelligent and high quality life.
/*
Basic ESP8266 MQTT for relay controller
Regis Hsu
2019-07-10
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h> // Include the mDNS library
#define BUILTIN_LED 2
#define RELAY1_PIN 5
#define RELAY2_PIN 4
#define AP_SSID "your-ssid"
#define AP_PASSWD "password"
#define MQTT_BROKER "xxx.xxx.xxx.xxx"
#define MQTT_PUB "Home/esp32_sub"
#define MQTT_SUB "Home/esp32_pub"
#define RELAY_1_ON 3
#define RELAY_1_OFF 3
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi()
{
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(AP_SSID);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(AP_SSID, AP_PASSWD);
int i = 0;
while ((WiFi.status() != WL_CONNECTED))
{
delay(500);
Serial.print(".");
digitalWrite(BUILTIN_LED, i % 2);
i = i + 1;
}
//turn off the LED
digitalWrite(BUILTIN_LED, HIGH);
randomSeed(micros());
// we send our IP address on the WiFi network
if (!MDNS.begin("esp8266")) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void callback(char* topic, byte * payload, unsigned int length) {
Serial.print(F("Message arrived ["));
Serial.print(topic);
Serial.print(F("] "));
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
client.publish(MQTT_PUB, (char *)payload);
// Switch on the LED if an 1 was received as first character
switch ((char)payload[0] - 48)
{
case 0:
ESP.restart();
break;
case 1:
digitalWrite(RELAY1_PIN, ((char)payload[1] - 48));
break;
case 2:
digitalWrite(RELAY2_PIN, ((char)payload[1] - 48));
break;
default:
Serial.println(F("Not a command!"));
break;
}
}
void reconnect() {
// Loop until we're reconnected
//while (!client.connected()) {
Serial.print(F("Attempting MQTT connection..."));
// Create a random client ID
String clientId = "ESP8266-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println(F("connected"));
// Once connected, publish an announcement...
//client.publish("Home/esp32_pub", "hello world");
// ... and resubscribe
client.subscribe(MQTT_SUB);
} else {
Serial.print(F("failed, rc="));
Serial.print(client.state());
Serial.println(F(" try again in few seconds"));
// Wait 5 seconds before retrying
delay(5000);
//delay(2000);
}
//}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
pinMode(RELAY1_PIN, OUTPUT); // Initialize the BUILTIN_LED pin as an output
pinMode(RELAY2_PIN, OUTPUT);
Serial.begin(115200);
delay(500);
Serial.println();
digitalWrite(RELAY1_PIN, HIGH);
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
client.setCallback(callback);
if (!client.connected()) {
reconnect();
}
client.publish(MQTT_PUB, "11");
}
void loop()
{
if (!client.connected()) {
reconnect();
}
else
client.loop();
long now = millis();
if (now > (RELAY_1_ON * 60 * 1000)) { //ms
client.publish(MQTT_PUB, "10");
Serial.printf("Going into deep sleep for %d mins", RELAY_1_OFF);
client.loop();
delay(2000);
ESP.deepSleep(RELAY_1_OFF * 60 * 1000000); //us
}
}
sudo apt update
sudo apt upgrade
sudo apt autoremove
sudo apt autoclean
sudo apt-get install mosquitto mosquitto-clients
service mosquitto status
nano SubscriberTest.py
python SubscriberTest.py
# SubscriberTest.py
import paho.mqtt.client as mqtt
import time
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("Home/esp32_sub")
client.subscribe("Home/esp32_pub")
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload))
tt = time.localtime(time.time())
print(str(tt.tm_hour)+":"+str(tt.tm_min)+" "+msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("xxx.xxx.xxx.xxx", 1883, 60)
client.loop_forever()
貓抓板? 聽說貓很會亂抓傢俱,所以第一時間就想到這個。。。瓦楞紙的材料,這也是大紙箱的基本材料。超強貓抓板,而且這嬌嬌女還很喜歡的說~~~~