#include "arduino_wifi.h" #if defined(ARDUINO_ARCH_ESP32) #include #elif defined(ARDUINO_ARCH_ESP8266) #include #endif #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) extern "C" { void c_wifi_connect(mrb_vm *vm, mrb_value *v, int argc){ if(argc>1) WiFi.begin( mrbc_string_cstr(&v[1]), mrbc_string_cstr(&v[2]) ); } void c_wifi_disconnect(mrb_vm *vm, mrb_value *v, int argc){ WiFi.disconnect(true); } void c_wifi_connected(mrb_vm *vm, mrb_value *v, int argc){ SET_BOOL_RETURN( WiFi.status() == WL_CONNECTED ); } void c_wifi_ip(mrb_vm *vm, mrb_value *v, int argc){ IPAddress ip=WiFi.localIP(); char buf[16]; sprintf(buf,"%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]); SET_RETURN( mrbc_string_new_cstr(vm, buf) ); } void c_wifi_mac(mrb_vm *vm, mrb_value *v, int argc){ SET_RETURN( mrbc_string_new_cstr(vm, WiFi.macAddress().c_str()) ); } void c_wifi_get(mrb_vm *vm, mrb_value *v, int argc){ const char* host=mrbc_string_cstr(&v[1]); const char* file=mrbc_string_cstr(&v[2]); int time_out=500; //ms int port_num=443; MRBC_KW_ARG( timeout, port ); if( MRBC_KW_ISVALID(timeout) ) time_out = mrbc_integer(timeout); if( MRBC_KW_ISVALID(port) ) port_num = mrbc_integer(port); MRBC_KW_DELETE( timeout, port ); bool ssl = !(port_num%100==80); BearSSL::WiFiClientSecure client; WiFiClient wclient=WiFiClient(client); if(ssl){ client.setTimeout(time_out); client.setInsecure(); }else{ wclient.setTimeout(time_out);} if (!( ssl ? client.connect(host,port_num) : wclient.connect(host,port_num) )) { //if (!client.connect(host,port_num)) { SET_RETURN( mrbc_string_new_cstr(vm, "connection failed") ); return; } String http_string=String(" ")+ file + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP8266/1.0\r\n" + "Connection: close\r\n"; if(argc>2){ const char* data=mrbc_string_cstr(&v[3]); http_string = String("POST") + http_string + "Content-Type: application/x-www-form-urlencoded;\r\n" + "Content-Length: " + String(strlen(data)) + "\r\n\r\n" + data; } else{ http_string = String("GET") + http_string + "\r\n"; } if(ssl){ client.print(http_string); } else{ wclient.print(http_string); } unsigned long count = micros(); while ( (ssl ? client.available() : wclient.available() ) == 0) { if ( micros() - count > time_out*1100) { if(ssl){client.stop();}else{wclient.stop();} SET_RETURN( mrbc_string_new_cstr(vm, "timeout") ); return; } } bool body=false; String str=""; while( ssl ? client.available() : wclient.available() ){ String line = ssl ? client.readStringUntil('\r') : wclient.readStringUntil('\r'); if(body) str+=line+"\r"; if(line.length()<3) body=true; if(str.length()>1000) break; } if(ssl){client.stop();}else{wclient.stop();} SET_RETURN( mrbc_string_new_cstr(vm, str.c_str()) ); } void mrbc_init_class_wifi(void){ mrb_class *wifi = mrbc_define_class(0, "WIFI", mrbc_class_object); mrbc_define_method(0, wifi, "connect", c_wifi_connect); mrbc_define_method(0, wifi, "disconnect", c_wifi_disconnect); mrbc_define_method(0, wifi, "connected?", c_wifi_connected); mrbc_define_method(0, wifi, "ip", c_wifi_ip); mrbc_define_method(0, wifi, "mac", c_wifi_mac); mrbc_define_method(0, wifi, "get", c_wifi_get); mrbc_define_method(0, wifi, "post", c_wifi_get); } } //extern #endif