Commit 81c562e3 authored by jamie.geddes's avatar jamie.geddes

Code for IoT finished

parents
Pipeline #435 failed with stages
#include <SoftwareSerial.h>
//ALWAYS USE PIN 6 ON APP
SoftwareSerial ss(8, 9);
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
/*
WiFi 101 ThingSpeak Data Uploader
Hardware Required:
* Arduino Zero or Uno Board
* Arduino Wifi Sheild 101
* Photocell
* Temperature Sensor (This example uses a TMP36)
* 10K Ohm Resistor
created Sept 2015
by Helena Bisby <support@arduino.cc>
This example code is in the public domain
http://arduino.cc/en/Tutorial/WiFi101ThingSpeakDataUploader
*/
#include <SPI.h>
#include <WiFi.h>
// Local Network Settings
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "cs"; // your network SSID (name)
char pass[] = "gkmrxuW6"; // 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;
WiFiServer server(80);
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "RFASZ7J7E3QY4M79"; // enter your channel's Write API Key
const int updateThingSpeakInterval = 3 * 1000; // 20 second interval at which to update ThingSpeak
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
// Initialize Arduino Ethernet Client
WiFiClient client;
void setup() {
// put your setup code here, to run once:
ss.begin(9600);
Serial.begin(9600);
pinMode(6, OUTPUT);
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);
}
// you're connected now, so print out the status:
printWifiStatus();
}
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 resensorPinceived signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
char waitForByte()
{
while (true) {
char c = ss.read();
if (c != -1) {
return c;
//while millis (give current time since booted up, then read when msg starts tehn check to see if run out of time then give up, read all 3 bytes potentially or else.
}
}
}
void Sensor(){
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println("got light value of:");
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
//delay(100);
if(sensorValue > 200 && sensorValue < 250){
Serial.println("The light is starting to dim when being on");
}
if(sensorValue > 150 && sensorValue < 200){
Serial.println("The light is dim when being on");
}
if(sensorValue < 150){
Serial.println("The light is too dim and needs changing soon");
}
if(sensorValue >= 250){
Serial.println("The light is working great");
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("looking for bluetooth message");
int x = waitForByte();
// Serial.println("got x");
// Serial.println(x);
int y = waitForByte();
// Serial.println(y);
int z = waitForByte();
// Serial.println(z);
// Serial.println("out of bt check");
// Serial.print("got ");
// Serial.print(x);
// Serial.print(" ");
// Serial.print(y);
// Serial.print(" ");
// Serial.println(z);
digitalWrite(6, y == 64);
if ( y == 64)
{
Sensor();
}
sensorValue = analogRead(sensorPin);
String sensorString = String(sensorValue);
// String sensorPin = String(analogRead(A0), DEC); // read light value
//int light = sensorValue;
// Print Update Response to Serial Monitor
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected) {
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
Serial.println("calling update thingspeak");
updateThingSpeak("field1=" + sensorString);
Serial.println(sensorString);
}
}
void updateThingSpeak(String tsData) {
if (client.connect(thingSpeakAddress, 80)) {
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected()) {
Serial.println("Connecting to ThingSpeak...");
Serial.println();
}
lastConnected = client.connected();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment