Skip to content

Instantly share code, notes, and snippets.

@RobertCNelson
Last active October 7, 2020 22:16
Show Gist options
  • Select an option

  • Save RobertCNelson/887d21a9dbdcdf924d9053e024e6825b to your computer and use it in GitHub Desktop.

Select an option

Save RobertCNelson/887d21a9dbdcdf924d9053e024e6825b to your computer and use it in GitHub Desktop.
STM32F746
#include <LwIP.h>
#include <STM32Ethernet.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
// the media access control (ethernet hardware) address for the shield:
byte mac[] = {0x00, 0x80, 0xE1, 0x2B, 0x00, 0x40};
char serverAddress[] = "192.168.3.104"; // server address
int port = 8100;
// initialize the library instance:
EthernetClient eth;
HttpClient client = HttpClient(eth, serverAddress, port);
// Create a unique ID for the data from each STM32 running this code
const char* jediID = "STM32F7_IKSO1A3";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac);
//Ethernet.begin(mac, ip, subnet, gateway, dns);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
String postData;
float tempF = 34.5;
delay(2000);
StaticJsonDocument <200> doc;
JsonObject context = doc.createNestedObject("context");
context["target_id"] = String(jediID);
JsonObject data = doc.createNestedObject("data");
data["tempF"] = tempF;
serializeJson(doc, postData);
//This prints the JSON to the serial monitor screen
Serial.println(postData);
if (Ethernet.linkStatus() == LinkON) {
Serial.println("Link status: On");
Serial.println("making POST request");
String contentType = "application/json";
client.post("/v1/data/mc", contentType, postData);
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
}
else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Link status: Off");
}
}
{"context":{"target_id":"STM32F7_IKSO1A3"},"data":{"tempF":34.5}}
Link status: On
making POST request
Status code: 200
Response: "Data sent successfully"
{"context":{"target_id":"STM32F7_IKSO1A3"},"data":{"tempF":34.5}}
Link status: On
making POST request
Status code: 200
Response: "Data sent successfully"
#include <LwIP.h>
#include <STM32Ethernet.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include <LPS22HHSensor.h>
#include <STTS751Sensor.h>
#include <HTS221Sensor.h>
LPS22HHSensor *PressTemp;
HTS221Sensor *HumTemp;
STTS751Sensor *Temp3;
// the media access control (ethernet hardware) address for the shield:
byte mac[] = {0x00, 0x80, 0xE1, 0x2B, 0x00, 0x40};
char serverAddress[] = "192.168.3.104"; // server address
int port = 8100;
// initialize the library instance:
EthernetClient eth;
HttpClient client = HttpClient(eth, serverAddress, port);
// Create a unique ID for the data from each STM32 running this code
const char* jediID = "STM32F7_IKSO1A3";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Initialize I2C bus.
Wire.begin();
PressTemp = new LPS22HHSensor(&Wire);
PressTemp->Enable();
HumTemp = new HTS221Sensor (&Wire);
HumTemp->Enable();
Temp3 = new STTS751Sensor (&Wire);
Temp3->Enable();
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
String postData;
delay(100);
// Read humidity and temperature.
float HTS221_humidity = 0, HTS221_tempC = 0;
HumTemp->GetHumidity(&HTS221_humidity);
HumTemp->GetTemperature(&HTS221_tempC);
float HTS221_tempF = (HTS221_tempC * 1.8) + 32.0F;
// Read pressure and temperature.
float LPS22HH_pressure = 0, LPS22HH_tempC = 0;
PressTemp->GetPressure(&LPS22HH_pressure);
PressTemp->GetTemperature(&LPS22HH_tempC);
float LPS22HH_tempF = (LPS22HH_tempC * 1.8) + 32.0F;
//Read temperature
float STTS751_tempC = 0;
Temp3->GetTemperature(&STTS751_tempC);
float STTS751_tempF = (STTS751_tempC * 1.8) + 32.0F;
StaticJsonDocument <200> doc;
JsonObject context = doc.createNestedObject("context");
context["target_id"] = String(jediID);
JsonObject data = doc.createNestedObject("data");
data["HTS221_humidity"] = HTS221_humidity;
data["HTS221_tempF"] = HTS221_tempF;
data["LPS22HH_pressure"] = LPS22HH_pressure;
data["LPS22HH_tempF"] = LPS22HH_tempF;
data["STTS751_tempF"] = STTS751_tempF;
serializeJson(doc, postData);
//This prints the JSON to the serial monitor screen
Serial.println(postData);
if (Ethernet.linkStatus() == LinkON) {
Serial.println("making POST request");
String contentType = "application/json";
client.post("/v1/data/mc", contentType, postData);
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
}
else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Link status: Off");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment