Andrew Reed
/
CITY1082-i2c_master_wifi_mqtt
CITY3032-wifi-mqtt
Diff: main.cpp
- Revision:
- 2:3dabedbecf5e
- Parent:
- 0:e9613d017b5e
- Child:
- 3:62825c5f3cd7
diff -r f9ef03b6aab8 -r 3dabedbecf5e main.cpp --- a/main.cpp Tue Jan 14 11:30:06 2020 +0000 +++ b/main.cpp Tue Feb 04 09:30:49 2020 +0000 @@ -8,101 +8,135 @@ * This module sets the PSoC 6 up as the master on the I2C bus * and periodically collects a set of environment data from an arduino * running the code below: - * - * //I2C SLAVE CODE - * //I2C Communication between Two Arduino - * //CircuitDigest - * //Pramoth.T - * - * #include<Wire.h> //Library for I2C Communication functions - * - * #include <PreciseLM35.h> - * - * const int pinLM35 = A2; - * PreciseLM35 lm35(pinLM35, DEFAULT); - * - * - * unsigned char txDataPacket[8]; - * unsigned char rxCommandPacket[8]; - * unsigned char rxCommandNum; - * unsigned char rxCommand = 0; - * bool SW1Pressed = 0; - * bool SW2Pressed = 0; - * - * - * int SW1Pin = 2; - * int SW2Pin = 3; - * - * void setup() - * - * { - * pinMode(SW1Pin, INPUT); - * pinMode(SW2Pin, INPUT); - * Serial.begin(9600); //Begins Serial Communication at 9600 baud rate - * Wire.begin(8); //Begins I2C communication with Slave Address as 8 at pin (A4,A5) - * Wire.onReceive(receiveEvent); //Function call when Slave receives value from master - * Wire.onRequest(requestEvent); //Function call when Master request value from Slave - * } - * - * void loop(void) - * { - * unsigned char SW1Debounced; - * unsigned char SW2Debounced; - * - * if (rxCommandNum) { - * - * - * Serial.println("Slave Received Command From Master:"); //Prints in Serial Monitor - * Serial.print(rxCommand); - * Serial.print(" - "); - * Serial.println(rxCommandNum); - * for (int i = 0; i < 8; i++) { - * Serial.print( rxCommandPacket[i] ); - * Serial.print( " - " ); - * } - * Serial.println(" "); - * for (int i = 0; i < 8; i++) { - * Serial.print( txDataPacket[i] ); - * Serial.print( " - " ); - * } - * Serial.println(" "); - * rxCommandNum = 0; - * } - * int potvalue = map( analogRead(A0), 0, 1023, 0, 100); // Reads analog value from POT (0-5V) - * int lightlevel = map(analogRead(A1), 0, 1023, 0, 100); // Ambient light level - * int temperature = (int)((lm35.readCelsius()) * 10); // Ambient Temperature - * txDataPacket[0] = potvalue; - * txDataPacket[1] = 0; // or potvalue >> 8; - * txDataPacket[2] = lightlevel; - * txDataPacket[3] = 0; // or lightlevel >> 8; - * txDataPacket[4] = temperature; - * txDataPacket[5] = temperature >> 8; - * SW1Debounced = (SW1Debounced << 1) | digitalRead(SW1Pin); - * if (SW1Debounced == 0) SW1Pressed = 1; - * SW2Debounced = (SW2Debounced << 1) | digitalRead(SW2Pin); - * if (SW2Debounced == 0) SW2Pressed = 1; - * txDataPacket[6] = SW1Pressed; - * txDataPacket[7] = SW2Pressed; - * - * // delay(500); - * } - * - * void receiveEvent (int howMany) //This Function is called when Slave receives value from master - * { for (int i = 0; i < howMany; i++) { - * rxCommandPacket[i] = Wire.read(); //Used to read value received from master and store in variable SlaveReceived - * } - * rxCommandNum = howMany; - * rxCommand = rxCommandPacket[0]; - * } - * - * void requestEvent() //This Function is called when Master wants data from slave - * { - * Wire.write(txDataPacket, 8); // sends eight bytes of data to master - * SW1Pressed = 0; // Clear key presses on send to master - * SW2Pressed = 0; - * } - * - */ + */ +// +// //I2C SLAVE CODE +// //I2C Communication between Two Arduino +// //CircuitDigest +// //Pramoth.T +// +// #include<Wire.h> //Library for I2C Communication // // functions +// #include <Adafruit_Sensor.h> +// #include <DHT.h> +// #include <DHT_U.h> +// +// #define DHTPIN 4 // Digital pin connected to the DHT sensor +// // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- +// // Pin 15 can work but DHT must be disconnected during program upload. +// +// // Uncomment the type of sensor in use: +// #define DHTTYPE DHT11 // DHT 11 +// //#define DHTTYPE DHT22 // DHT 22 (AM2302) +// //#define DHTTYPE DHT21 // DHT 21 (AM2301) +// +// // See guide for details on sensor wiring and usage: +// // https://learn.adafruit.com/dht/overview +// +// DHT_Unified dht(DHTPIN, DHTTYPE); +// +// uint32_t delayMS; +// #include <PreciseLM35.h> +// #define TESTING false +// +// const int pinLM35 = A2; +// PreciseLM35 lm35(pinLM35, DEFAULT); +// +// +// unsigned char txDataPacket[10]; +// unsigned char rxCommandPacket[8]; +// unsigned char rxCommandNum; +// unsigned char rxCommand = 0; +// bool SW1Pressed = 0; +// bool SW2Pressed = 0; +// +// int blueLed = 13; +// int SW1Pin = 2; +// int SW2Pin = 3; +// +// void setup() +// +// { +// pinMode(SW1Pin, INPUT); +// pinMode(SW2Pin, INPUT); +// pinMode(blueLed, OUTPUT); +// dht.begin(); +// sensor_t sensor; +// dht.temperature().getSensor(&sensor); +// dht.humidity().getSensor(&sensor); +// delayMS = sensor.min_delay / 1000; +// Serial.begin(115200); //Begins Serial Communication at // 9600 baud rate +// Wire.begin(8); //Begins I2C communication with // // Slave Address as 8 at pin (A4,A5) +// Wire.onReceive(receiveEvent); //Function call when Slave receives // value from master +// Wire.onRequest(requestEvent); //Function call when Master request // value from Slave +// } +// +// void loop(void) +// { +// unsigned char SW1Debounced; +// unsigned char SW2Debounced; +// int temperature = 20; +// while (1) { +// if (rxCommandNum || TESTING) { +// +// +// Serial.println("Slave Received Command From Master:"); //Prints in // // Serial Monitor +// Serial.print(rxCommand); +// Serial.print(" - "); +// Serial.println(rxCommandNum); +// for (int i = 0; i < 8; i++) { +// Serial.print( rxCommandPacket[i] ); +// Serial.print( " - " ); +// } +// Serial.println(" "); +// for (int i = 0; i < 10; i++) { +// Serial.print( txDataPacket[i] ); +// Serial.print( " - " ); +// } +// Serial.println(" "); +// rxCommandNum = 0; +// } +// sensors_event_t event; +// int potvalue = map( analogRead(A0), 0, 1023, 0, 100); // // Reads analog value from POT (0-5V) +// int lightlevel = map(analogRead(A1), 0, 1023, 0, 100); // // Ambient light level +// int humidityInt = 101; +// temperature = ((int)((lm35.readCelsius()) * 10)); // // // Ambient Temperature +// dht.humidity().getEvent(&event); +// if (isnan(event.relative_humidity)) { +// Serial.println(F("Error reading humidity!")); +// } +// else { +// humidityInt = (int)event.relative_humidity; +// Serial.print(F("Humidity: ")); +// Serial.print(humidityInt); +// Serial.println(F("%")); +// } +// txDataPacket[0] = potvalue; +// txDataPacket[1] = 0; // or potvalue >> 8; +// txDataPacket[2] = lightlevel; +// txDataPacket[3] = 0; // or lightlevel >> 8; +// txDataPacket[4] = temperature; +// txDataPacket[5] = temperature >> 8; +// SW1Debounced = (SW1Debounced << 1) | digitalRead(SW1Pin); +// if (SW1Debounced == 0) SW1Pressed = 1; +// SW2Debounced = (SW2Debounced << 1) | digitalRead(SW2Pin); +// if (SW2Debounced == 0) SW2Pressed = 1; +// txDataPacket[6] = SW1Pressed; +// txDataPacket[7] = SW2Pressed; +// txDataPacket[8] = humidityInt; +// txDataPacket[9] = 0; +// digitalWrite(blueLed, rxCommandPacket[1] & 1); +// delay(delayMS); +// if (TESTING) delay(2000); +// } +// } +// +// void receiveEvent (int howMany) //This Function is called // when Slave receives value from master +// { for (int i = 0; i < howMany; i++) { +// rxCommandPacket[i] = Wire.read(); //Used to read value // received from master and store in variable SlaveReceived +// } +// rxCommandNum = howMany; +// rxCommand = rxCommandPacket[0]; +// } * #include "mbed.h" #include "platform/mbed_thread.h" @@ -118,7 +152,7 @@ //I2C i2c(P8_1, P8_0); I2C i2c(I2C_SDA, I2C_SCL); TextLCD_I2C lcd(&i2c, 0x7e, TextLCD::LCD16x2 /*, TextLCD::WS0010*/); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type -//TextLCD_I2C lcd(&i2c, 0x4e, TextLCD::LCD16x2 /*, TextLCD::WS0010*/); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type +TextLCD_I2C lcd2(&i2c, 0x4e, TextLCD::LCD16x2 /*, TextLCD::WS0010*/); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type DigitalOut myled(LED1); //Serial pc(SERIAL_TX, SERIAL_RX); @@ -130,7 +164,7 @@ { char data_write[8]; - char data_read[8]; + char data_read[10]; char buffer[120]; /* Initialise display */ @@ -140,6 +174,9 @@ lcd.cls(); lcd.setBacklight(TextLCD::LightOn); lcd.setCursor(TextLCD::CurOff_BlkOff); + lcd2.cls(); + lcd2.setBacklight(TextLCD::LightOn); + lcd2.setCursor(TextLCD::CurOff_BlkOff); /* Configure the Temperature sensor device STLM75: - Thermostat mode Interrupt @@ -166,7 +203,7 @@ // Read Arduino data data_write[0] = UNO_CMD; i2c.write(UNO_ADDR, data_write, 2, 1); // no stop - if (i2c.read(UNO_ADDR, data_read, 8, 0)) { + if (i2c.read(UNO_ADDR, data_read, 10, 0)) { GUI_SetFont(GUI_FONT_10_1); GUI_DispStringAt("I2C receive error ", 0, 220); } else { @@ -179,6 +216,7 @@ // temperature - msb-4, lsb-5 // sw1 state - 6 - 0-255 based on how long pressed // sw2 state - 7 - 0-255 based on how long pressed + // Humidity - msb-9, lsb-8 // Calculate temperature value in Celcius int tempval = (int)((int)data_read[5] << 8) | data_read[4]; @@ -204,21 +242,28 @@ //potentiometer value int potValue = (int)((int)data_read[1] << 8) | data_read[0]; + + int humidity = (int)((int)data_read[9] << 8) | data_read[8]; // Display result - pc.printf("temp = %s, Light is %3d%%, Pot is %3d%%%s%s\n", TempCelsiusDisplay, lightDisplay, potValue, SW1State?", SW1 pressed":"", SW2State?", SW2 pressed":""); + pc.printf("temp = %s, Light is %3d%%, Hum is %3d%%%s%s\n", TempCelsiusDisplay, lightDisplay, humidity, SW1State?", SW1 pressed":"", SW2State?", SW2 pressed":""); lcd.locate(0,0); - lcd.printf("Temp Lght Pot 12"); + lcd.printf("Temp Lght Hum 12"); lcd.locate(0,1); - lcd.printf(" %2dC %2d%c %2d%c %s%s", tempval/10, lightDisplay, 0x25, potValue, 0x25, SW1State?"1":"_", SW2State?"2":"_"); + lcd.printf(" %2dC %2d%c %2d%c %s%s", tempval/10, lightDisplay, 0x25, humidity, 0x25, SW1State?"1":"_", SW2State?"2":"_"); + lcd2.locate(0,0); + lcd2.printf("Temp Lght Pot 12"); + lcd2.locate(0,1); + lcd2.printf(" %2dC %2d%c %2d%c %s%s", tempval/10, lightDisplay, 0x25, potValue, 0x25, SW1State?"1":"_", SW2State?"2":"_"); - sprintf(buffer,"Temp is %2dC \nLight Level is %2d%c \nPot Value is %2d%c \n%s \n%s ", tempval/10, lightDisplay, 0x25, potValue, 0x25, SW1State?"SW1 Pressed ":"SW1 Released", SW2State?"SW2 Pressed ":"SW2 Released"); + sprintf(buffer,"Temp is %2dC \nLight Level is %2d%c \nPot Value is %2d%c \nHumidity is %2d%c \n%s \n%s ", tempval/10, lightDisplay, 0x25, potValue, 0x25, humidity, 0x25, SW1State?"SW1 Pressed ":"SW1 Released", SW2State?"SW2 Pressed ":"SW2 Released"); GUI_SetFont(GUI_FONT_20_1); GUI_DispStringAt(buffer, 0, 40); } myled = !myled; - thread_sleep_for(100); + data_write[1] = data_write[1] ^ 0x01; + thread_sleep_for(250); } }