Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * I2C Master code for Cypress PSoC 6 communicating with Arduino 00006 * ============================================================= 00007 * 00008 * This module sets the PSoC 6 up as the master on the I2C bus 00009 * and periodically collects a set of environment data from an arduino 00010 * running the code below: 00011 */ 00012 // 00013 // //I2C SLAVE CODE 00014 // //I2C Communication between Two Arduino 00015 // //CircuitDigest 00016 // //Pramoth.T 00017 // 00018 // #include<Wire.h> //Library for I2C Communication // // functions 00019 // #include <Adafruit_Sensor.h> 00020 // #include <DHT.h> 00021 // #include <DHT_U.h> 00022 // 00023 // #define DHTPIN 4 // Digital pin connected to the DHT sensor 00024 // // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- 00025 // // Pin 15 can work but DHT must be disconnected during program upload. 00026 // 00027 // // Uncomment the type of sensor in use: 00028 // #define DHTTYPE DHT11 // DHT 11 00029 // //#define DHTTYPE DHT22 // DHT 22 (AM2302) 00030 // //#define DHTTYPE DHT21 // DHT 21 (AM2301) 00031 // 00032 // // See guide for details on sensor wiring and usage: 00033 // // https://learn.adafruit.com/dht/overview 00034 // 00035 // DHT_Unified dht(DHTPIN, DHTTYPE); 00036 // 00037 // uint32_t delayMS; 00038 // #include <PreciseLM35.h> 00039 // #define TESTING false 00040 // 00041 // const int pinLM35 = A2; 00042 // PreciseLM35 lm35(pinLM35, DEFAULT); 00043 // 00044 // 00045 // unsigned char txDataPacket[10]; 00046 // unsigned char rxCommandPacket[8]; 00047 // unsigned char rxCommandNum; 00048 // unsigned char rxCommand = 0; 00049 // bool SW1Pressed = 0; 00050 // bool SW2Pressed = 0; 00051 // 00052 // int blueLed = 13; 00053 // int SW1Pin = 2; 00054 // int SW2Pin = 3; 00055 // 00056 // void setup() 00057 // 00058 // { 00059 // pinMode(SW1Pin, INPUT); 00060 // pinMode(SW2Pin, INPUT); 00061 // pinMode(blueLed, OUTPUT); 00062 // dht.begin(); 00063 // sensor_t sensor; 00064 // dht.temperature().getSensor(&sensor); 00065 // dht.humidity().getSensor(&sensor); 00066 // delayMS = sensor.min_delay / 1000; 00067 // Serial.begin(115200); //Begins Serial Communication at // 9600 baud rate 00068 // Wire.begin(8); //Begins I2C communication with // // Slave Address as 8 at pin (A4,A5) 00069 // Wire.onReceive(receiveEvent); //Function call when Slave receives // value from master 00070 // Wire.onRequest(requestEvent); //Function call when Master request // value from Slave 00071 // } 00072 // 00073 // void loop(void) 00074 // { 00075 // unsigned char SW1Debounced; 00076 // unsigned char SW2Debounced; 00077 // int temperature = 20; 00078 // while (1) { 00079 // if (rxCommandNum || TESTING) { 00080 // 00081 // 00082 // Serial.println("Slave Received Command From Master:"); //Prints in // // Serial Monitor 00083 // Serial.print(rxCommand); 00084 // Serial.print(" - "); 00085 // Serial.println(rxCommandNum); 00086 // for (int i = 0; i < 8; i++) { 00087 // Serial.print( rxCommandPacket[i] ); 00088 // Serial.print( " - " ); 00089 // } 00090 // Serial.println(" "); 00091 // for (int i = 0; i < 10; i++) { 00092 // Serial.print( txDataPacket[i] ); 00093 // Serial.print( " - " ); 00094 // } 00095 // Serial.println(" "); 00096 // rxCommandNum = 0; 00097 // } 00098 // sensors_event_t event; 00099 // int potvalue = map( analogRead(A0), 0, 1023, 0, 100); // // Reads analog value from POT (0-5V) 00100 // int lightlevel = map(analogRead(A1), 0, 1023, 0, 100); // // Ambient light level 00101 // int humidityInt = 101; 00102 // temperature = ((int)((lm35.readCelsius()) * 10)); // // // Ambient Temperature 00103 // dht.humidity().getEvent(&event); 00104 // if (isnan(event.relative_humidity)) { 00105 // Serial.println(F("Error reading humidity!")); 00106 // } 00107 // else { 00108 // humidityInt = (int)event.relative_humidity; 00109 // Serial.print(F("Humidity: ")); 00110 // Serial.print(humidityInt); 00111 // Serial.println(F("%")); 00112 // } 00113 // txDataPacket[0] = potvalue; 00114 // txDataPacket[1] = 0; // or potvalue >> 8; 00115 // txDataPacket[2] = lightlevel; 00116 // txDataPacket[3] = 0; // or lightlevel >> 8; 00117 // txDataPacket[4] = temperature; 00118 // txDataPacket[5] = temperature >> 8; 00119 // SW1Debounced = (SW1Debounced << 1) | digitalRead(SW1Pin); 00120 // if (SW1Debounced == 0) SW1Pressed = 1; 00121 // SW2Debounced = (SW2Debounced << 1) | digitalRead(SW2Pin); 00122 // if (SW2Debounced == 0) SW2Pressed = 1; 00123 // txDataPacket[6] = SW1Pressed; 00124 // txDataPacket[7] = SW2Pressed; 00125 // txDataPacket[8] = humidityInt; 00126 // txDataPacket[9] = 0; 00127 // digitalWrite(blueLed, rxCommandPacket[1] & 1); 00128 // delay(delayMS); 00129 // if (TESTING) delay(2000); 00130 // } 00131 // } 00132 // 00133 // void receiveEvent (int howMany) //This Function is called // when Slave receives value from master 00134 // { for (int i = 0; i < howMany; i++) { 00135 // rxCommandPacket[i] = Wire.read(); //Used to read value // received from master and store in variable SlaveReceived 00136 // } 00137 // rxCommandNum = howMany; 00138 // rxCommand = rxCommandPacket[0]; 00139 // } 00140 // void requestEvent() //This Function is called when Master wants data from slave 00141 // { 00142 // Wire.write(txDataPacket, 10); // sends eight bytes of data to master 00143 // SW1Pressed = 0; // Clear key presses on send to master 00144 // SW2Pressed = 0; 00145 // } 00146 00147 #include "mbed.h" 00148 #include "platform/mbed_thread.h" 00149 #include "GUI.h" 00150 #include "cy8ckit_028_tft.h" 00151 00152 #define UNO_CMD (0x10) // Command byte to Uno 00153 #define UNO_CMD_CONF (0x01) // Configuration data 00154 #define UNO_ADDR (0x10) // LM75 address 00155 00156 //I2C i2c(P8_1, P8_0); 00157 I2C i2c(I2C_SDA, I2C_SCL); 00158 DigitalOut myled(LED1); 00159 #include <MQTTClientMbedOs.h> 00160 00161 //NetworkInterface* network; 00162 00163 //Serial pc(SERIAL_TX, SERIAL_RX); 00164 Serial pc(USBTX, USBRX); 00165 00166 #define APP_INFO( x ) pc.printf x 00167 00168 volatile char TempCelsiusDisplay[] = "+abc.d C"; 00169 int lastLightDisplay = 101; 00170 int lthresh = 50; 00171 00172 int arrivedcount = 0; 00173 00174 void messageArrived(MQTT::MessageData& md) 00175 { 00176 // char buf[80]; 00177 MQTT::Message &message = md.message; 00178 APP_INFO(("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id)); 00179 APP_INFO(("Payload %.*s\r\n", message.payloadlen, (char*)message.payload)); 00180 // lthresh = stoi((string)message.payload); 00181 // } 00182 arrivedcount++; 00183 APP_INFO(("Arrived = %d\r\n",arrivedcount)); 00184 } 00185 int main() 00186 { 00187 00188 char data_write[8]; 00189 char data_read[10]; 00190 char buffer[120]; 00191 int rc; 00192 00193 /* Initialise display */ 00194 GUI_Init(); 00195 GUI_Clear(); 00196 00197 APP_INFO(("Connecting to the network using Wifi...\r\n")); 00198 // network = NetworkInterface::get_default_instance(); 00199 NetworkInterface *network = NetworkInterface::get_default_instance(); 00200 00201 nsapi_error_t net_status = -1; 00202 for (int tries = 0; tries < 10; tries++) { 00203 net_status = network->connect(); 00204 if (net_status == NSAPI_ERROR_OK) { 00205 break; 00206 } else { 00207 APP_INFO(("Unable to connect to network. Retrying...\r\n")); 00208 } 00209 } 00210 00211 if (net_status != NSAPI_ERROR_OK) { 00212 APP_INFO(("ERROR: Connecting to the network failed (%d)!\r\n", net_status)); 00213 // return -1; 00214 } 00215 00216 APP_INFO(("Connected to the network successfully. IP address: %s\n", network->get_ip_address())); 00217 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00218 data.clientID.cstring = (char *)"my_thing"; 00219 data.keepAliveInterval = 20; 00220 data.cleansession = 1; 00221 data.username.cstring = (char *)""; 00222 data.password.cstring = (char *)""; 00223 // char *host = "10.0.0.2"; 00224 char *host = (char *)"192.168.1.174"; 00225 int port = 1883; 00226 TCPSocket socket; 00227 MQTTClient client(&socket); 00228 socket.open(network); 00229 socket.connect(host, port); 00230 client.connect(data); 00231 MQTT::Message message; 00232 sprintf(buffer, "Hello World! from My Thang\r\n"); 00233 message.qos = MQTT::QOS0; 00234 message.retained = false; 00235 message.dup = false; 00236 message.payload = (void*)buffer; 00237 message.payloadlen = strlen(buffer)+1; 00238 00239 client.publish( "mytopic/announce", message); 00240 rc = client.subscribe("mytopic/lthresh", MQTT::QOS0, messageArrived); 00241 if (rc) APP_INFO(("Subscription Error %d", rc)); 00242 else APP_INFO(("Subscribed to mytopic/lthresh")); 00243 00244 sprintf(buffer, "%d",lthresh); 00245 message.payload = (void*)buffer; 00246 message.payloadlen = strlen(buffer)+1; 00247 client.publish("mytopic/lthresh", message); 00248 data_write[0] = UNO_CMD; 00249 data_write[1] = 0x08; 00250 00251 GUI_SetFont(GUI_FONT_10_1); 00252 GUI_SetTextAlign(GUI_TA_LEFT); 00253 int status = i2c.write(UNO_ADDR, data_write, 2, 0); 00254 // if (status != 0) { // Error 00255 // GUI_DispStringAt("I2C Error", 0, 220); 00256 // pc.printf("I2C connect error\n"); 00257 // while (1) { 00258 // myled = !myled; 00259 // ThisThread::sleep_for(200); 00260 // } 00261 // } 00262 pc.printf("I2C connected"); 00263 GUI_SetFont(GUI_FONT_20B_1); 00264 GUI_DispStringAt("Data from Arduino", 0, 0); 00265 int lightDisplay; 00266 int potValue; 00267 int humidity; 00268 00269 while (1) { 00270 00271 // Read Arduino data 00272 data_write[0] = UNO_CMD; 00273 i2c.write(UNO_ADDR, data_write, 2, 1); // no stop 00274 // if (i2c.read(UNO_ADDR, data_read, 10, 0)) { 00275 // GUI_SetFont(GUI_FONT_10_1); 00276 // GUI_DispStringAt("I2C receive error ", 0, 220); 00277 // pc.printf("I2C Rx Error\n"); 00278 // } else 00279 { 00280 i2c.read(UNO_ADDR, data_read, 10, 0); 00281 for (int i; i < 10; i++) pc.printf("%2x - ", data_read[i]); 00282 pc.printf("\n"); 00283 GUI_SetFont(GUI_FONT_10_1); 00284 sprintf(buffer, "I2c Data Received, IP address is: %s arrived %d", network->get_ip_address(), arrivedcount); 00285 GUI_DispStringAt(buffer, 0, 220); 00286 pc.printf("I2C Rx data recevied\n"); 00287 00288 // read eight bytes 00289 // pot value - msb-0, lsb-1 00290 // light level - msb-2, lsb-3 00291 // temperature - msb-4, lsb-5 00292 // sw1 state - 6 - 0-255 based on how long pressed 00293 // sw2 state - 7 - 0-255 based on how long pressed 00294 // Humidity - msb-9, lsb-8 00295 00296 // Calculate temperature value in Celcius 00297 int tempval = (int)((int)data_read[5] << 8) | data_read[4]; 00298 if (tempval < 0) { 00299 TempCelsiusDisplay[0] = '-'; 00300 } else { 00301 TempCelsiusDisplay[0] = '+'; 00302 } 00303 00304 // Integer part 00305 TempCelsiusDisplay[1] = (tempval / 1000) + 0x30; 00306 TempCelsiusDisplay[2] = ((tempval % 1000) / 100) + 0x30; 00307 TempCelsiusDisplay[3] = ((tempval % 1000) % 100 / 10) + 0x30; 00308 TempCelsiusDisplay[5] = ((tempval % 1000) % 100 % 10) + 0x30; 00309 00310 //Switches 00311 00312 unsigned char SW1State = data_read[6]; 00313 unsigned char SW2State = data_read[7]; 00314 00315 //light level 00316 lightDisplay = (int)((int)data_read[3] << 8) | data_read[2]; 00317 00318 //potentiometer value 00319 potValue = (int)((int)data_read[1] << 8) | data_read[0]; 00320 00321 // humidity value 00322 humidity = (int)((int)data_read[9] << 8) | data_read[8]; 00323 00324 // Display result 00325 pc.printf("temp = %s, Light is %3d%%, Hum is %3d%%%s%s\n", TempCelsiusDisplay, lightDisplay, humidity, SW1State?", SW1 pressed":"", SW2State?", SW2 pressed":""); 00326 00327 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"); 00328 GUI_SetFont(GUI_FONT_20_1); 00329 GUI_DispStringAt(buffer, 0, 40); 00330 } 00331 sprintf(buffer, "Light Level is %d\r\n", lightDisplay); 00332 message.payload = (void*)buffer; 00333 message.payloadlen = strlen(buffer)+1; 00334 00335 // if (lightDisplay != lastLightDisplay) { 00336 client.publish( "mytopic/light", message); 00337 lastLightDisplay = lightDisplay; 00338 // } 00339 00340 myled = !myled; 00341 data_write[1] = data_write[1] ^ 0x01; 00342 thread_sleep_for(1000); 00343 } 00344 00345 } 00346
Generated on Thu Jul 14 2022 12:58:41 by
1.7.2