请求服务器地址状态
标签(空格分隔):
ESP8266
基础版本
WIFI固定
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
ESP8266WiFiMulti WiFiMulti;
WiFiClient wifiClient;
int ledPin = 2;//板载led ==2 到D3==2
#define pin0 D3
#define pin2 D4 //板载蓝色led
//请求地址
#define HTTPIP "42.192.56.54"
#define HTTPPORT 8766
//#define WIFINAME "GuiDao"
//#define WIFIPW "iflytek123"
#define WIFINAME "HUAWEI-F2MS2B_HiLink"
#define WIFIPW "88888888"
void setup() {
//打开串口
Serial.begin(115200);
Serial.println();
Serial.println("wifi ok????");
WiFiMulti.addAP(WIFINAME,WIFIPW);
while(WiFiMulti.run()!=WL_CONNECTED)
{
delay(500);
Serial.print(">> ?..?");
}
Serial.print("wifi ok");
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(pin0, OUTPUT);
digitalWrite(pin0, LOW);
pinMode(pin2, OUTPUT);
digitalWrite(pin2, LOW);
}
void loop() {
HTTPClient httpClient;
Serial.println("go to http");
//连接指定的网页ip和端口,以及路径
httpClient.begin(wifiClient,HTTPIP,HTTPPORT,"/d/s?id=3");
int Code = httpClient.GET();
String htmls = "";
if(Code)
{
Serial.printf("HTTP Code:%d\n",Code);
if(Code == 200)
{
htmls = httpClient.getString();
Serial.println(htmls);
}else
{
Serial.println(">> sorry ERR string=null");
}
}
int state = 0;
if(htmls == "1"){
state = 1;
digitalWrite(ledPin, LOW);
digitalWrite(pin0, LOW);
digitalWrite(pin2, LOW);
}else if(htmls == "0"){
state = 0;
digitalWrite(ledPin, HIGH);
digitalWrite(pin0, HIGH);
digitalWrite(pin2, HIGH);
}else{
state = 500 ;
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin , HIGH);
Serial.println(">> return 500");
}
//串口回复
Serial.println("hello,sl!");
//设置等待时间
delay(100);
}
手机配网
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
bool autoConfig()
{
WiFi.begin();
for (int i = 0; i < 20; i++)
{
int wstatus = WiFi.status();
if (wstatus == WL_CONNECTED)
{
Serial.println("AutoConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
WiFi.printDiag(Serial);
return true;
//break;
}
else
{
Serial.print("AutoConfig Waiting......");
Serial.println(wstatus);
delay(1000);
}
}
Serial.println("AutoConfig Faild!" );
return false;
//WiFi.printDiag(Serial);
}
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
WiFi.setAutoConnect(true); // 设置自动连接
break;
}
delay(1000); // 这个地方一定要加延时,否则极易崩溃重启
}
}
void setup(){
Serial.begin(9600);
Serial.println("Wait for Smartconfig");
if (!autoConfig())
{
Serial.println("Start module");
smartConfig();
}
pinMode(2, OUTPUT);
}
void loop(){
digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2,LOW);
delay(1000);
}
#整合
//智能配网
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <EEPROM.h>
WiFiClient wifiClient;
int led_blue = 2;//板载led
#define pin0 D3
#define pin2 D4
String smart_data = "";
int size_data = 0;//wifi长度
String wifiname;
String wifipsw;
bool flag;//读取eerom联网,是否成功标志位
int wifiname_len;//wifi名称长度
#define HTTPIP "42.192.56.54"
#define HTTPPORT 8766
void Config_wifi(const char *wifiname,const char *psw){//函数读取eeprom中的数据,直接完成联网
pinMode(led_blue, OUTPUT);
digitalWrite(led_blue, LOW);
int i = 0 ;
WiFi.begin(wifiname, psw);
while(WiFi.status() != WL_CONNECTED){
digitalWrite(led_blue,LOW);
delay(63);
digitalWrite(led_blue,HIGH);
delay(62);
digitalWrite(led_blue,LOW);
delay(63);
digitalWrite(led_blue,HIGH);
delay(62);
digitalWrite(led_blue,LOW);
delay(63);
digitalWrite(led_blue,HIGH);
delay(62);
digitalWrite(led_blue,LOW);
delay(63);
digitalWrite(led_blue,HIGH);
delay(62);
if (i == 0){
Serial.print("历史网络配置中");
}
Serial.print(".");
i++;
if(i==30){
Serial.println("连接失败请用手机配网");
flag = true;
break;
}
}
if(WiFi.isConnected()){
Serial.println("连接成功");
Serial.print("Local IP:");
Serial.println(WiFi.localIP());
}
}
void read_eeprom(){
wifiname = "HUAWEI-F2MS2B_HiLink";
wifipsw = "88888888";
int a = int(EEPROM.read(0) - int('0'));//wifi名长度,eeprom的长度,保存的只的wifi名的长度
int b = (int(EEPROM.read(1)) - int('0')) * 10 + (int(EEPROM.read(2)) - int('0')) ; //读数据,地址1和地址2分别保存wifi密码的长度的十位和个位
Serial.println(a);
Serial.println(b);
for(int addr = 3;addr < b + 1; addr++){
int data = EEPROM.read(addr);
if(addr < a + 3){
wifiname = wifiname +char(data);
}else{
wifipsw = wifipsw + char(data);
}
delay(2);
}
digitalWrite(led_blue,LOW);
Serial.print("SSID:");
Serial.println(wifiname);
Serial.print("PASSWORD:");
Serial.println(wifipsw);
}
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
delay(1000);
// 等待配网
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
digitalWrite(led_blue,LOW);
delay(250);
digitalWrite(led_blue,HIGH);
delay(250);
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
WiFi.setAutoConnect(true); // 设置自动连接
wifiname_len = String(WiFi.SSID().c_str()).length(); //获取wifi名的长度
// EEPROM.write(0, char(wifiname_len)); //写在地址0的数据为存入的wifi名的长度
// EEPROM.write(1, char(size_data)); //写在地址1的数据为存入的数据长度
smart_data = String(wifiname_len) + String(size_data) + String (WiFi.SSID().c_str()) + String(WiFi.psk().c_str());
size_data = String(smart_data).length();
smart_data = String(wifiname_len) + String(size_data) + String (WiFi.SSID().c_str()) + String(WiFi.psk().c_str());
Serial.println(size_data);
Serial.println(smart_data); //打印出保存在eeprom数据
Serial.println(String(smart_data).length());//打印出保存在eeprom中的数据长度
for (int addr = 0; addr < size_data + 1; addr++)
{
//int data = addr%256; //在该代码中等同于int data = addr;因为下面write方法是以字节为存储单位的.
//String(smart_data).charAt(addr);
EEPROM.write(addr, toascii(String(smart_data).charAt(addr))); //写数据
}
EEPROM.commit(); //保存更改的数据
// EEPROM.end(); //同EEPROM.commit();
Serial.println("End write");
break;
digitalWrite(led_blue,LOW);
}
}
delay(300);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
EEPROM.begin(50);//申请操作到地址4095(比如你只需要读写地址为100上的一个字节,该处也需输入参数101)
flag = false;
read_eeprom();
Config_wifi(wifiname.c_str(),wifipsw.c_str());
if(flag){
smartConfig();
};
}
void loop() {
// put your main code here, to run repeatedly:
HTTPClient httpClient;
Serial.println("go to http");
//连接指定的网页ip和端口,以及路径
httpClient.begin(wifiClient,HTTPIP,HTTPPORT,"/d/s?id=2");
int Code = httpClient.GET();
String htmls = "";
if(Code)
{
Serial.printf("HTTP Code:%d\n",Code);
if(Code == 200)
{
htmls = httpClient.getString();
Serial.println(htmls);
}else
{
Serial.println(">> sorry ERR string=null");
}
}
if(htmls == "1"){
digitalWrite(led_blue, LOW);
digitalWrite(pin0, LOW);
digitalWrite(pin2, LOW);
}else if(htmls == "0"){
digitalWrite(led_blue, HIGH);
digitalWrite(pin0, HIGH);
digitalWrite(pin2, HIGH);
}else{
digitalWrite(led_blue, LOW);
digitalWrite(pin0, LOW);
digitalWrite(pin2, LOW);
delay(500);
digitalWrite(led_blue , HIGH);
digitalWrite(pin0, HIGH);
digitalWrite(pin2, HIGH);
}
//串口回复
Serial.println("hello,sl!");
//设置等待时间
delay(100);
}