![](/media/cache/profiles/20200518_163046.jpg.50x50_q85.jpg)
CITY3032-wifi-mqtt
main.cpp@0:e9613d017b5e, 2020-01-14 (annotated)
- Committer:
- reedas
- Date:
- Tue Jan 14 11:29:18 2020 +0000
- Revision:
- 0:e9613d017b5e
- Child:
- 2:3dabedbecf5e
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
reedas | 0:e9613d017b5e | 1 | /* mbed Microcontroller Library |
reedas | 0:e9613d017b5e | 2 | * Copyright (c) 2019 ARM Limited |
reedas | 0:e9613d017b5e | 3 | * SPDX-License-Identifier: Apache-2.0 |
reedas | 0:e9613d017b5e | 4 | * |
reedas | 0:e9613d017b5e | 5 | * I2C Master code for Cypress PSoC 6 communicating with Arduino |
reedas | 0:e9613d017b5e | 6 | * ============================================================= |
reedas | 0:e9613d017b5e | 7 | * |
reedas | 0:e9613d017b5e | 8 | * This module sets the PSoC 6 up as the master on the I2C bus |
reedas | 0:e9613d017b5e | 9 | * and periodically collects a set of environment data from an arduino |
reedas | 0:e9613d017b5e | 10 | * running the code below: |
reedas | 0:e9613d017b5e | 11 | * |
reedas | 0:e9613d017b5e | 12 | * //I2C SLAVE CODE |
reedas | 0:e9613d017b5e | 13 | * //I2C Communication between Two Arduino |
reedas | 0:e9613d017b5e | 14 | * //CircuitDigest |
reedas | 0:e9613d017b5e | 15 | * //Pramoth.T |
reedas | 0:e9613d017b5e | 16 | * |
reedas | 0:e9613d017b5e | 17 | * #include<Wire.h> //Library for I2C Communication functions |
reedas | 0:e9613d017b5e | 18 | * |
reedas | 0:e9613d017b5e | 19 | * #include <PreciseLM35.h> |
reedas | 0:e9613d017b5e | 20 | * |
reedas | 0:e9613d017b5e | 21 | * const int pinLM35 = A2; |
reedas | 0:e9613d017b5e | 22 | * PreciseLM35 lm35(pinLM35, DEFAULT); |
reedas | 0:e9613d017b5e | 23 | * |
reedas | 0:e9613d017b5e | 24 | * |
reedas | 0:e9613d017b5e | 25 | * unsigned char txDataPacket[8]; |
reedas | 0:e9613d017b5e | 26 | * unsigned char rxCommandPacket[8]; |
reedas | 0:e9613d017b5e | 27 | * unsigned char rxCommandNum; |
reedas | 0:e9613d017b5e | 28 | * unsigned char rxCommand = 0; |
reedas | 0:e9613d017b5e | 29 | * bool SW1Pressed = 0; |
reedas | 0:e9613d017b5e | 30 | * bool SW2Pressed = 0; |
reedas | 0:e9613d017b5e | 31 | * |
reedas | 0:e9613d017b5e | 32 | * |
reedas | 0:e9613d017b5e | 33 | * int SW1Pin = 2; |
reedas | 0:e9613d017b5e | 34 | * int SW2Pin = 3; |
reedas | 0:e9613d017b5e | 35 | * |
reedas | 0:e9613d017b5e | 36 | * void setup() |
reedas | 0:e9613d017b5e | 37 | * |
reedas | 0:e9613d017b5e | 38 | * { |
reedas | 0:e9613d017b5e | 39 | * pinMode(SW1Pin, INPUT); |
reedas | 0:e9613d017b5e | 40 | * pinMode(SW2Pin, INPUT); |
reedas | 0:e9613d017b5e | 41 | * Serial.begin(9600); //Begins Serial Communication at 9600 baud rate |
reedas | 0:e9613d017b5e | 42 | * Wire.begin(8); //Begins I2C communication with Slave Address as 8 at pin (A4,A5) |
reedas | 0:e9613d017b5e | 43 | * Wire.onReceive(receiveEvent); //Function call when Slave receives value from master |
reedas | 0:e9613d017b5e | 44 | * Wire.onRequest(requestEvent); //Function call when Master request value from Slave |
reedas | 0:e9613d017b5e | 45 | * } |
reedas | 0:e9613d017b5e | 46 | * |
reedas | 0:e9613d017b5e | 47 | * void loop(void) |
reedas | 0:e9613d017b5e | 48 | * { |
reedas | 0:e9613d017b5e | 49 | * unsigned char SW1Debounced; |
reedas | 0:e9613d017b5e | 50 | * unsigned char SW2Debounced; |
reedas | 0:e9613d017b5e | 51 | * |
reedas | 0:e9613d017b5e | 52 | * if (rxCommandNum) { |
reedas | 0:e9613d017b5e | 53 | * |
reedas | 0:e9613d017b5e | 54 | * |
reedas | 0:e9613d017b5e | 55 | * Serial.println("Slave Received Command From Master:"); //Prints in Serial Monitor |
reedas | 0:e9613d017b5e | 56 | * Serial.print(rxCommand); |
reedas | 0:e9613d017b5e | 57 | * Serial.print(" - "); |
reedas | 0:e9613d017b5e | 58 | * Serial.println(rxCommandNum); |
reedas | 0:e9613d017b5e | 59 | * for (int i = 0; i < 8; i++) { |
reedas | 0:e9613d017b5e | 60 | * Serial.print( rxCommandPacket[i] ); |
reedas | 0:e9613d017b5e | 61 | * Serial.print( " - " ); |
reedas | 0:e9613d017b5e | 62 | * } |
reedas | 0:e9613d017b5e | 63 | * Serial.println(" "); |
reedas | 0:e9613d017b5e | 64 | * for (int i = 0; i < 8; i++) { |
reedas | 0:e9613d017b5e | 65 | * Serial.print( txDataPacket[i] ); |
reedas | 0:e9613d017b5e | 66 | * Serial.print( " - " ); |
reedas | 0:e9613d017b5e | 67 | * } |
reedas | 0:e9613d017b5e | 68 | * Serial.println(" "); |
reedas | 0:e9613d017b5e | 69 | * rxCommandNum = 0; |
reedas | 0:e9613d017b5e | 70 | * } |
reedas | 0:e9613d017b5e | 71 | * int potvalue = map( analogRead(A0), 0, 1023, 0, 100); // Reads analog value from POT (0-5V) |
reedas | 0:e9613d017b5e | 72 | * int lightlevel = map(analogRead(A1), 0, 1023, 0, 100); // Ambient light level |
reedas | 0:e9613d017b5e | 73 | * int temperature = (int)((lm35.readCelsius()) * 10); // Ambient Temperature |
reedas | 0:e9613d017b5e | 74 | * txDataPacket[0] = potvalue; |
reedas | 0:e9613d017b5e | 75 | * txDataPacket[1] = 0; // or potvalue >> 8; |
reedas | 0:e9613d017b5e | 76 | * txDataPacket[2] = lightlevel; |
reedas | 0:e9613d017b5e | 77 | * txDataPacket[3] = 0; // or lightlevel >> 8; |
reedas | 0:e9613d017b5e | 78 | * txDataPacket[4] = temperature; |
reedas | 0:e9613d017b5e | 79 | * txDataPacket[5] = temperature >> 8; |
reedas | 0:e9613d017b5e | 80 | * SW1Debounced = (SW1Debounced << 1) | digitalRead(SW1Pin); |
reedas | 0:e9613d017b5e | 81 | * if (SW1Debounced == 0) SW1Pressed = 1; |
reedas | 0:e9613d017b5e | 82 | * SW2Debounced = (SW2Debounced << 1) | digitalRead(SW2Pin); |
reedas | 0:e9613d017b5e | 83 | * if (SW2Debounced == 0) SW2Pressed = 1; |
reedas | 0:e9613d017b5e | 84 | * txDataPacket[6] = SW1Pressed; |
reedas | 0:e9613d017b5e | 85 | * txDataPacket[7] = SW2Pressed; |
reedas | 0:e9613d017b5e | 86 | * |
reedas | 0:e9613d017b5e | 87 | * // delay(500); |
reedas | 0:e9613d017b5e | 88 | * } |
reedas | 0:e9613d017b5e | 89 | * |
reedas | 0:e9613d017b5e | 90 | * void receiveEvent (int howMany) //This Function is called when Slave receives value from master |
reedas | 0:e9613d017b5e | 91 | * { for (int i = 0; i < howMany; i++) { |
reedas | 0:e9613d017b5e | 92 | * rxCommandPacket[i] = Wire.read(); //Used to read value received from master and store in variable SlaveReceived |
reedas | 0:e9613d017b5e | 93 | * } |
reedas | 0:e9613d017b5e | 94 | * rxCommandNum = howMany; |
reedas | 0:e9613d017b5e | 95 | * rxCommand = rxCommandPacket[0]; |
reedas | 0:e9613d017b5e | 96 | * } |
reedas | 0:e9613d017b5e | 97 | * |
reedas | 0:e9613d017b5e | 98 | * void requestEvent() //This Function is called when Master wants data from slave |
reedas | 0:e9613d017b5e | 99 | * { |
reedas | 0:e9613d017b5e | 100 | * Wire.write(txDataPacket, 8); // sends eight bytes of data to master |
reedas | 0:e9613d017b5e | 101 | * SW1Pressed = 0; // Clear key presses on send to master |
reedas | 0:e9613d017b5e | 102 | * SW2Pressed = 0; |
reedas | 0:e9613d017b5e | 103 | * } |
reedas | 0:e9613d017b5e | 104 | * |
reedas | 0:e9613d017b5e | 105 | */ |
reedas | 0:e9613d017b5e | 106 | |
reedas | 0:e9613d017b5e | 107 | #include "mbed.h" |
reedas | 0:e9613d017b5e | 108 | #include "platform/mbed_thread.h" |
reedas | 0:e9613d017b5e | 109 | #include "TextLCD.h" |
reedas | 0:e9613d017b5e | 110 | #include "GUI.h" |
reedas | 0:e9613d017b5e | 111 | #include "cy8ckit_028_tft.h" |
reedas | 0:e9613d017b5e | 112 | |
reedas | 0:e9613d017b5e | 113 | |
reedas | 0:e9613d017b5e | 114 | #define UNO_CMD (0x10) // Command byte to Uno |
reedas | 0:e9613d017b5e | 115 | #define UNO_CMD_CONF (0x01) // Configuration data |
reedas | 0:e9613d017b5e | 116 | #define UNO_ADDR (0x10) // LM75 address |
reedas | 0:e9613d017b5e | 117 | |
reedas | 0:e9613d017b5e | 118 | //I2C i2c(P8_1, P8_0); |
reedas | 0:e9613d017b5e | 119 | I2C i2c(I2C_SDA, I2C_SCL); |
reedas | 0:e9613d017b5e | 120 | TextLCD_I2C lcd(&i2c, 0x7e, TextLCD::LCD16x2 /*, TextLCD::WS0010*/); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type |
reedas | 0:e9613d017b5e | 121 | //TextLCD_I2C lcd(&i2c, 0x4e, TextLCD::LCD16x2 /*, TextLCD::WS0010*/); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type |
reedas | 0:e9613d017b5e | 122 | DigitalOut myled(LED1); |
reedas | 0:e9613d017b5e | 123 | |
reedas | 0:e9613d017b5e | 124 | //Serial pc(SERIAL_TX, SERIAL_RX); |
reedas | 0:e9613d017b5e | 125 | Serial pc(USBTX, USBRX); |
reedas | 0:e9613d017b5e | 126 | |
reedas | 0:e9613d017b5e | 127 | volatile char TempCelsiusDisplay[] = "+abc.d C"; |
reedas | 0:e9613d017b5e | 128 | |
reedas | 0:e9613d017b5e | 129 | int main() |
reedas | 0:e9613d017b5e | 130 | { |
reedas | 0:e9613d017b5e | 131 | |
reedas | 0:e9613d017b5e | 132 | char data_write[8]; |
reedas | 0:e9613d017b5e | 133 | char data_read[8]; |
reedas | 0:e9613d017b5e | 134 | char buffer[120]; |
reedas | 0:e9613d017b5e | 135 | |
reedas | 0:e9613d017b5e | 136 | /* Initialise display */ |
reedas | 0:e9613d017b5e | 137 | GUI_Init(); |
reedas | 0:e9613d017b5e | 138 | GUI_Clear(); |
reedas | 0:e9613d017b5e | 139 | |
reedas | 0:e9613d017b5e | 140 | lcd.cls(); |
reedas | 0:e9613d017b5e | 141 | lcd.setBacklight(TextLCD::LightOn); |
reedas | 0:e9613d017b5e | 142 | lcd.setCursor(TextLCD::CurOff_BlkOff); |
reedas | 0:e9613d017b5e | 143 | |
reedas | 0:e9613d017b5e | 144 | /* Configure the Temperature sensor device STLM75: |
reedas | 0:e9613d017b5e | 145 | - Thermostat mode Interrupt |
reedas | 0:e9613d017b5e | 146 | - Fault tolerance: 0 |
reedas | 0:e9613d017b5e | 147 | */ |
reedas | 0:e9613d017b5e | 148 | data_write[0] = UNO_CMD; |
reedas | 0:e9613d017b5e | 149 | data_write[1] = 0x08; |
reedas | 0:e9613d017b5e | 150 | |
reedas | 0:e9613d017b5e | 151 | GUI_SetFont(GUI_FONT_10_1); |
reedas | 0:e9613d017b5e | 152 | GUI_SetTextAlign(GUI_TA_LEFT); |
reedas | 0:e9613d017b5e | 153 | int status = i2c.write(UNO_ADDR, data_write, 2, 0); |
reedas | 0:e9613d017b5e | 154 | if (status != 0) { // Error |
reedas | 0:e9613d017b5e | 155 | GUI_DispStringAt("I2C Error", 0, 220); |
reedas | 0:e9613d017b5e | 156 | while (1) { |
reedas | 0:e9613d017b5e | 157 | myled = !myled; |
reedas | 0:e9613d017b5e | 158 | ThisThread::sleep_for(200); |
reedas | 0:e9613d017b5e | 159 | } |
reedas | 0:e9613d017b5e | 160 | } |
reedas | 0:e9613d017b5e | 161 | GUI_DispStringAt("I2C Connected", 0, 220); |
reedas | 0:e9613d017b5e | 162 | GUI_SetFont(GUI_FONT_20B_1); |
reedas | 0:e9613d017b5e | 163 | GUI_DispStringAt("Data from Arduino", 0, 0); |
reedas | 0:e9613d017b5e | 164 | |
reedas | 0:e9613d017b5e | 165 | while (1) { |
reedas | 0:e9613d017b5e | 166 | // Read Arduino data |
reedas | 0:e9613d017b5e | 167 | data_write[0] = UNO_CMD; |
reedas | 0:e9613d017b5e | 168 | i2c.write(UNO_ADDR, data_write, 2, 1); // no stop |
reedas | 0:e9613d017b5e | 169 | if (i2c.read(UNO_ADDR, data_read, 8, 0)) { |
reedas | 0:e9613d017b5e | 170 | GUI_SetFont(GUI_FONT_10_1); |
reedas | 0:e9613d017b5e | 171 | GUI_DispStringAt("I2C receive error ", 0, 220); |
reedas | 0:e9613d017b5e | 172 | } else { |
reedas | 0:e9613d017b5e | 173 | GUI_SetFont(GUI_FONT_10_1); |
reedas | 0:e9613d017b5e | 174 | GUI_DispStringAt("I2C Data Received ", 0, 220); |
reedas | 0:e9613d017b5e | 175 | |
reedas | 0:e9613d017b5e | 176 | // read eight bytes |
reedas | 0:e9613d017b5e | 177 | // pot value - msb-0, lsb-1 |
reedas | 0:e9613d017b5e | 178 | // light level - msb-2, lsb-3 |
reedas | 0:e9613d017b5e | 179 | // temperature - msb-4, lsb-5 |
reedas | 0:e9613d017b5e | 180 | // sw1 state - 6 - 0-255 based on how long pressed |
reedas | 0:e9613d017b5e | 181 | // sw2 state - 7 - 0-255 based on how long pressed |
reedas | 0:e9613d017b5e | 182 | |
reedas | 0:e9613d017b5e | 183 | // Calculate temperature value in Celcius |
reedas | 0:e9613d017b5e | 184 | int tempval = (int)((int)data_read[5] << 8) | data_read[4]; |
reedas | 0:e9613d017b5e | 185 | if (tempval < 0) { |
reedas | 0:e9613d017b5e | 186 | TempCelsiusDisplay[0] = '-'; |
reedas | 0:e9613d017b5e | 187 | } else { |
reedas | 0:e9613d017b5e | 188 | TempCelsiusDisplay[0] = '+'; |
reedas | 0:e9613d017b5e | 189 | } |
reedas | 0:e9613d017b5e | 190 | |
reedas | 0:e9613d017b5e | 191 | // Integer part |
reedas | 0:e9613d017b5e | 192 | TempCelsiusDisplay[1] = (tempval / 1000) + 0x30; |
reedas | 0:e9613d017b5e | 193 | TempCelsiusDisplay[2] = ((tempval % 1000) / 100) + 0x30; |
reedas | 0:e9613d017b5e | 194 | TempCelsiusDisplay[3] = ((tempval % 1000) % 100 / 10) + 0x30; |
reedas | 0:e9613d017b5e | 195 | TempCelsiusDisplay[5] = ((tempval % 1000) % 100 % 10) + 0x30; |
reedas | 0:e9613d017b5e | 196 | |
reedas | 0:e9613d017b5e | 197 | //Switches |
reedas | 0:e9613d017b5e | 198 | |
reedas | 0:e9613d017b5e | 199 | unsigned char SW1State = data_read[6]; |
reedas | 0:e9613d017b5e | 200 | unsigned char SW2State = data_read[7]; |
reedas | 0:e9613d017b5e | 201 | |
reedas | 0:e9613d017b5e | 202 | //light level |
reedas | 0:e9613d017b5e | 203 | int lightDisplay = (int)((int)data_read[3] << 8) | data_read[2]; |
reedas | 0:e9613d017b5e | 204 | |
reedas | 0:e9613d017b5e | 205 | //potentiometer value |
reedas | 0:e9613d017b5e | 206 | int potValue = (int)((int)data_read[1] << 8) | data_read[0]; |
reedas | 0:e9613d017b5e | 207 | |
reedas | 0:e9613d017b5e | 208 | // Display result |
reedas | 0:e9613d017b5e | 209 | pc.printf("temp = %s, Light is %3d%%, Pot is %3d%%%s%s\n", TempCelsiusDisplay, lightDisplay, potValue, SW1State?", SW1 pressed":"", SW2State?", SW2 pressed":""); |
reedas | 0:e9613d017b5e | 210 | |
reedas | 0:e9613d017b5e | 211 | lcd.locate(0,0); |
reedas | 0:e9613d017b5e | 212 | lcd.printf("Temp Lght Pot 12"); |
reedas | 0:e9613d017b5e | 213 | lcd.locate(0,1); |
reedas | 0:e9613d017b5e | 214 | lcd.printf(" %2dC %2d%c %2d%c %s%s", tempval/10, lightDisplay, 0x25, potValue, 0x25, SW1State?"1":"_", SW2State?"2":"_"); |
reedas | 0:e9613d017b5e | 215 | |
reedas | 0:e9613d017b5e | 216 | 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"); |
reedas | 0:e9613d017b5e | 217 | GUI_SetFont(GUI_FONT_20_1); |
reedas | 0:e9613d017b5e | 218 | GUI_DispStringAt(buffer, 0, 40); |
reedas | 0:e9613d017b5e | 219 | } |
reedas | 0:e9613d017b5e | 220 | myled = !myled; |
reedas | 0:e9613d017b5e | 221 | thread_sleep_for(100); |
reedas | 0:e9613d017b5e | 222 | } |
reedas | 0:e9613d017b5e | 223 | |
reedas | 0:e9613d017b5e | 224 | } |
reedas | 0:e9613d017b5e | 225 |