wifinetwork.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. goog.provide('Blockly.Python.wifinetwork');
  3. goog.require('Blockly.Python');
  4. Blockly.Python['wifinetwork_connect_setup'] = function(block) {
  5. var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
  6. var wifi_wifimulti_include_declaration = "#include <ESP8266WiFiMulti.h>";
  7. Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
  8. Blockly.Python.addInclude("wifi_wifimulti_include_declaration", wifi_wifimulti_include_declaration);
  9. var ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC) || "";
  10. var pw = Blockly.Python.valueToCode(block, 'PW', Blockly.Python.ORDER_ATOMIC) || "";
  11. var wifiMulti_declaration = "ESP8266WiFiMulti WiFiMulti;\n";
  12. Blockly.Python.addDeclaration("wifiMulti_declaration", wifiMulti_declaration);
  13. var code = " delay(10);\n" +
  14. // Start by connecting to a WiFi network
  15. " WiFiMulti.addAP(" + ssid + "," + pw + ");\n" +
  16. " Serial.print(\"Wait for WiFi... \");\n" +
  17. " while(WiFiMulti.run() != WL_CONNECTED) {\n" +
  18. " Serial.print(\".\");\n" +
  19. " delay(500);\n" +
  20. " }\n" +
  21. " Serial.println(\"\");\n" +
  22. " Serial.println(\"WiFi connected\");";
  23. Blockly.Python.addSetup("wifinetwork_connect", code);
  24. return '';
  25. };
  26. Blockly.Python['wifinetwork_getMacAddr'] = function(block) {
  27. var code = "WiFi.macAddress()";
  28. return [code, Blockly.Python.ORDER_ATOMIC];
  29. };
  30. Blockly.Python['wifinetwork_getGatewayIp'] = function(block) {
  31. var code = "WiFi.gatewayIP()";
  32. return [code, Blockly.Python.ORDER_ATOMIC];
  33. };
  34. Blockly.Python['wifinetwork_AP_setup'] = function(block) {
  35. var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
  36. Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
  37. var wifi_client_declaration = "#include <WiFiClient.h>\n";
  38. Blockly.Python.addInclude("wifi_client_declaration", wifi_client_declaration);
  39. var wifi_esp8266_webserver_declaration = "#include <ESP8266WebServer.h>\n";
  40. Blockly.Python.addInclude("wifi_esp8266_webserver_declaration", wifi_esp8266_webserver_declaration);
  41. var ssid = Blockly.Python.valueToCode(block, 'SSID', Blockly.Python.ORDER_ATOMIC) || "";
  42. var pw = Blockly.Python.valueToCode(block, 'PW', Blockly.Python.ORDER_ATOMIC) || "";
  43. // var handleRoot = 'void handleRoot() {\n'+
  44. // 'server.send(200, "text/html", "<h1>You are connected</h1>");\n'+
  45. // '}\n';
  46. // Blockly.Python.addFunction("handleRoot",handleRoot);
  47. var AP_setup = 'delay(1000);\n' +
  48. 'Serial.begin(9600);\n' +
  49. 'Serial.println();\n' +
  50. 'Serial.print("Configuring access point...");\n' +
  51. /* You can remove the password parameter if you want the AP to be open. */
  52. 'WiFi.softAP(' + ssid + ',' + pw + ');\n' +
  53. 'IPAddress myIP = WiFi.softAPIP();\n' +
  54. 'Serial.print("AP IP address: ");\n' +
  55. 'Serial.println(myIP);\n';
  56. // 'server.on("/", handleRoot);\n'+
  57. // 'server.begin();\n'+
  58. // 'Serial.println("HTTP server started");';
  59. Blockly.Python.addSetup("AP_setup", AP_setup);
  60. // var code = "server.handleClient();\n";
  61. return '';
  62. }
  63. /*************************
  64. * version 2 *
  65. * eaysmode *
  66. *************************/
  67. Blockly.Python['wifiNetwork_network'] = function(block) {
  68. var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
  69. var wifi_wifimulti_include_declaration = "#include <ESP8266WiFiMulti.h>";
  70. Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
  71. Blockly.Python.addInclude("wifi_wifimulti_include_declaration", wifi_wifimulti_include_declaration);
  72. var ssid = block.getFieldValue("wifi_ssid");
  73. var pw = block.getFieldValue("wifi_pw");
  74. var wifiMulti_declaration = "ESP8266WiFiMulti WiFiMulti;\n";
  75. Blockly.Python.addDeclaration("wifiMulti_declaration", wifiMulti_declaration);
  76. let serial_setup = "Serial.begin(9600);";
  77. Blockly.Python.addSetup('serial_9600', serial_setup);
  78. var code = " WiFi.mode(WIFI_STA);\n delay(10);\n" +
  79. // Start by connecting to a WiFi network
  80. " WiFiMulti.addAP(\"" + ssid + "\",\"" + pw + "\");\n" +
  81. " Serial.print(\"Wait for WiFi... \");\n" +
  82. " while(WiFiMulti.run() != WL_CONNECTED) {\n" +
  83. " Serial.print(\".\");\n" +
  84. " delay(500);\n" +
  85. " }\n" +
  86. " Serial.println(\"\");\n" +
  87. " Serial.println(\"WiFi connected\");";
  88. Blockly.Python.addSetup("wifinetwork_connect", code);
  89. return '';
  90. }
  91. Blockly.Python['wifiNetwork_AP'] = function(block) {
  92. var wifi_esp8266_include_declaration = "#include <ESP8266WiFi.h>\n";
  93. Blockly.Python.addInclude("wifi_esp8266_declaration", wifi_esp8266_include_declaration);
  94. var wifi_client_declaration = "#include <WiFiClient.h>\n";
  95. Blockly.Python.addInclude("wifi_client_declaration", wifi_client_declaration);
  96. var wifi_esp8266_webserver_declaration = "#include <ESP8266WebServer.h>\n";
  97. Blockly.Python.addInclude("wifi_esp8266_webserver_declaration", wifi_esp8266_webserver_declaration);
  98. let serial_setup = "Serial.begin(9600);";
  99. Blockly.Python.addSetup('serial_9600', serial_setup);
  100. var ssid = block.getFieldValue('SSID') || "";
  101. var pw = block.getFieldValue('PW') || "";
  102. var AP_setup = 'delay(1000);\n' +
  103. 'Serial.println();\n' +
  104. 'Serial.print("Configuring access point...");\n' +
  105. /* You can remove the password parameter if you want the AP to be open. */
  106. 'WiFi.softAP("' + ssid + '","' + pw + '");\n' +
  107. 'IPAddress myAPIP = WiFi.softAPIP();\n' +
  108. 'Serial.print("AP IP address: ");\n' +
  109. 'Serial.println(myAPIP);\n';
  110. Blockly.Python.addSetup("AP_setup", AP_setup);
  111. return '';
  112. }
  113. Blockly.Python['wifiNetwork_getLocalIp'] = function(block) {
  114. Blockly.Python.addDeclaration("myWiFiModuleIp","String myWiFiModuleIp;")
  115. Blockly.Python.addSetup("getlocalIP",'IPAddress myIP = WiFi.localIP();\n'+
  116. ' myWiFiModuleIp = String(myIP[0]) + "." + String(myIP[1]) + "." + String(myIP[2]) + "." + String(myIP[3]);\n')
  117. var code = "myWiFiModuleIp";
  118. return [code, Blockly.Python.ORDER_ATOMIC];
  119. };
  120. Blockly.Python['wifiNetwork_isConnected'] = function(block) {
  121. var code = "(WiFiMulti.run() == WL_CONNECTED)";
  122. return [code, Blockly.Python.ORDER_ATOMIC];
  123. };
  124. Blockly.Python['wifiNetwork_getStationNum'] = function(block) {
  125. var code = "WiFi.softAPgetStationNum()";
  126. return [code, Blockly.Python.ORDER_ATOMIC];
  127. }