Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- const char* ssid = "nhtcntt";
- const char* pass = "123456789";
- String URL = "http://192.168.123.1:80/update";
- void SendInfo2Server(float Nhietdo, float DoAm);
- void setup() {
- Serial.begin(115200);
- Serial.print("Connectting to");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, pass);
- while( WiFi.status() != WL_CONNECTED)
- {
- delay(2000);
- Serial.println("Failed - Try connect");
- }
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("MAC: ");
- Serial.println(WiFi.macAddress());
- }
- void loop()
- {
- SendInfo2Server(10.20, 10.10);
- }
- /*
- URL: http://IPorName:80/update
- method: POST
- http header:
- User-Agent: MAC Address
- Content-Type: application/json
- Content-Length: length(http body)
- Connection: close
- http body:
- {
- "nhietdo" : float,
- "doam" : float
- }
- */
- void SendInfo2Server(float Nhietdo, float DoAm)
- {
- HTTPClient http;
- http.begin(URL);
- http.addHeader("Content-Type", "application/json");
- http.setUserAgent(WiFi.macAddress());
- int statuscode = http.POST("{\"nhietdo\" : " + String(Nhietdo) + ", \"doam\" : \"" + String(DoAm) + "%\"}");
- if(statuscode > 0)
- {
- String payload = http.getString();
- Serial.print("payload: ");
- Serial.println(payload);
- }
- http.end();
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment