The final project of Embedde class.

Dependencies:   C12832 LM75B ESP-call MMA7660

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WIFI.cpp Source File

WIFI.cpp

00001 #include "WIFI.h"
00002 
00003 char wifiIpAddress[16] = {0,};
00004 char wifiSSID[1025] = "fig";
00005 char wifiSSIDPassword[1025] = "10281028";
00006 volatile char wifiReceivedBuffer[2049] = {0};
00007 volatile int wifiReceivedBufferIndex = 0;
00008 
00009 Semaphore semaphoreWifiHasBuffer(0, 1);
00010 
00011 void wifiInit(void)
00012 {
00013     PC.baud(115200);
00014     ESP.baud(115200);
00015     PC.printf("\r\nReset ESP...\r\n");
00016     ESP_reset();
00017     PC.printf("Setup ESP noecho...\r\n");
00018     ESP_noecho();
00019 }
00020 
00021 void wifiConnectToAP(void)
00022 {
00023     char wifiSSIDInstruction[1042];
00024     char wifiSSIDPasswordInstruction[1041];
00025 
00026     PC.printf("\r\nTry to connect to %s wifi!!\r\n", wifiSSID);
00027 
00028     sprintf(wifiSSIDInstruction, "station_cfg.ssid=\"%s\"", wifiSSID);
00029     sprintf(wifiSSIDPasswordInstruction, "station_cfg.pwd=\"%s\"", wifiSSIDPassword);
00030 
00031     NodeMCU_PGM wifiConnect[] = {
00032         "", 1,
00033         "wifi.setmode(wifi.STATION)", 1,
00034         "", 1,
00035         "station_cfg={}", 1,
00036         wifiSSIDInstruction, 1,
00037         wifiSSIDPasswordInstruction, 1,
00038         "station_cfg.save=false", 1,    //false
00039         "wifi.sta.config(station_cfg)", 1,
00040         "", 1,
00041         "wifi.sta.connect()", 80, // long delay here!
00042         NULL, 0,
00043     };
00044 
00045     ESP_call_multi(wifiConnect);
00046 }
00047 
00048 char* wifiGetIpAddress(void)
00049 {
00050     NodeMCU_PGM wifiGetIpAddress[] = {
00051         "", 1,
00052         "print(wifi.sta.getip())", 1,
00053         NULL, 0,
00054     };
00055 
00056     ESP_call_multi(wifiGetIpAddress);
00057 
00058     char *pIp[2] = {ESP_recv_buffer, 0};
00059 
00060     /*
00061     ESP_recv_buffer 안에 저장된 xxx.xxx.xxx.xxx   yyy.yyy.yyy.yyy zzz.zzz.zzz중 xxx.xxx.xxx.xxx IP를 얻기 위해
00062     xxx.xxx.xxx.xxx가 시작하는 문자열 상의 주소와 끝나는 주소 값이 저장됩니다.
00063     즉 \t가 나올 때까지 검사합니다.
00064     */
00065 
00066     pIp[1] = strchr(pIp[0] + 1, '\t');
00067 
00068     int ipLength = pIp[1] - pIp[0];
00069 
00070     for(int i = 0; i < ipLength; i++) {
00071         wifiIpAddress[i] = *(pIp[0] + i);
00072     }
00073 
00074     PC.printf("\r\nYour IP: %s\r\n", wifiIpAddress);
00075 
00076     return wifiIpAddress;
00077 }
00078 
00079 void wifiInitServer(void)
00080 {
00081     NodeMCU_PGM wifiServerCode[] = {
00082         "", 1,
00083         "http_resp = \"HTTP/1.0 200 OK\\r\\nContent-Type: text/html\\r\\n\\r\\n\"", 1,
00084         "srv = net.createServer(net.TCP)", 1,
00085         "srv:listen(80, function(conn)", 1,
00086         "   conn:on(\"receive\", function(sck, payload)", 1,
00087         "       if string.match(payload, \"NEW_ALARM_DATA\") then", 1,
00088         "           i, j = string.find(payload, \"NEW_ALARM_DATA\")", 1,
00089         "           i = i + 15", 1,
00090         "           j = j + 14", 1,
00091         "           print(\"@\"..string.sub(payload, i, j)..\"~\")", 1,
00092         "       end", 1,
00093         "       sck:send(http_resp)", 1,
00094         "       sck:send(htmlTag)", 1,
00095         "       sck:send(htmlOpenTag)", 1,
00096         "       sck:send(headOpenTag)", 1,
00097         "       sck:send(styleOpenTag)", 1,
00098         "       sck:send(styleTag)", 1,
00099         "       sck:send(styleCloseTag)", 1,
00100         "       sck:send(headTag)", 1,
00101         "       sck:send(headCloseTag)", 1,
00102 
00103         "       sck:send(bodyOpenTag)", 1,
00104         "       sck:send(divOpenTag)", 1,
00105 
00106 
00107         "       sck:send(brTags)", 1,
00108         "       sck:send(table1OpenTag)", 1,
00109         "       sck:send(table1HeadOpenTag)", 1,
00110         "       sck:send(table1HeadRow1Tag)", 1,
00111         "       sck:send(table1HeadRow2Tag)", 1,
00112         "       sck:send(table1HeadRow3Tag)", 1,
00113         "       sck:send(table1HeadCloseTag)", 1,
00114 
00115         "       sck:send(table1BodyOpenTag)", 1,
00116         "       sck:send(string.format(table1BodyRow1Tag, timeState, timeHour, timeMinute, timeSecond))", 1,
00117         "       sck:send(string.format(table1BodyRow2Tag, alarmState, alarmHour, alarmMinute, alarmSecond))", 1,
00118         "       sck:send(string.format(table1BodyRow3Tag, temperature, temperatureUnit))", 1,
00119         "       sck:send(string.format(table1BodyRow4Tag, ledState, ledColor))", 1,
00120         "       sck:send(table1BodyCloseTag)", 1,
00121         "       sck:send(table1CloseTag)", 1,
00122 
00123         "       sck:send(brTags)", 1,
00124 
00125         "       sck:send(table2OpenTag)", 1,
00126         "       sck:send(table2HeadOpenTag)", 1,
00127         "       sck:send(table2HeadRow1Tag)", 1,
00128         "       sck:send(table2HeadRow2Tag)", 1,
00129         "       sck:send(table2HeadRow3Tag)", 1,
00130         "       sck:send(table2HeadCloseTag)", 1,
00131 
00132         "       sck:send(table2BodyOpenTag)", 1,
00133 //        "       sck:send(table2BodyRow1Tag)", 1,
00134 //        "       sck:send(table2BodyRow2Tag)", 1,
00135         "       sck:send(table2BodyRow3Tag)", 1,
00136 //        "       sck:send(table2BodyRow4Tag)", 1,
00137 //        "       sck:send(table2BodyRow5Tag)", 1,
00138         "       sck:send(table2BodyCloseTag)", 1,
00139         "       sck:send(table2CloseTag)", 1,
00140 
00141 
00142         "       sck:send(divCloseTag)", 1,
00143         "       sck:send(bodyCloseTag)", 1,
00144         "       sck:send(htmlCloseTag)", 1,
00145         "       end)", 1,
00146         "   conn:on(\"sent\", function(sck) sck:close() end)", 1,
00147         "end)", 1,
00148         NULL, 0,
00149     };
00150 
00151     ESP_call_multi(wifiServerCode);
00152 }
00153 
00154 void wifiGenerateValueF(const char *value, float data)
00155 {
00156     ESP.printf("%s = %f\r\n", value, data);
00157 }
00158 
00159 void wifiGenerateValueI(const char *value, int data)
00160 {
00161     ESP.printf("%s = %d\r\n", value, data);
00162 }
00163 
00164 void wifiGenerateValueS(const char *value, const char *data)
00165 {
00166     ESP.printf("%s = \"%s\"\r\n", value, data);
00167 }
00168 
00169 void wifiGenerateValueS(const char *value, char *data)
00170 {
00171     ESP.printf("%s = \"%s\"\r\n", value, data);
00172 }
00173 
00174 void wifiReceiveData(void)
00175 {
00176     int data = ESP.getc();
00177     if((data != ' ') && (data != '>')) {
00178         wifiReceivedBuffer[wifiReceivedBufferIndex++] = (char) data;
00179         if(data == '~') {
00180             wifiReceivedBuffer[wifiReceivedBufferIndex] = '\0';
00181             semaphoreWifiHasBuffer.release();
00182         }
00183     } else {
00184         wifiReceivedBuffer[0] = '\0';
00185         wifiReceivedBufferIndex = 0;
00186     }
00187 }
00188 
00189 void wifiInitReceiveData(void)
00190 {
00191     ESP.attach(wifiReceiveData, Serial::RxIrq);
00192 }