Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Radu Motisan
Published © GPL3+

Simple IOT

IOT refers to small devices equipped with direct internet connectivity. Learn IOT top to bottom with this remote temperature sensor project.

IntermediateFull instructions provided2 hours3,008
Simple IOT

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

Repo

Schematics

Simple IOT source code

Code

HTTP Post with EXP Protocol

C/C++
// send a HTTP Post to the backend, using the EXP protocol
bool sendSensorData (uint32_t seconds, float temperature, String userId, String userKey, uint32_t devId) {
  Serial.println("sendSensorData " + String(seconds) + " " + String(temperature) + " " + userId + " device:" + String(devId, HEX));
  // prepare post data
  String postUrl = String(ID_TIME_SECONDS) + "/" + String(seconds, DEC) + "/" + String(ID_VERSION_HW) + "/" + String(VER_HW, DEC) + "/" +  String(ID_VERSION_SW) + "/" + String(VER_SW, DEC) + "/" + String(ID_TEMPERATURE_CELSIUS) + "/" + String(temperature);
  Serial.println(postUrl);
  // prepare http client to do the post
  HTTPClient http;
  // data is sent as URL / RESTful API
  http.begin(URADMONITOR_SERVER + postUrl);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  // the expProtocol requires these customs headers
  http.addHeader("X-User-id", userId);
  http.addHeader("X-User-hash", userKey);
  http.addHeader("X-Device-id", String(devId, HEX));
  int httpCode = http.POST("");
  // check server results
  if (httpCode != 200) {
    Serial.println("not successful");
    http.end();
    return false;
  } else {
    char buffer[200] = {0};
    http.getString().toCharArray(buffer, 200);
    Serial.print("Server response:"); Serial.println(buffer);
    // check response
    char value[10] = {0}; // this has to be inited, or when parsing we could have extra junk screwing results
    if (jsonKeyFind(buffer, "setid", value, 10)) {
      Serial.print("Server allocated new ID:"); Serial.println(value);
      deviceID = hex2int(value);
      eeprom_write_dword(EEPROM_ADDR_DEVID, deviceID);
      EEPROM.commit();
    } 
    http.end();
    return true;
  }
}

Credits

Radu Motisan
0 projects • 6 followers
By training I'm a software engineer. By hobby I'm a chemist, physicist and electronics amateur.

Comments