Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include "SoftwareSerial.h"
- const char* ssid = "nhtcntt";
- const char* pass = "123456789";
- String URL = "http://192.168.123.1:80/update";
- SoftwareSerial sw(D1, D2, false, 128); //rx, tx
- void SendInfo2Server(uint8_t *buff, size_t size);
- void RecvFromMSP430();
- void setup() {
- Serial.begin(115200);
- Serial.println("debug");
- Serial.print("Connectting to");
- sw.begin(115200);
- 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()
- {
- RecvFromMSP430();
- delay(1000);
- }
- /*
- Message format:
- {"nhietdo":"xx.xx", "doam": "yy.yy"}| message | ...
- */
- void RecvFromMSP430()
- {
- uint8_t buff[50];
- if(sw.available() > 0)
- {
- size_t len = sw.readBytesUntil('|', buff, sizeof(buff));
- if(len > 0)
- {
- SendInfo2Server(buff, len);
- }
- }
- }
- /*
- 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(uint8_t *buff, size_t size)
- {
- 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) + "\"}");
- int statuscode = http.POST(buff, size);
- if(statuscode > 0)
- {
- String payload = http.getString();
- Serial.print("payload: ");
- Serial.println(payload);
- }
- http.end();
- }
Advertisement
Add Comment
Please, Sign In to add comment