#include <SPI.h>
#include <WiFi.h>
char ssid[] = "xxxx"; // network SSID (name)
char pass[] = "xxxx"; // network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
char server[] = "xxxxxxxxxxxxxxxxxxxxxx";
int port = 80;
WiFiClient client;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60000; // delay between updates, in milliseconds
boolean incomingData = false;
//----------------VALVE CONTROL - BEGIN----------------//
int so1_1_1 = 8;
int sol_1_2 = 9;
int sol_1_Enable = 6;
int so1_2_1 = 3;
int sol_2_2 = 5;
int sol_2_Enable = 2;
//----------------VALVE CONTROL - END----------------//
void setup() {
//Valve control pins
pinMode(so1_1_1, OUTPUT);
pinMode(sol_1_2, OUTPUT);
pinMode(sol_1_Enable, OUTPUT);
pinMode(so1_2_1, OUTPUT);
pinMode(sol_2_2, OUTPUT);
pinMode(sol_2_Enable, OUTPUT);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// 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);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
Serial.println("========================");
Serial.println("--- START ---");
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("--- disconnecting_client_1.");
client.stop();
}
//--------------------------------------------------------------
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// if there's a successful connection:
sendDataOverWiFi();
}
//--------------------------------------------------------------
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
//--------------------------------------------------------------
// wait 60 seconds
Serial.println("--- wait 60 seconds ---");
delay(60000);
Serial.println("--- wait done");
Serial.println("--- END ---");
//get flow reading
//captureFlowMeterReading();
}
void sol_OFF(int solx1, int solx2, int solxEn)
{
digitalWrite(solxEn, HIGH);
digitalWrite(solx2, LOW);
digitalWrite(solx1, HIGH);
delay(100);
digitalWrite(solxEn, LOW);
}
void sol_ON(int solx1, int solx2, int solxEn)
{
digitalWrite(solxEn, HIGH);
digitalWrite(solx1, LOW);
digitalWrite(solx2, HIGH);
delay(100);
digitalWrite(solxEn, LOW);
}
String lastWebResponse = "";
// this method makes a HTTP connection to the server:
void sendDataOverWiFi() {
int analogChannel1 = 0;
int analogChannel2 = 1;
Serial.print("\nStarting connection to server ");
Serial.println(server);
//Serial.println("--- connecting to server ---");
if (client.connect(server, 80)) {
Serial.println();
Serial.println("--- sending data");
int sensorReading1 = analogRead(analogChannel1);
char PostData1[] = "0000000";
String pdataString1 = itoa(sensorReading1, PostData1, 10);
int sensorReading2 = analogRead(analogChannel2);
char PostData12[] = "0000000";
String pdataString2 = itoa(sensorReading2, PostData12, 10);
String dataString = pdataString1 + "," + pdataString2;
Serial.println(dataString);
//Serial.println(pdataString1);
// Make a HTTP request:
client.println("POST /api/device/7579DB95-9A6E-44C4-883A-1FB8ACAB60F9 HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.print("Content-Length: ");
//// calculate the length of the sensor reading in bytes:
//// 8 bytes for "sensor1," + 8 bytes for "sensor2," + number of digits of the data:
//int thisLength = 16 + getLength(sensorReading) + getLength(fakeSecondChannel);
//client.println(thisLength);
client.println(dataString.length());
client.println();
client.println(dataString);
client.println();
// wait 10 seconds:
delay(1000);
// note the time that the connection was made:
lastConnectionTime = millis();
Serial.println("--- sending data done");
}
else {
// if you couldn't make a connection:
Serial.println("--- connection failed");
Serial.println("--- disconnecting_client_2.");
}
//-------------------------------------------------------------
// give the server time to respond
delay(1000);
//--------------------------------------------------------------
// if there's incoming data from the net connection.
// send it out the serial port.
while (client.available() && status == WL_CONNECTED) {
if (incomingData == false)
{
Serial.println();
Serial.println("--- Incoming data Start ---");
incomingData = true;
}
char c = client.read();
Serial.write(c);
lastWebResponse += c;
}
client.flush();
client.stop();
// Debugging --------------------------------------------------
if (incomingData == true)
{
Serial.println("--- Incoming data End");
if(lastWebResponse.indexOf("vcomm_1_On") >=0) {
sol_ON(so1_1_1, sol_1_2, sol_1_Enable);
Serial.println("--- vcomm_1_On ---");
}
if(lastWebResponse.indexOf("vcomm_1_Off") >=0) {
sol_OFF(so1_1_1, sol_1_2, sol_1_Enable);
Serial.println("--- vcomm_1_Off ---");
}
if(lastWebResponse.indexOf("vcomm_2_On") >=0) {
sol_ON(so1_2_1, sol_2_2, sol_2_Enable);
Serial.println("--- vcomm_2_On ---");
}
if(lastWebResponse.indexOf("vcomm_2_Off") >=0) {
sol_OFF(so1_2_1, sol_2_2, sol_2_Enable);
Serial.println("--- vcomm_2_Off ---");
}
lastWebResponse = "";
incomingData = false;
}
if (status == WL_CONNECTED){
Serial.println("--- WiFi connected");
}
else{
Serial.println("--- WiFi not connected");
}
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield'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");
}