【问题标题】:Save data from NodeMCU to PHP MySQL将数据从 NodeMCU 保存到 PHP MySQL
【发布时间】:2017-07-22 16:20:40
【问题描述】:

我目前正在为我的最终项目工作,但我遇到了问题。我想使用 XAMPP 和 phpMyAdmin 将我的传感器数据发送到 MySQL 数据库。我一直在使用 NodeMCU 进行 WiFi 连接。但我无法发送值。

这是我工作的完整代码。

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiEsp.h>
#include <WiFiEspClient.h>

// Sensor 1
int sensorPin1 = 5;
int ledPin1 = 13;

// Sensor 2
int sensorPin2 = 4;
int ledPin2 = 12;

// Sensor 3
int sensorPin3 = 0;
int ledPin3 = 14;

//Wifi Connection
const char* ssid = "xxxxxx";
const char* password = "xxxxx";
const char* web = "192.168.1.69";

int status = WL_IDLE_STATUS;

WiFiServer server(80);

//Initialize the Wifi werver library
WiFiClient client;


void setup(void) {
  //start serial port
  Serial.begin(9600);

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

  // Connect to WiFi network  
  Serial.println();  
  Serial.println();  
  Serial.print("Connecting to ");  
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {    
   delay(500);    
   Serial.print(".");  
   }  
   Serial.println("");  
   Serial.println("WiFi connected");
   // Start the server  
   server.begin();  
   Serial.println("Server started");
   // Print the IP address  
   Serial.print("This is your ip address: ");  
   Serial.print(WiFi.localIP());  

}

void loop() {
  // Check if a client has connected  
  WiFiClient client = server.available();  
  if (!client) {    
    return;
  }
  // Wait until the client sends some data  
  Serial.println("new client");
  while(!client.available()){    
    delay(1);  
  }
  // Read the first line of the request  
  String request = client.readStringUntil('\r');  
  Serial.println(request);  
  client.flush(); // Match the request
 if (client.connect(web, 80)) {
  int sensor = digitalRead(sensorPin1);
  int sensor1 = digitalRead(sensorPin2);
  int sensor2 = digitalRead(sensorPin3);
  //connect to the server (your computer or web page)
    Serial.println("--> connection ok\n");
    client.print("POST /smart_parking/sensor.php?"); //This
    client.print("sensor=");
    client.print(sensor);
    client.print("&sensor1=");
    client.print(sensor1);
    client.print("&sensor2=");
    client.print(sensor2);
//    client.print("sensor="); //This
//    client.print("100");
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(web);
    client.println("Connection: close"); //Part of the GET request telling the server that we are over transmitting the message
    client.println();//empty line
    client.println(); //empty line
    client.stop(); //Closing connection to server
    Serial.println("--> finished transmission\n");
   }
    else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
}

【问题讨论】:

  • 密码不错....

标签: php mysql arduino nodemcu


【解决方案1】:

希望对您有所帮助! :)

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

// Sensor 1
int sensorPin1 = 5;
int ledPin1 = 13;

// Sensor 2
int sensorPin2 = 4;
int ledPin2 = 12;

// Sensor 3
int sensorPin3 = 0;
int ledPin3 = 14;

void setup() {

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  Serial.begin(115200);                                  //Serial connection
  WiFi.begin("yourSSID", "yourPASS");   //WiFi connection

  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion

    delay(500);
    Serial.println("Waiting for connection");

  }

}

void loop() {

 if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status

   HTTPClient http;    //Declare object of class HTTPClient
   int sensor = digitalRead(sensorPin1);
   int sensor1 = digitalRead(sensorPin2);
   int sensor2 = digitalRead(sensorPin3);
   http.begin("http://192.168.1.69:80/smart_parking/sensor.php");      //Specify request destination
   http.addHeader("Content-Type", "text/plain");  //Specify content-type header
   String reqBody = "sensor=" + sensor + "&sensor1=" + sensor1 + "&sensor2=" + sensor2;
   int httpCode = http.POST(reqBody);   //Send the request
   String payload = http.getString();                  //Get the response payload

   Serial.println(httpCode);   //Print HTTP return code
   Serial.println(payload);    //Print request response payload

   http.end();  //Close connection

 }else{

    Serial.println("Error in WiFi connection");   

 }

  delay(30000);  //Send a request every 30 seconds

}

【讨论】:

    【解决方案2】:

    使用这个示例代码

    #include <ESP8266WiFi.h>
    #include <WiFiClient.h> 
    #include <ESP8266WebServer.h>
    #include <ESP8266HTTPClient.h>
    
    /* Set these to your desired credentials. */
    const char *ssid = "";  //ENTER YOUR WIFI SETTINGS
    const char *password = "";
    
    //Web/Server address to read/write from 
    const char *host = "192.168.8.100";   / website or IP address of server
    
    //=======================================================================
    //                    Power on setup
    //=======================================================================
    
    void setup() {
      delay(1000);
      Serial.begin(115200);
      WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
      delay(1000);
      WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
    
      WiFi.begin(ssid, password);     //Connect to your WiFi router
      Serial.println("");
    
      Serial.print("Connecting");
      // Wait for connection
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      //If connection successful show IP address in serial monitor
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());  //IP address assigned to your ESP
    }
    
    //=======================================================================
    //                    Main Program Loop
    //=======================================================================
    
    void loop() {
      HTTPClient http;    //Declare object of class HTTPClient
    
      String ADCData, station, postData;
      int adcvalue=analogRead(A0);  //Read Analog value of LDR
      ADCData = String(adcvalue);   //String to interger conversion
      station = "A";
    
      //Post Data
      postData = "status=" + ADCData + "&station=" + station ;
    
      http.begin("http://192.168.8.100/Plot_01/test.py");              //Specify request destination
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
    
      int httpCode = http.POST(postData);   //Send the request
      String payload = http.getString();    //Get the response payload
    
      Serial.println(httpCode);   //Print HTTP return code
      Serial.println(payload);    //Print request response payload
    
      http.end();  //Close connection
    
      delay(5000);  //Post Data at every 5 seconds
    }
    //=======================================================================
    

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 2012-09-29
      • 1970-01-01
      • 2010-12-31
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 2017-03-25
      相关资源
      最近更新 更多