Hey guys,
I have a Core2 with Lan module attached and an ENV IV unit plugged in.
I'm trying to send the recorded data from the ENV IV sensors to a server by POST request
I have succesfully ran an arduino example that collects data from both sensors and displays to the screen and to the serial monitor.
https://github.com/m5stack/M5Unit-ENV/blob/master/examples/Unit_ENVIV_M5Core2/Unit_ENVIV_M5Core2.ino
I have also successfully ran the following example that performs a GET and a POST request and displays the results.
https://github.com/m5stack/M5Module-LAN-13.2/blob/main/examples/HTTP/HTTP.ino
Now I've attempted to combine the two and the below is what I've ended up with. The problem I'm facing is that it compiles ok, but only the bmp280 sensor gives any data, the SHT40 gives 'Error writing to I2C bus'. Can anyone see what I'm doing wrong or does the LAN module just block the SHT40 somehow?
Serial:
making GET request
Status code: 200
Response: {
"args": {},
"headers": {
"Host": "httpbin.org",
"User-Agent": "Arduino/2.2.0",
"X-Amzn-Trace-Id": "Root=1-66574f8b-19e46aa01976708f76ff6811"
},
"origin": "81.96.198.248",
"url": "http://httpbin.org/get"
}
making POST request
Error trying to execute measureHighPrecision(): Error writing to I2C bus
,,99983.526679
Code:
#include <M5Unified.h>
#include <Wire.h>
#include <M5GFX.h>
#include <SPI.h>
#include <M5Module_LAN.h>
#include <ArduinoHttpClient.h>
#include <SensirionI2cSht4x.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Sensor.h"
#define THEME_COLOR 0x0760
#define SERVER "httpbin.org"
#define POST_INTERVAL 5000
Adafruit_BMP280 bmp;
SensirionI2cSht4x sht;
float temp, pressure, humd;
long lastTime = 0;
uint8_t cs_pin;
uint8_t rst_pin;
uint8_t int_pin;
int screen_height;
int screen_width;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x89};
M5Module_LAN LAN;
M5Canvas canvas(&M5.Display);
EthernetClient ethClient;
HttpClient client = HttpClient(ethClient, SERVER);
void setup() {
M5.begin();
M5.Display.begin();
M5.Display.setTextColor(WHITE);
M5.Display.setTextDatum(middle_center);
M5.Display.setTextFont(&fonts::FreeSansBoldOblique18pt7b);
screen_height = M5.Display.height();
screen_width = M5.Display.width();
while (!bmp.begin(0x76))
{ // Init this sensor,True if the init was successful, otherwise
// false.
M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!");
Serial.println(F("BMP280 fail"));
}
uint16_t error;
char errorMessage[256];
Wire.begin();
sht.begin(Wire, 0x44);
uint32_t serialNumber;
error = sht.serialNumber(serialNumber);
if (error)
{
Serial.print("Error trying to execute serialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else
{
Serial.print("Serial Number: ");
Serial.println(serialNumber);
}
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
Serial.print("Serial initialized...");
M5.Lcd.clear();
M5.Display.fillSmoothRoundRect(2, 2, screen_width - 4, 36, 4, THEME_COLOR);
M5.Display.drawString("Wait For Ethernet", screen_width / 2, 22);
M5.Display.drawRoundRect(2, 40, screen_width - 4, 192, 4, THEME_COLOR);
canvas.createSprite(280, 160);
canvas.setTextColor(WHITE);
canvas.setTextFont(&fonts::FreeSansBoldOblique9pt7b);
canvas.setTextScroll(true);
canvas.setTextSize(0.5);
cs_pin = 33;
rst_pin = 0;
int_pin = 35;
SPI.begin(SCK, MISO, MOSI, -1);
LAN.setResetPin(rst_pin);
LAN.reset();
LAN.init(cs_pin);
while (LAN.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(2000);
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println(
"Ethernet shield was not found. Sorry, can't run without "
"hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
}
String t,h,p;
void loop() {
Serial.println("making GET request");
canvas.println("making GET request");
canvas.pushSprite(20, 55);
client.get("/get");
// 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);
M5.Display.fillSmoothRoundRect(2, 2, screen_width - 4, 36, 4, THEME_COLOR);
M5.Display.drawString("HTTP POST", screen_width / 2, 22);
uint16_t error;
char errorMessage[256];
error = sht.measureHighPrecision(temp, humd);
if (error)
{
Serial.print("Error trying to execute measureHighPrecision(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else
{
t = String(temp);
h = String(humd);
}
pressure = bmp.readPressure();
p = String(pressure);
Serial.print(t+","+h+","+p);
String contentType = "text/plain";
String postData = String("ServerRoom,temp="+t+",humidity="+h);
Serial.println(millis() - lastTime);
if (millis() - lastTime > POST_INTERVAL) {
lastTime = millis();
Serial.println("making POST request");
canvas.println("making POST request");
canvas.pushSprite(20, 55);
delay(1000);
error = client.post("/post", contentType, postData);
if (error){
Serial.print("error during POST:");
Serial.println(error);
}
// read the status code and body of the response
statusCode = client.responseStatusCode();
response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
canvas.print("Status code: ");
canvas.println(statusCode);
canvas.print("Response: ");
canvas.println(response);
canvas.pushSprite(20, 55);
Serial.println("Wait five seconds");
}
delay(1000);
}