Skip to content
Snippets Groups Projects
Commit db52f63e authored by joalhelk's avatar joalhelk
Browse files

add arduino code

parent a30895be
No related branches found
No related tags found
No related merge requests found
/*
Repeating WiFi Web Client
This sketch connects to a a web server and makes a request
using a WiFi equipped Arduino board.
created 23 April 2012
modified 31 May 2012
by Tom Igoe
modified 13 Jan 2014
by Federico Vanzati
http://www.arduino.cc/en/Tutorial/WifiWebClientRepeating
This code is in the public domain.
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include "arduino_secrets.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Adafruit_TSL2591.h"
// Initialize sensors
Adafruit_BME280 bme; // BME sensor which has pressure, humidity and temperature
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // Luminosity sensor
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
int status = WL_IDLE_STATUS;
int temp, humidity, luminosity;
boolean api_success = false;
// server address:
char server[] = "192.168.0.8";
IPAddress server_ip(192,168,0,8);
int port = 8080;
// Initialize the WiFi client library
WiFiClient client;
HttpClient http = HttpClient(client, server, port);
unsigned long lastConnectionTime = 0, lastConnectionTime2 = 0, lastMovementTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 60L * 30L * 1000L; // delay (1 800 000 ms = 30 min) between updates, in milliseconds
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Initialize pins
bme.begin(0x76);
tsl.begin();
tsl.setGain(TSL2591_GAIN_MED);
tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// 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);
}
// you're connected now, so print out the status:
printWifiStatus();
}
// MAIN LOOP
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// Send the data
// If 30 minutes has been passed since last posting, then take measurements and post again
if (millis() - lastConnectionTime > postingInterval) {
temp = bme.readTemperature();
humidity = bme.readHumidity();
luminosity = tsl.getLuminosity(TSL2591_VISIBLE);
httpRequest();
}
}
// Lähetetään pilveen sensori1:lle sensoreiden tiedot
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the NINA module
Serial.println("making POST request");
String contentType = "application/x-www-form-urlencoded";
String postData ="temperature=";
postData += temp;
postData += "&humidity=";
postData += humidity;
postData += "&luminosity=";
postData += luminosity;
postData += "&parent_id=";
postData += 1;
http.post("/api/v1/nodeData", contentType, postData);
// read the status code and body of the response
int statusCode = http.responseStatusCode();
String response = http.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
// note the time that the connection was made:
lastConnectionTime = millis();
}
// Tulostetaan wifin tiedot
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment