micro:bit - ESP8266 ESP-01 Serial Wireless WIFI Module

OS/micro:bit 2018.02.23 23:31 Posted by 파란크리스마스

출처 : micro:bit IoT In C - Getting On WiFi

micro:bit - ESP8266 ESP-01 Serial Wireless WIFI Module

pulse-combined.hex

micro:bit의 한가지 아쉬운 점은 Wifi가 내장되어 있지 않아 Wifi를 따로 설치해야 되는데, 이번에 아두이노에서 많이 사용하는 Wifi 모듈인 ESP8266을 사용해서 공유기에 접속하고 간단하게 웹서비스로 온도를 제공하는 서비스를 구현했습니다. 

결과는 PC 브라우져로 접속하면 결과를 확인 해볼 수 있습니다.

배선

핀 배열

  1. Ground - connect to ground 
  2. TXO - the serial tx pin 
  3. GPIO2 - ignore 
  4. CHPD - chip enable connect to 3.3V 
  5. GPIO0 - ignore 
  6. RST - reset leave unconnected 
  7. RXI - the serial rx pin 
  8. VDD - Supply voltage connect to 3.3V

소스

#include "MicroBit.h" 

MicroBit uBit;

void initWiFi();
int ATWiFi();
int resetWiFi();
int setUARTWiFi();
int scanWiFi();
int getIPWiFi(); 
int modeWiFi(int mode);
int connectWiFi(ManagedString ssid, ManagedString pass); int getWebPageWiFi(ManagedString URL, ManagedString page); 
int getVersionWiFi(); 
int startServerWiFi(); 
int waitForWiFi(ManagedString target, int retry, int pause);
int find(ManagedString c, ManagedString s);
void debug(ManagedString s);

#define Tx MICROBIT_PIN_P0 
#define Rx MICROBIT_PIN_P1 
#define DEBUG 1

int main() { 
	uBit.init();
	initWiFi();
	modeWiFi(1);
	connectWiFi("ssid", "password");
	getIPWiFi();
	startServerWiFi();
	release_fiber();
}

void initWiFi() { 
	uBit.serial.redirect(Tx, Rx);
	uBit.serial.baud(115200);
	uBit.serial.setRxBufferSize(500);
}

int ATWiFi() {
	uBit.serial.send("AT\r\n", SYNC_SPINWAIT);
	return waitForWiFi("OK", 150, 10); 
}

int getVersionWiFi() {
	uBit.serial.send("AT+GMR\r\n", SYNC_SPINWAIT);
	return waitForWiFi("OK", 200, 10);
}

int resetWiFi() { 
	uBit.serial.send("AT+RST\r\n", SYNC_SPINWAIT);
	return waitForWiFi("OK", 1000, 10);
}

int setUARTWiFi() { 
	uBit.serial.send("AT+UART_CUR=115200,8,1,0,0\r\n", SYNC_SPINWAIT);
	return waitForWiFi("OK", 200, 10);
}

int scanWiFi() {
	uBit.serial.send("AT+CWLAP\r\n", SYNC_SPINWAIT);
	return waitForWiFi("OK", 500, 50);
}

int modeWiFi(int mode) {
	ManagedString cmd = "AT+CWMODE_CUR=" + ManagedString(mode) + "\r\n";
	uBit.serial.send(cmd, SYNC_SPINWAIT);
	return waitForWiFi("OK", 200, 10); 
}

int connectWiFi( ManagedString ssid, ManagedString pass) { 
	ManagedString cmd = "AT+CWJAP_CUR=\"" + ssid + "\",\"" + pass + "\"\r\n";
	uBit.serial.send(cmd, SYNC_SPINWAIT);
	return waitForWiFi("OK", 200, 20);
}

int getIPWiFi() {
	uBit.serial.send("AT+CIFSR\r\n", SYNC_SPINWAIT);
	return waitForWiFi("OK", 200, 10);
}

int getWebPageWiFi(ManagedString URL, ManagedString page) {
	ManagedString cmd = "AT+CIPSTART=\"TCP\",\"" + URL + "\",80\r\n";
	uBit.serial.send(cmd, SYNC_SPINWAIT);
	
	if (waitForWiFi("OK", 100, 20) == 0) 
		return 0;
		
	ManagedString http = "GET " + page + " HTTP/1.0\r\nHost:" + URL + "\r\n\r\n";
	cmd = "AT+CIPSEND=" +  ManagedString(http.length()) + "\r\n"; 
	uBit.serial.send(cmd, SYNC_SPINWAIT); 
	
	int retry; 
	ManagedString s;
	s = "";
	retry = 40;
	
	do {
		uBit.sleep(100);
		s = s + uBit.serial.read(500, ASYNC); 
		retry--;
	} while (find(">", s) == 0 && retry != 0);
	
	uBit.serial.send(http, SYNC_SPINWAIT);
	retry = 100; 
	
	do { 
		uBit.sleep(100);
		s = s + uBit.serial.read(500, ASYNC);
		retry--;
	} while (s.length() < 500 && retry != 0);
	
	if (DEBUG)debug("\n\rPage\n\r" + s + "\n\r");
	return 1; 
}

int startServerWiFi() {  
	uBit.serial.send("AT+CIPMUX=1\r\n", SYNC_SPINWAIT);
	if (waitForWiFi("OK", 100, 20) == 0) return 0; 
	uBit.serial.send("AT+CIPSERVER=1,80\r\n", 
	                                  SYNC_SPINWAIT);
	if (waitForWiFi("OK", 100, 20) == 0) return 0;
	ManagedString s;
	for (;;) {
		s="";
		do {
		 uBit.sleep(100);
		 if(s>500)s=""; 
		 s = uBit.serial.read(500, ASYNC);
		} while (find("+IPD", s) == 0);
	
		if (DEBUG)debug("\n\rClient Connected\n\r" + s + "\n\r");
			
		int b = find("+IPD", s);
		s = s.substring(b + 1, s.length());
		b = find(",", s); 
		s = s.substring(b + 1, s.length());
		b = find(",", s);
		ManagedString id = s.substring(0, b);
		
		if (DEBUG)debug("\n\rTCP id:" + id + "\n\r"); 
			
		ManagedString headers = "HTTP/1.0 200 OK\r\n";
		headers = headers + "Server: micro:bit\r\n"; 
		headers = headers + "Content-type: text/html\r\n\r\n";
		// ManagedString html = "<html><head><title>Temperature</title></head><body>{\"humidity\":82%,\"airtemperature\":23.5C}</p></body></html>\r\n";
		
		char html [100];
		sprintf (html, "<html><head><title>Temperature</title></head><body>{\"airtemperature\":%dC}</p></body></html>\r\n", uBit.thermometer.getTemperature());
		
		//ManagedString html = buffer;
		ManagedString data = headers + html;
		ManagedString cmd = "AT+CIPSEND=" + id + "," + ManagedString(data.length()) + "\r\n";
		uBit.serial.send(cmd, SYNC_SPINWAIT);
		
		s = "";
		int retry = 40;
		
		do {
			uBit.sleep(100);
			s = s + uBit.serial.read(500, ASYNC);
			retry--;
		} while (find(">", s) == 0 && retry != 0);
	
		uBit.serial.send(data, SYNC_SPINWAIT);
		if (waitForWiFi("OK", 100, 100) == 0) return 0;
			
		if (DEBUG)debug("\n\rData Sent\n\r");
			
		cmd = "AT+CIPCLOSE=" + id + "\r\n"; 
		uBit.serial.send(cmd, SYNC_SPINWAIT);
		if (waitForWiFi("OK", 100, 100) == 0) return 0;
	}
}

void debug(ManagedString s) {
	uBit.serial.redirect(USBTX, USBRX);
	uBit.serial.send(s, SYNC_SPINWAIT);
	uBit.serial.redirect(Tx, Rx);
}

int find(ManagedString c, ManagedString s) {
	int i; 
	for (i = 0; i < (s.length() - c.length()); i++) {
		if (c == s.substring(i, c.length())) break; 
	}
	if (i == (s.length() - c.length())) return 0;
	return i; 
}

int waitForWiFi(ManagedString target, int retry, int pause) {
	ManagedString s;
	do { 
		uBit.sleep(pause);
		if(s.length()>500)s="";
		s = s + uBit.serial.read(500, ASYNC); retry--;
	} while (find(target, s) == 0 && retry != 0);
	
	if (DEBUG)debug("\n\r" + s + "\n\r");
	return retry;
}

실행 - 콘솔 로그

실행결과 - 브러우저에서 확인