Hi all,
I'm trying to get my MKR1010 WiFi to add sensor data to a MySQL database but it doesn't seem to be working.
thingProperties.h code
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <WiFi.h>
const char THING_ID[] = "c942a18f-7696-4431-952c-76eaea3214ea";
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)
const char server[] = SECRET_IP;
void onRainChange();
int airPressure;
float lightIntensity;
float relativeHumidity;
bool rain;
float temperature;
void initProperties(){
ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(airPressure, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(lightIntensity, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(relativeHumidity, READ, 60 * SECONDS, NULL);
ArduinoCloud.addProperty(rain, READWRITE, 60 * SECONDS, onRainChange);
ArduinoCloud.addProperty(temperature, READ, 60 * SECONDS, NULL);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
Main code:
#include "thingProperties.h"
#include <Arduino_MKRENV.h>
#include <WiFi.h>
#define WATER_SENSOR 2
WiFiClient client;
int status = WL_IDLE_STATUS;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(SSID, PASS);
// wait 10 seconds for connection:
delay(10000);
}
// Defined in thingProperties.h
initProperties();
pinMode(WATER_SENSOR, INPUT);
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while(1);
}
/*WiFi setup*/
}
void loop() {
ArduinoCloud.update();
// Your code here
relativeHumidity = int(ENV.readHumidity());
lightIntensity = (ENV.readIlluminance());
airPressure = int(ENV.readPressure(MILLIBAR));
temperature = int(ENV.readTemperature());
if(digitalRead(WATER_SENSOR) == LOW){
rain = 1;
}else if(digitalRead(WATER_SENSOR) == HIGH){
rain = 0;
}
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("GET /weather_data.php?");
client.print("temp=");
client.print(temperature);
client.print("&pressure=");
client.print(airPressure);
client.print("&humidity=");
client.print(relativeHumidity);
client.print("&illumination=");
client.print(lightIntensity);
client.print("&rain=");
client.print(rain);
client.print(" HTTP/1.1");
client.println("Host: ");
client.print(server);
client.println("Connection: close");
}
}
void onRainChange() {
// Do something
}
Thanks