乐鑫技术交流
直播中

李莉

7年用户 1420经验值
私信 关注
[问答]

esp8266和canvas仪表问题求解

大家好!
终于有时间来攻击这个项目了。
它包括从 Pot 收集数据(在 NodeMcu 上模拟读取),将数据传递到网页,然后在仪表上显示数据。
有用!!!
以下是 Sketch 的主要部分: Ino
代码:全选* Wifi-AP MODE
* 07/11/2019
*/

#include
#include
#include    //Include File System Headers

const char* htmlfile = "/index.html";

          //WiFi Connection configuration
const char *ssid = "My Wifi";
const char *password = "123456789";

  ESP8266WebServer server(80);

void handleADC(){
  int a = analogRead(A0);
  a = map(a,0,1023,0,100);
  String adc = String(a);
  Serial.println(adc);
  server.send(200, "text/plane",adc); //sends data to server
}

void handleRoot(){
  server.sendHeader("Location", "/index.html",true);   //Redirect to our html web page
  server.send(302, "text/plane","");
}

void handleWebRequests(){
  if(loadFromSpiffs(server.uri())) return;
  String message = "File Not Detectednn";
  message += "URI: ";
  message += server.uri();
  message += "nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "nArguments: ";
  message += server.args();
  message += "n";
  for (uint8_t i=0; i     message += " NAME:"+server.argName(i) + "n VALUE:" + server.arg(i) + "n";
  }
  server.send(404, "text/plain", message);
  Serial.println(message);
}

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();


             //Initialize File System
  SPIFFS.begin();
  Serial.println("File System Initialized");
  Serial.print("Setting AP (Access Point)…");
  Serial.println();

             //Connect to wifi Network
  WiFi.mode(WIFI_AP);           //Only Access point
  WiFi.softAP(ssid, password);   //Start HOTspot


      //If connection successful show IP address in serial monitor
  Serial.print("Connected to: ");
  Serial.println(ssid);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("Server IP address: ");
  Serial.println(IP);
  

              //Initialize Webserver
  server.on("/",handleRoot);
  server.on("/getADC",handleADC); //Reads ADC function is called from out index.html
  server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
  server.begin();
  Serial.println("HTTP server started");  
}

void loop() {
server.handleClient();
}

bool loadFromSpiffs(String path){
  String dataType = "text/plain";
  if(path.endsWith("/")) path += "index.htm";
  if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  else if(path.endsWith(".html")) dataType = "text/html";
  else if(path.endsWith(".htm")) dataType = "text/html";
  else if(path.endsWith(".css")) dataType = "text/css";
  else if(path.endsWith(".js")) dataType = "application/javascript";
  else if(path.endsWith(".png")) dataType = "image/png";
  else if(path.endsWith(".gif")) dataType = "image/gif";
  else if(path.endsWith(".jpg")) dataType = "image/jpeg";
  else if(path.endsWith(".ico")) dataType = "image/x-icon";
  else if(path.endsWith(".xml")) dataType = "text/xml";
  else if(path.endsWith(".pdf")) dataType = "application/pdf";
  else if(path.endsWith(".zip")) dataType = "application/zip";
  File dataFile = SPIFFS.open(path.c_str(), "r");
  if (server.hasArg("download")) dataType = "application/octet-stream";
  if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  }

  dataFile.close();
  return true;
}


这是 Index.html:
代码:全选


    Testing Gauges
   
   








Testing Gauge


  




   

   

更多回帖

发帖
×
20
完善资料,
赚取积分