├── NTP_Client_ESP01 ├── README.md ├── espLib.h └── NTP_Client_ESP01.ino ├── SNTP_Client_ESP01 ├── README.md ├── SNTP_Client_ESP01.ino └── espLib.h ├── MQTT_Publish_ESP01 ├── README.md ├── espLib.h └── MQTT_Publish_ESP01.ino ├── MQTT_Subscribe_ESP01 ├── README.md ├── espLib.h └── MQTT_Subscribe_ESP01.ino ├── SMTP_Client_gmail_ESP01 ├── README.md ├── espLib.h └── SMTP_Client_gmail_ESP01.ino ├── UDP_Broadcast_Server_ESP01 ├── README.md ├── UDP_Broadcast_Server_ESP01.ino └── espLib.h ├── UDP_Broadcast_Client_ESP01 ├── README.md ├── espLib.h └── UDP_Broadcast_Client_ESP01.ino ├── TCP_Server_ESP01 ├── README.md ├── TCP_Server_ESP01.ino └── espLib.h ├── Flash_AT_firmware └── README.md ├── TCP_Client_ESP01 ├── README.md ├── TCP_Client_ESP01.ino └── espLib.h └── README.md /NTP_Client_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define NTP_SERVER "time.google.com" 5 | NTP server you use 6 | - #define TIME_ZONE 9 7 | Your local time zone 8 | 9 | -------------------------------------------------------------------------------- /SNTP_Client_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define SNTP_SERVER "time.google.com" 5 | SNTP server you use 6 | - #define TIME_ZONE 9 7 | Your local time zone 8 | 9 | 10 | # Using terminal software such as CoolTerm 11 | ``` 12 | AT+CIPDNS_CUR? 13 | +CIPDNS_CUR:192.168.10.1 14 | +CIPDNS_CUR:208.67.222.222 15 | 16 | OK 17 | 18 | AT+CIPDNS_CUR=1,"8.8.8.8","8.8.4.4" 19 | 20 | OK 21 | 22 | AT+CIPDNS_CUR? 23 | +CIPDNS_CUR:8.8.8.8 24 | +CIPDNS_CUR:8.8.4.4 25 | 26 | OK 27 | 28 | AT+CIPSNTPCFG=1,9,"time.google.com" 29 | 30 | OK 31 | 32 | AT+CIPSNTPTIME? 33 | +CIPSNTPTIME:Tue May 07 20:00:11 2024 34 | OK 35 | ``` 36 | -------------------------------------------------------------------------------- /MQTT_Publish_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define MQTT_SERVER "broker.hivemq.com" 5 | MQTT server you use 6 | - #define INTERVAL 10 7 | MQTT Publish Interval(Second) 8 | - #define MQTT_TOPIC "/ESP-AT-MQTT/" 9 | MQTT Publish Topic 10 | - #define MQTT_WILL_TOPIC "/ESP-AT-MQTT/" 11 | MQTT Will Topic 12 | - #define MQTT_WILL_MSG "I am leaving..." 13 | MQTT Will Payload 14 | - #define STOP_BUTTON 0 15 | Stop Button GPIO 16 | - #define RUNNING_LED 13 17 | Running LED GPIO 18 | 19 | 20 | # Subscribe using mosquitto_sub 21 | ``` 22 | mosquitto_sub -v -h "broker.hivemq.com" -p 1883 -t "/ESP-AT-MQTT/#" 23 | ``` 24 | -------------------------------------------------------------------------------- /MQTT_Subscribe_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define MQTT_SERVER "broker.hivemq.com" 5 | MQTT server you use 6 | - #define SUB_TOPIC "/ESP-AT-MQTT/#" 7 | MQTT Subscribe Topic 8 | - #define MAX_TOPIC 64 9 | MQTT Subscribe Topic Maximun Length 10 | - #define MAX_PAYLOAD 64 11 | MQTT Subscribe Payload Maximum Length 12 | - #define STOP_BUTTON 0 13 | Stop Button GPIO 14 | - define RUNNING_LED 13 15 | Running LED GPIO 16 | 17 | 18 | # Publish using mosquitto_pub 19 | ``` 20 | #!/bin/bash 21 | #set -x 22 | fail=0 23 | while : 24 | do 25 | payload=`date "+%Y/%m/%d %H:%M:%S"` 26 | echo ${payload} 27 | mosquitto_pub -h broker.hivemq.com -p 1883 -t "/ESP-AT-MQTT/text" -m "${payload}" 28 | if [ $? -ne 0 ]; then 29 | fail=$((++fail)) 30 | echo ${fail} 31 | sleep 3 32 | fi 33 | sleep 1 34 | done 35 | ``` -------------------------------------------------------------------------------- /SMTP_Client_gmail_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define SMTP_SERVER "smtp.gmail.com" 5 | SMTP Server 6 | - #define SMTP_PORT 465 7 | SMTP Server port 8 | - #define BASE64_USER "Base64 Encorded Your Username of gmail" 9 | Gmain user name converted to BASE64 10 | See below 11 | - #define BASE64_PASS "Base64 Encorded Your Password of gmail 12 | Gmail Password converted to BASE64 13 | See below 14 | - #define MAIL_FROM "mailFrom@gmail.com" 15 | Mail from 16 | - #define MAIL_TO "mailTo@provider.com" 17 | Mail To 18 | - #define JAPANESE 0 19 | 1 to send Japanese text 20 | 21 | 22 | # How to convert mail address and password. 23 | 24 | You can convert your mail address of gmail and a password to a character string of Base64 using this page. 25 | 26 | https://www.base64encode.org/ 27 | 28 | For example when your mail address is "aaa@gmail.com", enter "aaa@gmail.com" and press a ENCODE button. 29 | "YWFhQGdtYWlsLmNvbQ==" is shown to a lower. 30 | You use this as a mail address. 31 | 32 | About a password, you can convert by the same procedure. 33 | -------------------------------------------------------------------------------- /UDP_Broadcast_Server_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define LOCAL_PORT 8080 5 | Server listen Port 6 | 7 | 8 | # UDP broadcast client using python 9 | ``` 10 | #!/usr/bin/python 11 | #-*- encoding: utf-8 -*- 12 | import socket 13 | import signal 14 | import time 15 | import argparse 16 | 17 | 18 | ADDRESS = "255.255.255.255" # limited broadcast address 19 | #ADDRESS = "" # limited broadcast address 20 | 21 | def handler(signal, frame): 22 | global running 23 | print('handler') 24 | running = False 25 | 26 | if __name__=='__main__': 27 | signal.signal(signal.SIGINT, handler) 28 | running = True 29 | 30 | parser = argparse.ArgumentParser() 31 | parser.add_argument('--port', type=int, help='udp port', default=8080) 32 | args = parser.parse_args() 33 | print("port={}".format(args.port)) 34 | 35 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 36 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) 37 | 38 | counter = 0 39 | while running: 40 | smsg = "data {} from client".format(counter) 41 | counter = counter + 1 42 | sock.sendto(smsg.encode('utf-8'), (ADDRESS, args.port)) 43 | print("[{}]---->".format(smsg),end="") 44 | time.sleep(1) 45 | 46 | sock.settimeout(2.0) 47 | try: 48 | rmsg, cli_addr = sock.recvfrom(1024) 49 | if (type(rmsg) == bytes): 50 | rmsg=rmsg.decode('utf-8') 51 | print("[{}]".format(rmsg)) 52 | except: 53 | print("receive timeout") 54 | continue 55 | 56 | sock.close() 57 | ``` 58 | -------------------------------------------------------------------------------- /UDP_Broadcast_Client_ESP01/README.md: -------------------------------------------------------------------------------- 1 | UDP Broadcast Client using ESP8266 AT Instruction Set 2 | 3 | # Environment 4 | You need to change the following environment. 5 | 6 | - #define REMOTE_PORT 8080 7 | Port number of the UDP server to use 8 | - #define INTERVAL 1000 9 | Interval of Packet Send(MillSecond) 10 | 11 | # UDP Broadcast server using python 12 | ``` 13 | #!/usr/bin/python3 14 | # -*- coding : UTF-8 -*- 15 | import time 16 | import select 17 | import socket 18 | import signal 19 | import argparse 20 | 21 | def handler(signal, frame): 22 | global running 23 | print('handler') 24 | running = False 25 | 26 | if __name__=='__main__': 27 | signal.signal(signal.SIGINT, handler) 28 | running = True 29 | 30 | parser = argparse.ArgumentParser() 31 | parser.add_argument('--port', type=int, help='tcp port', default=8080) 32 | args = parser.parse_args() 33 | print("port={}".format(args.port)) 34 | 35 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 36 | sock.bind(('', args.port)) 37 | sock.settimeout(1.0) 38 | 39 | while running: 40 | try: 41 | rmsg, cli_addr = sock.recvfrom(1024) 42 | if (type(rmsg) == bytes): 43 | rmsg=rmsg.decode('utf-8') 44 | print("[{}]".format(rmsg),end="") 45 | except: 46 | #print("timeout") 47 | continue 48 | 49 | smsg = "" 50 | for ch in rmsg: 51 | #print("ch={}".format(ch)) 52 | if ch.islower(): 53 | smsg = smsg + ch.upper() 54 | else: 55 | smsg = smsg + ch.lower() 56 | 57 | time.sleep(1) 58 | print("---->[{}]".format(smsg)) 59 | sock.sendto(smsg.encode(encoding='utf-8'), cli_addr) 60 | 61 | sock.close() 62 | ``` 63 | -------------------------------------------------------------------------------- /TCP_Server_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define LOCAL_IP "192.168.10.21" 5 | Server IP Address 6 | - #define LOCAL_PORT 8080 7 | Server listen Port 8 | 9 | 10 | # TCP client using python 11 | ``` 12 | #!/usr/bin/python3 13 | #-*- encoding: utf-8 -*- 14 | import socket 15 | import signal 16 | import time 17 | import argparse 18 | 19 | def handler(signal, frame): 20 | global running 21 | print('handler') 22 | running = False 23 | 24 | if __name__=='__main__': 25 | signal.signal(signal.SIGINT, handler) 26 | running = True 27 | 28 | parser = argparse.ArgumentParser() 29 | parser.add_argument('--host', help='tcp host', default="192.168.10.21") 30 | parser.add_argument('--port', type=int, help='tcp port', default=8080) 31 | args = parser.parse_args() 32 | print("host={}".format(args.host)) 33 | print("port={}".format(args.port)) 34 | 35 | counter = 0 36 | while running: 37 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 38 | 39 | client.settimeout(10.0) 40 | try: 41 | client.connect((args.host, args.port)) 42 | #print("connect to host") 43 | except: 44 | print("connect fail") 45 | continue 46 | 47 | smsg = "data {} from client".format(counter) 48 | counter = counter + 1 49 | client.send(smsg.encode(encoding='utf-8')) 50 | print("[{}]---->".format(smsg),end="") 51 | 52 | client.settimeout(2.0) 53 | try: 54 | rmsg = client.recv(1024) 55 | if (type(rmsg) == bytes): 56 | rmsg=rmsg.decode('utf-8') 57 | print("[{}]".format(rmsg)) 58 | except: 59 | print("receive timeout") 60 | 61 | client.close() 62 | time.sleep(2) 63 | ``` 64 | -------------------------------------------------------------------------------- /Flash_AT_firmware/README.md: -------------------------------------------------------------------------------- 1 | # How to flash AT firmware using Flash Download Tools 2 | 3 | ESP8266 AT-Firmware writing uses the tools and firmware published below: 4 | 5 | - Flash Download Tools 6 | You can download from [here](http://www.espressif.com/en/support/download/other-tools). 7 | 8 | - AT-Firmware 9 | You can download from [here](https://github.com/espressif/ESP8266_NONOS_SDK/tags). 10 | Some firmware version does not support ESP01. 11 | So we flash version 3.0.4 and do an firmware update. 12 | The latest version is 3.0.6. 13 | ``` 14 | AT+GMR 15 | AT version:1.7.6.0(Jan 24 2022 08:56:02) 16 | SDK version:3.0.6-dev(072755c) 17 | compile time:Jun 17 2024 07:37:49 18 | Bin version(Wroom 02):1.7.6 19 | OK 20 | ``` 21 | 22 | # Goto UART Download Mode 23 | Start Flash Download Tools 24 | Select firmware and select com port. 25 | Connect the GPIO2 of the ESP8266 to GND to reset it. 26 | Start flash. 27 | ![FLASH_DOWNLOAD_TOOLS-1](https://user-images.githubusercontent.com/6020549/233518393-50a92b0c-91ae-463e-8221-bc92657aedd7.jpg) 28 | ![FLASH_DOWNLOAD_TOOLS-2](https://github.com/nopnop2002/Arduino-ESPAT-TCP/assets/6020549/c79f0980-5021-4846-a46a-8a6ddcea6d23) 29 | ![FLASH_DOWNLOAD_TOOLS-3](https://github.com/nopnop2002/Arduino-ESPAT-TCP/assets/6020549/6e3c7bce-96ce-4481-80c7-8f44bf51f242) 30 | ![FLASH_DOWNLOAD_TOOLS-4](https://github.com/nopnop2002/Arduino-ESPAT-TCP/assets/6020549/7a0e28ef-bbdf-4d0e-ac6b-ac96860aaae8) 31 | 32 | # Goto Flash Boot Mode 33 | Open terminal software such as CoolTerm and connect to ESP8266. 34 | The default baud rate is 115200bps. 35 | ESP8266's AT firmware treats CR+LF as the end of the command. 36 | Change the terminal software's transmission termination character to CR+LF. 37 | ```AT``` 38 | Change GPIO2 of ESP8266 to PullUp and reset. 39 | ![FLASH_DOWNLOAD_TOOLS-6](https://github.com/nopnop2002/Arduino-ESPAT-TCP/assets/6020549/ec7535d3-dfc5-4033-a113-980bcf2fd391) 40 | -------------------------------------------------------------------------------- /TCP_Client_ESP01/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | You need to change the following environment. 3 | 4 | - #define SERVER "192.168.10.46" 5 | IP address of the TCP server to use 6 | - #define PORT 8080 7 | Port number of the TCP server to use 8 | - #define INTERVAL 5000 9 | Interval of Packet Send(MillSecond) 10 | 11 | 12 | # TCP server using python 13 | ``` 14 | #!/usr/bin/python3 15 | # -*- coding : UTF-8 -*- 16 | import socket 17 | #!/usr/bin/python3 18 | # -*- coding : UTF-8 -*- 19 | import socket 20 | import signal 21 | import argparse 22 | server_ip = "0.0.0.0" 23 | 24 | def handler(signal, frame): 25 | global running 26 | print('handler') 27 | running = False 28 | 29 | if __name__=='__main__': 30 | signal.signal(signal.SIGINT, handler) 31 | running = True 32 | 33 | parser = argparse.ArgumentParser() 34 | parser.add_argument('--port', type=int, help='tcp port', default=8080) 35 | args = parser.parse_args() 36 | print("port={}".format(args.port)) 37 | 38 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 39 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 40 | sock.bind((server_ip, args.port)) 41 | 42 | while True: 43 | sock.listen(5) 44 | sock.settimeout(1.0) 45 | try: 46 | client,address = sock.accept() 47 | print("Connected!! [ Source : {}]".format(address)) 48 | except: 49 | #print("Timeout. running={}".format(running)) 50 | if running is False: break 51 | continue 52 | 53 | while running: 54 | rmsg = client.recv(1024) 55 | print("len(rmsg)={}".format(len(rmsg))) 56 | if (len(rmsg) == 0): break 57 | if (type(rmsg) == bytes): 58 | rmsg=rmsg.decode('utf-8') 59 | print("[{}]".format(rmsg),end="") 60 | 61 | smsg = "" 62 | for ch in rmsg: 63 | #print("ch={}".format(ch)) 64 | if ch.islower(): 65 | smsg = smsg + ch.upper() 66 | else: 67 | smsg = smsg + ch.lower() 68 | 69 | print("---->[{}]".format(smsg)) 70 | client.send(smsg.encode(encoding='utf-8')) 71 | 72 | client.close() 73 | ``` 74 | -------------------------------------------------------------------------------- /NTP_Client_ESP01/espLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Socket library using ESP8266 AT Instruction Set 3 | */ 4 | 5 | #define _DEBUG_ 0 6 | 7 | //Serial Object 8 | Stream *espSerial = 0; 9 | 10 | //Save Serial Object 11 | void serialSetup(Stream &port) { 12 | espSerial = &port; 13 | } 14 | 15 | //Wait for specific input string until timeout runs out 16 | bool waitForString(char* input, int length, unsigned int timeout, bool print=false) { 17 | 18 | unsigned long end_time = millis() + timeout; 19 | char current_byte = 0; 20 | int index = 0; 21 | 22 | while (end_time >= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(""); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | //Send Message 68 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 69 | char atcmd[64]; 70 | if (strlen(rmote)) { 71 | if (id >= 0) { 72 | //AT+CIPSEND=,,, 73 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 74 | } else { 75 | //AT+CIPSEND=,, 76 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 77 | } 78 | } else { 79 | if (id >= 0) { 80 | //AT+CIPSEND=, 81 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 82 | } else { 83 | //AT+CIPSEND= 84 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 85 | } 86 | } 87 | sendCommand(atcmd); 88 | if (!waitForString(">", 1, 1000)) return 1; 89 | clearBuffer(); 90 | 91 | //Send Packet 92 | for(int i=0;iwrite(buf[i]); 93 | if (!waitForString("SEND OK", 7, 1000)) return 2; 94 | // clearBuffer(); 95 | return 0; 96 | } 97 | 98 | //Receive Message 99 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 100 | int len=0; 101 | int flag=0; 102 | int datalen; 103 | long int time = millis(); 104 | 105 | // id < 0 +IPD,nn:ReceiveData 106 | // id = 0 +IPD,0,nn:ReceiveData 107 | // id > 0 +IPD,id,nn:ReceiveData 108 | while( (time+timeout) > millis()) { 109 | while(espSerial->available()) { 110 | char current_byte = espSerial->read(); // read the next character. 111 | if (_DEBUG_) { 112 | Serial.print("0x"); 113 | if (current_byte < 0x10) Serial.print("0"); 114 | Serial.print(current_byte,HEX); 115 | Serial.print("["); 116 | if (current_byte < 0x20) { 117 | Serial.print(" "); 118 | } else if (current_byte > 0x7F) { 119 | Serial.print(" "); 120 | } else { 121 | Serial.print(current_byte); 122 | } 123 | Serial.print("]"); 124 | Serial.print(flag); 125 | Serial.print(" "); 126 | Serial.print(datalen); 127 | Serial.print(" "); 128 | Serial.println(len); 129 | } 130 | if (flag == 0) { 131 | if (current_byte != 0x2c) continue; 132 | flag++; 133 | if (id < 0) flag++; 134 | } else if (flag == 1) { 135 | if (current_byte != 0x2c) continue; 136 | flag++; 137 | } else if (flag == 2) { 138 | if (current_byte == 0x3a) { // : 139 | datalen=atoi(buf); 140 | flag++; 141 | len=0; 142 | } else { 143 | buf[len++]=current_byte; 144 | } 145 | } else { 146 | buf[len++]=current_byte; 147 | if (len == datalen) return len; 148 | } 149 | } // end while 150 | } // end while 151 | return -len; 152 | } 153 | 154 | //Get IP Address 155 | int getIpAddress(char *buf, int szbuf, int timeout) { 156 | int len=0; 157 | int pos=0; 158 | char line[128] = {0}; 159 | 160 | long int time = millis(); 161 | 162 | sendCommand("AT+CIPSTA?"); 163 | 164 | while( (time+timeout) > millis()) { 165 | while(espSerial->available()) { 166 | char c = espSerial->read(); // read the next character. 167 | if (c == 0x0d) { 168 | 169 | } else if (c == 0x0a) { 170 | if (_DEBUG_) { 171 | Serial.print("Read=["); 172 | Serial.print(line); 173 | Serial.println("]"); 174 | } 175 | int offset; 176 | for(offset=0;offset millis()) { 207 | while(espSerial->available()) { 208 | char c = espSerial->read(); // read the next character. 209 | if (c == 0x0d) { 210 | 211 | } else if (c == 0x0a) { 212 | if (_DEBUG_) { 213 | Serial.print("Read=["); 214 | Serial.print(line); 215 | Serial.println("]"); 216 | } 217 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 218 | strcpy(buf,&line[12]); 219 | len = strlen(buf) - 1; 220 | buf[len] = 0; 221 | } 222 | if (strcmp(line,"OK") == 0) return len; 223 | pos=0; 224 | line[pos]=0; 225 | } else { 226 | line[pos++]=c; 227 | line[pos]=0; 228 | } 229 | } 230 | } 231 | return len; 232 | } 233 | 234 | //Print error 235 | void errorDisplay(char* buff) { 236 | Serial.print("Error:"); 237 | Serial.println(buff); 238 | while(1) {} 239 | } 240 | -------------------------------------------------------------------------------- /UDP_Broadcast_Client_ESP01/espLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Socket library using ESP8266 AT Instruction Set 3 | */ 4 | 5 | #define _DEBUG_ 0 6 | 7 | //Serial Object 8 | Stream *espSerial = 0; 9 | 10 | //Save Serial Object 11 | void serialSetup(Stream &port) { 12 | espSerial = &port; 13 | } 14 | 15 | //Wait for specific input string until timeout runs out 16 | bool waitForString(char* input, int length, unsigned int timeout, bool print=false) { 17 | 18 | unsigned long end_time = millis() + timeout; 19 | char current_byte = 0; 20 | int index = 0; 21 | 22 | while (end_time >= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(""); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | //Send Message 68 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 69 | char atcmd[64]; 70 | if (strlen(rmote)) { 71 | if (id >= 0) { 72 | //AT+CIPSEND=,,, 73 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 74 | } else { 75 | //AT+CIPSEND=,, 76 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 77 | } 78 | } else { 79 | if (id >= 0) { 80 | //AT+CIPSEND=, 81 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 82 | } else { 83 | //AT+CIPSEND= 84 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 85 | } 86 | } 87 | sendCommand(atcmd); 88 | if (!waitForString(">", 1, 1000)) return 1; 89 | clearBuffer(); 90 | 91 | //Send Packet 92 | for(int i=0;iwrite(buf[i]); 93 | if (!waitForString("SEND OK", 7, 1000)) return 2; 94 | // clearBuffer(); 95 | return 0; 96 | } 97 | 98 | //Receive Message 99 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 100 | int len=0; 101 | int flag=0; 102 | int datalen; 103 | long int time = millis(); 104 | 105 | // id < 0 +IPD,nn:ReceiveData 106 | // id = 0 +IPD,0,nn:ReceiveData 107 | // id > 0 +IPD,id,nn:ReceiveData 108 | while( (time+timeout) > millis()) { 109 | while(espSerial->available()) { 110 | char current_byte = espSerial->read(); // read the next character. 111 | if (_DEBUG_) { 112 | Serial.print("0x"); 113 | if (current_byte < 0x10) Serial.print("0"); 114 | Serial.print(current_byte,HEX); 115 | Serial.print("["); 116 | if (current_byte < 0x20) { 117 | Serial.print(" "); 118 | } else if (current_byte > 0x7F) { 119 | Serial.print(" "); 120 | } else { 121 | Serial.print(current_byte); 122 | } 123 | Serial.print("]"); 124 | Serial.print(flag); 125 | Serial.print(" "); 126 | Serial.print(datalen); 127 | Serial.print(" "); 128 | Serial.println(len); 129 | } 130 | if (flag == 0) { 131 | if (current_byte != 0x2c) continue; 132 | flag++; 133 | if (id < 0) flag++; 134 | } else if (flag == 1) { 135 | if (current_byte != 0x2c) continue; 136 | flag++; 137 | } else if (flag == 2) { 138 | if (current_byte == 0x3a) { // : 139 | datalen=atoi(buf); 140 | flag++; 141 | len=0; 142 | } else { 143 | buf[len++]=current_byte; 144 | } 145 | } else { 146 | buf[len++]=current_byte; 147 | if (len == datalen) return len; 148 | } 149 | } // end while 150 | } // end while 151 | return -len; 152 | } 153 | 154 | //Get IP Address 155 | int getIpAddress(char *buf, int szbuf, int timeout) { 156 | int len=0; 157 | int pos=0; 158 | char line[128] = {0}; 159 | 160 | long int time = millis(); 161 | 162 | sendCommand("AT+CIPSTA?"); 163 | 164 | while( (time+timeout) > millis()) { 165 | while(espSerial->available()) { 166 | char c = espSerial->read(); // read the next character. 167 | if (c == 0x0d) { 168 | 169 | } else if (c == 0x0a) { 170 | if (_DEBUG_) { 171 | Serial.print("Read=["); 172 | Serial.print(line); 173 | Serial.println("]"); 174 | } 175 | int offset; 176 | for(offset=0;offset millis()) { 207 | while(espSerial->available()) { 208 | char c = espSerial->read(); // read the next character. 209 | if (c == 0x0d) { 210 | 211 | } else if (c == 0x0a) { 212 | if (_DEBUG_) { 213 | Serial.print("Read=["); 214 | Serial.print(line); 215 | Serial.println("]"); 216 | } 217 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 218 | strcpy(buf,&line[12]); 219 | len = strlen(buf) - 1; 220 | buf[len] = 0; 221 | } 222 | if (strcmp(line,"OK") == 0) return len; 223 | pos=0; 224 | line[pos]=0; 225 | } else { 226 | line[pos++]=c; 227 | line[pos]=0; 228 | } 229 | } 230 | } 231 | return len; 232 | } 233 | 234 | //Print error 235 | void errorDisplay(char* buff) { 236 | Serial.print("Error:"); 237 | Serial.println(buff); 238 | while(1) {} 239 | } 240 | -------------------------------------------------------------------------------- /TCP_Server_ESP01/TCP_Server_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Simple TCP/IP Server using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | #include "espLib.h" 52 | 53 | //for Arduino UNO(ATmega328) 54 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 55 | #include 56 | #define SERIAL_RX 4 57 | #define SERIAL_TX 5 58 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, 59 | #define _BAUDRATE_ 4800 60 | #define _SERIAL_ Serial2 61 | #define _MODEL_ "ATmega328" 62 | 63 | //for Arduino MEGA(ATmega2560) 64 | #elif defined(__AVR_ATmega2560__) 65 | #define _BAUDRATE_ 115200 66 | #define _SERIAL_ Serial1 67 | #define _MODEL_ "ATmega2560" 68 | 69 | //for STM32F103(MAPLE Core) 70 | #elif defined(__STM32F1__) 71 | #define _BAUDRATE_ 115200 72 | #define _SERIAL_ Serial2 73 | #define _MODEL_ "STM32F103 MAPLE Core" 74 | 75 | //for STM32F103(ST Core) 76 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 77 | HardwareSerial Serial2(PA3, PA2); 78 | #define _BAUDRATE_ 115200 79 | #define _SERIAL_ Serial2 80 | #define _MODEL_ "STM32F103 ST Core" 81 | 82 | //for STM32F303(ST Core) 83 | #elif defined(ARDUINO_BLACKPILL_F303CC) 84 | HardwareSerial Serial2(PA3, PA2); 85 | #define _BAUDRATE_ 115200 86 | #define _SERIAL_ Serial2 87 | #define _MODEL_ "STM32F303 ST Core" 88 | 89 | //for STM32F401(ST Core) 90 | #elif defined(ARDUINO_BLACKPILL_F401CC) 91 | HardwareSerial Serial2(PA3, PA2); 92 | #define _BAUDRATE_ 115200 93 | #define _SERIAL_ Serial2 94 | #define _MODEL_ "STM32F401 ST Core" 95 | 96 | //for STM32F4DISC1(ST Core) 97 | #elif defined(ARDUINO_DISCO_F407VG) 98 | HardwareSerial Serial3(PD9, PD8); 99 | #define _BAUDRATE_ 115200 100 | #define _SERIAL_ Serial3 101 | #define _MODEL_ "STM32 F4DISC1 ST Core" 102 | 103 | //for STM32F407(ST Core) 104 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 105 | HardwareSerial Serial2(PA3, PA2); 106 | #define _BAUDRATE_ 115200 107 | #define _SERIAL_ Serial2 108 | #define _MODEL_ "STM32F407 ST Core" 109 | 110 | //for STM32 NUCLEO64(ST Core) 111 | #else 112 | HardwareSerial Serial1(PA10, PA9); 113 | #define _BAUDRATE_ 115200 114 | #define _SERIAL_ Serial1 115 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 116 | #endif 117 | 118 | #define LOCAL_IP "192.168.10.21" // My IP Address 119 | #define LOCAL_PORT 8080 // Listen Port 120 | 121 | void setup(void) 122 | { 123 | Serial.begin(115200); 124 | delay(5000); 125 | Serial.print("_MODEL_="); 126 | Serial.println(_MODEL_); 127 | _SERIAL_.begin(_BAUDRATE_); 128 | 129 | //Save Serial Object 130 | serialSetup(_SERIAL_); 131 | 132 | //Enable autoconnect 133 | sendCommand("AT+CWAUTOCONN=1"); 134 | if (!waitForString("OK", 2, 1000)) { 135 | errorDisplay("AT+CWAUTOCONN Fail"); 136 | } 137 | clearBuffer(); 138 | 139 | //Restarts the Module 140 | sendCommand("AT+RST"); 141 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 142 | errorDisplay("ATE+RST Fail"); 143 | } 144 | clearBuffer(); 145 | 146 | //Local echo off 147 | sendCommand("ATE0"); 148 | if (!waitForString("OK", 2, 1000)) { 149 | errorDisplay("ATE0 Fail"); 150 | } 151 | clearBuffer(); 152 | 153 | //Set IP address of Station 154 | char cmd[64]; 155 | sprintf(cmd, "AT+CIPSTA_CUR=\"%s\"", LOCAL_IP); 156 | sendCommand(cmd); 157 | if (!waitForString("OK", 2, 1000)) { 158 | errorDisplay("AT+CIPSTA_CUR fail"); 159 | } 160 | clearBuffer(); 161 | 162 | //Get My IP Address 163 | char IPaddress[64]; 164 | getIpAddress(IPaddress,sizeof(IPaddress),2000); 165 | Serial.print("IP Address: "); 166 | Serial.println(IPaddress); 167 | 168 | //Enable multi connections 169 | sendCommand("AT+CIPMUX=1"); 170 | if (!waitForString("OK", 2, 1000)) { 171 | errorDisplay("AT+CIPMUX Fail"); 172 | } 173 | clearBuffer(); 174 | 175 | //Configure as TCP server 176 | //AT+CIPSERVER=[,][,<"type">][,] 177 | sprintf(cmd, "AT+CIPSERVER=1,%d", LOCAL_PORT); 178 | sendCommand(cmd); 179 | if (!waitForString("OK", 2, 1000)) { 180 | errorDisplay("AT+CIPSERVER Fail"); 181 | } 182 | clearBuffer(); 183 | 184 | Serial.println("Start TCP Server [" + String(_MODEL_) + "] wait for " + String(LOCAL_PORT) + " Port"); 185 | } 186 | 187 | void(* resetFunc) (void) = 0; //declare reset function @ address 0 188 | 189 | void loop(void) { 190 | char smsg[64]; 191 | char rmsg[64]; 192 | int rlen; 193 | int id; 194 | 195 | //Wait from client connection 196 | id = waitConnect(1, 10000); 197 | if (_DEBUG_) { 198 | Serial.print("Connect id="); 199 | Serial.println(id); 200 | } 201 | 202 | if (id < 0) { 203 | Serial.println("waitConnect Fail"); 204 | delay(1000); 205 | resetFunc(); // call reset 206 | } 207 | 208 | if (id >= 0) { 209 | //Receive data 210 | rlen = readResponse(id, rmsg, sizeof(rmsg), 5000); 211 | clearBuffer(); 212 | 213 | memset (smsg,0,sizeof(smsg)); 214 | for (int i=0; i< rlen; i++) { 215 | if(isalpha(rmsg[i])) { 216 | smsg[i] = toupper(rmsg[i]); 217 | } else { 218 | smsg[i] = rmsg[i]; 219 | } 220 | } 221 | Serial.write((uint8_t *)rmsg,rlen); 222 | Serial.print("----->"); 223 | Serial.write((uint8_t *)smsg,rlen); 224 | Serial.println(); 225 | 226 | //Send response 227 | int ret = sendData(id, smsg, rlen, "", 0); 228 | if (ret) { 229 | Serial.println("sendData Fail"); 230 | delay(1000); 231 | resetFunc(); // call reset 232 | } 233 | 234 | //Wait from client disconnection 235 | while(1) { 236 | id = waitConnect(2, 10000); 237 | if (_DEBUG_) { 238 | Serial.print("Close id="); 239 | Serial.println(id); 240 | } 241 | if (id >= 0) break; 242 | } 243 | 244 | } 245 | } 246 | -------------------------------------------------------------------------------- /SNTP_Client_ESP01/SNTP_Client_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * SNTP Client using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | #include "espLib.h" 52 | 53 | //for Arduino UNO(ATmega328) 54 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 55 | #include 56 | #define SERIAL_RX 4 57 | #define SERIAL_TX 5 58 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, TX 59 | #define _BAUDRATE_ 4800 60 | #define _SERIAL_ Serial2 61 | #define _MODEL_ "ATmega328" 62 | 63 | //for Arduino MEGA(ATmega2560) 64 | #elif defined(__AVR_ATmega2560__) 65 | #define _BAUDRATE_ 115200 66 | #define _SERIAL_ Serial1 67 | #define _MODEL_ "ATmega2560" 68 | 69 | //for STM32F103(MAPLE Core) 70 | #elif defined(__STM32F1__) 71 | #define _BAUDRATE_ 115200 72 | #define _SERIAL_ Serial2 73 | #define _MODEL_ "STM32F103 MAPLE Core" 74 | 75 | //for STM32F103(ST Core) 76 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 77 | HardwareSerial Serial2(PA3, PA2); 78 | #define _BAUDRATE_ 115200 79 | #define _SERIAL_ Serial2 80 | #define _MODEL_ "STM32F103 ST Core" 81 | 82 | //for STM32F303(ST Core) 83 | #elif defined(ARDUINO_BLACKPILL_F303CC) 84 | HardwareSerial Serial2(PA3, PA2); 85 | #define _BAUDRATE_ 115200 86 | #define _SERIAL_ Serial2 87 | #define _MODEL_ "STM32F303 ST Core" 88 | 89 | //for STM32F401(ST Core) 90 | #elif defined(ARDUINO_BLACKPILL_F401CC) 91 | HardwareSerial Serial2(PA3, PA2); 92 | #define _BAUDRATE_ 115200 93 | #define _SERIAL_ Serial2 94 | #define _MODEL_ "STM32F401 ST Core" 95 | 96 | //for STM32F4DISC1(ST Core) 97 | #elif defined(ARDUINO_DISCO_F407VG) 98 | HardwareSerial Serial3(PD9, PD8); 99 | #define _BAUDRATE_ 115200 100 | #define _SERIAL_ Serial3 101 | #define _MODEL_ "STM32 F4DISC1 ST Core" 102 | 103 | //for STM32F407(ST Core) 104 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 105 | HardwareSerial Serial2(PA3, PA2); 106 | #define _BAUDRATE_ 115200 107 | #define _SERIAL_ Serial2 108 | #define _MODEL_ "STM32F407 ST Core" 109 | 110 | //for STM32 NUCLEO64(ST Core)#else 111 | #else 112 | HardwareSerial Serial1(PA10, PA9); 113 | #define _BAUDRATE_ 115200 114 | #define _SERIAL_ Serial1 115 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 116 | #endif 117 | 118 | #define INTERVAL 10 // Interval of Packet Send(Second) 119 | #define SNTP_SERVER "time.google.com" // SNTP Server 120 | #define TIME_ZONE 9 // Time Difference from GMT 121 | #define DNS_SERVER1 "8.8.8.8" // DNS SERVER1 122 | #define DNS_SERVER2 "8.8.4.4" // DNS SERVER2 123 | 124 | 125 | // Last Packet Send Time (MilliSecond) 126 | unsigned long lastSendPacketTime = 0; 127 | 128 | void setup(){ 129 | Serial.begin(115200); 130 | delay(5000); 131 | Serial.print("_MODEL_="); 132 | Serial.println(_MODEL_); 133 | _SERIAL_.begin(_BAUDRATE_); 134 | 135 | //Save Serial Object 136 | serialSetup(_SERIAL_); 137 | 138 | //Enable autoconnect 139 | sendCommand("AT+CWAUTOCONN=1"); 140 | if (!waitForString("OK", 2, 1000)) { 141 | errorDisplay("AT+CWAUTOCONN Fail"); 142 | } 143 | clearBuffer(); 144 | 145 | //Restarts the Module 146 | sendCommand("AT+RST"); 147 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 148 | errorDisplay("ATE+RST Fail"); 149 | } 150 | clearBuffer(); 151 | 152 | //Local echo off 153 | sendCommand("ATE0"); 154 | if (!waitForString("OK", 2, 1000)) { 155 | errorDisplay("ATE0 Fail"); 156 | } 157 | clearBuffer(); 158 | 159 | //Get My IP Address 160 | char IPaddress[32]; 161 | getIpAddress(IPaddress,sizeof(IPaddress),2000); 162 | Serial.print("IPaddress=["); 163 | Serial.print(IPaddress); 164 | Serial.println("]"); 165 | 166 | //Get My MAC Address 167 | char MACaddress[32]; 168 | getMacAddress(MACaddress,sizeof(MACaddress),2000); 169 | Serial.print("MACaddress=["); 170 | Serial.print(MACaddress); 171 | Serial.println("]"); 172 | 173 | //Get DNS Server Information 174 | sendCommand("AT+CIPDNS_CUR?"); 175 | if (!waitForString("OK", 2, 1000, true)) { 176 | errorDisplay("AT+CIPDNS_CUR Fail"); 177 | } 178 | clearBuffer(); 179 | 180 | //Set DNS Server Information 181 | //AT+CIPDNS_CUR=[,,] 182 | char cmd[128]; 183 | sprintf(cmd,"AT+CIPDNS_CUR=1,\"%s\",\"%s\"", DNS_SERVER1, DNS_SERVER2); 184 | sendCommand(cmd); 185 | if (!waitForString("OK", 2, 1000)) { 186 | errorDisplay("AT+CIPDNS_CUR Fail"); 187 | } 188 | clearBuffer(); 189 | 190 | //Get DNS Server Information again 191 | sendCommand("AT+CIPDNS_CUR?"); 192 | if (!waitForString("OK", 2, 1000, true)) { 193 | errorDisplay("AT+CIPDNS_CUR Fail"); 194 | } 195 | clearBuffer(); 196 | 197 | //Sets the Configuration of SNTP 198 | #ifdef SNTP_SERVER 199 | sprintf(cmd,"AT+CIPSNTPCFG=1,%d,\"%s\"",TIME_ZONE,SNTP_SERVER); 200 | #else 201 | sprintf(cmd,"AT+CIPSNTPCFG=1,%d",TIME_ZONE); 202 | #endif 203 | sendCommand(cmd); 204 | if (!waitForString("OK", 2, 5000)) { 205 | errorDisplay("AT+CIPSNTPCFG Fail"); 206 | } 207 | clearBuffer(); 208 | 209 | Serial.println("Start SNTP Client [" + String(_MODEL_) + "] using " + String(SNTP_SERVER)); 210 | lastSendPacketTime = millis(); 211 | } 212 | 213 | void loop(){ 214 | static int counter=0; 215 | char buf[128]; 216 | 217 | long now = millis(); 218 | if (now - lastSendPacketTime > 1000) { // One second has elapsed 219 | lastSendPacketTime = now; 220 | counter++; 221 | if ( (counter % 10) == 0) { 222 | Serial.print("+"); 223 | } else { 224 | Serial.print("."); 225 | } 226 | if (counter == INTERVAL) { 227 | if (getSNTPtime(buf, 128, 5000)) { 228 | Serial.println(); 229 | Serial.println("SNTP response is [" + String(buf) + "]"); 230 | } 231 | counter = 0; 232 | } 233 | 234 | } // endif 235 | 236 | } 237 | -------------------------------------------------------------------------------- /UDP_Broadcast_Client_ESP01/UDP_Broadcast_Client_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * UDP Broadcast Client using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32F103 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32F103 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32F103 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32F103 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | #include "espLib.h" 52 | 53 | //for Arduino UNO(ATmega328) 54 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 55 | #include 56 | #define SERIAL_RX 4 57 | #define SERIAL_TX 5 58 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, TX 59 | #define _BAUDRATE_ 4800 60 | #define _SERIAL_ Serial2 61 | #define _MODEL_ "ATmega328" 62 | 63 | //for Arduino MEGA(ATmega2560) 64 | #elif defined(__AVR_ATmega2560__) 65 | #define _BAUDRATE_ 115200 66 | #define _SERIAL_ Serial1 67 | #define _MODEL_ "ATmega2560" 68 | 69 | //for STM32F103(MAPLE Core) 70 | #elif defined(__STM32F1__) 71 | #define _BAUDRATE_ 115200 72 | #define _SERIAL_ Serial2 73 | #define _MODEL_ "STM32F103 MAPLE Core" 74 | 75 | //for STM32F103(ST Core) 76 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 77 | HardwareSerial Serial2(PA3, PA2); 78 | #define _BAUDRATE_ 115200 79 | #define _SERIAL_ Serial2 80 | #define _MODEL_ "STM32F103 ST Core" 81 | 82 | //for STM32F303(ST Core) 83 | #elif defined(ARDUINO_BLACKPILL_F303CC) 84 | HardwareSerial Serial2(PA3, PA2); 85 | #define _BAUDRATE_ 115200 86 | #define _SERIAL_ Serial2 87 | #define _MODEL_ "STM32F303 ST Core" 88 | 89 | //for STM32F401(ST Core) 90 | #elif defined(ARDUINO_BLACKPILL_F401CC) 91 | HardwareSerial Serial2(PA3, PA2); 92 | #define _BAUDRATE_ 115200 93 | #define _SERIAL_ Serial2 94 | #define _MODEL_ "STM32F401 ST Core" 95 | 96 | //for STM32F4DISC1(ST Core) 97 | #elif defined(ARDUINO_DISCO_F407VG) 98 | HardwareSerial Serial3(PD9, PD8); 99 | #define _BAUDRATE_ 115200 100 | #define _SERIAL_ Serial3 101 | #define _MODEL_ "STM32 F4DISC1 ST Core" 102 | 103 | //for STM32F407(ST Core) 104 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 105 | HardwareSerial Serial2(PA3, PA2); 106 | #define _BAUDRATE_ 115200 107 | #define _SERIAL_ Serial2 108 | #define _MODEL_ "STM32F407 ST Core" 109 | 110 | //for STM32 NUCLEO64(ST Core) 111 | #else 112 | HardwareSerial Serial1(PA10, PA9); 113 | #define _BAUDRATE_ 115200 114 | #define _SERIAL_ Serial1 115 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 116 | #endif 117 | 118 | #define REMOTE_HOST "255.255.255.255" // Remote Host 119 | #define REMOTE_PORT 8080 // Remote Port 120 | #define INTERVAL 1000 // Interval of Packet Send(MillSecond) 121 | 122 | // Last Packet Send Time (MilliSecond) 123 | long lastSendPacketTime; 124 | 125 | void setup(void) 126 | { 127 | Serial.begin(115200); 128 | delay(5000); 129 | Serial.print("_MODEL_="); 130 | Serial.println(_MODEL_); 131 | _SERIAL_.begin(_BAUDRATE_); 132 | 133 | //Save Serial Object 134 | serialSetup(_SERIAL_); 135 | 136 | //Enable autoconnect 137 | sendCommand("AT+CWAUTOCONN=1"); 138 | if (!waitForString("OK", 2, 1000)) { 139 | errorDisplay("AT+CWAUTOCONN Fail"); 140 | } 141 | clearBuffer(); 142 | 143 | //Restarts the Module 144 | sendCommand("AT+RST"); 145 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 146 | errorDisplay("ATE+RST Fail"); 147 | } 148 | clearBuffer(); 149 | 150 | //Local echo off 151 | sendCommand("ATE0"); 152 | if (!waitForString("OK", 2, 1000)) { 153 | errorDisplay("ATE0 Fail"); 154 | } 155 | clearBuffer(); 156 | 157 | //Get My IP Address 158 | char IPaddress[64]; 159 | getIpAddress(IPaddress,sizeof(IPaddress),2000); 160 | Serial.print("IPaddress=["); 161 | Serial.print(IPaddress); 162 | Serial.println("]"); 163 | 164 | //Get My MAC Address 165 | char MACaddress[64]; 166 | getMacAddress(MACaddress,sizeof(MACaddress),2000); 167 | Serial.print("MACaddress=["); 168 | Serial.print(MACaddress); 169 | Serial.println("]"); 170 | 171 | //Establish UDP Transmission (Single connection) 172 | //AT+CIPSTART=,, 173 | char cmd[64]; 174 | sprintf(cmd,"AT+CIPSTART=\"UDP\",\"%s\",%u", REMOTE_HOST, REMOTE_PORT); 175 | sendCommand(cmd); 176 | if (!waitForString("OK", 2, 1000)) { 177 | errorDisplay("AT+CIPSTART Fail"); 178 | } 179 | clearBuffer(); 180 | 181 | Serial.println("Start UDP Broadcast Client [" + String(_MODEL_) + "] via ESP8266"); 182 | lastSendPacketTime = millis(); 183 | } 184 | 185 | void loop(void) { 186 | static int num = 0; 187 | char smsg[64]; 188 | char rmsg[64]; 189 | 190 | //If there is some input, a program is ended. 191 | if (Serial.available() > 0) { 192 | char inChar = Serial.read(); 193 | Serial.println("KeyIn"); 194 | //Close UDP connection 195 | sendCommand("AT+CIPCLOSE"); 196 | if (!waitForString("OK", 2, 1000)) { 197 | errorDisplay("AT+CIPCLOSE Fail"); 198 | } 199 | clearBuffer(); 200 | //Disconnect from an AP 201 | sendCommand("AT+CWQAP"); 202 | if (!waitForString("OK", 2, 1000)) { 203 | errorDisplay("AT+CWQAP Fail"); 204 | } 205 | clearBuffer(); 206 | Serial.println("client end"); 207 | while (1) { } 208 | } 209 | 210 | long now = millis(); 211 | if (( now - lastSendPacketTime) > 0) { 212 | lastSendPacketTime = now + INTERVAL; 213 | int sz_smsg = sprintf(smsg, "data from %s %05d", _MODEL_, num); 214 | Serial.write((uint8_t *)smsg, sz_smsg); 215 | num++; 216 | 217 | //Send Data 218 | int ret = sendData(-1, smsg, sz_smsg, "", 0); 219 | if (ret) { 220 | errorDisplay("sendData Fail"); 221 | } 222 | 223 | //Read Response 224 | int readLen = readResponse(-1, rmsg, sizeof(rmsg), 5000); 225 | if (_DEBUG_) { 226 | Serial.println(); 227 | Serial.print("readLen="); 228 | Serial.println(readLen); 229 | } 230 | if (readLen < 0) { 231 | errorDisplay("Server not response"); 232 | } 233 | Serial.print("---->"); 234 | Serial.write((uint8_t *)rmsg, readLen); 235 | Serial.println(); 236 | clearBuffer(); 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /UDP_Broadcast_Server_ESP01/UDP_Broadcast_Server_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * UDP Broadcast Client using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32F103 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32F103 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32F103 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32F103 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | #include "espLib.h" 52 | 53 | //for Arduino UNO(ATmega328) 54 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 55 | #include 56 | #define SERIAL_RX 4 57 | #define SERIAL_TX 5 58 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, TX 59 | #define _BAUDRATE_ 4800 60 | #define _SERIAL_ Serial2 61 | #define _MODEL_ "ATmega328" 62 | 63 | //for Arduino MEGA(ATmega2560) 64 | #elif defined(__AVR_ATmega2560__) 65 | #define _BAUDRATE_ 115200 66 | #define _SERIAL_ Serial1 67 | #define _MODEL_ "ATmega2560" 68 | 69 | //for STM32F103(MAPLE Core) 70 | #elif defined(__STM32F1__) 71 | #define _BAUDRATE_ 115200 72 | #define _SERIAL_ Serial2 73 | #define _MODEL_ "STM32F103 MAPLE Core" 74 | 75 | //for STM32F103(ST Core) 76 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 77 | HardwareSerial Serial2(PA3, PA2); 78 | #define _BAUDRATE_ 115200 79 | #define _SERIAL_ Serial2 80 | #define _MODEL_ "STM32F103 ST Core" 81 | 82 | //for STM32F303(ST Core) 83 | #elif defined(ARDUINO_BLACKPILL_F303CC) 84 | HardwareSerial Serial2(PA3, PA2); 85 | #define _BAUDRATE_ 115200 86 | #define _SERIAL_ Serial2 87 | #define _MODEL_ "STM32F303 ST Core" 88 | 89 | //for STM32F401(ST Core) 90 | #elif defined(ARDUINO_BLACKPILL_F401CC) 91 | HardwareSerial Serial2(PA3, PA2); 92 | #define _BAUDRATE_ 115200 93 | #define _SERIAL_ Serial2 94 | #define _MODEL_ "STM32F401 ST Core" 95 | 96 | //for STM32F4DISC1(ST Core) 97 | #elif defined(ARDUINO_DISCO_F407VG) 98 | HardwareSerial Serial3(PD9, PD8); 99 | #define _BAUDRATE_ 115200 100 | #define _SERIAL_ Serial3 101 | #define _MODEL_ "STM32 F4DISC1 ST Core" 102 | 103 | //for STM32F407(ST Core) 104 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 105 | HardwareSerial Serial2(PA3, PA2); 106 | #define _BAUDRATE_ 115200 107 | #define _SERIAL_ Serial2 108 | #define _MODEL_ "STM32F407 ST Core" 109 | 110 | //for STM32 NUCLEO64(ST Core) 111 | #else 112 | HardwareSerial Serial1(PA10, PA9); 113 | #define _BAUDRATE_ 115200 114 | #define _SERIAL_ Serial1 115 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 116 | #endif 117 | 118 | #define REMOTE_HOST "255.255.255.255" // Remote Host 119 | #define REMOTE_PORT 28080 // Remote Port 120 | #define LOCAL_PORT 8080 // Local Port 121 | #define INTERVAL 1000 // Interval of Packet Send(MillSecond) 122 | 123 | // Last Packet Send Time (MilliSecond) 124 | long lastSendPacketTime; 125 | 126 | void setup(void) 127 | { 128 | Serial.begin(115200); 129 | delay(5000); 130 | Serial.print("_MODEL_="); 131 | Serial.println(_MODEL_); 132 | _SERIAL_.begin(_BAUDRATE_); 133 | 134 | //Save Serial Object 135 | serialSetup(_SERIAL_); 136 | 137 | //Enable autoconnect 138 | sendCommand("AT+CWAUTOCONN=1"); 139 | if (!waitForString("OK", 2, 1000)) { 140 | errorDisplay("AT+CWAUTOCONN Fail"); 141 | } 142 | clearBuffer(); 143 | 144 | //Restarts the Module 145 | sendCommand("AT+RST"); 146 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 147 | errorDisplay("ATE+RST Fail"); 148 | } 149 | clearBuffer(); 150 | 151 | //Local echo off 152 | sendCommand("ATE0"); 153 | if (!waitForString("OK", 2, 1000)) { 154 | errorDisplay("ATE0 Fail"); 155 | } 156 | clearBuffer(); 157 | 158 | //Get My IP Address 159 | char IPaddress[64]; 160 | getIpAddress(IPaddress,sizeof(IPaddress),2000); 161 | Serial.print("IPaddress=["); 162 | Serial.print(IPaddress); 163 | Serial.println("]"); 164 | 165 | //Get My MAC Address 166 | char MACaddress[64]; 167 | getMacAddress(MACaddress,sizeof(MACaddress),2000); 168 | Serial.print("MACaddress=["); 169 | Serial.print(MACaddress); 170 | Serial.println("]"); 171 | 172 | //Establish UDP Transmission (Single connection) 173 | //AT+CIPSTART=,,,, 174 | //mode=2 175 | //Each time UDP data is received, the <"remote host"> and will be changed to the IP address and port of the device that sends the data. 176 | char cmd[64]; 177 | sprintf(cmd,"AT+CIPSTART=\"UDP\",\"%s\",%u,%u,2", REMOTE_HOST, REMOTE_PORT, LOCAL_PORT); 178 | sendCommand(cmd); 179 | if (!waitForString("OK", 2, 1000)) { 180 | errorDisplay("AT+CIPSTART Fail"); 181 | } 182 | clearBuffer(); 183 | 184 | Serial.println("Start UDP Server [" + String(_MODEL_) + "] wait for " + String(LOCAL_PORT) + " Port"); 185 | } 186 | 187 | void loop(void) { 188 | static int num = 0; 189 | char smsg[64]; 190 | char rmsg[64]; 191 | char stat[64]; 192 | 193 | //If there is some input, a program is ended. 194 | if (Serial.available() > 0) { 195 | char inChar = Serial.read(); 196 | Serial.println("KeyIn"); 197 | //Close UDP connection 198 | sendCommand("AT+CIPCLOSE"); 199 | if (!waitForString("OK", 2, 1000)) { 200 | errorDisplay("AT+CIPCLOSE Fail"); 201 | } 202 | clearBuffer(); 203 | //Disconnect from an AP 204 | sendCommand("AT+CWQAP"); 205 | if (!waitForString("OK", 2, 1000)) { 206 | errorDisplay("AT+CWQAP Fail"); 207 | } 208 | clearBuffer(); 209 | Serial.println("client end"); 210 | while (1) { } 211 | } 212 | 213 | //Read packet 214 | int rlen = readData(rmsg, sizeof(rmsg), 5000); 215 | if (_DEBUG_) { 216 | Serial.println(); 217 | Serial.print("rlen="); 218 | Serial.println(rlen); 219 | } 220 | 221 | if (rlen > 0) { 222 | memset (smsg,0,sizeof(smsg)); 223 | for (int i=0; i< rlen; i++) { 224 | if(isalpha(rmsg[i])) { 225 | smsg[i] = toupper(rmsg[i]); 226 | } else { 227 | smsg[i] = rmsg[i]; 228 | } 229 | } 230 | Serial.write((uint8_t *)rmsg,rlen); 231 | Serial.print("----->"); 232 | Serial.write((uint8_t *)smsg,rlen); 233 | Serial.println(); 234 | 235 | //Send Data 236 | int ret = sendData(-1, smsg, rlen, "", 0); 237 | if (ret) { 238 | errorDisplay("sendData Fail"); 239 | } 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /TCP_Client_ESP01/TCP_Client_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Simple TCP/IP Client using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | 52 | #include "espLib.h" 53 | 54 | //for Arduino UNO(ATmega328) 55 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 56 | #include 57 | #define SERIAL_RX 4 58 | #define SERIAL_TX 5 59 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, 60 | #define _BAUDRATE_ 4800 61 | #define _SERIAL_ Serial2 62 | #define _MODEL_ "ATmega328" 63 | 64 | //for Arduino MEGA(ATmega2560) 65 | #elif defined(__AVR_ATmega2560__) 66 | #define _BAUDRATE_ 115200 67 | #define _SERIAL_ Serial1 68 | #define _MODEL_ "ATmega2560" 69 | 70 | //for STM32F103(MAPLE Core) 71 | #elif defined(__STM32F1__) 72 | #define _BAUDRATE_ 115200 73 | #define _SERIAL_ Serial2 74 | #define _MODEL_ "STM32F103 MAPLE Core" 75 | 76 | //for STM32F103(ST Core) 77 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 78 | HardwareSerial Serial2(PA3, PA2); 79 | #define _BAUDRATE_ 115200 80 | #define _SERIAL_ Serial2 81 | #define _MODEL_ "STM32F103 ST Core" 82 | 83 | //for STM32F303(ST Core) 84 | #elif defined(ARDUINO_BLACKPILL_F303CC) 85 | HardwareSerial Serial2(PA3, PA2); 86 | #define _BAUDRATE_ 115200 87 | #define _SERIAL_ Serial2 88 | #define _MODEL_ "STM32F303 ST Core" 89 | 90 | //for STM32F401(ST Core) 91 | #elif defined(ARDUINO_BLACKPILL_F401CC) 92 | HardwareSerial Serial2(PA3, PA2); 93 | #define _BAUDRATE_ 115200 94 | #define _SERIAL_ Serial2 95 | #define _MODEL_ "STM32F401 ST Core" 96 | 97 | //for STM32F4DISC1(ST Core) 98 | #elif defined(ARDUINO_DISCO_F407VG) 99 | HardwareSerial Serial3(PD9, PD8); 100 | #define _BAUDRATE_ 115200 101 | #define _SERIAL_ Serial3 102 | #define _MODEL_ "STM32 F4DISC1 ST Core" 103 | 104 | //for STM32F407(ST Core) 105 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 106 | HardwareSerial Serial2(PA3, PA2); 107 | #define _BAUDRATE_ 115200 108 | #define _SERIAL_ Serial2 109 | #define _MODEL_ "STM32F407 ST Core" 110 | 111 | //for STM32 NUCLEO64(ST Core) 112 | #else 113 | HardwareSerial Serial1(PA10, PA9); 114 | #define _BAUDRATE_ 115200 115 | #define _SERIAL_ Serial1 116 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 117 | #endif 118 | 119 | #define REMOTE_HOST "192.168.10.46" // Remote Host 120 | #define REMOTE_PORT 8080 // Remote Port 121 | #define INTERVAL 1000 // Interval of Packet Send(MillSecond) 122 | 123 | // Last Packet Send Time (MilliSecond) 124 | long lastSendPacketTime; 125 | 126 | void setup(void) 127 | { 128 | Serial.begin(115200); 129 | delay(5000); 130 | Serial.print("_MODEL_="); 131 | Serial.println(_MODEL_); 132 | _SERIAL_.begin(_BAUDRATE_); 133 | 134 | //Save Serial Object 135 | serialSetup(_SERIAL_); 136 | 137 | //Enable autoconnect 138 | sendCommand("AT+CWAUTOCONN=1"); 139 | if (!waitForString("OK", 2, 1000)) { 140 | errorDisplay("AT+CWAUTOCONN Fail"); 141 | } 142 | clearBuffer(); 143 | 144 | //Restarts the Module 145 | sendCommand("AT+RST"); 146 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 147 | errorDisplay("ATE+RST Fail"); 148 | } 149 | clearBuffer(); 150 | 151 | //Local echo off 152 | sendCommand("ATE0"); 153 | if (!waitForString("OK", 2, 1000)) { 154 | errorDisplay("ATE0 Fail"); 155 | } 156 | clearBuffer(); 157 | 158 | //Get My IP Address 159 | char IPaddress[64]; 160 | getIpAddress(IPaddress, sizeof(IPaddress), 2000); 161 | Serial.print("IPaddress=["); 162 | Serial.print(IPaddress); 163 | Serial.println("]"); 164 | 165 | //Get My MAC Address 166 | char MACaddress[64]; 167 | getMacAddress(MACaddress, sizeof(MACaddress), 2000); 168 | Serial.print("MACaddress=["); 169 | Serial.print(MACaddress); 170 | Serial.println("]"); 171 | 172 | Serial.println("Start TCP/IP Client [" + String(_MODEL_) + "] to " + String(REMOTE_HOST) + "/" + String(REMOTE_PORT) ); 173 | lastSendPacketTime = millis(); 174 | } 175 | 176 | void loop(void) { 177 | static int num = 0; 178 | char cmd[64]; 179 | char smsg[64]; 180 | char rmsg[64]; 181 | 182 | //If there is some input, a program is ended. 183 | if (Serial.available() > 0) { 184 | char inChar = Serial.read(); 185 | Serial.println("KeyIn"); 186 | //Disconnect from an AP 187 | sendCommand("AT+CWQAP"); 188 | if (!waitForString("OK", 2, 1000)) { 189 | errorDisplay("AT+CWQAP Fail"); 190 | } 191 | clearBuffer(); 192 | Serial.println("client end"); 193 | while (1) { } 194 | } 195 | 196 | long now = millis(); 197 | if (( now - lastSendPacketTime) > 0) { 198 | lastSendPacketTime = now + INTERVAL; 199 | //Start connection 200 | //AT+CIPSTART=,, 201 | sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%u", REMOTE_HOST, REMOTE_PORT); 202 | sendCommand(cmd); 203 | if (!waitForString("OK", 2, 10000)) { 204 | Serial.println("Check Server IP Address and Server Port"); 205 | Serial.print("Server IP:"); 206 | Serial.println(REMOTE_HOST); 207 | Serial.print("Server Port:"); 208 | Serial.println(REMOTE_PORT); 209 | errorDisplay("AT+CIPSTART Fail"); 210 | } 211 | clearBuffer(); 212 | 213 | int sz_smsg = sprintf(smsg, "data from %s %05d", _MODEL_, num); 214 | //int sz_smsg = strlen(smsg); 215 | Serial.write((uint8_t *)smsg, sz_smsg); 216 | num++; 217 | 218 | //Send Data 219 | int ret = sendData(-1, smsg, sz_smsg, "", 0); 220 | if (ret) { 221 | errorDisplay("sendData Fail"); 222 | } 223 | 224 | //Read Response 225 | int readLen = readResponse(-1, rmsg, sizeof(rmsg), 5000); 226 | if (_DEBUG_) { 227 | Serial.println(); 228 | Serial.print("readLen="); 229 | Serial.println(readLen); 230 | } 231 | if (readLen < 0) { 232 | errorDisplay("Server not response"); 233 | } 234 | Serial.print("---->"); 235 | Serial.write((uint8_t *)rmsg, readLen); 236 | Serial.println(); 237 | clearBuffer(); 238 | 239 | //Close TCP connection 240 | sendCommand("AT+CIPCLOSE"); 241 | if (!waitForString("OK", 2, 1000)) { 242 | errorDisplay("AT+CIPCLOSE Fail"); 243 | } 244 | clearBuffer(); 245 | } 246 | } 247 | -------------------------------------------------------------------------------- /MQTT_Subscribe_ESP01/espLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Socket library using ESP8266 AT Instruction Set 3 | */ 4 | 5 | #define _DEBUG_ 0 6 | 7 | //Serial Object 8 | Stream *espSerial = 0; 9 | 10 | //Save Serial Object 11 | void serialSetup(Stream &port) { 12 | espSerial = &port; 13 | } 14 | 15 | //Wait for specific input string until timeout runs out 16 | bool waitForString(char* input, int length, unsigned int timeout, bool print=false) { 17 | 18 | unsigned long end_time = millis() + timeout; 19 | char current_byte = 0; 20 | int index = 0; 21 | 22 | while (end_time >= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(""); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | //Send Message 68 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 69 | char atcmd[64]; 70 | if (strlen(rmote)) { 71 | if (id >= 0) { 72 | //AT+CIPSEND=,,, 73 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 74 | } else { 75 | //AT+CIPSEND=,, 76 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 77 | } 78 | } else { 79 | if (id >= 0) { 80 | //AT+CIPSEND=, 81 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 82 | } else { 83 | //AT+CIPSEND= 84 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 85 | } 86 | } 87 | sendCommand(atcmd); 88 | if (!waitForString(">", 1, 1000)) return 1; 89 | clearBuffer(); 90 | 91 | //Send Packet 92 | for(int i=0;iwrite(buf[i]); 93 | if (!waitForString("SEND OK", 7, 1000)) return 2; 94 | // clearBuffer(); 95 | return 0; 96 | } 97 | 98 | //Receive Message 99 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 100 | int len=0; 101 | int flag=0; 102 | int datalen; 103 | long int time = millis(); 104 | 105 | // id < 0 +IPD,nn:ReceiveData 106 | // id = 0 +IPD,0,nn:ReceiveData 107 | // id > 0 +IPD,id,nn:ReceiveData 108 | while( (time+timeout) > millis()) { 109 | while(espSerial->available()) { 110 | char current_byte = espSerial->read(); // read the next character. 111 | if (_DEBUG_) { 112 | Serial.print("0x"); 113 | if (current_byte < 0x10) Serial.print("0"); 114 | Serial.print(current_byte,HEX); 115 | Serial.print("["); 116 | if (current_byte < 0x20) { 117 | Serial.print(" "); 118 | } else if (current_byte > 0x7F) { 119 | Serial.print(" "); 120 | } else { 121 | Serial.print(current_byte); 122 | } 123 | Serial.print("]"); 124 | Serial.print(flag); 125 | Serial.print(" "); 126 | Serial.print(datalen); 127 | Serial.print(" "); 128 | Serial.println(len); 129 | } 130 | if (flag == 0) { 131 | if (current_byte != 0x2c) continue; 132 | flag++; 133 | if (id < 0) flag++; 134 | } else if (flag == 1) { 135 | if (current_byte != 0x2c) continue; 136 | flag++; 137 | } else if (flag == 2) { 138 | if (current_byte == 0x3a) { // : 139 | datalen=atoi(buf); 140 | flag++; 141 | len=0; 142 | } else { 143 | buf[len++]=current_byte; 144 | } 145 | } else { 146 | buf[len++]=current_byte; 147 | if (len == datalen) return len; 148 | } 149 | } // end while 150 | } // end while 151 | return -len; 152 | } 153 | 154 | //Get IP Address 155 | int getIpAddress(char *buf, int szbuf, int timeout) { 156 | int len=0; 157 | int pos=0; 158 | char line[128] = {0}; 159 | 160 | long int time = millis(); 161 | 162 | sendCommand("AT+CIPSTA?"); 163 | 164 | while( (time+timeout) > millis()) { 165 | while(espSerial->available()) { 166 | char c = espSerial->read(); // read the next character. 167 | if (c == 0x0d) { 168 | 169 | } else if (c == 0x0a) { 170 | if (_DEBUG_) { 171 | Serial.print("Read=["); 172 | Serial.print(line); 173 | Serial.println("]"); 174 | } 175 | int offset; 176 | for(offset=0;offset millis()) { 207 | while(espSerial->available()) { 208 | char c = espSerial->read(); // read the next character. 209 | if (c == 0x0d) { 210 | 211 | } else if (c == 0x0a) { 212 | if (_DEBUG_) { 213 | Serial.print("Read=["); 214 | Serial.print(line); 215 | Serial.println("]"); 216 | } 217 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 218 | strcpy(buf,&line[12]); 219 | len = strlen(buf) - 1; 220 | buf[len] = 0; 221 | } 222 | if (strcmp(line,"OK") == 0) return len; 223 | pos=0; 224 | line[pos]=0; 225 | } else { 226 | line[pos++]=c; 227 | line[pos]=0; 228 | } 229 | } 230 | } 231 | return len; 232 | } 233 | 234 | void getResponse(int timeout){ 235 | char c; 236 | bool flag = false; 237 | char tmp[10]; 238 | 239 | long int time = millis() + timeout; 240 | while( time > millis()) { 241 | if (espSerial->available()) { 242 | flag = true; 243 | c = espSerial->read(); 244 | if (c == 0x0d) { 245 | 246 | } else if (c == 0x0a) { 247 | if (_DEBUG_) Serial.println(); 248 | } else if ( c < 0x20) { 249 | uint8_t cc = c; 250 | sprintf(tmp,"[0x%.2X]",cc); 251 | if (_DEBUG_) Serial.print(tmp); 252 | } else { 253 | if (_DEBUG_) Serial.print(c); 254 | } 255 | } // end if 256 | } // end while 257 | if (flag & _DEBUG_) Serial.println(); 258 | } 259 | 260 | void hexDump(char *buf, int msize) { 261 | Serial.print("\nmsize="); 262 | Serial.println(msize); 263 | for(int i=0;i= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(""); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | 68 | //Send Message 69 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 70 | char atcmd[64]; 71 | if (strlen(rmote)) { 72 | if (id >= 0) { 73 | //AT+CIPSEND=,,, 74 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 75 | } else { 76 | //AT+CIPSEND=,, 77 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 78 | } 79 | } else { 80 | if (id >= 0) { 81 | //AT+CIPSEND=, 82 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 83 | } else { 84 | //AT+CIPSEND= 85 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 86 | } 87 | } 88 | sendCommand(atcmd); 89 | if (!waitForString(">", 1, 1000)) return 1; 90 | clearBuffer(); 91 | 92 | //Send Packet 93 | for(int i=0;iwrite(buf[i]); 94 | if (!waitForString("SEND OK", 7, 1000)) return 2; 95 | // clearBuffer(); 96 | return 0; 97 | } 98 | 99 | //Receive Message 100 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 101 | int len=0; 102 | int flag=0; 103 | int datalen; 104 | long int time = millis(); 105 | 106 | // id < 0 +IPD,nn:ReceiveData 107 | // id = 0 +IPD,0,nn:ReceiveData 108 | // id > 0 +IPD,id,nn:ReceiveData 109 | while( (time+timeout) > millis()) { 110 | while(espSerial->available()) { 111 | char current_byte = espSerial->read(); // read the next character. 112 | if (_DEBUG_) { 113 | Serial.print("0x"); 114 | if (current_byte < 0x10) Serial.print("0"); 115 | Serial.print(current_byte,HEX); 116 | Serial.print("["); 117 | if (current_byte < 0x20) { 118 | Serial.print(" "); 119 | } else if (current_byte > 0x7F) { 120 | Serial.print(" "); 121 | } else { 122 | Serial.print(current_byte); 123 | } 124 | Serial.print("]"); 125 | Serial.print(flag); 126 | Serial.print(" "); 127 | Serial.print(datalen); 128 | Serial.print(" "); 129 | Serial.println(len); 130 | } 131 | if (flag == 0) { 132 | if (current_byte != 0x2c) continue; 133 | flag++; 134 | if (id < 0) flag++; 135 | } else if (flag == 1) { 136 | if (current_byte != 0x2c) continue; 137 | flag++; 138 | } else if (flag == 2) { 139 | if (current_byte == 0x3a) { // : 140 | datalen=atoi(buf); 141 | flag++; 142 | len=0; 143 | } else { 144 | buf[len++]=current_byte; 145 | } 146 | } else { 147 | buf[len++]=current_byte; 148 | if (len == datalen) return len; 149 | } 150 | } // end while 151 | } // end while 152 | return -len; 153 | } 154 | 155 | //Get IP Address 156 | int getIpAddress(char *buf, int szbuf, int timeout) { 157 | int len=0; 158 | int pos=0; 159 | char line[128] = {0}; 160 | 161 | long int time = millis(); 162 | 163 | sendCommand("AT+CIPSTA?"); 164 | 165 | while( (time+timeout) > millis()) { 166 | while(espSerial->available()) { 167 | char c = espSerial->read(); // read the next character. 168 | if (c == 0x0d) { 169 | 170 | } else if (c == 0x0a) { 171 | if (_DEBUG_) { 172 | Serial.print("Read=["); 173 | Serial.print(line); 174 | Serial.println("]"); 175 | } 176 | int offset; 177 | for(offset=0;offset millis()) { 208 | while(espSerial->available()) { 209 | char c = espSerial->read(); // read the next character. 210 | if (c == 0x0d) { 211 | 212 | } else if (c == 0x0a) { 213 | if (_DEBUG_) { 214 | Serial.print("Read=["); 215 | Serial.print(line); 216 | Serial.println("]"); 217 | } 218 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 219 | strcpy(buf,&line[12]); 220 | len = strlen(buf) - 1; 221 | buf[len] = 0; 222 | } 223 | if (strcmp(line,"OK") == 0) return len; 224 | pos=0; 225 | line[pos]=0; 226 | } else { 227 | line[pos++]=c; 228 | line[pos]=0; 229 | } 230 | } 231 | } 232 | return len; 233 | } 234 | 235 | void getResponse(int timeout){ 236 | char c; 237 | bool flag = false; 238 | char tmp[10]; 239 | 240 | long int time = millis() + timeout; 241 | while( time > millis()) { 242 | if (espSerial->available()) { 243 | flag = true; 244 | c = espSerial->read(); 245 | if (c == 0x0d) { 246 | 247 | } else if (c == 0x0a) { 248 | if (_DEBUG_) Serial.println(); 249 | } else if ( c < 0x20) { 250 | uint8_t cc = c; 251 | sprintf(tmp,"[0x%.2X]",cc); 252 | if (_DEBUG_) Serial.print(tmp); 253 | } else { 254 | if (_DEBUG_) Serial.print(c); 255 | } 256 | } // end if 257 | } // end while 258 | if (flag & _DEBUG_) Serial.println(); 259 | } 260 | 261 | void hexDump(char *buf, int msize) { 262 | Serial.print("\nmsize="); 263 | Serial.println(msize); 264 | for(int i=0;i= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (_DEBUG_) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | return true; 36 | } 37 | //Restart position of character to look for 38 | } else { 39 | index = 0; 40 | } 41 | } 42 | } 43 | } 44 | //Timed out 45 | return false; 46 | } 47 | 48 | //Remove all bytes from the receive buffer 49 | void clearBuffer() { 50 | while (espSerial->available()) 51 | espSerial->read(); 52 | if (_DEBUG_) Serial.println(""); 53 | } 54 | 55 | //Send AT Command 56 | void sendCommand(char* buff) { 57 | if (_DEBUG_) { 58 | Serial.println(""); 59 | Serial.print(buff); 60 | Serial.println("-->"); 61 | } 62 | espSerial->println(buff); 63 | espSerial->flush(); 64 | } 65 | 66 | //Send Message 67 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 68 | char atcmd[64]; 69 | if (strlen(rmote)) { 70 | if (id >= 0) { 71 | //AT+CIPSEND=,,, 72 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 73 | } else { 74 | //AT+CIPSEND=,, 75 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 76 | } 77 | } else { 78 | if (id >= 0) { 79 | //AT+CIPSEND=, 80 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 81 | } else { 82 | //AT+CIPSEND= 83 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 84 | } 85 | } 86 | sendCommand(atcmd); 87 | if (!waitForString(">", 1, 1000)) return 1; 88 | clearBuffer(); 89 | 90 | //Send Packet 91 | for(int i=0;iwrite(buf[i]); 92 | if (!waitForString("SEND OK", 7, 1000)) return 2; 93 | // clearBuffer(); 94 | return 0; 95 | } 96 | 97 | //Receive Message 98 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 99 | int len=0; 100 | int flag=0; 101 | int datalen; 102 | long int time = millis(); 103 | 104 | // id < 0 +IPD,nn:ReceiveData 105 | // id = 0 +IPD,0,nn:ReceiveData 106 | // id > 0 +IPD,id,nn:ReceiveData 107 | while( (time+timeout) > millis()) { 108 | while(espSerial->available()) { 109 | char current_byte = espSerial->read(); // read the next character. 110 | if (_DEBUG_) { 111 | Serial.print("0x"); 112 | if (current_byte < 0x10) Serial.print("0"); 113 | Serial.print(current_byte,HEX); 114 | Serial.print("["); 115 | if (current_byte < 0x20) { 116 | Serial.print(" "); 117 | } else if (current_byte > 0x7F) { 118 | Serial.print(" "); 119 | } else { 120 | Serial.print(current_byte); 121 | } 122 | Serial.print("]"); 123 | Serial.print(flag); 124 | Serial.print(" "); 125 | Serial.print(datalen); 126 | Serial.print(" "); 127 | Serial.println(len); 128 | } 129 | if (flag == 0) { 130 | if (current_byte != 0x2c) continue; 131 | flag++; 132 | if (id < 0) flag++; 133 | } else if (flag == 1) { 134 | if (current_byte != 0x2c) continue; 135 | flag++; 136 | } else if (flag == 2) { 137 | if (current_byte == 0x3a) { // : 138 | datalen=atoi(buf); 139 | flag++; 140 | len=0; 141 | } else { 142 | buf[len++]=current_byte; 143 | } 144 | } else { 145 | buf[len++]=current_byte; 146 | if (len == datalen) return len; 147 | } 148 | } // end while 149 | } // end while 150 | return -len; 151 | } 152 | 153 | //Get IP Address 154 | int getIpAddress(char *buf, int szbuf, int timeout) { 155 | int len=0; 156 | int pos=0; 157 | char line[128] = {0}; 158 | 159 | long int time = millis(); 160 | 161 | sendCommand("AT+CIPSTA?"); 162 | 163 | while( (time+timeout) > millis()) { 164 | while(espSerial->available()) { 165 | char c = espSerial->read(); // read the next character. 166 | if (c == 0x0d) { 167 | 168 | } else if (c == 0x0a) { 169 | if (_DEBUG_) { 170 | Serial.print("Read=["); 171 | Serial.print(line); 172 | Serial.println("]"); 173 | } 174 | int offset; 175 | for(offset=0;offset millis()) { 206 | while(espSerial->available()) { 207 | char c = espSerial->read(); // read the next character. 208 | if (c == 0x0d) { 209 | 210 | } else if (c == 0x0a) { 211 | if (_DEBUG_) { 212 | Serial.print("Read=["); 213 | Serial.print(line); 214 | Serial.println("]"); 215 | } 216 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 217 | strcpy(buf,&line[12]); 218 | len = strlen(buf) - 1; 219 | buf[len] = 0; 220 | } 221 | if (strcmp(line,"OK") == 0) return len; 222 | pos=0; 223 | line[pos]=0; 224 | } else { 225 | line[pos++]=c; 226 | line[pos]=0; 227 | } 228 | } 229 | } 230 | return len; 231 | } 232 | 233 | //Print error 234 | void errorDisplay(char* buff) { 235 | Serial.print("Error:"); 236 | Serial.println(buff); 237 | while(1) {} 238 | } 239 | 240 | void getResponse(int timeout){ 241 | char c; 242 | bool flag = false; 243 | char tmp[10]; 244 | 245 | long int time = millis() + timeout; 246 | while( time > millis()) { 247 | if (espSerial->available()) { 248 | flag = true; 249 | c = espSerial->read(); 250 | if (c == 0x0d) { 251 | 252 | } else if (c == 0x0a) { 253 | if (_DEBUG_) Serial.println(); 254 | } else if ( c < 0x20) { 255 | uint8_t cc = c; 256 | sprintf(tmp,"[0x%.2X]",cc); 257 | if (_DEBUG_) Serial.print(tmp); 258 | } else { 259 | if (_DEBUG_) Serial.print(c); 260 | } 261 | } // end if 262 | } // end while 263 | if (flag & _DEBUG_) Serial.println(); 264 | } 265 | 266 | void hexDump(char *buf, int msize) { 267 | Serial.print("\nmsize="); 268 | Serial.println(msize); 269 | for(int i=0;i", 1, 5000)) { 286 | errorDisplay("Server Not Response"); 287 | } 288 | 289 | if (_DEBUG_) { 290 | // hexDump(buf, msize); 291 | Serial.println(); 292 | Serial.print(">>>>>>"); 293 | Serial.print(buf); 294 | } 295 | for (int i=0;iwrite(buf[i]); 296 | if (!waitForString("SEND OK", 7, 5000)) { 297 | errorDisplay("Server Not Receive my data"); 298 | } 299 | return 1; 300 | } 301 | 302 | -------------------------------------------------------------------------------- /SNTP_Client_ESP01/espLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Socket library using ESP8266 AT Instruction Set 3 | */ 4 | 5 | #define _DEBUG_ 0 6 | 7 | //Serial Object 8 | Stream *espSerial = 0; 9 | 10 | //Save Serial Object 11 | void serialSetup(Stream &port) { 12 | espSerial = &port; 13 | } 14 | 15 | //Wait for specific input string until timeout runs out 16 | bool waitForString(char* input, int length, unsigned int timeout, bool print=false) { 17 | 18 | unsigned long end_time = millis() + timeout; 19 | char current_byte = 0; 20 | int index = 0; 21 | 22 | while (end_time >= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(""); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | //Send Message 68 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 69 | char atcmd[64]; 70 | if (strlen(rmote)) { 71 | if (id >= 0) { 72 | //AT+CIPSEND=,,, 73 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 74 | } else { 75 | //AT+CIPSEND=,, 76 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 77 | } 78 | } else { 79 | if (id >= 0) { 80 | //AT+CIPSEND=, 81 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 82 | } else { 83 | //AT+CIPSEND= 84 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 85 | } 86 | } 87 | sendCommand(atcmd); 88 | if (!waitForString(">", 1, 1000)) return 1; 89 | clearBuffer(); 90 | 91 | //Send Packet 92 | for(int i=0;iwrite(buf[i]); 93 | if (!waitForString("SEND OK", 7, 1000)) return 2; 94 | // clearBuffer(); 95 | return 0; 96 | } 97 | 98 | //Receive Message 99 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 100 | int len=0; 101 | int flag=0; 102 | int datalen; 103 | long int time = millis(); 104 | 105 | // id < 0 +IPD,nn:ReceiveData 106 | // id = 0 +IPD,0,nn:ReceiveData 107 | // id > 0 +IPD,id,nn:ReceiveData 108 | while( (time+timeout) > millis()) { 109 | while(espSerial->available()) { 110 | char current_byte = espSerial->read(); // read the next character. 111 | if (_DEBUG_) { 112 | Serial.print("0x"); 113 | if (current_byte < 0x10) Serial.print("0"); 114 | Serial.print(current_byte,HEX); 115 | Serial.print("["); 116 | if (current_byte < 0x20) { 117 | Serial.print(" "); 118 | } else if (current_byte > 0x7F) { 119 | Serial.print(" "); 120 | } else { 121 | Serial.print(current_byte); 122 | } 123 | Serial.print("]"); 124 | Serial.print(flag); 125 | Serial.print(" "); 126 | Serial.print(datalen); 127 | Serial.print(" "); 128 | Serial.println(len); 129 | } 130 | if (flag == 0) { 131 | if (current_byte != 0x2c) continue; 132 | flag++; 133 | if (id < 0) flag++; 134 | } else if (flag == 1) { 135 | if (current_byte != 0x2c) continue; 136 | flag++; 137 | } else if (flag == 2) { 138 | if (current_byte == 0x3a) { // : 139 | datalen=atoi(buf); 140 | flag++; 141 | len=0; 142 | } else { 143 | buf[len++]=current_byte; 144 | } 145 | } else { 146 | buf[len++]=current_byte; 147 | if (len == datalen) return len; 148 | } 149 | } // end while 150 | } // end while 151 | return -len; 152 | } 153 | 154 | //Get IP Address 155 | int getIpAddress(char *buf, int szbuf, int timeout) { 156 | int len=0; 157 | int pos=0; 158 | char line[128] = {0}; 159 | 160 | long int time = millis(); 161 | 162 | sendCommand("AT+CIPSTA?"); 163 | 164 | while( (time+timeout) > millis()) { 165 | while(espSerial->available()) { 166 | char c = espSerial->read(); // read the next character. 167 | if (c == 0x0d) { 168 | 169 | } else if (c == 0x0a) { 170 | if (_DEBUG_) { 171 | Serial.print("Read=["); 172 | Serial.print(line); 173 | Serial.println("]"); 174 | } 175 | int offset; 176 | for(offset=0;offset millis()) { 207 | while(espSerial->available()) { 208 | char c = espSerial->read(); // read the next character. 209 | if (c == 0x0d) { 210 | 211 | } else if (c == 0x0a) { 212 | if (_DEBUG_) { 213 | Serial.print("Read=["); 214 | Serial.print(line); 215 | Serial.println("]"); 216 | } 217 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 218 | strcpy(buf,&line[12]); 219 | len = strlen(buf) - 1; 220 | buf[len] = 0; 221 | } 222 | if (strcmp(line,"OK") == 0) return len; 223 | pos=0; 224 | line[pos]=0; 225 | } else { 226 | line[pos++]=c; 227 | line[pos]=0; 228 | } 229 | } 230 | } 231 | return len; 232 | } 233 | 234 | //Print error 235 | void errorDisplay(char* buff) { 236 | Serial.print("Error:"); 237 | Serial.println(buff); 238 | while(1) {} 239 | } 240 | 241 | //Wait for CIPSNTPTIME response 242 | int getSNTPtime(char* buf, int szbuf, unsigned int timeout) { 243 | int len=0; 244 | int pos=0; 245 | char line[40]; 246 | 247 | //Get current time 248 | sendCommand("AT+CIPSNTPTIME?"); 249 | long int time = millis(); 250 | 251 | memset(line,0,sizeof(line)); 252 | while( (time+timeout) > millis()) { 253 | while(espSerial->available()) { 254 | char c = espSerial->read(); // read the next character. 255 | if (_DEBUG_) Serial.print(c); 256 | if (c == 0x0d) { 257 | 258 | } else if (c == 0x0a) { 259 | if (_DEBUG_) { 260 | Serial.print("Read=["); 261 | Serial.print(line); 262 | Serial.println("]"); 263 | } 264 | if (strcmp(line,"OK") == 0) return len; 265 | int offset; 266 | for(offset=0;offset millis()) { 295 | while(espSerial->available()) { 296 | char c = espSerial->read(); // read the next character. 297 | if (c == 0x0d) { 298 | 299 | } else if (c == 0x0a) { 300 | if (_DEBUG_) { 301 | Serial.print("Read=["); 302 | Serial.print(line); 303 | Serial.println("]"); 304 | } 305 | if (strcmp(line,"OK") == 0) return len; 306 | if (strncmp(line,"AT version:",11) == 0) { 307 | strcpy(buf,&line[11]); 308 | len = strlen(buf); 309 | buf[len] = 0; 310 | } 311 | pos=0; 312 | line[pos]=0; 313 | } else { 314 | line[pos++]=c; 315 | if (pos == 128) return -1; 316 | line[pos]=0; 317 | } 318 | } 319 | } 320 | return len; 321 | } 322 | -------------------------------------------------------------------------------- /TCP_Client_ESP01/espLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Socket library using ESP8266 AT Instruction Set 3 | */ 4 | 5 | #define _DEBUG_ 0 6 | 7 | //Serial Object 8 | Stream *espSerial = 0; 9 | 10 | //Save Serial Object 11 | void serialSetup(Stream &port) { 12 | espSerial = &port; 13 | } 14 | 15 | //Wait for specific input string until timeout runs out 16 | bool waitForString(char* input, int length, unsigned int timeout, bool print=false) { 17 | 18 | unsigned long end_time = millis() + timeout; 19 | char current_byte = 0; 20 | int index = 0; 21 | 22 | while (end_time >= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(""); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | //Send Message 68 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 69 | char atcmd[64]; 70 | if (strlen(rmote)) { 71 | if (id >= 0) { 72 | //AT+CIPSEND=,,, 73 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 74 | } else { 75 | //AT+CIPSEND=,, 76 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 77 | } 78 | } else { 79 | if (id >= 0) { 80 | //AT+CIPSEND=, 81 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 82 | } else { 83 | //AT+CIPSEND= 84 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 85 | } 86 | } 87 | sendCommand(atcmd); 88 | if (!waitForString(">", 1, 1000)) return 1; 89 | clearBuffer(); 90 | 91 | //Send Packet 92 | for(int i=0;iwrite(buf[i]); 93 | if (!waitForString("SEND OK", 7, 1000)) return 2; 94 | // clearBuffer(); 95 | return 0; 96 | } 97 | 98 | //Receive Message 99 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 100 | int len=0; 101 | int flag=0; 102 | int datalen; 103 | long int time = millis(); 104 | 105 | // id < 0 +IPD,nn:ReceiveData 106 | // id = 0 +IPD,0,nn:ReceiveData 107 | // id > 0 +IPD,id,nn:ReceiveData 108 | while( (time+timeout) > millis()) { 109 | while(espSerial->available()) { 110 | char current_byte = espSerial->read(); // read the next character. 111 | if (_DEBUG_) { 112 | Serial.print("0x"); 113 | if (current_byte < 0x10) Serial.print("0"); 114 | Serial.print(current_byte,HEX); 115 | Serial.print("["); 116 | if (current_byte < 0x20) { 117 | Serial.print(" "); 118 | } else if (current_byte > 0x7F) { 119 | Serial.print(" "); 120 | } else { 121 | Serial.print(current_byte); 122 | } 123 | Serial.print("]"); 124 | Serial.print(flag); 125 | Serial.print(" "); 126 | Serial.print(datalen); 127 | Serial.print(" "); 128 | Serial.println(len); 129 | } 130 | if (flag == 0) { 131 | if (current_byte != 0x2c) continue; 132 | flag++; 133 | if (id < 0) flag++; 134 | } else if (flag == 1) { 135 | if (current_byte != 0x2c) continue; 136 | flag++; 137 | } else if (flag == 2) { 138 | if (current_byte == 0x3a) { // : 139 | datalen=atoi(buf); 140 | flag++; 141 | len=0; 142 | } else { 143 | buf[len++]=current_byte; 144 | buf[len]=0; 145 | } 146 | } else { 147 | buf[len++]=current_byte; 148 | if (len == sz_buf) return -len; 149 | buf[len]=0; 150 | if (len == datalen) return len; 151 | } 152 | } // end while 153 | } // end while 154 | return -len; 155 | } 156 | 157 | //Get IP Address 158 | int getIpAddress(char *buf, int szbuf, int timeout) { 159 | int len=0; 160 | int pos=0; 161 | char line[128] = {0}; 162 | 163 | long int time = millis(); 164 | 165 | sendCommand("AT+CIPSTA?"); 166 | 167 | while( (time+timeout) > millis()) { 168 | while(espSerial->available()) { 169 | char c = espSerial->read(); // read the next character. 170 | if (c == 0x0d) { 171 | 172 | } else if (c == 0x0a) { 173 | if (_DEBUG_) { 174 | Serial.print("Read=["); 175 | Serial.print(line); 176 | Serial.println("]"); 177 | } 178 | int offset; 179 | for(offset=0;offset millis()) { 210 | while(espSerial->available()) { 211 | char c = espSerial->read(); // read the next character. 212 | if (c == 0x0d) { 213 | 214 | } else if (c == 0x0a) { 215 | if (_DEBUG_) { 216 | Serial.print("Read=["); 217 | Serial.print(line); 218 | Serial.println("]"); 219 | } 220 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 221 | strcpy(buf,&line[12]); 222 | len = strlen(buf) - 1; 223 | buf[len] = 0; 224 | } 225 | if (strcmp(line,"OK") == 0) return len; 226 | pos=0; 227 | line[pos]=0; 228 | } else { 229 | line[pos++]=c; 230 | line[pos]=0; 231 | } 232 | } 233 | } 234 | return len; 235 | } 236 | 237 | //Print error 238 | void errorDisplay(char* buff) { 239 | Serial.print("Error:"); 240 | Serial.println(buff); 241 | while(1) {} 242 | } 243 | 244 | //Wait for CIPSNTPTIME response 245 | int getSNTPtime(char* buf, int szbuf, unsigned int timeout) { 246 | int len=0; 247 | int pos=0; 248 | char line[40]; 249 | 250 | //Get current time 251 | sendCommand("AT+CIPSNTPTIME?"); 252 | long int time = millis(); 253 | 254 | memset(line,0,sizeof(line)); 255 | while( (time+timeout) > millis()) { 256 | while(espSerial->available()) { 257 | char c = espSerial->read(); // read the next character. 258 | if (_DEBUG_) Serial.print(c); 259 | if (c == 0x0d) { 260 | 261 | } else if (c == 0x0a) { 262 | if (_DEBUG_) { 263 | Serial.print("Read=["); 264 | Serial.print(line); 265 | Serial.println("]"); 266 | } 267 | if (strcmp(line,"OK") == 0) return len; 268 | int offset; 269 | for(offset=0;offset millis()) { 298 | while(espSerial->available()) { 299 | char c = espSerial->read(); // read the next character. 300 | if (c == 0x0d) { 301 | 302 | } else if (c == 0x0a) { 303 | if (_DEBUG_) { 304 | Serial.print("Read=["); 305 | Serial.print(line); 306 | Serial.println("]"); 307 | } 308 | if (strcmp(line,"OK") == 0) return len; 309 | if (strncmp(line,"AT version:",11) == 0) { 310 | strcpy(buf,&line[11]); 311 | len = strlen(buf); 312 | buf[len] = 0; 313 | } 314 | pos=0; 315 | line[pos]=0; 316 | } else { 317 | line[pos++]=c; 318 | if (pos == 128) return -1; 319 | line[pos]=0; 320 | } 321 | } 322 | } 323 | return len; 324 | } 325 | -------------------------------------------------------------------------------- /SMTP_Client_gmail_ESP01/SMTP_Client_gmail_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * SMTP Client using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | #include "espLib.h" 52 | 53 | //for Arduino UNO(ATmega328) 54 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 55 | #include 56 | #define SERIAL_RX 4 57 | #define SERIAL_TX 5 58 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, 59 | #define _BAUDRATE_ 4800 60 | #define _SERIAL_ Serial2 61 | #define _MODEL_ "ATmega328" 62 | 63 | //for Arduino MEGA(ATmega2560) 64 | #elif defined(__AVR_ATmega2560__) 65 | #define _BAUDRATE_ 115200 66 | #define _SERIAL_ Serial1 67 | #define _MODEL_ "ATmega2560" 68 | 69 | //for STM32F103(MAPLE Core) 70 | #elif defined(__STM32F1__) 71 | #define _BAUDRATE_ 115200 72 | #define _SERIAL_ Serial2 73 | #define _MODEL_ "STM32F103 MAPLE Core" 74 | 75 | //for STM32F103(ST Core) 76 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 77 | HardwareSerial Serial2(PA3, PA2); 78 | #define _BAUDRATE_ 115200 79 | #define _SERIAL_ Serial2 80 | #define _MODEL_ "STM32F103 ST Core" 81 | 82 | //for STM32F303(ST Core) 83 | #elif defined(ARDUINO_BLACKPILL_F303CC) 84 | HardwareSerial Serial2(PA3, PA2); 85 | #define _BAUDRATE_ 115200 86 | #define _SERIAL_ Serial2 87 | #define _MODEL_ "STM32F303 ST Core" 88 | 89 | //for STM32F401(ST Core) 90 | #elif defined(ARDUINO_BLACKPILL_F401CC) 91 | HardwareSerial Serial2(PA3, PA2); 92 | #define _BAUDRATE_ 115200 93 | #define _SERIAL_ Serial2 94 | #define _MODEL_ "STM32F401 ST Core" 95 | 96 | //for STM32F4DISC1(ST Core) 97 | #elif defined(ARDUINO_DISCO_F407VG) 98 | HardwareSerial Serial3(PD9, PD8); 99 | #define _BAUDRATE_ 115200 100 | #define _SERIAL_ Serial3 101 | #define _MODEL_ "STM32 F4DISC1 ST Core" 102 | 103 | //for STM32F407(ST Core) 104 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 105 | HardwareSerial Serial2(PA3, PA2); 106 | #define _BAUDRATE_ 115200 107 | #define _SERIAL_ Serial2 108 | #define _MODEL_ "STM32F407 ST Core" 109 | 110 | //for STM32 NUCLEO64(ST Core) 111 | #else 112 | HardwareSerial Serial1(PA10, PA9); 113 | #define _BAUDRATE_ 115200 114 | #define _SERIAL_ Serial1 115 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 116 | #endif 117 | 118 | 119 | #define SMTP_SERVER "smtp.gmail.com" 120 | #define SMTP_PORT 465 121 | #define BASE64_USER "Base64 Encorded Your Username of gmail" // Username of gmail 122 | #define BASE64_PASS "Base64 Encorded Your Password of gmail" // Password of gmail 123 | #define MAIL_FROM "mailFrom@gmail.com" // Your gmail account 124 | #define MAIL_TO "mailTo@provider.com" // Mail To 125 | #define JAPANESE 0 // Japanese mail contents 126 | 127 | 128 | void setup(){ 129 | char buf[128]; 130 | int msize; 131 | 132 | Serial.begin(115200); 133 | delay(5000); 134 | Serial.print("_MODEL_="); 135 | Serial.println(_MODEL_); 136 | _SERIAL_.begin(_BAUDRATE_); 137 | 138 | //Save Serial Object 139 | serialSetup(_SERIAL_); 140 | 141 | Serial.println("Start SMTP Client [" + String(_MODEL_) + "] to " + String(SMTP_SERVER) ); 142 | 143 | //Enable autoconnect 144 | sendCommand("AT+CWAUTOCONN=1"); 145 | if (!waitForString("OK", 2, 1000)) { 146 | errorDisplay("AT+CWAUTOCONN Fail"); 147 | } 148 | clearBuffer(); 149 | 150 | //Restarts the Module 151 | sendCommand("AT+RST"); 152 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 153 | errorDisplay("ATE+RST Fail"); 154 | } 155 | clearBuffer(); 156 | 157 | //Local echo off 158 | sendCommand("ATE0"); 159 | if (!waitForString("OK", 2, 1000)) { 160 | errorDisplay("ATE0 Fail"); 161 | } 162 | clearBuffer(); 163 | 164 | //Set the size of SSL buffer 165 | sendCommand("AT+CIPSSLSIZE=4096"); 166 | if (!waitForString("OK", 2, 5000)) { 167 | errorDisplay("AT+CIPSSLSIZET Fail"); 168 | } 169 | clearBuffer(); 170 | 171 | //Establishes SSL Connection 172 | Serial.print("Connect " + String(SMTP_SERVER) + "....."); 173 | sprintf(buf,"AT+CIPSTART=\"SSL\",\"%s\",%d\r\n",SMTP_SERVER, SMTP_PORT); 174 | sendCommand(buf); 175 | if (!waitForString("220 smtp.gmail.com", 18, 5000)) { 176 | errorDisplay("AT+CIPSTART Fail"); 177 | } 178 | getResponse(2000); 179 | Serial.println("OK"); 180 | 181 | //Sending EHLO 182 | Serial.print("Send EHLO....."); 183 | sprintf(buf,"EHLO www.example.com\r\n"); 184 | msize = strlen(buf); 185 | sendSMTP(buf, msize); 186 | if (!waitForString("250 SMTPUTF8", 12, 1000)) { 187 | errorDisplay("EHLO Fail"); 188 | } 189 | getResponse(1000); 190 | Serial.println("OK"); 191 | 192 | //Sending AUTH LOGIN 193 | Serial.print("Send AUTH LOGIN....."); 194 | sprintf(buf,"AUTH LOGIN\r\n"); 195 | msize = strlen(buf); 196 | sendSMTP(buf, msize); 197 | if (!waitForString("334", 3, 1000)) { 198 | errorDisplay("AUTH LOGIN Fail"); 199 | } 200 | getResponse(1000); 201 | Serial.println("OK"); 202 | 203 | //Sending User Name of Bas64 Encording 204 | Serial.print("Send USER....."); 205 | sprintf(buf,"%s\r\n",BASE64_USER); 206 | msize = strlen(buf); 207 | sendSMTP(buf, msize); 208 | if (!waitForString("334", 3, 1000)) { 209 | errorDisplay("USER Fail"); 210 | } 211 | getResponse(1000); 212 | Serial.println("OK"); 213 | 214 | //Sending Password of Bas64 Encording 215 | Serial.print("Send PASSWORD....."); 216 | sprintf(buf,"%s\r\n",BASE64_PASS); 217 | msize = strlen(buf); 218 | sendSMTP(buf, msize); 219 | if (!waitForString("235", 3, 1000)) { 220 | errorDisplay("PASSWORD FROM Fail"); 221 | } 222 | getResponse(1000); 223 | Serial.println("OK"); 224 | 225 | //Sending MAIL FROM 226 | Serial.print("Send MAIL FROM....."); 227 | sprintf(buf,"MAIL FROM: <%s>\r\n",MAIL_FROM); 228 | msize = strlen(buf); 229 | sendSMTP(buf, msize); 230 | if (!waitForString("250", 3, 1000)) { 231 | errorDisplay("MAIL FROM Fail"); 232 | } 233 | getResponse(1000); 234 | Serial.println("OK"); 235 | 236 | //Sending RCPT TO 237 | Serial.print("Send RCPT TO....."); 238 | sprintf(buf,"RCPT TO: <%s>\r\n",MAIL_TO); 239 | msize = strlen(buf); 240 | sendSMTP(buf, msize); 241 | if (!waitForString("250", 3, 1000)) { 242 | errorDisplay("RCPT TO Fail"); 243 | } 244 | getResponse(1000); 245 | Serial.println("OK"); 246 | 247 | //Sending DATA 248 | Serial.print("Send DATA....."); 249 | sprintf(buf,"DATA\r\n"); 250 | msize = strlen(buf); 251 | sendSMTP(buf, msize); 252 | if (!waitForString("354", 3, 1000)) { 253 | errorDisplay("DATA Fail"); 254 | } 255 | getResponse(1000); 256 | Serial.println("OK"); 257 | 258 | //Sending From 259 | sprintf(buf,"From: <%s>\r\n",MAIL_FROM); 260 | msize = strlen(buf); 261 | getResponse(1000); 262 | 263 | //Sending To 264 | sprintf(buf,"To: <%s>\r\n",MAIL_TO); 265 | msize = strlen(buf); 266 | sendSMTP(buf, msize); 267 | getResponse(1000); 268 | 269 | //Sending Subject 270 | sprintf(buf,"Subject: Mail From %s\r\n\r\n",_MODEL_); 271 | msize = strlen(buf); 272 | sendSMTP(buf, msize); 273 | getResponse(1000); 274 | 275 | //Sending Message 276 | sprintf(buf,"Hello\r\n"); 277 | msize = strlen(buf); 278 | sendSMTP(buf, msize); 279 | getResponse(1000); 280 | 281 | sprintf(buf,"This mail from %s + ESP-01\r\n",_MODEL_); 282 | msize = strlen(buf); 283 | sendSMTP(buf, msize); 284 | getResponse(1000); 285 | 286 | if (JAPANESE) { 287 | sprintf(buf,"このメールは%s + ESP-01からのメールです\r\n",_MODEL_); 288 | msize = strlen(buf); 289 | sendSMTP(buf, msize); 290 | getResponse(1000); 291 | } 292 | 293 | //Sending . 294 | Serial.print("Send End of Data....."); 295 | sprintf(buf,".\r\n"); 296 | msize = strlen(buf); 297 | sendSMTP(buf, msize); 298 | if (!waitForString("250", 3, 1000)) { 299 | errorDisplay("End of Data Fail"); 300 | } 301 | getResponse(1000); 302 | Serial.println("OK"); 303 | 304 | //Sending QUIT 305 | Serial.print("QUIT....."); 306 | sprintf(buf,"QUIT\r\n"); 307 | msize = strlen(buf); 308 | sendSMTP(buf, msize); 309 | if (!waitForString("221", 3, 1000)) { 310 | errorDisplay("QUIT Fail"); 311 | } 312 | Serial.println("OK"); 313 | 314 | waitForString("CLOSE", 5, 1000); 315 | getResponse(1000); 316 | Serial.println("Mail send!!"); 317 | } 318 | 319 | void loop(){ 320 | 321 | } 322 | -------------------------------------------------------------------------------- /UDP_Broadcast_Server_ESP01/espLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Socket library using ESP8266 AT Instruction Set 3 | */ 4 | 5 | #define _DEBUG_ 0 6 | 7 | //Serial Object 8 | Stream *espSerial = 0; 9 | 10 | //Save Serial Object 11 | void serialSetup(Stream &port) { 12 | espSerial = &port; 13 | } 14 | 15 | //Wait for specific input string until timeout runs out 16 | bool waitForString(char* input, int length, unsigned int timeout, bool print=false) { 17 | 18 | unsigned long end_time = millis() + timeout; 19 | char current_byte = 0; 20 | int index = 0; 21 | 22 | while (end_time >= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | //Send Message 68 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 69 | char atcmd[64]; 70 | if (strlen(rmote)) { 71 | if (id >= 0) { 72 | //AT+CIPSEND=,,, 73 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 74 | } else { 75 | //AT+CIPSEND=,, 76 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 77 | } 78 | } else { 79 | if (id >= 0) { 80 | //AT+CIPSEND=, 81 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 82 | } else { 83 | //AT+CIPSEND= 84 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 85 | } 86 | } 87 | sendCommand(atcmd); 88 | if (!waitForString(">", 1, 1000)) return 1; 89 | clearBuffer(); 90 | 91 | //Send Packet 92 | for(int i=0;iwrite(buf[i]); 93 | if (!waitForString("SEND OK", 7, 1000)) return 2; 94 | // clearBuffer(); 95 | return 0; 96 | } 97 | 98 | //Receive Message 99 | int readData(char *buf, int sz_buf, int timeout) { 100 | int len=0; 101 | int flag=0; 102 | int datalen=0; 103 | char work[4]; 104 | long int time = millis(); 105 | 106 | // +IPD,nn:ReceiveData 107 | while( (time+timeout) > millis()) { 108 | while(espSerial->available()) { 109 | char current_byte = espSerial->read(); // read the next character. 110 | if (_DEBUG_) { 111 | Serial.print("0x"); 112 | if (current_byte < 0x10) Serial.print("0"); 113 | Serial.print(current_byte,HEX); 114 | Serial.print("["); 115 | if (current_byte < 0x20) { 116 | Serial.print(" "); 117 | } else if (current_byte > 0x7F) { 118 | Serial.print(" "); 119 | } else { 120 | Serial.print(current_byte); 121 | } 122 | Serial.print("] "); 123 | Serial.print(flag); 124 | Serial.print(" "); 125 | Serial.print(datalen); 126 | Serial.print(" "); 127 | Serial.println(len); 128 | } 129 | if (flag == 0) { 130 | //Check if it is the letter + 131 | if (current_byte != 0x2B) continue; 132 | flag++; 133 | 134 | } else if (flag == 1) { 135 | //Check if it is the letter I 136 | if (current_byte == 0x49) { 137 | flag++; 138 | } else { 139 | flag = 0; 140 | } 141 | 142 | } else if (flag == 2) { 143 | //Check if it is the letter P 144 | if (current_byte == 0x50) { 145 | flag++; 146 | } else { 147 | flag = 0; 148 | } 149 | 150 | } else if (flag == 3) { 151 | //Check if it is the letter D 152 | if (current_byte == 0x44) { 153 | flag++; 154 | } else { 155 | flag = 0; 156 | } 157 | 158 | } else if (flag == 4) { 159 | //Check if it is the letter , 160 | if (current_byte == 0x2C) { 161 | flag++; 162 | } else { 163 | flag = 0; 164 | } 165 | 166 | } else if (flag == 5) { 167 | //Check if it is the letter : 168 | if (current_byte == 0x3a) { // : 169 | if (_DEBUG_) { 170 | Serial.print("work=["); 171 | Serial.print(work); 172 | Serial.println("]"); 173 | } 174 | datalen=atoi(work); 175 | flag++; 176 | len=0; 177 | } else { 178 | work[len++]=current_byte; 179 | work[len]=0x00; 180 | } 181 | 182 | } else if (flag == 6) { 183 | buf[len++]=current_byte; 184 | if (len == datalen) return datalen; 185 | 186 | } // end if 187 | } // end while 188 | } // end while 189 | return -1; 190 | } 191 | 192 | //Receive Message 193 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 194 | int len=0; 195 | int flag=0; 196 | int datalen; 197 | long int time = millis(); 198 | 199 | // id < 0 +IPD,nn:ReceiveData 200 | // id = 0 +IPD,0,nn:ReceiveData 201 | // id > 0 +IPD,id,nn:ReceiveData 202 | while( (time+timeout) > millis()) { 203 | while(espSerial->available()) { 204 | char current_byte = espSerial->read(); // read the next character. 205 | if (_DEBUG_) { 206 | Serial.print("0x"); 207 | if (current_byte < 0x10) Serial.print("0"); 208 | Serial.print(current_byte,HEX); 209 | Serial.print("["); 210 | if (current_byte < 0x20) { 211 | Serial.print(" "); 212 | } else if (current_byte > 0x7F) { 213 | Serial.print(" "); 214 | } else { 215 | Serial.print(current_byte); 216 | } 217 | Serial.print("]"); 218 | Serial.print(flag); 219 | Serial.print(" "); 220 | Serial.print(datalen); 221 | Serial.print(" "); 222 | Serial.println(len); 223 | } 224 | if (flag == 0) { 225 | if (current_byte != 0x2c) continue; 226 | flag++; 227 | if (id < 0) flag++; 228 | } else if (flag == 1) { 229 | if (current_byte != 0x2c) continue; 230 | flag++; 231 | } else if (flag == 2) { 232 | if (current_byte == 0x3a) { // : 233 | datalen=atoi(buf); 234 | flag++; 235 | len=0; 236 | } else { 237 | buf[len++]=current_byte; 238 | } 239 | } else { 240 | buf[len++]=current_byte; 241 | if (len == datalen) return len; 242 | } 243 | } // end while 244 | } // end while 245 | return -len; 246 | } 247 | 248 | //Get IP Address 249 | int getIpAddress(char *buf, int szbuf, int timeout) { 250 | int len=0; 251 | int pos=0; 252 | char line[128] = {0}; 253 | 254 | long int time = millis(); 255 | 256 | sendCommand("AT+CIPSTA?"); 257 | 258 | while( (time+timeout) > millis()) { 259 | while(espSerial->available()) { 260 | char c = espSerial->read(); // read the next character. 261 | if (c == 0x0d) { 262 | 263 | } else if (c == 0x0a) { 264 | if (_DEBUG_) { 265 | Serial.print("Read=["); 266 | Serial.print(line); 267 | Serial.println("]"); 268 | } 269 | int offset; 270 | for(offset=0;offset millis()) { 301 | while(espSerial->available()) { 302 | char c = espSerial->read(); // read the next character. 303 | if (c == 0x0d) { 304 | 305 | } else if (c == 0x0a) { 306 | if (_DEBUG_) { 307 | Serial.print("Read=["); 308 | Serial.print(line); 309 | Serial.println("]"); 310 | } 311 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 312 | strcpy(buf,&line[12]); 313 | len = strlen(buf) - 1; 314 | buf[len] = 0; 315 | } 316 | if (strcmp(line,"OK") == 0) return len; 317 | pos=0; 318 | line[pos]=0; 319 | } else { 320 | line[pos++]=c; 321 | line[pos]=0; 322 | } 323 | } 324 | } 325 | return len; 326 | } 327 | 328 | //Print error 329 | void errorDisplay(char* buff) { 330 | Serial.print("Error:"); 331 | Serial.println(buff); 332 | while(1) {} 333 | } 334 | -------------------------------------------------------------------------------- /TCP_Server_ESP01/espLib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Socket library using ESP8266 AT Instruction Set 3 | */ 4 | 5 | #define _DEBUG_ 0 6 | 7 | //Serial Object 8 | Stream *espSerial = 0; 9 | 10 | //Save Serial Object 11 | void serialSetup(Stream &port) { 12 | espSerial = &port; 13 | } 14 | 15 | //Wait for specific input string until timeout runs out 16 | bool waitForString(char* input, int length, unsigned int timeout, bool print=false) { 17 | 18 | unsigned long end_time = millis() + timeout; 19 | char current_byte = 0; 20 | int index = 0; 21 | 22 | while (end_time >= millis()) { 23 | if(espSerial->available()) { 24 | 25 | //Read one byte from serial port 26 | current_byte = espSerial->read(); 27 | if (print) Serial.print(current_byte); 28 | if (current_byte != -1) { 29 | //Search one character at a time 30 | if (current_byte == input[index]) { 31 | index++; 32 | 33 | //Found the string 34 | if (index == length) { 35 | if (print) Serial.println(); 36 | return true; 37 | } 38 | //Restart position of character to look for 39 | } else { 40 | index = 0; 41 | } 42 | } 43 | } 44 | } 45 | //Timed out 46 | return false; 47 | } 48 | 49 | //Remove all bytes from the receive buffer 50 | void clearBuffer() { 51 | while (espSerial->available()) 52 | espSerial->read(); 53 | if (_DEBUG_) Serial.println(""); 54 | } 55 | 56 | //Send AT Command 57 | void sendCommand(char* buff) { 58 | if (_DEBUG_) { 59 | Serial.println(""); 60 | Serial.print(buff); 61 | Serial.println("-->"); 62 | } 63 | espSerial->println(buff); 64 | espSerial->flush(); 65 | } 66 | 67 | //Send Message 68 | int sendData(int id, char *buf, int blen, char *rmote, unsigned int port) { 69 | char atcmd[64]; 70 | if (strlen(rmote)) { 71 | if (id >= 0) { 72 | //AT+CIPSEND=,,, 73 | sprintf(atcmd,"AT+CIPSEND=%d,%d,\"%s\",%u", id, blen, rmote, port); 74 | } else { 75 | //AT+CIPSEND=,, 76 | sprintf(atcmd,"AT+CIPSEND=%d,\"%s\",%u", blen, rmote, port); 77 | } 78 | } else { 79 | if (id >= 0) { 80 | //AT+CIPSEND=, 81 | sprintf(atcmd,"AT+CIPSEND=%d,%d", id, blen); 82 | } else { 83 | //AT+CIPSEND= 84 | sprintf(atcmd,"AT+CIPSEND=%d", blen); 85 | } 86 | } 87 | sendCommand(atcmd); 88 | if (!waitForString(">", 1, 1000)) return 1; 89 | clearBuffer(); 90 | 91 | //Send Packet 92 | for(int i=0;iwrite(buf[i]); 93 | if (!waitForString("SEND OK", 7, 2000)) return 2; 94 | return 0; 95 | } 96 | 97 | //Receive Message 98 | int readResponse(int id, char *buf, int sz_buf, int timeout) { 99 | int len=0; 100 | int flag=0; 101 | int datalen; 102 | long int time = millis(); 103 | 104 | // id < 0 +IPD,nn:ReceiveData 105 | // id = 0 +IPD,0,nn:ReceiveData 106 | // id > 0 +IPD,id,nn:ReceiveData 107 | while( (time+timeout) > millis()) { 108 | while(espSerial->available()) { 109 | char current_byte = espSerial->read(); // read the next character. 110 | if (_DEBUG_) { 111 | Serial.print("0x"); 112 | if (current_byte < 0x10) Serial.print("0"); 113 | Serial.print(current_byte,HEX); 114 | Serial.print("["); 115 | if (current_byte < 0x20) { 116 | Serial.print(" "); 117 | } else if (current_byte > 0x7F) { 118 | Serial.print(" "); 119 | } else { 120 | Serial.print(current_byte); 121 | } 122 | Serial.print("]"); 123 | Serial.print(flag); 124 | Serial.print(" "); 125 | Serial.print(datalen); 126 | Serial.print(" "); 127 | Serial.println(len); 128 | } 129 | if (flag == 0) { 130 | if (current_byte != 0x2c) continue; 131 | flag++; 132 | if (id < 0) flag++; 133 | } else if (flag == 1) { 134 | if (current_byte != 0x2c) continue; 135 | flag++; 136 | } else if (flag == 2) { 137 | if (current_byte == 0x3a) { // : 138 | datalen=atoi(buf); 139 | flag++; 140 | len=0; 141 | } else { 142 | buf[len++]=current_byte; 143 | buf[len]=0; 144 | } 145 | } else { 146 | buf[len++]=current_byte; 147 | if (len == sz_buf) return -len; 148 | buf[len]=0; 149 | if (len == datalen) return len; 150 | } 151 | } // end while 152 | } // end while 153 | return -len; 154 | } 155 | 156 | //Get IP Address 157 | int getIpAddress(char *buf, int szbuf, int timeout) { 158 | int len=0; 159 | int pos=0; 160 | char line[128] = {0}; 161 | 162 | long int time = millis(); 163 | 164 | sendCommand("AT+CIPSTA?"); 165 | 166 | while( (time+timeout) > millis()) { 167 | while(espSerial->available()) { 168 | char c = espSerial->read(); // read the next character. 169 | if (c == 0x0d) { 170 | 171 | } else if (c == 0x0a) { 172 | if (_DEBUG_) { 173 | Serial.print("Read=["); 174 | Serial.print(line); 175 | Serial.println("]"); 176 | } 177 | int offset; 178 | for(offset=0;offset millis()) { 209 | while(espSerial->available()) { 210 | char c = espSerial->read(); // read the next character. 211 | if (c == 0x0d) { 212 | 213 | } else if (c == 0x0a) { 214 | if (_DEBUG_) { 215 | Serial.print("Read=["); 216 | Serial.print(line); 217 | Serial.println("]"); 218 | } 219 | if (strncmp(line,"+CIPSTAMAC:",11) == 0) { 220 | strcpy(buf,&line[12]); 221 | len = strlen(buf) - 1; 222 | buf[len] = 0; 223 | } 224 | if (strcmp(line,"OK") == 0) return len; 225 | pos=0; 226 | line[pos]=0; 227 | } else { 228 | line[pos++]=c; 229 | line[pos]=0; 230 | } 231 | } 232 | } 233 | return len; 234 | } 235 | 236 | //Print error 237 | void errorDisplay(char* buff) { 238 | Serial.print("Error:"); 239 | Serial.println(buff); 240 | while(1) {} 241 | } 242 | 243 | //Wait for CIPSNTPTIME response 244 | int getSNTPtime(char* buf, int szbuf, unsigned int timeout) { 245 | int len=0; 246 | int pos=0; 247 | char line[40]; 248 | 249 | //Get current time 250 | sendCommand("AT+CIPSNTPTIME?"); 251 | long int time = millis(); 252 | 253 | memset(line,0,sizeof(line)); 254 | while( (time+timeout) > millis()) { 255 | while(espSerial->available()) { 256 | char c = espSerial->read(); // read the next character. 257 | if (_DEBUG_) Serial.print(c); 258 | if (c == 0x0d) { 259 | 260 | } else if (c == 0x0a) { 261 | if (_DEBUG_) { 262 | Serial.print("Read=["); 263 | Serial.print(line); 264 | Serial.println("]"); 265 | } 266 | if (strcmp(line,"OK") == 0) return len; 267 | int offset; 268 | for(offset=0;offset millis()) { 297 | while(espSerial->available()) { 298 | char c = espSerial->read(); // read the next character. 299 | if (c == 0x0d) { 300 | 301 | } else if (c == 0x0a) { 302 | if (_DEBUG_) { 303 | Serial.print("Read=["); 304 | Serial.print(line); 305 | Serial.println("]"); 306 | } 307 | if (strcmp(line,"OK") == 0) return len; 308 | if (strncmp(line,"AT version:",11) == 0) { 309 | strcpy(buf,&line[11]); 310 | len = strlen(buf); 311 | buf[len] = 0; 312 | } 313 | pos=0; 314 | line[pos]=0; 315 | } else { 316 | line[pos++]=c; 317 | if (pos == 128) return -1; 318 | line[pos]=0; 319 | } 320 | } 321 | } 322 | return len; 323 | } 324 | 325 | // Wait for Connect/Close 326 | // irq=1 : Wait for CONNECT 327 | // irq=2 : Wait for CLOSED 328 | int waitConnect(int irq, int timeout) { 329 | int len=0; 330 | long int time = millis(); 331 | char current_byte = 0; 332 | bool endFlag = 0; 333 | char str_buffer[64]; 334 | 335 | while( (time+timeout) > millis()) { 336 | while(espSerial->available()) { 337 | char current_byte = espSerial->read(); // read the next character. 338 | if (_DEBUG_) Serial.print(current_byte); 339 | 340 | if (current_byte == 0x0d) { // CR 341 | 342 | } else if (current_byte == 0x0a) { // LF 343 | if (_DEBUG_) { 344 | Serial.print("len="); 345 | Serial.println(len); 346 | } 347 | if (len == 0) continue; 348 | if (_DEBUG_) { 349 | Serial.print("str_buffer=["); 350 | Serial.print(str_buffer); 351 | Serial.println("]"); 352 | } 353 | for(int i=0;i // https://github.com/PaulStoffregen/Time 53 | 54 | //for Arduino UNO(ATmega328) 55 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 56 | #include 57 | #define SERIAL_RX 4 58 | #define SERIAL_TX 5 59 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, TX 60 | #define _BAUDRATE_ 4800 61 | #define _SERIAL_ Serial2 62 | #define _MODEL_ "ATmega328" 63 | 64 | //for Arduino MEGA(ATmega2560) 65 | #elif defined(__AVR_ATmega2560__) 66 | #define _BAUDRATE_ 115200 67 | #define _SERIAL_ Serial1 68 | #define _MODEL_ "ATmega2560" 69 | 70 | //for STM32F103(MAPLE Core) 71 | #elif defined(__STM32F1__) 72 | #define _BAUDRATE_ 115200 73 | #define _SERIAL_ Serial2 74 | #define _MODEL_ "STM32F103 MAPLE Core" 75 | 76 | //for STM32F103(ST Core) 77 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 78 | HardwareSerial Serial2(PA3, PA2); 79 | #define _BAUDRATE_ 115200 80 | #define _SERIAL_ Serial2 81 | #define _MODEL_ "STM32F103 ST Core" 82 | 83 | //for STM32F303(ST Core) 84 | #elif defined(ARDUINO_BLACKPILL_F303CC) 85 | HardwareSerial Serial2(PA3, PA2); 86 | #define _BAUDRATE_ 115200 87 | #define _SERIAL_ Serial2 88 | #define _MODEL_ "STM32F303 ST Core" 89 | 90 | //for STM32F401(ST Core) 91 | #elif defined(ARDUINO_BLACKPILL_F401CC) 92 | HardwareSerial Serial2(PA3, PA2); 93 | #define _BAUDRATE_ 115200 94 | #define _SERIAL_ Serial2 95 | #define _MODEL_ "STM32F401 ST Core" 96 | 97 | //for STM32F4DISC1(ST Core) 98 | #elif defined(ARDUINO_DISCO_F407VG) 99 | HardwareSerial Serial3(PD9, PD8); 100 | #define _BAUDRATE_ 115200 101 | #define _SERIAL_ Serial3 102 | #define _MODEL_ "STM32 F4DISC1 ST Core" 103 | 104 | //for STM32F407(ST Core) 105 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 106 | HardwareSerial Serial2(PA3, PA2); 107 | #define _BAUDRATE_ 115200 108 | #define _SERIAL_ Serial2 109 | #define _MODEL_ "STM32F407 ST Core" 110 | 111 | //for STM32 NUCLEO64(ST Core) 112 | #else 113 | HardwareSerial Serial1(PA10, PA9); 114 | #define _BAUDRATE_ 115200 115 | #define _SERIAL_ Serial1 116 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 117 | #endif 118 | 119 | #define INTERVAL 10 // Interval of Packet Send(Second) 120 | #define NTP_SERVER "time.google.com" // NTP Server 121 | #define NTP_PORT 123 // NTP Port 122 | #define LOCAL_PORT 2390 // Local Port 123 | #define NTP_PACKET_SIZE 48 // NTP Packet Size 124 | #define TIME_ZONE 9 // Time Difference from GMT 125 | #define DNS_SERVER1 "8.8.8.8" // DNS SERVER1 126 | #define DNS_SERVER2 "8.8.4.4" // DNS SERVER2 127 | 128 | // NTP Packet Buffer (Send & Receive) 129 | char packetBuffer[NTP_PACKET_SIZE]; 130 | // Last Packet Send Time (MilliSecond) 131 | unsigned long lastSendPacketTime = 0; 132 | 133 | // dow_char() Return day of the week string(English) [Sun,Mon....] 134 | char * dow_char_EN(byte days) { 135 | char *you[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 136 | return you[days]; 137 | } 138 | 139 | // dow_char() Return day of the week string(Japanese) [日曜,火曜....] 140 | char * dow_char_JP(byte days) { 141 | char *you[] = {"日曜","月曜","火曜","水曜","木曜","金曜","土曜"}; 142 | return you[days]; 143 | } 144 | 145 | // dow() Return day of the week number[0-Sunday, 1-Monday etc.] 146 | uint8_t dow(unsigned long t) { 147 | return ((t / 86400) + 4) % 7; 148 | } 149 | 150 | void showTime(char * title, time_t timet, char * dow) { 151 | //Serial.println("[showTime] timet=" + String(timet)); 152 | Serial.print("[showTime] timet="); 153 | Serial.println(timet); 154 | Serial.print(title); 155 | Serial.print(year(timet)); 156 | Serial.print("/"); 157 | Serial.print(month(timet)); 158 | Serial.print("/"); 159 | Serial.print(day(timet)); 160 | Serial.print(" "); 161 | Serial.print(hour(timet)); 162 | Serial.print(":"); 163 | Serial.print(minute(timet)); 164 | Serial.print(":"); 165 | Serial.print(second(timet)); 166 | Serial.print(" ["); 167 | Serial.print(dow); 168 | Serial.println("]"); 169 | } 170 | 171 | // send an NTP request to the time server at the given address 172 | void sendNTPpacket(char *ntpSrv, int ntpPort) 173 | { 174 | // set all bytes in the buffer to 0 175 | memset(packetBuffer, 0, NTP_PACKET_SIZE); 176 | // Initialize values needed to form NTP request 177 | // (see URL above for details on the packets) 178 | 179 | packetBuffer[0] = 0b11100011; // LI, Version, Mode 180 | packetBuffer[1] = 0; // Stratum, or type of clock 181 | packetBuffer[2] = 6; // Polling Interval 182 | packetBuffer[3] = 0xEC; // Peer Clock Precision 183 | // 8 bytes of zero for Root Delay & Root Dispersion 184 | packetBuffer[12] = 49; 185 | packetBuffer[13] = 0x4E; 186 | packetBuffer[14] = 49; 187 | packetBuffer[15] = 52; 188 | 189 | //Send Data 190 | int ret = sendData(-1, packetBuffer, NTP_PACKET_SIZE, ntpSrv, ntpPort); 191 | if (ret) { 192 | errorDisplay("sendData Fail"); 193 | } 194 | } 195 | 196 | void setup(void) 197 | { 198 | Serial.begin(115200); 199 | delay(5000); 200 | Serial.print("_MODEL_="); 201 | Serial.println(_MODEL_); 202 | _SERIAL_.begin(_BAUDRATE_); 203 | 204 | //Save Serial Object 205 | serialSetup(_SERIAL_); 206 | 207 | //Enable autoconnect 208 | sendCommand("AT+CWAUTOCONN=1"); 209 | if (!waitForString("OK", 2, 1000)) { 210 | errorDisplay("AT+CWAUTOCONN Fail"); 211 | } 212 | clearBuffer(); 213 | 214 | //Restarts the Module 215 | sendCommand("AT+RST"); 216 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 217 | errorDisplay("ATE+RST Fail"); 218 | } 219 | clearBuffer(); 220 | 221 | //Local echo off 222 | sendCommand("ATE0"); 223 | if (!waitForString("OK", 2, 1000)) { 224 | errorDisplay("ATE0 Fail"); 225 | } 226 | clearBuffer(); 227 | 228 | //Get My IP Address 229 | char IPaddress[64]; 230 | getIpAddress(IPaddress,sizeof(IPaddress),2000); 231 | Serial.print("IPaddress=["); 232 | Serial.print(IPaddress); 233 | Serial.println("]"); 234 | 235 | //Get My MAC Address 236 | char MACaddress[64]; 237 | getMacAddress(MACaddress,sizeof(MACaddress),2000); 238 | Serial.print("MACaddress=["); 239 | Serial.print(MACaddress); 240 | Serial.println("]"); 241 | 242 | //Get DNS Server Information 243 | sendCommand("AT+CIPDNS_CUR?"); 244 | if (!waitForString("OK", 2, 1000, true)) { 245 | errorDisplay("AT+CIPDNS_CUR Fail"); 246 | } 247 | clearBuffer(); 248 | 249 | //Set DNS Server Information 250 | //AT+CIPDNS_CUR=[,,] 251 | char cmd[64]; 252 | sprintf(cmd,"AT+CIPDNS_CUR=1,\"%s\",\"%s\"", DNS_SERVER1, DNS_SERVER2); 253 | sendCommand(cmd); 254 | if (!waitForString("OK", 2, 1000)) { 255 | errorDisplay("AT+CIPDNS_CUR Fail"); 256 | } 257 | clearBuffer(); 258 | 259 | //Get DNS Server Information again 260 | sendCommand("AT+CIPDNS_CUR?"); 261 | if (!waitForString("OK", 2, 1000, true)) { 262 | errorDisplay("AT+CIPDNS_CUR Fail"); 263 | } 264 | clearBuffer(); 265 | 266 | //Establish UDP Transmission 267 | //AT+CIPSTART=,,,, 268 | sprintf(cmd,"AT+CIPSTART=\"UDP\",\"0\",0,%u,2", LOCAL_PORT); 269 | //sendCommand("AT+CIPSTART=\"UDP\",\"0\",0,2390,2"); 270 | sendCommand(cmd); 271 | if (!waitForString("OK", 2, 1000)) { 272 | errorDisplay("AT+CIPSTART Fail"); 273 | } 274 | clearBuffer(); 275 | Serial.println("Start NTP Client [" + String(_MODEL_) + "] using " + String(NTP_SERVER)); 276 | 277 | // send an NTP packet to a time server 278 | sendNTPpacket(NTP_SERVER, NTP_PORT); 279 | lastSendPacketTime = millis(); 280 | } 281 | 282 | void loop(void) { 283 | static int counter=0; 284 | byte DayOfWeek; 285 | 286 | long now = millis(); 287 | if (now - lastSendPacketTime > 1000) { // One second has elapsed 288 | lastSendPacketTime = now; 289 | counter++; 290 | if ( (counter % 10) == 0) { 291 | Serial.print("+"); 292 | } else { 293 | Serial.print("."); 294 | } 295 | //if (_DEBUG_) Serial.println("counter=" + String(counter)); 296 | if (counter == INTERVAL) { 297 | // Send request o NTP server 298 | sendNTPpacket(NTP_SERVER, NTP_PORT); 299 | counter=0; 300 | } 301 | } 302 | 303 | // Read data from NTP server 304 | int readLen = readResponse(-1, packetBuffer, NTP_PACKET_SIZE, 1000); 305 | //Serial.print("packet received, length="); 306 | //Serial.println(readLen); 307 | if (readLen == NTP_PACKET_SIZE) { 308 | Serial.println("\nNTP packet received"); 309 | 310 | //the timestamp starts at byte 40 of the received packet and is four bytes, 311 | // or two words, long. First, esxtract the two words: 312 | 313 | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); 314 | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); 315 | 316 | // combine the four bytes (two words) into a long integer 317 | // this is NTP time (seconds since Jan 1 1900): 318 | unsigned long secsSince1900 = highWord << 16 | lowWord; 319 | Serial.print("Seconds since Jan 1 1900 = " ); 320 | Serial.println(secsSince1900); 321 | 322 | // now convert NTP time into everyday time: 323 | // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: 324 | const unsigned long seventyYears = 2208988800UL; 325 | // subtract seventy years: 326 | unsigned long epoch = secsSince1900 - seventyYears; 327 | Serial.print("Unix time = "); 328 | Serial.println(epoch); 329 | 330 | // Greenwich Mean Time(GMT) 331 | uint8_t DayOfWeek = dow(epoch); 332 | showTime("The UTC time is ", epoch, dow_char_EN(DayOfWeek)); 333 | 334 | // Local time 335 | if (TIME_ZONE == 9) { 336 | DayOfWeek = dow(epoch + (TIME_ZONE * 60 * 60)); 337 | showTime("Local time is ", epoch + (TIME_ZONE * 60 * 60), dow_char_JP(DayOfWeek)); 338 | } else { 339 | DayOfWeek = dow(epoch + (TIME_ZONE * 60 * 60)); 340 | showTime("Local time is ", epoch + (TIME_ZONE * 60 * 60), dow_char_EN(DayOfWeek)); 341 | } 342 | } 343 | } 344 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino-ESPAT-NETWORK 2 | MQTT/NTP/SMTP/SNTP/TCP/UDP Applicaton for Arduino using ESP8266's AT firmware. 3 | You don't need Ethernet card. 4 | You need only ESP8266 module. 5 | 6 | # Memory usage 7 | __Saves Flash and SRAM__ 8 | 9 | _UNO+ENC28J60+UIPEthernet+PubSubClient_ 10 | 11 | ``` 12 | Sketch uses 25090 bytes (77%) of program storage space. Maximum is 32256 bytes. 13 | Global variables use 1736 bytes (84%) of dynamic memory, leaving 312 bytes for local variables. Maximum is 2048 bytes. 14 | Low memory available, stability problems may occur. 15 | ``` 16 | 17 | _UNO+W5100+Ethernet+PubSubClient_ 18 | 19 | ``` 20 | Sketch uses 19582 bytes (60%) of program storage space. Maximum is 32256 bytes. 21 | Global variables use 1101 bytes (53%) of dynamic memory, leaving 947 bytes for local variables. Maximum is 2048 bytes. 22 | ``` 23 | 24 | _UNO+ESP01+Software Serial+AT firmware_ 25 | 26 | ``` 27 | Sketch uses 9938 bytes (30%) of program storage space. Maximum is 32256 bytes. 28 | Global variables use 897 bytes (43%) of dynamic memory, leaving 1151 bytes for local variables. Maximum is 2048 bytes. 29 | ``` 30 | 31 | # Hardware requirements 32 | - ESP8266 module like ESP01 33 | ESP8266 has 1MB FLASH and 4MB FLASH (or more) SoCs, but it doesn't matter. 34 | 35 | - ATmega328/ATmega2560/STM32 36 | 37 | - UART-USB converter 38 | 39 | # Software requirements 40 | - Arduino Environment 41 | Core library is required when using STM32. 42 | It supports Maple cores and ST cores. 43 | 44 | - ESP8266 AT Firmware 45 | AT firmware version can be identified by AT version and SDK version. 46 | AT version V2 added MQTT and HTTP commands. 47 | However, AT version V2 does not support 1M SoCs like ESP01. 48 | When using 1M SoCs like ESP01, it is necessary to use AT version V1. 49 | This repository works with both V1 and V2. 50 | You can download AT version V1 from [here](https://github.com/espressif/ESP8266_NONOS_SDK/tags). 51 | __Note__ 52 | Some firmware here does not support 1M SoC such as ESP01. 53 | I used this. 54 | ``` 55 | AT+GMR 56 | AT version:1.7.5.0(Oct 20 2021 19:14:04) 57 | SDK version:3.0.5(b29dcd3) 58 | compile time:Oct 20 2021 20:13:50 59 | Bin version(Wroom 02):1.7.5 60 | ``` 61 | 62 | - Terminal software such as CoolTerm 63 | I use TeraTrem. 64 | You can down load from [here](https://download.cnet.com/tera-term/3000-2094_4-75766675.html). 65 | 66 | # Applications 67 | 68 | **MQTT_Publish_ESP01** 69 | Simple MQTT Pubish Application. 70 | 71 | **MQTT_Subscribe_ESP01** 72 | Simple MQTT Subscribe Application. 73 | 74 | ![slide1](https://user-images.githubusercontent.com/6020549/35101108-a13451d8-fca1-11e7-8cfd-37d71f18f880.JPG) 75 | 76 | **TCP_Client_ESP01** 77 | Simple TCP Socket Client Application. 78 | 79 | **TCP_Server_ESP01** 80 | Simple TCP Socket Server Application. 81 | 82 | **UDP_Broadcast_Client_ESP01** 83 | Simple UDP Socket Client Application using UDP Broadcast. 84 | 85 | **UDP_Broadcast_Server_ESP01** 86 | Simple UDP Socket Server Application using UDP Broadcast. 87 | 88 | 89 | ![slide2](https://user-images.githubusercontent.com/6020549/35101341-9019e394-fca2-11e7-9edd-0aa9086fd5db.JPG) 90 | 91 | **NTP_Client_ESP01** 92 | Simple NTP Client Application. 93 | It require [this](https://github.com/PaulStoffregen/Time) library. 94 | 95 | **SNTP_Client_ESP01** 96 | Simple SNTP Client Application. 97 | 98 | ![slide3](https://user-images.githubusercontent.com/6020549/35101499-241b1950-fca3-11e7-9876-0a22008ebc5a.JPG) 99 | 100 | **SMTP_Client_gmail_ESP01** 101 | Simple SMTP Client Application. 102 | You need gmail account. 103 | 104 | ![slide4](https://user-images.githubusercontent.com/6020549/35125598-90e2a360-fced-11e7-89ed-045cd6c49984.JPG) 105 | 106 | 107 | # Flash AT firmware to ESP01. 108 | The 3.3V output of the UART-USB converter has too little current to be used. 109 | Power is supplied using 5V from the UART-USB converter and 3.3V from the regulator. 110 | - GPIO2 must be pulled up. 111 | - GPIO0 must be connected to GND. 112 | - CH_PD must be pulled up. 113 | - RESET must be pulled up. 114 | 115 | See [here](https://github.com/nopnop2002/Arduino-ESPAT-TCP/tree/master/Flash_AT_firmware) for details.. 116 | 117 | ![esp01-flash](https://user-images.githubusercontent.com/6020549/33159146-b8456238-d053-11e7-8202-a86cca2f8a3d.jpg) 118 | 119 | # Setup ESP01 using terminal software such as CoolTerm. 120 | - GPIO2 must be pulled up. 121 | - GPIO0 must be pulled up. 122 | - CH_PD must be pulled up. 123 | - RESET must be pulled up. 124 | 125 | ![esp01-setup](https://user-images.githubusercontent.com/6020549/33159150-bdade984-d053-11e7-9b93-bbbf05573441.jpg) 126 | 127 | Connect to ESP01 at 115200 bps using terminal software. 128 | 129 | ``` 130 | AT+GMR 131 | AT version:1.6.2.0(Apr 13 2018 11:10:59) 132 | SDK version:2.2.1(6ab97e9) 133 | compile time:Jun 7 2018 19:34:26 134 | Bin version(Wroom 02):1.6.2 135 | OK 136 | 137 | AT+CWMODE=1 138 | 139 | OK 140 | AT+CWLAP 141 | +CWLAP:(3,"Picking",-86,"34:12:98:08:4b:4a",1,-4) 142 | +CWLAP:(4,"ctc-g-fa4a2e",-92,"c0:25:a2:b1:8c:2e",2,3) 143 | +CWLAP:(4,"aterm-e625c0-g",-49,"c0:25:a2:ac:cb:ba",3,15) 144 | +CWLAP:(1,"aterm-e625c0-gw",-48,"c2:25:a2:ac:cb:ba",3,15) 145 | 146 | OK 147 | 148 | AT+CWJAP="Your AP's SSID","Your AP's password" 149 | WIFI CONNECTED 150 | WIFI GOT IP 151 | 152 | OK 153 | 154 | AT+CIPSTA? 155 | +CIPSTA:ip:"192.168.10.142" 156 | +CIPSTA:gateway:"192.168.10.1" 157 | +CIPSTA:netmask:"255.255.255.0" 158 | 159 | OK 160 | AT+CWQAP 161 | 162 | OK 163 | 164 | WIFI DISCONNECT 165 | ``` 166 | 167 | _*** UNO ONLY ***_ 168 | _*** Change baudrate to 4800bps ***_ 169 | _*** Because there is no the 2nd UART in UNO ***_ 170 | _*** So UNO use Software Serial with low speed ***_ 171 | 172 | ``` 173 | AT+UART_DEF=4800,8,1,0,0 174 | ``` 175 | 176 | Re-Connect to ESP01 with 4800 bps. 177 | 178 | AT firmware has a function that automatically connects to the last connected AP when the module is reset. 179 | Using this function, you can omit the SSID and password. 180 | 181 | ``` 182 | AT+RST 183 | WIFI CONNECTED 184 | WIFI GOT IP 185 | AT+CIPSTA? 186 | +CIPSTA:ip:"192.168.10.142" 187 | +CIPSTA:gateway:"192.168.10.1" 188 | +CIPSTA:netmask:"255.255.255.0" 189 | 190 | OK 191 | ``` 192 | 193 | If you want to change the AP, execute the following command again. 194 | ``` 195 | AT+CWJAP="New AP's SSID","New AP's password" 196 | WIFI CONNECTED 197 | WIFI GOT IP 198 | ``` 199 | 200 | # How to Firmware Upate 201 | 202 | 1.Make sure TE(terminal equipment) is in sta mode 203 | ``` 204 | AT+CWMODE=1 205 | 206 | OK 207 | ``` 208 | 209 | 2.Make sure TE got ip address 210 | ``` 211 | AT+CIPSTA? 212 | +CIPSTA:ip:"192.168.10.115" 213 | +CIPSTA:gateway:"192.168.10.1" 214 | +CIPSTA:netmask:"255.255.255.0" 215 | 216 | OK 217 | ``` 218 | 219 | 3.Let's update 220 | ``` 221 | AT+CIUPDATE 222 | +CIPUPDATE:1 found server 223 | +CIPUPDATE:2 connect server 224 | +CIPUPDATE:3 got edition 225 | +CIPUPDATE:4 start start 226 | 227 | OK 228 | ``` 229 | 230 | 4.Check firmware version 231 | ``` 232 | AT+GMR 233 | AT version:1.7.5.0(Oct 20 2021 19:14:04) 234 | SDK version:3.0.5(b29dcd3) 235 | compile time:Oct 20 2021 20:13:50 236 | Bin version(Wroom 02):1.7.5 237 | OK 238 | ``` 239 | 240 | # Connect ESP01 to UNO. 241 | 242 | ESP01(Tx) - UNO(D4) 243 | ESP01(Rx) - UNO(D5) 244 | 245 | ![ESP01-MQTT-UNO](https://user-images.githubusercontent.com/6020549/55268764-656f9f00-52d0-11e9-9120-360e397ffae0.jpg) 246 | 247 | You can't use on-board 3.3V. 248 | An electric current is insufficient. 249 | 250 | # Connect ESP01 to MEGA2560. 251 | 252 | ESP01(Tx) - MEGA(D19) 253 | ESP01(Rx) - MEGA(D18) 254 | 255 | ![ESP01-MQTT-MEGA](https://user-images.githubusercontent.com/6020549/55268794-9fd93c00-52d0-11e9-8cca-4f4bd202d745.jpg) 256 | 257 | You can't use on-board 3.3V. 258 | An electric current is insufficient. 259 | 260 | # Connect ESP01 to STM32F103(MAPLE Core). 261 | 262 | ESP01(Tx) - STM32F103(PA3) 263 | ESP01(Rx) - STM32F103(PA2) 264 | 265 | ![ESP01-MQTT-STM32F103_MAPLE-Core](https://user-images.githubusercontent.com/6020549/55272404-869bb400-52ff-11e9-97aa-1ff31090f925.jpg) 266 | 267 | MAPLE Core. 268 | https://github.com/rogerclarkmelbourne/Arduino_STM32 269 | 270 | # Connect ESP01 to STM32 NUCLEO(ST Core). 271 | 272 | ESP01(Tx) - STM32F103(PA10) 273 | ESP01(Rx) - STM32F103(PA9) 274 | 275 | ![ESP01-MQTT-STM32F103_ST-Core](https://user-images.githubusercontent.com/6020549/55272409-94e9d000-52ff-11e9-9f4b-61386ed3e656.jpg) 276 | 277 | ST Core. 278 | https://github.com/stm32duino/Arduino_Core_STM32 279 | 280 | # Connect ESP01 to STM32 F103 BluePill(ST Core). 281 | 282 | ESP01(Tx) - STM32F103(PA3) 283 | ESP01(Rx) - STM32F103(PA2) 284 | 285 | ![ESP01-MQTT-BLUEPILL_STM32F103_ST-Core](https://user-images.githubusercontent.com/6020549/62212468-c1ac1200-b3db-11e9-9fa2-9460d29b46cb.jpg) 286 | 287 | Serial printing goes to PA9. 288 | 289 | # Connect ESP01 to STM32 F103 MapleMini(ST Core). 290 | 291 | ESP01(Tx) - STM32F103(PA3) 292 | ESP01(Rx) - STM32F103(PA2) 293 | 294 | ![ESP01-MQTT-MAPLEMINI_STM32F103_ST-Core](https://user-images.githubusercontent.com/6020549/62213727-88c16c80-b3de-11e9-9f10-54a274908c4c.jpg) 295 | 296 | Serial printing goes to PA9. 297 | 298 | # STM32F103 development board on which ESP01 can be mounted(ST Core). 299 | 300 | ESP01(Tx) - STM32F103(PA3) 301 | ESP01(Rx) - STM32F103(PA2) 302 | 303 | ![ESP01-STM32](https://user-images.githubusercontent.com/6020549/103254648-7c718280-49c9-11eb-9bd0-c89cb686335e.JPG) 304 | 305 | Serial printing goes to PA9. 306 | 307 | # Connect ESP01 to STM32 F303 BlackPill(ST Core). 308 | 309 | ESP01(Tx) - STM32F303(PA3) 310 | ESP01(Rx) - STM32F303(PA2) 311 | 312 | Serial printing goes to PA9. 313 | 314 | # Connect ESP01 to STM32 F401 BlackPill(ST Core). 315 | 316 | ESP01(Tx) - STM32F401(PA3) 317 | ESP01(Rx) - STM32F401(PA2) 318 | 319 | Serial printing goes to PA9. 320 | 321 | # Connect ESP01 to STM32 F4DISC1(ST Core). 322 | 323 | ESP01(Tx) - STM32F4DISC1(PD9) 324 | ESP01(Rx) - STM32F4DISC1(PD8) 325 | 326 | I want to Fritzing Part of this board. 327 | Serial printing goes to PA2. 328 | 329 | # Connect ESP01 to STM32 F407 development board that like DIYMORE F407VGT. 330 | 331 | ESP01(Tx) - STM32F407(PA3) 332 | ESP01(Rx) - STM32F407(PA2) 333 | 334 | https://stm32-base.org/boards/STM32F407VGT6-diymore 335 | Serial printing goes to PA9. 336 | 337 | # UART to WiFi module 338 | We can get a module for UART communication. 339 | We can write AT firmware to this. 340 | Although it supplies 5V power, the TTL level of UART is 3.3V. 341 | ![ESP-UART-MODULE-1](https://user-images.githubusercontent.com/6020549/104827197-b504cd80-589e-11eb-95a8-f12c75670ced.JPG) 342 | 343 | ![ESP-UART-MODULE-2](https://user-images.githubusercontent.com/6020549/104827200-b8985480-589e-11eb-9a01-e70d4fbd55cc.JPG) 344 | 345 | # ESP01 Adapter board 346 | Only $1 for AliExpress/eBay. 347 | External pin header is 5V,GND,TXD,RXD. 348 | It has a 5V->3.3V regulator and can supply power directly from UNO's 5V. 349 | Although it supplies 5V power, the TTL level of UART is 3.3V. 350 | ![ESP01-Adapter-3](https://user-images.githubusercontent.com/6020549/120063987-f3a86100-c0a4-11eb-95e4-c87164812af4.JPG) 351 | 352 | # Use with TFT Shield 353 | UNO compatibles can have a pin header inside the pin socket. 354 | If you do not use SD-CARD, you can freely use digital pins # 10 to # 13 of the TFT shield. 355 | These pins can be used for UART communication with the ESP01. 356 | 357 | ![ESP01-MQTT-UNO-TFT-2](https://user-images.githubusercontent.com/6020549/83363044-2277f280-a3d1-11ea-8688-76521819f1db.JPG) 358 | 359 | ![esp01-mqtt-uno-tft](https://user-images.githubusercontent.com/6020549/33193265-cbbd2618-d10a-11e7-9dba-dd60643c27bb.JPG) 360 | 361 | 362 | -------------------------------------------------------------------------------- /MQTT_Publish_ESP01/MQTT_Publish_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MQTT Publisher using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | #include "espLib.h" 52 | 53 | //for Arduino UNO(ATmega328) 54 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 55 | #include 56 | #define SERIAL_RX 4 57 | #define SERIAL_TX 5 58 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, TX 59 | #define STOP_BUTTON 0 // 2: Enable STOP_BUTTON 60 | #define RUNNING_LED 13 // 0: Disable RUNNING_LED 61 | #define _BAUDRATE_ 4800 62 | #define _SERIAL_ Serial2 63 | #define _MODEL_ "ATmega328" 64 | 65 | //for Arduino MEGA(ATmega2560) 66 | #elif defined(__AVR_ATmega2560__) 67 | #define STOP_BUTTON 0 // 2: Enable STOP_BUTTON 68 | #define RUNNING_LED 13 // 0: Disable RUNNING_LED 69 | #define _BAUDRATE_ 115200 70 | #define _SERIAL_ Serial1 71 | #define _MODEL_ "ATmega2560" 72 | 73 | //for STM32F103(MAPLE Core) 74 | #elif defined(__STM32F1__) 75 | #define STOP_BUTTON PB2 // 0: Disable STOP_BUTTON 76 | #define RUNNING_LED PB1 // 0: Disable RUNNING_LED 77 | #define _BAUDRATE_ 115200 78 | #define _SERIAL_ Serial2 79 | #define _MODEL_ "STM32F103 MAPLE Core" 80 | 81 | //for STM32F103(ST Core) 82 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 83 | HardwareSerial Serial2(PA3, PA2); 84 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 85 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 86 | #define _BAUDRATE_ 115200 87 | #define _SERIAL_ Serial2 88 | #define _MODEL_ "STM32F103 ST Core" 89 | 90 | //for STM32F303(ST Core) 91 | #elif defined(ARDUINO_BLACKPILL_F303CC) 92 | HardwareSerial Serial2(PA3, PA2); 93 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 94 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 95 | #define _BAUDRATE_ 115200 96 | #define _SERIAL_ Serial2 97 | #define _MODEL_ "STM32F303 ST Core" 98 | 99 | //for STM32F401(ST Core) 100 | #elif defined(ARDUINO_BLACKPILL_F401CC) 101 | HardwareSerial Serial2(PA3, PA2); 102 | #define STOP_BUTTON PB13 // 0: Disable STOP_BUTTON 103 | #define RUNNING_LED PB12 // 0: Disable RUNNING_LED 104 | #define _BAUDRATE_ 115200 105 | #define _SERIAL_ Serial2 106 | #define _MODEL_ "STM32F401 ST Core" 107 | 108 | //for STM32F4DISC1(ST Core) 109 | #elif defined(ARDUINO_DISCO_F407VG) 110 | HardwareSerial Serial3(PD9, PD8); 111 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 112 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 113 | #define _BAUDRATE_ 115200 114 | #define _SERIAL_ Serial3 115 | #define _MODEL_ "STM32 F4DISC1 ST Core" 116 | 117 | //for STM32F407(ST Core) 118 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 119 | HardwareSerial Serial2(PA3, PA2); 120 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 121 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 122 | #define _BAUDRATE_ 115200 123 | #define _SERIAL_ Serial2 124 | #define _MODEL_ "STM32F407 ST Core" 125 | 126 | //for STM32 NUCLEO64(ST Core) 127 | #else 128 | HardwareSerial Serial1(PA10, PA9); 129 | #define STOP_BUTTON PB2 // 0: Disable STOP_BUTTON 130 | #define RUNNING_LED PB1 // 0: Disable RUNNING_LED 131 | #define _BAUDRATE_ 115200 132 | #define _SERIAL_ Serial1 133 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 134 | #endif 135 | 136 | 137 | #define INTERVAL 10 // Publish interval [Sec] 138 | #define MQTT_SERVER "broker.hivemq.com" // MQTT broker 139 | //#define MQTT_SERVER "broker.emqx.io" // MQTT broker 140 | //#define MQTT_SERVER "iot.eclipse.org" // MQTT broker 141 | #define MQTT_PORT 1883 // MQTT port 142 | #define MQTT_TOPIC "/ESP-AT-MQTT/" // Publish topic 143 | #define MQTT_KEEP_ALIVE 60 144 | #define MQTT_WILL_TOPIC "/ESP-AT-MQTT/" // Will topic 145 | #define MQTT_WILL_MSG "I am leaving..." // Will payload 146 | #define DNS_SERVER1 "8.8.8.8" // DNS SERVER1 147 | #define DNS_SERVER2 "8.8.4.4" // DNS SERVER2 148 | #define _DEBUG_ 0 // for Debug 149 | 150 | // Last Packet Send Time (MilliSecond) 151 | unsigned long lastSendPacketTime = 0; 152 | 153 | int buildConnect(char *buf, int keep_alive, char *client_id, char *will_topic, char *will_msg) { 154 | int rlen = 12; 155 | int pos = 14; 156 | 157 | int client_id_len = strlen(client_id); 158 | //Serial.println(client_id_len); 159 | buf[pos++] = 0x00; 160 | buf[pos++] = client_id_len; 161 | for(int i=0;i[,,] 341 | char cmd[128]; 342 | sprintf(cmd,"AT+CIPDNS_CUR=1,\"%s\",\"%s\"", DNS_SERVER1, DNS_SERVER2); 343 | sendCommand(cmd); 344 | if (!waitForString("OK", 2, 1000)) { 345 | errorDisplay("AT+CIPDNS_CUR Fail"); 346 | } 347 | clearBuffer(); 348 | 349 | //Get DNS Server Information again 350 | sendCommand("AT+CIPDNS_CUR?"); 351 | if (!waitForString("OK", 2, 1000, true)) { 352 | errorDisplay("AT+CIPDNS_CUR Fail"); 353 | } 354 | clearBuffer(); 355 | 356 | //Establishes TCP Connection 357 | sprintf(cmd,"AT+CIPSTART=\"TCP\",\"%s\",%d",MQTT_SERVER,MQTT_PORT); 358 | sendCommand(cmd); 359 | if (!waitForString("OK", 2, 5000)) { 360 | Serial.println("Check your MQTT SERVER"); 361 | errorDisplay("AT+CIPSTART Fail"); 362 | } 363 | clearBuffer(); 364 | 365 | //Client requests a connection to a server 366 | Serial.print("MQTT CONNECT....."); 367 | //Client requests a connection to a server 368 | int msize = buildConnect(cmd,MQTT_KEEP_ALIVE,MACaddress,MQTT_WILL_TOPIC,MQTT_WILL_MSG); 369 | if (_DEBUG_) hexDump(cmd,msize); 370 | int ret = sendData(-1, cmd, msize, "", 0); 371 | if (ret) errorDisplay("MQTT Connect Fail"); 372 | 373 | //Wait for CONNACK 374 | if (!waitForString("+IPD", 4, 5000)) { 375 | errorDisplay("CONNACK Fail"); 376 | } 377 | getResponse(1000); 378 | Serial.println("OK"); 379 | 380 | Serial.println("Start MQTT Publish [" + String(_MODEL_) + "] to " + String(MQTT_SERVER)); 381 | lastSendPacketTime = millis(); 382 | } 383 | 384 | void loop(){ 385 | static int counter = 0; 386 | static int timer = INTERVAL; 387 | static int ledStatus = 1; 388 | char msg[128]; 389 | char payload[128]; 390 | int msize; 391 | 392 | if (STOP_BUTTON) { 393 | int buttonState = digitalRead(STOP_BUTTON); 394 | if (buttonState == 1) { 395 | mqttDisconnect(); 396 | Serial.println(); 397 | Serial.println("Sending DISCONNECT"); 398 | Serial.println("Publish end"); 399 | if (RUNNING_LED) digitalWrite(RUNNING_LED,LOW); 400 | while(1) { } 401 | } 402 | } 403 | 404 | unsigned long now = millis(); 405 | if ( (now - lastSendPacketTime) < 0) { 406 | lastSendPacketTime = now; 407 | } 408 | 409 | if ( (now - lastSendPacketTime) > 1000) { 410 | lastSendPacketTime = now; 411 | timer++; 412 | if (RUNNING_LED) digitalWrite(RUNNING_LED,ledStatus); 413 | ledStatus = !ledStatus; 414 | #if 0 415 | Serial.print("ledStatus="); 416 | Serial.print(ledStatus); 417 | Serial.print("timer="); 418 | Serial.print(timer); 419 | #endif 420 | if ( (timer % 10) == 0) Serial.print("+"); 421 | if ( (timer % 10) != 0) Serial.print("."); 422 | 423 | if (timer >= INTERVAL) { // Publish 424 | //Publish message 425 | sprintf(msg,"Publish from %s #%03d",_MODEL_, counter); 426 | Serial.println(); 427 | Serial.println("Sending PUBLISH"); 428 | Serial.println(msg); 429 | counter++; 430 | if (counter == 1000) counter = 0; 431 | msize = buildPublish(payload, MQTT_TOPIC, msg); 432 | if (_DEBUG_) hexDump(payload,msize); 433 | mqttPublish(payload,msize); 434 | timer = 0; 435 | } 436 | } // endif 437 | mqttPingreq(); 438 | } 439 | -------------------------------------------------------------------------------- /MQTT_Subscribe_ESP01/MQTT_Subscribe_ESP01.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MQTT Subscriber using ESP8266 AT Instruction Set 3 | * 4 | * for ATmega328 5 | * ESP8266----------ATmega328 6 | * TX ----------RX(D4) 7 | * RX ----------TX(D5) 8 | * 9 | * for ATmega2560 10 | * ESP8266----------ATmega2560 11 | * TX ----------RX(D19) 12 | * RX ----------TX(D18) 13 | * 14 | * for STM32 F103 MAPLE Core 15 | * ESP8266----------STM32 16 | * TX ----------RX(PA3) 17 | * RX ----------TX(PA2) 18 | * 19 | * for STM32 F103 ST Core 20 | * ESP8266----------STM32 21 | * TX ----------RX(PA3) 22 | * RX ----------TX(PA2) 23 | * 24 | * for STM32 F303 BLACKPILL ST Core 25 | * ESP8266----------STM32 26 | * TX ----------RX(PA3) 27 | * RX ----------TX(PA2) 28 | * 29 | * for STM32 F401 BLACKPILL ST Core 30 | * ESP8266----------STM32 31 | * TX ----------RX(PA3) 32 | * RX ----------TX(PA2) 33 | * 34 | * for STM32 F4DISC1 ST Core 35 | * ESP8266----------STM32 36 | * TX ----------RX(PD9) 37 | * RX ----------TX(PD8) 38 | * 39 | * for STM32 F407VE/F407VG ST Core 40 | * ESP8266----------STM32 41 | * TX ----------RX(PA3) 42 | * RX ----------TX(PA2) 43 | * 44 | * for STM32 NUCLEO64 ST Core 45 | * ESP8266----------STM32 46 | * TX ----------RX(PA10) 47 | * RX ----------TX(PA9) 48 | * 49 | */ 50 | 51 | #include "espLib.h" 52 | 53 | //for Arduino UNO(ATmega328) 54 | #if defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) 55 | #include 56 | #define SERIAL_RX 4 57 | #define SERIAL_TX 5 58 | SoftwareSerial Serial2(SERIAL_RX, SERIAL_TX); // RX, TX 59 | #define STOP_BUTTON 0 // 2: Enable STOP_BUTTON 60 | #define RUNNING_LED 13 // 0: Disable RUNNING_LED 61 | #define _BAUDRATE_ 4800 62 | #define _SERIAL_ Serial2 63 | #define _MODEL_ "ATmega328" 64 | 65 | //for Arduino MEGA(ATmega2560) 66 | #elif defined(__AVR_ATmega2560__) 67 | #define STOP_BUTTON 0 // 2: Enable STOP_BUTTON 68 | #define RUNNING_LED 13 // 0: Disable RUNNING_LED 69 | #define _BAUDRATE_ 115200 70 | #define _SERIAL_ Serial1 71 | #define _MODEL_ "ATmega2560" 72 | 73 | //for STM32F103(MAPLE Core) 74 | #elif defined(__STM32F1__) 75 | #define STOP_BUTTON PB2 // 0: Disable STOP_BUTTON 76 | #define RUNNING_LED PB1 // 0: Disable RUNNING_LED 77 | #define _BAUDRATE_ 115200 78 | #define _SERIAL_ Serial2 79 | #define _MODEL_ "STM32F103 MAPLE Core" 80 | 81 | //for STM32F103(ST Core) 82 | #elif defined(ARDUINO_BLUEPILL_F103C8) || defined(ARDUINO_BLACKPILL_F103C8) || defined(ARDUINO_MAPLEMINI_F103CB) 83 | HardwareSerial Serial2(PA3, PA2); 84 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 85 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 86 | #define _BAUDRATE_ 115200 87 | #define _SERIAL_ Serial2 88 | #define _MODEL_ "STM32F103 ST Core" 89 | 90 | //for STM32F303(ST Core) 91 | #elif defined(ARDUINO_BLACKPILL_F303CC) 92 | HardwareSerial Serial2(PA3, PA2); 93 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 94 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 95 | #define _BAUDRATE_ 115200 96 | #define _SERIAL_ Serial2 97 | #define _MODEL_ "STM32F303 ST Core" 98 | 99 | //for STM32F401(ST Core) 100 | #elif defined(ARDUINO_BLACKPILL_F401CC) 101 | HardwareSerial Serial2(PA3, PA2); 102 | #define STOP_BUTTON PB13 // 0: Disable STOP_BUTTON 103 | #define RUNNING_LED PB12 // 0: Disable RUNNING_LED 104 | #define _BAUDRATE_ 115200 105 | #define _SERIAL_ Serial2 106 | #define _MODEL_ "STM32F401 ST Core" 107 | 108 | //for STM32F4DISC1(ST Core) 109 | #elif defined(ARDUINO_DISCO_F407VG) 110 | HardwareSerial Serial3(PD9, PD8); 111 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 112 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 113 | #define _BAUDRATE_ 115200 114 | #define _SERIAL_ Serial3 115 | #define _MODEL_ "STM32 F4DISC1 ST Core" 116 | 117 | //for STM32F407(ST Core) 118 | #elif defined(ARDUINO_DIYMORE_F407VGT) || defined(ARDUINO_BLACK_F407VE) || defined(ARDUINO_BLACK_F407VG) 119 | HardwareSerial Serial2(PA3, PA2); 120 | #define STOP_BUTTON PB11 // 0: Disable STOP_BUTTON 121 | #define RUNNING_LED PB10 // 0: Disable RUNNING_LED 122 | #define _BAUDRATE_ 115200 123 | #define _SERIAL_ Serial2 124 | #define _MODEL_ "STM32F407 ST Core" 125 | 126 | //for STM32 NUCLEO64(ST Core) 127 | #else 128 | HardwareSerial Serial1(PA10, PA9); 129 | #define STOP_BUTTON PB2 // 0: Disable STOP_BUTTON 130 | #define RUNNING_LED PB1 // 0: Disable RUNNING_LED 131 | #define _BAUDRATE_ 115200 132 | #define _SERIAL_ Serial1 133 | #define _MODEL_ "STM32 NUCLEO64 ST Core" 134 | #endif 135 | 136 | #define MQTT_SERVER "broker.hivemq.com" // MQTT broker 137 | //#define MQTT_SERVER "broker.emqx.io" // MQTT broker 138 | //#define MQTT_SERVER "iot.eclipse.org" // MQTT broker 139 | #define MQTT_PORT 1883 // MQTT port 140 | #define SUB_TOPIC "/ESP-AT-MQTT/#" // Subscribe topic 141 | #define MQTT_KEEP_ALIVE 60 142 | #define MAX_TOPIC 64 // You can change 143 | #define MAX_PAYLOAD 64 // You can change 144 | #define DNS_SERVER1 "8.8.8.8" // DNS SERVER1 145 | #define DNS_SERVER2 "8.8.4.4" // DNS SERVER2 146 | #define _DEBUG_ 0 // for Debug 147 | 148 | // Last Packet Send Time (MilliSecond) 149 | unsigned long lastSendPacketTime = 0; 150 | 151 | int buildConnect(char *buf, int keep_alive, char *client_id, char *will_topic, char *will_msg) { 152 | int rlen = 12; 153 | int pos = 14; 154 | 155 | int client_id_len = strlen(client_id); 156 | // Serial.println(client_id_len); 157 | buf[pos++] = 0x00; 158 | buf[pos++] = client_id_len; 159 | for(int i=0;i= 0x20) { 297 | Serial.print("<"); 298 | Serial.print(c); 299 | Serial.print(">"); 300 | } 301 | } 302 | 303 | 304 | void setup(){ 305 | Serial.begin(115200); 306 | delay(5000); 307 | Serial.print("_MODEL_="); 308 | Serial.println(_MODEL_); 309 | _SERIAL_.begin(_BAUDRATE_); 310 | 311 | //Save Serial Object 312 | serialSetup(_SERIAL_); 313 | 314 | if (RUNNING_LED) { 315 | pinMode(RUNNING_LED,OUTPUT); 316 | digitalWrite(RUNNING_LED,LOW); 317 | } 318 | if (STOP_BUTTON) pinMode(STOP_BUTTON, INPUT); 319 | 320 | //Enable autoconnect 321 | sendCommand("AT+CWAUTOCONN=1"); 322 | if (!waitForString("OK", 2, 1000)) { 323 | errorDisplay("AT+CWAUTOCONN Fail"); 324 | } 325 | clearBuffer(); 326 | 327 | //Restarts the Module 328 | sendCommand("AT+RST"); 329 | if (!waitForString("WIFI GOT IP", 11, 10000)) { 330 | errorDisplay("ATE+RST Fail"); 331 | } 332 | clearBuffer(); 333 | 334 | //Local echo off 335 | sendCommand("ATE0"); 336 | if (!waitForString("OK", 2, 1000)) { 337 | errorDisplay("ATE0 Fail"); 338 | } 339 | clearBuffer(); 340 | 341 | //Get My IP Address 342 | char IPaddress[64]; 343 | getIpAddress(IPaddress,sizeof(IPaddress),2000); 344 | Serial.print("IPaddress=["); 345 | Serial.print(IPaddress); 346 | Serial.println("]"); 347 | 348 | //Get My MAC Address 349 | char MACaddress[64]; 350 | getMacAddress(MACaddress,sizeof(MACaddress),2000); 351 | Serial.print("MACaddress=["); 352 | Serial.print(MACaddress); 353 | Serial.println("]"); 354 | 355 | //Get DNS Server Information 356 | sendCommand("AT+CIPDNS_CUR?"); 357 | if (!waitForString("OK", 2, 1000, true)) { 358 | errorDisplay("AT+CIPDNS_CUR Fail"); 359 | } 360 | clearBuffer(); 361 | 362 | //Set DNS Server Information 363 | //AT+CIPDNS_CUR=[,,] 364 | char cmd[128]; 365 | sprintf(cmd,"AT+CIPDNS_CUR=1,\"%s\",\"%s\"", DNS_SERVER1, DNS_SERVER2); 366 | sendCommand(cmd); 367 | if (!waitForString("OK", 2, 1000)) { 368 | errorDisplay("AT+CIPDNS_CUR Fail"); 369 | } 370 | clearBuffer(); 371 | 372 | //Get DNS Server Information again 373 | sendCommand("AT+CIPDNS_CUR?"); 374 | if (!waitForString("OK", 2, 1000, true)) { 375 | errorDisplay("AT+CIPDNS_CUR Fail"); 376 | } 377 | clearBuffer(); 378 | 379 | //Establishes TCP Connection 380 | sprintf(cmd,"AT+CIPSTART=\"TCP\",\"%s\",%d",MQTT_SERVER,MQTT_PORT); 381 | sendCommand(cmd); 382 | if (!waitForString("OK", 2, 5000)) { 383 | Serial.println("Check your MQTT SERVER"); 384 | errorDisplay("AT+CIPSTART Fail"); 385 | } 386 | clearBuffer(); 387 | 388 | //Client requests a connection to a server 389 | Serial.print("MQTT CONNECTT....."); 390 | //ATmegaではNULL,NULLは正しく動かない 391 | int msize = buildConnect(cmd,MQTT_KEEP_ALIVE,MACaddress,"",""); 392 | if (_DEBUG_) hexDump(cmd,msize); 393 | int ret = sendData(-1, cmd, msize, "", 0); 394 | if (ret) errorDisplay("MQTT Connect Fail"); 395 | 396 | //Wait for CONNACK 397 | if (!waitForString("+IPD", 4, 5000)) { 398 | errorDisplay("CONNACK Fail"); 399 | } 400 | getResponse(1000); 401 | Serial.println("OK"); 402 | 403 | //Client requests a subscribe to a server 404 | Serial.print("MQTT SUBSCRIBE....."); 405 | msize = buildSubscribe(cmd,SUB_TOPIC); 406 | if (_DEBUG_) hexDump(cmd,msize); 407 | ret = sendData(-1, cmd, msize, "", 0); 408 | if (ret) errorDisplay("MQTT Subscribe Fail"); 409 | 410 | //Wait for SUBACK 411 | if (!waitForString("+IPD", 4, 5000)) { 412 | errorDisplay("SUBACK Fail"); 413 | } 414 | getResponse(1000); 415 | Serial.println("OK"); 416 | 417 | Serial.println("Start MQTT Subscribe [" + String(_MODEL_) + "] from " + String(MQTT_SERVER)); 418 | lastSendPacketTime = millis(); 419 | } 420 | 421 | void loop(){ 422 | static int timer = 0; 423 | static int ledStatus = 1; 424 | 425 | if (STOP_BUTTON) { 426 | int buttonState = digitalRead(STOP_BUTTON); 427 | if (buttonState == 1) { 428 | mqttDisconnect(); 429 | Serial.println(); 430 | Serial.println("Sending DISCONNECT"); 431 | Serial.println("Publish end"); 432 | if (RUNNING_LED) digitalWrite(RUNNING_LED,LOW); 433 | while(1) { } 434 | } 435 | } 436 | 437 | /* 438 | * Subscribe Message Structure 439 | * 440 | * +IPD,AA:xCxEfffffffffggggg 441 | * <------AA--------> 442 | * 443 | * AA(ASCII):Total Data Length 444 | * C(BINARY):{number of f} + {number of g} + 2 445 | * E(BINARY):{number of f} 446 | * {number of g} = C-E-2 447 | * f:Topic 448 | * g:Payload 449 | * 450 | * Example 451 | * +IPD,XX:[0x30][0x1E][0x00][0x18][24 byte Topic][4 byte Payload] 452 | * C=0x1E=30 453 | * E=0x18=24 454 | * {number of f}=0x18=24 455 | * {number of g}=30-24-2=4 456 | */ 457 | 458 | 459 | int isSubscribe = 0; 460 | static int step = 0; 461 | static int len0 = 0; 462 | static int len1 = 0; 463 | static int len2 = 0; 464 | static int pos0 = 0; 465 | static int pos1 = 0; 466 | static int pos2 = 0; 467 | static char buf1[MAX_TOPIC]; 468 | static char buf2[MAX_PAYLOAD]; 469 | char puback[] = {0x40,0x02}; 470 | char c; 471 | 472 | while (_SERIAL_.available()) { 473 | c = _SERIAL_.read(); 474 | if (_DEBUG_) { 475 | putChar(c); 476 | Serial.print("step="); 477 | Serial.println(step); 478 | } 479 | // if (c == 0x0d) continue; 480 | // if (c == 0x0a) continue; 481 | if(step == 0 && c == '+') { 482 | step=1; 483 | } else if (step == 1) { 484 | if (c == 'I') step=2; 485 | else step=0; 486 | } else if (step == 2) { 487 | if (c == 'P') step=3; 488 | else step=0; 489 | } else if (step == 3) { 490 | if (c == 'D') step=4; 491 | else step=0; 492 | } else if (step == 4) { 493 | if (c == ',') step=5; 494 | else step=0; 495 | } else if (step == 5 && c == ':') { 496 | step=6; 497 | pos0 = 0; 498 | pos1 = 0; 499 | pos2 = 0; 500 | } else if (step == 6) { // get BCDE data 501 | if (pos0 == 1) len0 = c; // len0 is C 502 | if (pos0 == 3) len1 = c; // len1 is E 503 | pos0++; 504 | if (pos0 == 4) step=7; 505 | } else if (step == 7) { // get topic 506 | if (pos1 == MAX_TOPIC) { 507 | Serial.println("TOPIC is too large!!"); 508 | Serial.println("Check MAX_TOPIC"); 509 | continue; 510 | } 511 | buf1[pos1++]=c; 512 | buf1[pos1]=0; 513 | // Serial.print("pos1="); 514 | // Serial.println(pos1); 515 | // Serial.print("buf1="); 516 | // Serial.println(buf1); 517 | if (pos1 == len1) { 518 | step=8; 519 | len2 = len0 - len1 - 2; // len2 is payload size 520 | pos2 = 0; 521 | } 522 | } else if (step == 8) { // get payload 523 | if (pos2 == MAX_PAYLOAD) { 524 | Serial.println("PAYLOAD is too large!!"); 525 | Serial.println("Check MAX_PAYLOAD"); 526 | continue; 527 | } 528 | buf2[pos2++]=c; 529 | buf2[pos2]=0; 530 | // Serial.print("pos2="); 531 | // Serial.println(pos2); 532 | // Serial.print("buf2="); 533 | // Serial.println(buf2); 534 | if (pos2 == len2) { 535 | isSubscribe=1; 536 | step=0; 537 | } 538 | #if 0 539 | Serial.print("step="); 540 | Serial.print(step); 541 | Serial.print(" len0="); 542 | Serial.print(len0); 543 | Serial.print(" len1="); 544 | Serial.print(len1); 545 | Serial.print(" len2="); 546 | Serial.print(len2); 547 | Serial.print(" pos1="); 548 | Serial.print(pos1); 549 | Serial.print(" pos2="); 550 | Serial.println(pos2); 551 | #endif 552 | } 553 | } 554 | 555 | if (isSubscribe) { //Publish acknowledgment 556 | Serial.println(); 557 | Serial.println("Sending PUBACK"); 558 | int ret = sendData(-1, puback, 2, "", 0); 559 | if (ret) errorDisplay("PUBACK send Fail"); 560 | 561 | Serial.println(); 562 | for(int i=0;i<40;i++) Serial.print("-"); 563 | Serial.println(); 564 | Serial.print("Topic=["); 565 | Serial.print(buf1); 566 | Serial.println("]"); 567 | Serial.print("Payload=["); 568 | Serial.print(buf2); 569 | Serial.println("]"); 570 | for(int i=0;i<40;i++) Serial.print("-"); 571 | Serial.println(); 572 | isSubscribe = 0; 573 | } 574 | 575 | 576 | unsigned long now = millis(); 577 | if ( (now - lastSendPacketTime) < 0) { 578 | lastSendPacketTime = now; 579 | } 580 | if ( (now - lastSendPacketTime) > 1000) { 581 | lastSendPacketTime = now; 582 | timer++; 583 | if (RUNNING_LED) digitalWrite(RUNNING_LED,ledStatus); 584 | ledStatus = !ledStatus; 585 | if ( (timer % 10) == 0) Serial.print("+"); 586 | if ( (timer % 10) != 0) Serial.print("."); 587 | } 588 | mqttPingreq(); 589 | } 590 | --------------------------------------------------------------------------------