准备
物联网编程,不像我们纯粹的软件编程,还需要硬件的支持。
硬件准备
需要esp8266板子一个,杜邦线若干条。(这是必须的)
我这里有一个三色LED ( KY-016 3色 LED),就拿它来试试手。
通讯准备
esp8266用数据线直连电脑是COM通讯的,直接连接电脑usb后,板子电源灯常亮,(如果不亮就要看看板子说明书了),这时候
记住 端口号 !!!
打开Arduino进行端口设置:
程序上手
GPIO点选择
选择的三色灯,需要三正一负。就随便选D2(GPIO4)、D3(GPIO0)、D4(GPIO2)、GND;
完整代码
/*
ESP8266 Blink by Simon Peter
Blink the blue LED on the ESP-01 module
This example code is in the public domain
The blue LED on the ESP-01 module is connected to GPIO1
(which is also the TXD pin; so we cannot use Serial.print() at the same time)
Note that this sketch uses BUILTIN_LED to find the pin with the internal LED
*/
int LED_PIN2 = 2;//GPIO2
int LED_PIN3 = 0;//GPIO0
int LED_PIN4 = 4;//GPIO4
void setup()
{
Serial.begin(115200);
pinMode(LED_PIN2, OUTPUT); // Initialize the BUILTIN_LED pin as an output
pinMode(LED_PIN3, OUTPUT);
pinMode(LED_PIN4, OUTPUT);
}
// the loop function runs over and over again forever
void loop()
{
int i=0;
for(i;i<8;i++)
{
switch(i)
{
case 0:
digitalWrite(LED_PIN2, 0); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 0);
digitalWrite(LED_PIN4, 0);
break;
case 1:
digitalWrite(LED_PIN2, 0); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 0);
digitalWrite(LED_PIN4, 1);
break;
case 2:
digitalWrite(LED_PIN2, 0); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 1);
digitalWrite(LED_PIN4, 0);
break;
case 3:
digitalWrite(LED_PIN2, 0); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 1);
digitalWrite(LED_PIN4, 1);
break;
case 4:
digitalWrite(LED_PIN2, 1); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 0);
digitalWrite(LED_PIN4, 0);
break;
case 5:
digitalWrite(LED_PIN2, 1); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 0);
digitalWrite(LED_PIN4, 1);
break;
case 6:
digitalWrite(LED_PIN2, 1); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 1);
digitalWrite(LED_PIN4, 0);
break;
case 7:
digitalWrite(LED_PIN2, 1); // Turn the LED on (Note that LOW is the voltage level
digitalWrite(LED_PIN3, 1);
digitalWrite(LED_PIN4, 1);
break;
}
delay(1000); //间隔1000ms也就是1秒
}
}
上传代码
编译 验证没问题就可以上传代码了,有了前面的准备和代码,我们就可以直接上传了。
按住esp8266板子上的 FLASH 按钮,点击上传,等出现上传进度 …。。。后可以松开 FLASH 按钮,继续等待上传完成。
最后的效果
最后 :感谢我们 胡工 一直以来的指导