Code for Master
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include "secrets.h"
char ssid[] = ""; // your network SSID (name)
char pass[] = ""; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = ;
const char * myWriteAPIKey = "";
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Define counter to count bytes in response
int bcount;
// Define array for return data
byte vib[3];
void setup()
{
Wire.begin();
Serial.begin(9600);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
byte readI2C(int address) {
// Define a variable to hold byte of data
byte bval ;
long entry = millis();
// Read one byte at a time
Wire.requestFrom(address, 3);
// Wait 100 ms for data to stabilize
while (Wire.available() == 0 && (millis() - entry) < 100) Serial.print("Waiting");
// Place data into byte
if (millis() - entry < 100) bval = Wire.read();
return bval;
}
void loop()
{
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print("\nConnecting...");
delay(5000);
}
Serial.println("\nConnected.");
Serial.println("The IP Address of ESP8266 Module is: ");
Serial.println(WiFi.localIP());// Print the IP address
Serial.println("Signal strength: ");
Serial.println(WiFi.RSSI());
}
/while (readI2C(SLAVE_ADDR) < 255) {
// Until first byte has been received print a waiting message
Serial.println("Waiting for data");
}/
for (bcount = 0; bcount < 3; bcount++) {
vib[bcount] = readI2C(SLAVE_ADDR);
}
for (int i = 0; i < 3; i++) {
//float Ax= (( ( ( (float)(vib[0] * 5) / 1024) - 1.65 ) / 0.330 ) * 9.8) ;
int A = ThingSpeak.writeField(myChannelNumber, 1, vib[0], myWriteAPIKey);
// int B = ThingSpeak.writeField(myChannelNumber, 2, vib[1], myWriteAPIKey);
// int C = ThingSpeak.writeField(myChannelNumber, 3, vib[2], myWriteAPIKey);
Serial.println(vib[0]);
}
Serial.println();
Serial.println("NEW");
}
COde for SLave
/*
Acceleration sensors Test
*/
#include <Wire.h>
#include <math.h>
#define SLAVE_ADDR 9
const int x_out = A0; /* connect x_out of module to A2 of UNO board /
const int y_out = A1; / connect y_out of module to A2 of UNO board /
const int z_out = A2; / connect z_out of module to A3 of UNO board */
// Define return data array, one element per axis
int vib[3];
// Define counter to count bytes in response
int bcount = 0;
void setup() {
Serial.begin(9600);
// Initialize I2C communications as Slave
Wire.begin(SLAVE_ADDR);
// Function to run when data requested from master
Wire.onRequest(requestEvent);
}
int x_adc_value, y_adc_value, z_adc_value;
int Gx, Gy, Gz;
void requestEvent() {
// Define a byte to hold data
byte bval;
// Cycle through data
switch (bcount) {
case 0:
bval = vib[0];
break;
case 1:
bval = vib[1];
break;
case 2:
bval = vib[2];
break;
}
// Send response back to Master
Wire.write(bval);
// Increment byte counter
bcount = bcount + 1;
if (bcount > 4) bcount = 0;
}
void readvib()
{
x_adc_value = analogRead(x_out); /* Digital value of voltage on x_out pin /
Gx = ( ( ( (double)(x_adc_value * 5) / 1024) - 1.65 ) / 0.330 ); / Acceleration in x-direction in g units */
vib[0] = Gx * 9.8;
y_adc_value = analogRead(y_out); /* Digital value of voltage on y_out pin /
Gy = ( ( ( (double)(y_adc_value * 5) / 1024) - 1.65 ) / 0.330 ); / Acceleration in y-direction in g units */
vib[1] = Gy * 9.8;
z_adc_value = analogRead(z_out); /* Digital value of voltage on z_out pin */
Gz = ( ( ( (double)(z_adc_value * 5) / 1024) - 1.80 ) / 0.330 ); /Acceleration in z-direction in g units/
vib[2] = Gz * 9.8;
Serial.println();
Serial.print(vib[0]);
Serial.print("\t");
Serial.print(vib[1]);
Serial.print("\t");
Serial.print(vib[2]);
Serial.print("\t");
}
void loop()
{
// Refresh readings every half second
delay(500);
readvib();
Serial.println();
Serial.println("NEW");
}