1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| #include "SSD1306Wire.h" #include <ESP8266WiFi.h> #include <PubSubClient.h> WiFiClient espClient; PubSubClient client (espClient); const char* wifissid="test"; const char* password="12345678"; const char* mqtt_server="xxx.xxx.xxx.xxx"; const char* mqtt_id="isNineSun_ESP"; const char* Mqtt_sub_topic="pub_ywt"; const char* Mqtt_pub_topic="sub_ywt"; long lastMsg=0;
int MIC = 4; int LED = 0; int HC = 15; int D_in=16; int VALOR; int LEDDO; int HoneWai; int Lighting; bool auto_mode=1,led_mode=1;
static const uint8_t wu_you[][32]={0x00,0x80,0x40,0x40,0x42,0x20,0x42,0x10,0x42,0x0C,0xC2,0x03,0x7E,0x00,0x42,0x00,0xC2,0x3F,0x42,0x40,0x42,0x40,0x42,0x40,0x40,0x40,0x40,0x70,0x00,0x00,0x00,0x00, 0x04,0x04,0x04,0x02,0x04,0x01,0x84,0x00,0xE4,0xFF,0x3C,0x09,0x27,0x09,0x24,0x09,0x24,0x09,0x24,0x49,0x24,0x89,0xE4,0x7F,0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00,}; static const uint8_t ren[][32]={0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x0C,0x00,0x03,0xC0,0x00,0x3F,0x00,0xC0,0x00,0x00,0x03,0x00,0x0C,0x00,0x10,0x00,0x20,0x00,0x40,0x00,0x80,0x00,0x00,}; static const uint8_t sheng[][32]={0x04,0x80,0x14,0x60,0xD4,0x1F,0x54,0x02,0x54,0x02,0x54,0x02,0x54,0x02,0xDF,0x03,0x54,0x02,0x54,0x02,0x54,0x02,0x54,0x02,0xD4,0x03,0x14,0x00,0x04,0x00,0x00,0x00,}; static const uint8_t guang[][32]={0x40,0x80,0x40,0x80,0x42,0x40,0x44,0x20,0x58,0x18,0xC0,0x07,0x40,0x00,0x7F,0x00,0x40,0x00,0xC0,0x3F,0x50,0x40,0x48,0x40,0x46,0x40,0x40,0x40,0x40,0x78,0x00,0x00,};
const uint8_t image1[] PROGMEM ={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0x01,0x00,0x00,0x00,0x00,0x00,0xF0,0xFC,0x01,0x00,0x00,0xF8,0xFF,0x3F,0xFF,0xFF,0x7F,0x00,0x00,0xF8,0xFF,0x7F,0xFF,0xF1,0x7F,0x00,0x00,0xF8,0xFF,0x7F,0xFF,0xE4,0x7F,0x00,0x00,0xF8,0xFF,0x7F,0x7F,0x9E,0x7F,0x00, 0x00,0x78,0xCE,0x79,0x60,0x3E,0x00,0x00,0x00,0x7E,0xCE,0x79,0x0C,0xBC,0x39,0x00,0x00,0x7F,0xCE,0x79,0x9C,0x99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01,0x1E,0x00,0x00,0xFE,0xFF,0x77,0xFF,0x77,0x0C,0x00,0x00,0xFF,0xFF,0x3F,0xFB,0xFF,0x01,0x00,0x00,0xFF,0xFF,0x03,0xE0,0xFF,0x0F,0x00,0x00,0xFF,0x1F,0x38,0xEE,0xFF,0x1F,0x00,0x00,0x78,0x00,0x78,0xCE,0xF0,0x3F,0x00,0x00,0x70,0x00,0x7C,0x9E,0x80,0x7C,0x00,0x00,0xF0,0xFF,0x7F, 0x9E,0x00,0x7F,0x00,0x00,0xF0,0xFF,0x7F,0x9C,0x00,0x7F,0x00,0x00,0xF0,0xFF,0x3F,0xC0,0x00,0x3F,0x00,0x00,0x40,0x00,0x00,0x60,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, }; const uint8_t image2[] PROGMEM ={ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x38,0x70,0x00,0x00,0x00,0xC0,0x79,0x00,0x78,0xF8,0x00,0x00,0x00,0xCE,0x7B,0x00,0x78,0xFE,0x00,0x00,0x00,0xCF,0x7B,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0xCF,0x07,0x1E,0x7C,0xFF,0x1F,0x00,0x00,0xCF,0x3B,0x1E,0x7C,0xFF,0x1F,0x00,0x00,0xCF,0xF9,0x1F,0x7C,0xFF,0x1F,0x00,0x00,0xC3,0xF9,0x1F,0x7C,0x1F,0x1F,0x00,0x00,0xB9,0xE1,0x1F,0x7C,0x1F,0x0F,0x00,0x00,0xBC,0x80,0x0F,0x7F,0x0E,0x0F,0x00, 0x00,0x3C,0x38,0xA0,0xFF,0x00,0x0F,0x00,0x00,0xFF,0xFF,0xBF,0xFF,0x3F,0x06,0x00,0x80,0xFF,0xFF,0xBF,0xFF,0xFF,0x00,0x00,0x80,0xFF,0xFF,0x01,0xF0,0xFF,0x07,0x00,0x80,0xFF,0x1F,0x1C,0xF7,0xFF,0x0F,0x00,0x00,0x3D,0x00,0x3E,0x6F,0xFC,0x1F,0x00,0x00,0x3C,0xFC,0x3F,0x4F,0x40,0x3F,0x00,0x00,0xFC,0xFF,0x3F, 0x5F,0x80,0x3F,0x00,0x00,0xFC,0xFF,0x3F,0x4E,0x80,0x3F,0x00,0x00,0xF8,0xFF,0x1F,0x60,0x80,0x1F,0x00,0x00,0xF0,0xFF,0x07,0x30,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };
const int I2C_ADDR = 0x3c; #define SDA_PIN 12 #define SCL_PIN 13
SSD1306Wire oled(I2C_ADDR, SDA_PIN, SCL_PIN);
void setup(){ pinMode(MIC, INPUT); pinMode(LED, OUTPUT); pinMode(HC, INPUT); pinMode(D_in, INPUT); pinMode(2,OUTPUT);
Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server,1883); client.setCallback(callback);
oled.init(); oled.flipScreenVertically(); oled.setContrast(255); drawRect(); oled.clear(); oled.display(); }
void loop(){ if(!client.connected()){ reconnect(); } client.loop();
if(auto_mode){ oled.clear(); Auto_Mode(); } else{ oled.clear(); oled.drawFastImage(30, 0, 64, 64, image2); oled.display(); manual_mode(); delay(200); } }
void Auto_Mode(){ oled.setFont(ArialMT_Plain_16); oled.println("AUTO"); oled.drawFastImage(4 + 20*1, 21*0, 16, 16, sheng[0]); oled.drawFastImage(4 + 20*1, 21*1, 16, 16, ren[0]); oled.drawFastImage(4 + 20*1, 21*2, 16, 16, guang[0]); oled.drawFastImage(4 + 20*0, 21*0, 16, 16, wu_you[VALOR]); oled.drawFastImage(4 + 20*0, 21*1, 16, 16, wu_you[HoneWai]); oled.drawFastImage(4 + 20*0, 21*2, 16, 16, wu_you[!Lighting]); oled.drawFastImage(45, 0, 64, 64, image1); oled.display(); delay(500); oled.display();
VALOR = digitalRead(MIC); HoneWai = digitalRead(HC); Lighting = digitalRead(D_in); if(Lighting==HIGH){ oled.clear(); if (VALOR == HIGH||HoneWai == HIGH){ oled.drawFastImage(4 + 20*1, 21*0, 16, 16, sheng[0]); oled.drawFastImage(4 + 20*1, 21*1, 16, 16, ren[0]); oled.drawFastImage(4 + 20*1, 21*2, 16, 16, guang[0]); oled.drawFastImage(4 + 20*0, 21*0, 16, 16, wu_you[VALOR]); oled.drawFastImage(4 + 20*0, 21*1, 16, 16, wu_you[HoneWai]); oled.drawFastImage(4 + 20*0, 21*2, 16, 16, wu_you[!Lighting]);
oled.drawFastImage(45, 0, 64, 64, image1); oled.display(); digitalWrite(LED, HIGH); delay(5000); } else{ digitalWrite(LED, LOW); } } else{ digitalWrite(LED, LOW); } }
void manual_mode(){ digitalWrite(LED, led_mode?HIGH:LOW); }
void drawRect(void) { for (int16_t i=0; i<oled.getHeight()/2; i+=2) { oled.drawRect(i, i, oled.getWidth()-2*i, oled.getHeight()-2*i); oled.display(); delay(50); } }
void setup_wifi(){ Serial.println(); Serial.print("Connecting to"); Serial.println(wifissid); WiFi.begin(wifissid,password); while(WiFi.status()!=WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(" "); Serial.println("WiFi connected!"); Serial.println("IP Address:"); Serial.println(WiFi.localIP()); } void callback(char* topic,byte* payload,unsigned int length){ String msg=""; String Led_set=""; String auto_set=""; Serial.println("Message arrived{"); Serial.println(topic); Serial.println("}"); for (int i=0;i<length;i++){ msg+=(char)payload[i]; } Serial.println(msg); if(msg.indexOf("led")){ Led_set = msg.substring(msg.indexOf("led\":")+5,msg.indexOf("}")); led_mode = Led_set.toInt(); } if(msg.indexOf("auto")){ auto_set = msg.substring(msg.indexOf("auto\":")+6,msg.indexOf("}")); auto_mode = auto_set.toInt(); } } void reconnect(){ while(!client.connected()){ Serial.println("Attempting MQTT connection..."); if(client.connect(mqtt_id)){ Serial.println("Connected!"); client.publish(Mqtt_pub_topic,"设备已上线");
client.subscribe(Mqtt_sub_topic,1); }else{ Serial.println("Failed,rc="); Serial.println(client.state()); Serial.println("try again in 5 seconds"); delay(50000); } } }
|